Skip to content

Add Modular Inverse operation#2207

Open
p-leriche wants to merge 5 commits intogchq:masterfrom
p-leriche:modular-inverse
Open

Add Modular Inverse operation#2207
p-leriche wants to merge 5 commits intogchq:masterfrom
p-leriche:modular-inverse

Conversation

@p-leriche
Copy link

Description

Computes the modular multiplicative inverse of a value modulo m.

Changes

  • Add ModularInverse.mjs operation
  • Update Categories.json to include Modular Inverse in Crypto category

Functionality

Finds x such that: a·x ≡ 1 (mod m)

The modular inverse is essential for:

  • RSA decryption and signing
  • Elliptic curve cryptography
  • Solving modular equations
  • Diffie-Hellman key exchange

Features

  • Supports decimal and hexadecimal input (0x prefix)
  • Handles arbitrarily large integers using BigInt
  • Uses Extended Euclidean Algorithm (iterative, no recursion limits)
  • Clear error messages when inverse doesn't exist (gcd ≠ 1)
  • Flexible input: can use operation args or input field

Dependencies

Depends on #2205

Testing

  • Tested with coprime values (inverse exists)
  • Tested with non-coprime values (proper error handling)
  • Tested with crypto-grade large integers (RSA-2048 size)
  • Verified: (a · inverse) mod m = 1
  • Built successfully with npm run build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants