commit b22524d4fcc34d97a13f2fa04da078389db303dc
parent f81d2564152a8843ddd3cb2ce2dff24ad8d2da7f
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 6 Dec 2025 11:51:57 +0400
test: montgomery modinv property tests
Diffstat:
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/test/Montgomery/Curve.hs b/test/Montgomery/Curve.hs
@@ -116,13 +116,17 @@ mul_matches a b =
im = W.from m
in W.eq_vartime (W.to ((ia * ib) `mod` im)) (C.from (ma * mb))
+inv_valid :: Q.NonZero C.Montgomery -> Bool
+inv_valid (Q.NonZero s) = C.inv s * s == 1
+
tests :: TestTree
tests = testGroup "montgomery tests (curve)" [
H.testCase "representation" repr
, H.testCase "add" add
, H.testCase "sub" sub
, H.testCase "mul" mul
- , Q.testProperty "a + b mod m ~ ma + mb" $ Q.withMaxSuccess 1000 add_matches
- , Q.testProperty "a * b mod m ~ ma * mb" $ Q.withMaxSuccess 1000 mul_matches
+ , Q.testProperty "a + b mod m ~ ma + mb" $ Q.withMaxSuccess 500 add_matches
+ , Q.testProperty "a * b mod m ~ ma * mb" $ Q.withMaxSuccess 500 mul_matches
+ , Q.testProperty "n ^ -1 mod m * n ~ 1" $ Q.withMaxSuccess 500 inv_valid
]
diff --git a/test/Montgomery/Scalar.hs b/test/Montgomery/Scalar.hs
@@ -116,13 +116,17 @@ mul_matches a b =
im = W.from m
in W.eq_vartime (W.to ((ia * ib) `mod` im)) (S.from (ma * mb))
+inv_valid :: Q.NonZero S.Montgomery -> Bool
+inv_valid (Q.NonZero s) = S.inv s * s == 1
+
tests :: TestTree
tests = testGroup "montgomery tests (scalar)" [
H.testCase "representation" repr
, H.testCase "add" add
, H.testCase "sub" sub
, H.testCase "mul" mul
- , Q.testProperty "a + b mod m ~ ma + mb" $ Q.withMaxSuccess 1000 add_matches
- , Q.testProperty "a * b mod m ~ ma * mb" $ Q.withMaxSuccess 1000 mul_matches
+ , Q.testProperty "a + b mod m ~ ma + mb" $ Q.withMaxSuccess 500 add_matches
+ , Q.testProperty "a * b mod m ~ ma * mb" $ Q.withMaxSuccess 500 mul_matches
+ , Q.testProperty "n ^ -1 mod m * n ~ 1" $ Q.withMaxSuccess 500 inv_valid
]