fixed

Pure Haskell large fixed-width integers and Montgomery arithmetic.
git clone git://git.ppad.tech/fixed.git
Log | Files | Refs | README | LICENSE

commit e951fc1235ae5407d8bcef6ba572f13f2b6924dc
parent 4b74a737e247690d7640d821ea478b2e8088d38d
Author: Jared Tobin <jared@jtobin.io>
Date:   Sat, 27 Dec 2025 10:04:10 -0330

lib: fix mistaken vartime suffix

Looks like a search-and-replace went awry.

Diffstat:
Mlib/Numeric/Montgomery/Secp256k1/Curve.hs | 16++++++++--------
Mlib/Numeric/Montgomery/Secp256k1/Scalar.hs | 16++++++++--------
Mtest/Montgomery/Curve.hs | 32++++++++++++++++----------------
Mtest/Montgomery/Scalar.hs | 30+++++++++++++++---------------
4 files changed, 47 insertions(+), 47 deletions(-)

diff --git a/lib/Numeric/Montgomery/Secp256k1/Curve.hs b/lib/Numeric/Montgomery/Secp256k1/Curve.hs @@ -19,8 +19,8 @@ module Numeric.Montgomery.Secp256k1.Curve ( -- * Montgomery form, secp256k1 field prime modulus Montgomery(..) , render - , to_vartime - , from_vartime + , to + , from , zero , one @@ -93,7 +93,7 @@ render (Montgomery (# Limb a, Limb b, Limb c, Limb d #)) = <> show (W# c) <> ", " <> show (W# d) <> ")" instance Show Montgomery where - show = show . from_vartime + show = show . from -- | Note that 'fromInteger' necessarily runs in variable time due -- to conversion from the variable-size, potentially heap-allocated @@ -104,7 +104,7 @@ instance Num Montgomery where a * b = mul a b negate a = neg a abs = id - fromInteger = to_vartime . WW.to_vartime + fromInteger = to . WW.to_vartime signum (Montgomery (# l0, l1, l2, l3 #)) = let !(Limb l) = l0 `L.or#` l1 `L.or#` l2 `L.or#` l3 !n = C.from_word_nonzero# l @@ -369,14 +369,14 @@ to# x = {-# INLINE to# #-} -- | Convert a 'Wider' word to the Montgomery domain. -to_vartime :: Wider -> Montgomery -to_vartime (Wider x) = Montgomery (to# x) +to :: Wider -> Montgomery +to (Wider x) = Montgomery (to# x) -- | Retrieve a 'Montgomery' word from the Montgomery domain. -- -- This function is a synonym for 'retr'. -from_vartime :: Montgomery -> Wider -from_vartime = retr +from :: Montgomery -> Wider +from = retr add# :: (# Limb, Limb, Limb, Limb #) -- ^ augend diff --git a/lib/Numeric/Montgomery/Secp256k1/Scalar.hs b/lib/Numeric/Montgomery/Secp256k1/Scalar.hs @@ -19,8 +19,8 @@ module Numeric.Montgomery.Secp256k1.Scalar ( -- * Montgomery form, secp256k1 scalar group order modulus Montgomery(..) , render - , to_vartime - , from_vartime + , to + , from , zero , one @@ -81,7 +81,7 @@ import Prelude hiding (or, and, not, exp) data Montgomery = Montgomery !(# Limb, Limb, Limb, Limb #) instance Show Montgomery where - show = show . from_vartime + show = show . from -- | Render a 'Montgomery' value as a 'String', showing its individual -- 'Limb's. @@ -102,7 +102,7 @@ instance Num Montgomery where a * b = mul a b negate a = neg a abs = id - fromInteger = to_vartime . WW.to_vartime + fromInteger = to . WW.to_vartime signum (Montgomery (# l0, l1, l2, l3 #)) = let !(Limb l) = l0 `L.or#` l1 `L.or#` l2 `L.or#` l3 !n = C.from_word_nonzero# l @@ -367,14 +367,14 @@ to# x = {-# INLINE to# #-} -- | Convert a 'Wider' word to the Montgomery domain. -to_vartime :: Wider -> Montgomery -to_vartime (Wider x) = Montgomery (to# x) +to :: Wider -> Montgomery +to (Wider x) = Montgomery (to# x) -- | Retrieve a 'Montgomery' word from the Montgomery domain. -- -- This function is a synonym for 'retr'. -from_vartime :: Montgomery -> Wider -from_vartime = retr +from :: Montgomery -> Wider +from = retr add# :: (# Limb, Limb, Limb, Limb #) -- ^ augend diff --git a/test/Montgomery/Curve.hs b/test/Montgomery/Curve.hs @@ -41,7 +41,7 @@ mm :: C.Montgomery mm = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F repr :: H.Assertion -repr = H.assertBool mempty (W.eq_vartime 0 (C.from_vartime mm)) +repr = H.assertBool mempty (W.eq_vartime 0 (C.from mm)) add_case :: String -> W.Wider -> W.Wider -> W.Wider -> H.Assertion add_case t a b s = do @@ -49,7 +49,7 @@ add_case t a b s = do ((W.from_vartime a + W.from_vartime b) `mod` W.from_vartime m) (W.from_vartime s) H.assertBool t - (W.eq_vartime s (C.from_vartime (C.to_vartime a + C.to_vartime b))) + (W.eq_vartime s (C.from (C.to a + C.to b))) add :: H.Assertion add = do @@ -73,7 +73,7 @@ sub_case t b a d = do ((W.from_vartime b - W.from_vartime a) `mod` W.from_vartime m) (W.from_vartime d) H.assertBool t - (W.eq_vartime d (C.from_vartime (C.to_vartime b - C.to_vartime a))) + (W.eq_vartime d (C.from (C.to b - C.to a))) sub :: H.Assertion sub = do @@ -96,7 +96,7 @@ mul_case t a b p = do ((W.from_vartime a * W.from_vartime b) `mod` W.from_vartime m) (W.from_vartime p) H.assertBool t - (W.eq_vartime p (C.from_vartime (C.to_vartime a * C.to_vartime b))) + (W.eq_vartime p (C.from (C.to a * C.to b))) mul :: H.Assertion mul = do @@ -122,54 +122,54 @@ instance Q.Arbitrary W.Wider where arbitrary = fmap W.to_vartime Q.arbitrary instance Q.Arbitrary C.Montgomery where - arbitrary = fmap C.to_vartime Q.arbitrary + arbitrary = fmap C.to Q.arbitrary add_matches :: W.Wider -> W.Wider -> Bool add_matches a b = - let ma = C.to_vartime a - mb = C.to_vartime b + let ma = C.to a + mb = C.to b ia = W.from_vartime a ib = W.from_vartime b im = W.from_vartime m in W.eq_vartime (W.to_vartime ((ia + ib) `mod` im)) - (C.from_vartime (ma + mb)) + (C.from (ma + mb)) mul_matches :: W.Wider -> W.Wider -> Bool mul_matches a b = - let ma = C.to_vartime a - mb = C.to_vartime b + let ma = C.to a + mb = C.to b ia = W.from_vartime a ib = W.from_vartime b im = W.from_vartime m in W.eq_vartime (W.to_vartime ((ia * ib) `mod` im)) - (C.from_vartime (ma * mb)) + (C.from (ma * mb)) sqr_matches :: W.Wider -> Bool sqr_matches a = - let ma = C.to_vartime a + let ma = C.to a ia = W.from_vartime a im = W.from_vartime m in W.eq_vartime (W.to_vartime ((ia * ia) `mod` im)) - (C.from_vartime (C.sqr ma)) + (C.from (C.sqr ma)) exp_matches :: C.Montgomery -> W.Wider -> Bool exp_matches a b = - let ia = W.from_vartime (C.from_vartime a) + let ia = W.from_vartime (C.from a) nb = fromIntegral (W.from_vartime b) nm = fromIntegral (W.from_vartime m) in W.eq_vartime (W.to_vartime (modexp ia nb nm)) - (C.from_vartime (C.exp a b)) + (C.from (C.exp a b)) inv_valid :: Q.NonZero C.Montgomery -> Bool inv_valid (Q.NonZero s) = C.eq_vartime (C.inv s * s) 1 odd_correct :: C.Montgomery -> Bool odd_correct w = - C.odd_vartime w == I.integerTestBit (W.from_vartime (C.from_vartime w)) 0 + C.odd_vartime w == I.integerTestBit (W.from_vartime (C.from w)) 0 tests :: TestTree tests = testGroup "montgomery tests (curve)" [ diff --git a/test/Montgomery/Scalar.hs b/test/Montgomery/Scalar.hs @@ -41,7 +41,7 @@ mm :: S.Montgomery mm = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 repr :: H.Assertion -repr = H.assertBool mempty (W.eq_vartime 0 (S.from_vartime mm)) +repr = H.assertBool mempty (W.eq_vartime 0 (S.from mm)) add_case :: String -> W.Wider -> W.Wider -> W.Wider -> H.Assertion add_case t a b s = do @@ -49,7 +49,7 @@ add_case t a b s = do ((W.from_vartime a + W.from_vartime b) `mod` W.from_vartime m) (W.from_vartime s) H.assertBool t - (W.eq_vartime s (S.from_vartime (S.to_vartime a + S.to_vartime b))) + (W.eq_vartime s (S.from (S.to a + S.to b))) add :: H.Assertion add = do @@ -73,7 +73,7 @@ sub_case t b a d = do ((W.from_vartime b - W.from_vartime a) `mod` W.from_vartime m) (W.from_vartime d) H.assertBool t - (W.eq_vartime d (S.from_vartime (S.to_vartime b - S.to_vartime a))) + (W.eq_vartime d (S.from (S.to b - S.to a))) sub :: H.Assertion sub = do @@ -96,7 +96,7 @@ mul_case t a b p = do ((W.from_vartime a * W.from_vartime b) `mod` W.from_vartime m) (W.from_vartime p) H.assertBool t - (W.eq_vartime p (S.from_vartime (S.to_vartime a * S.to_vartime b))) + (W.eq_vartime p (S.from (S.to a * S.to b))) mul :: H.Assertion mul = do @@ -122,47 +122,47 @@ instance Q.Arbitrary W.Wider where arbitrary = fmap W.to_vartime Q.arbitrary instance Q.Arbitrary S.Montgomery where - arbitrary = fmap S.to_vartime Q.arbitrary + arbitrary = fmap S.to Q.arbitrary add_matches :: W.Wider -> W.Wider -> Bool add_matches a b = - let ma = S.to_vartime a - mb = S.to_vartime b + let ma = S.to a + mb = S.to b ia = W.from_vartime a ib = W.from_vartime b im = W.from_vartime m in W.eq_vartime (W.to_vartime ((ia + ib) `mod` im)) - (S.from_vartime (ma + mb)) + (S.from (ma + mb)) mul_matches :: W.Wider -> W.Wider -> Bool mul_matches a b = - let ma = S.to_vartime a - mb = S.to_vartime b + let ma = S.to a + mb = S.to b ia = W.from_vartime a ib = W.from_vartime b im = W.from_vartime m in W.eq_vartime (W.to_vartime ((ia * ib) `mod` im)) - (S.from_vartime (ma * mb)) + (S.from (ma * mb)) sqr_matches :: W.Wider -> Bool sqr_matches a = - let ma = S.to_vartime a + let ma = S.to a ia = W.from_vartime a im = W.from_vartime m in W.eq_vartime (W.to_vartime ((ia * ia) `mod` im)) - (S.from_vartime (S.sqr ma)) + (S.from (S.sqr ma)) exp_matches :: S.Montgomery -> W.Wider -> Bool exp_matches a b = - let ia = W.from_vartime (S.from_vartime a) + let ia = W.from_vartime (S.from a) nb = fromIntegral (W.from_vartime b) nm = fromIntegral (W.from_vartime m) in W.eq_vartime (W.to_vartime (modexp ia nb nm)) - (S.from_vartime (S.exp a b)) + (S.from (S.exp a b)) inv_valid :: Q.NonZero S.Montgomery -> Bool inv_valid (Q.NonZero s) = S.eq_vartime (S.inv s * s) 1