summaryrefslogtreecommitdiffstats
path: root/lib/rsa_test.go
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-08-11 19:34:11 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-08-11 20:21:51 -0400
commitc8bfbfb85e368511a033561cd68f7e86f14fcde7 (patch)
treead39db1d7f399a360944389e64ea68c9c09eddea /lib/rsa_test.go
parent40d1606d63b2aa37cad07add55b418f238fc9e81 (diff)
lib: add egcd
Diffstat (limited to 'lib/rsa_test.go')
-rw-r--r--lib/rsa_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/rsa_test.go b/lib/rsa_test.go
new file mode 100644
index 0000000..ee71755
--- /dev/null
+++ b/lib/rsa_test.go
@@ -0,0 +1,52 @@
+// Copyright © 2021 siddharth ravikumar <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package lib
+
+import (
+ "math/big"
+ "testing"
+)
+
+func TestEGCD(t *testing.T) {
+ a := big.NewInt(128)
+ b := big.NewInt(96)
+ r := egcd(a, b)
+ if r.Gcd.Cmp(big.NewInt(32)) != 0 {
+ t.Errorf("gcd(128, 96) != 32")
+ }
+ if r.X.Cmp(big.NewInt(1)) != 0 || r.Y.Cmp(big.NewInt(-1)) != 0 {
+ t.Errorf("bézout_coef(128, 96) != {1,-1}")
+ }
+
+ a = big.NewInt(360)
+ b = big.NewInt(210)
+ r = egcd(a, b)
+ if r.Gcd.Cmp(big.NewInt(30)) != 0 {
+ t.Errorf("gcd(360, 210) != 30")
+ }
+ if r.X.Cmp(big.NewInt(3)) != 0 || r.Y.Cmp(big.NewInt(-5)) != 0 {
+ t.Errorf("bézout_coef(360, 210) != {3,-5}")
+ }
+
+ a = big.NewInt(108)
+ b = big.NewInt(144)
+ r = egcd(a, b)
+ if r.Gcd.Cmp(big.NewInt(36)) != 0 {
+ t.Errorf("gcd(108, 144) != 36")
+ }
+ if r.X.Cmp(big.NewInt(-1)) != 0 || r.Y.Cmp(big.NewInt(1)) != 0 {
+ t.Errorf("bézout_coef(108, 144) != {-1,1}")
+ }
+
+ a = big.NewInt(240)
+ b = big.NewInt(46)
+ r = egcd(a, b)
+ if r.Gcd.Cmp(big.NewInt(2)) != 0 {
+ t.Errorf("gcd(240, 46) != 2")
+ }
+ if r.X.Cmp(big.NewInt(-9)) != 0 || r.Y.Cmp(big.NewInt(47)) != 0 {
+ t.Errorf("bézout_coef(240, 46) != {-9,47}")
+ }
+
+}