Skip to content

Commit 03c0c4e

Browse files
committed
make interface to switch to naive dot product approach for a certain threshold
1 parent 1783b8f commit 03c0c4e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

cardano-crypto-class/src/Cardano/Crypto/EllipticCurve/BLS12_381/Internal.hs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,8 @@ scalarCanonical scalar =
10001000
-- by means of a modulo operation over the 'scalarPeriod'.
10011001
-- Negative numbers will also be brought to the range
10021002
-- [0, 'scalarPeriod' - 1] via modular reduction.
1003-
blsMSM :: forall curve. BLS curve => [Integer] -> [Point curve] -> Point curve
1004-
blsMSM ss ps = unsafePerformIO $ do
1003+
blsMSM :: forall curve. BLS curve => Int -> [Integer] -> [Point curve] -> Point curve
1004+
blsMSM threshold ss ps = unsafePerformIO $ do
10051005
zeroScalar <- scalarFromInteger 0
10061006
filteredPoints <-
10071007
foldM
@@ -1026,6 +1026,12 @@ blsMSM ss ps = unsafePerformIO $ do
10261026
[(scalar, pt)] -> do
10271027
i <- scalarToInteger scalar
10281028
return (blsMult pt i)
1029+
_ | length filteredPoints <= threshold -> do
1030+
return $
1031+
foldr
1032+
(\(scalar, pt) acc -> blsAddOrDouble acc (blsMult pt (unsafePerformIO $ scalarToInteger scalar)))
1033+
blsZero
1034+
filteredPoints
10291035
_ -> do
10301036
let (scalars, points) = unzip filteredPoints
10311037
numPoints = length points

cardano-crypto-tests/src/Test/Crypto/EllipticCurve.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ testBLSCurve name _ =
135135
, testProperty "MSM matches naive approach" $ \((ps, ss) :: ([BLS.Point curve], [BigInteger])) ->
136136
let pairs = [(p, i) | (BigInteger i, p) <- zip ss ps]
137137
(ps', ss') = unzip pairs
138-
in BLS.blsMSM ss' ps'
138+
in BLS.blsMSM 10 ss' ps'
139139
=== foldr (\(p, s) acc -> BLS.blsAddOrDouble acc (BLS.blsMult p s)) (BLS.blsZero @curve) pairs
140140
, testProperty "scalar mult distributive right" $ \(a :: BLS.Point curve) (b :: BLS.Point curve) (BigInteger c) ->
141141
BLS.blsMult (BLS.blsAddOrDouble a b) c === BLS.blsAddOrDouble (BLS.blsMult a c) (BLS.blsMult b c)

0 commit comments

Comments
 (0)