diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-08-11 20:30:53 -0400 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-08-11 20:30:53 -0400 |
commit | e12ccae7a96c01dce5e86aa17c27e20644c75a15 (patch) | |
tree | 86aa7a239277ba7d86e1110faa73a45eae329023 /lib | |
parent | c8bfbfb85e368511a033561cd68f7e86f14fcde7 (diff) |
lib: add invmod
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rsa.go | 35 | ||||
-rw-r--r-- | lib/rsa_test.go | 24 |
2 files changed, 59 insertions, 0 deletions
@@ -63,3 +63,38 @@ func egcd(a, b *big.Int) GCDResult { Y: y, } } + +func invmod(a, n *big.Int) (*big.Int, error) { + // Initialize. + t0 := big.NewInt(0) + t1 := big.NewInt(1) + r0 := biCopy(big.NewInt(0), n) + r1 := biCopy(big.NewInt(0), a) + + for r1.Cmp(big.NewInt(0)) != 0 { + q := big.NewInt(0) + q.Div(r0, r1) + + tt := big.NewInt(0) + tt = tt.Mul(q, t1) + tt = tt.Sub(t0, tt) + + biCopy(t0, t1) + biCopy(t1, tt) + + tr := big.NewInt(0) + tr = tr.Mul(q, r1) + tr = tr.Sub(r0, tr) + + biCopy(r0, r1) + biCopy(r1, tr) + } + + if r0.Cmp(big.NewInt(1)) > 0 { + return nil, CPError{"not invertible"} + } + if t0.Cmp(big.NewInt(0)) < 0 { + t0.Add(t0, n) + } + return t0, nil +} diff --git a/lib/rsa_test.go b/lib/rsa_test.go index ee71755..4f65468 100644 --- a/lib/rsa_test.go +++ b/lib/rsa_test.go @@ -50,3 +50,27 @@ func TestEGCD(t *testing.T) { } } + +func TestInvMod(t *testing.T) { + a := big.NewInt(17) + b := big.NewInt(3120) + e := big.NewInt(2753) // Expected inverse. + i, err := invmod(a, b) + if err != nil { + t.Errorf("invmod(%v,%v) failed: %v", a, b, err) + } + if i.Cmp(e) != 0 { + t.Errorf("gcd(%v,%v) != %v", a, b, e) + } + + a = big.NewInt(240) + b = big.NewInt(47) + e = big.NewInt(19) // Expected inverse. + i, err = invmod(a, b) + if err != nil { + t.Errorf("invmod(%v,%v) failed: %v", a, b, err) + } + if i.Cmp(e) != 0 { + t.Errorf("gcd(%v,%v) != %v", a, b, e) + } +} |