From 1df86e23d8432dea9518eef4f5ed2e8ab0730881 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 13 Nov 2023 14:34:42 -0600 Subject: [PATCH 1/2] use constantine msm --- bn128.nim | 224 +++++++++++++++++++++++++++++------------------------- 1 file changed, 120 insertions(+), 104 deletions(-) diff --git a/bn128.nim b/bn128.nim index 4830311..ee8f03f 100644 --- a/bn128.nim +++ b/bn128.nim @@ -17,10 +17,12 @@ import std/sequtils import std/streams import std/random +import constantine/platforms/abstractions + import constantine/math/arithmetic import constantine/math/io/io_fields import constantine/math/io/io_bigints -import constantine/math/config/curves +import constantine/math/config/curves import constantine/math/config/type_ff as tff import constantine/math/extension_fields/towers as ext @@ -28,7 +30,8 @@ import constantine/math/elliptic/ec_shortweierstrass_affine as aff import constantine/math/elliptic/ec_shortweierstrass_projective as prj import constantine/math/pairings/pairings_bn as ate import constantine/math/elliptic/ec_scalar_mul as scl -# import constantine/math/elliptic/ec_multi_scalar_mul as msm +import constantine/math/elliptic/ec_multi_scalar_mul as msm +import constantine/math/isogenies/frobenius as frb #------------------------------------------------------------------------------- @@ -49,10 +52,10 @@ func mkFp2* (i: Fp, u: Fp) : Fp2 = let c : array[2, Fp] = [i,u] return ext.QuadraticExt[Fp]( coords: c ) -func unsafeMkG1* ( X, Y: Fp ) : G1 = +func unsafeMkG1* ( X, Y: Fp ) : G1 = return aff.ECP_ShortW_Aff[Fp, aff.G1](x: X, y: Y) -func unsafeMkG2* ( X, Y: Fp2 ) : G2 = +func unsafeMkG2* ( X, Y: Fp2 ) : G2 = return aff.ECP_ShortW_Aff[Fp2, aff.G2](x: X, y: Y) #------------------------------------------------------------------------------- @@ -113,25 +116,25 @@ func `===`*(x, y: Fr): bool = isEqualFr(x,y) #------------------- -func isEqualFpSeq*(xs, ys: seq[Fp]): bool = +func isEqualFpSeq*(xs, ys: seq[Fp]): bool = let n = xs.len assert( n == ys.len ) var b = true - for i in 0.. 0): if bitand(e,1) > 0: a *= s e = (e shr 1) square(s) return a -func smallPowFr*(base: Fr, expo: int): Fr = +func smallPowFr*(base: Fr, expo: int): Fr = if expo >= 0: return smallPowFr( base, uint(expo) ) else: @@ -233,7 +236,7 @@ proc debugPrintFr*(prefix: string, x: Fr) = proc debugPrintFrSeq*(msg: string, xs: seq[Fr]) = echo "---------------------" echo msg - for x in xs: + for x in xs: debugPrintFr( " " , x ) proc debugPrintG1*(msg: string, pt: G1) = @@ -249,7 +252,7 @@ proc debugPrintG2*(msg: string, pt: G2) = #------------------------------------------------------------------------------- # Montgomery batch inversion -func batchInverse*( xs: seq[Fr] ) : seq[Fr] = +func batchInverse*( xs: seq[Fr] ) : seq[Fr] = let n = xs.len assert(n>0) var us : seq[Fr] = newSeq[Fr](n+1) @@ -269,9 +272,9 @@ proc sanityCheckBatchInverse*() = let n = xs.len # for i in 0..