From 2231d800c747e9f726efe4626ec07411ba4901ab Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Sat, 13 Jun 2026 13:29:07 +0200 Subject: [PATCH] add group FFT --- groth16.nimble | 2 +- groth16/bn128/curves.nim | 7 +- groth16/dynamic/group_fft.nim | 188 ++++++++++++++++++++++++++++++++++ tests/groth16/testFFT.nim | 76 +++++++++----- 4 files changed, 247 insertions(+), 26 deletions(-) create mode 100644 groth16/dynamic/group_fft.nim diff --git a/groth16.nimble b/groth16.nimble index 29b38d3..ec8762a 100644 --- a/groth16.nimble +++ b/groth16.nimble @@ -1,5 +1,5 @@ -version = "0.1.1" +version = "0.1.2" author = "Balazs Komuves" description = "Groth16 proof system" license = "MIT OR Apache-2.0" diff --git a/groth16/bn128/curves.nim b/groth16/bn128/curves.nim index 76edcac..b4e0d1d 100644 --- a/groth16/bn128/curves.nim +++ b/groth16/bn128/curves.nim @@ -34,11 +34,14 @@ import groth16/bn128/fields #------------------------------------------------------------------------------- -type G1* = aff.EC_ShortW_Aff[Fp[BN254_Snarks] , aff.G1] -type G2* = aff.EC_ShortW_Aff[Fp2[BN254_Snarks], aff.G2] +type AffG1* = aff.EC_ShortW_Aff[Fp[BN254_Snarks] , aff.G1] +type AffG2* = aff.EC_ShortW_Aff[Fp2[BN254_Snarks], aff.G2] type ProjG1* = prj.EC_ShortW_Prj[Fp[BN254_Snarks] , prj.G1] type ProjG2* = prj.EC_ShortW_Prj[Fp2[BN254_Snarks], prj.G2] + +type G1* = AffG1 +type G2* = AffG2 #------------------------------------------------------------------------------- # compressed points (supposedly compatible with arkworks-0.5) diff --git a/groth16/dynamic/group_fft.nim b/groth16/dynamic/group_fft.nim new file mode 100644 index 0000000..27a5b06 --- /dev/null +++ b/groth16/dynamic/group_fft.nim @@ -0,0 +1,188 @@ + +# +# FFT for groups elements +# + +#------------------------------------------------------------------------------- + +import constantine/math/arithmetic +import constantine/math/io/io_fields +import constantine/named/properties_fields + +# import constantine/math/elliptic/ec_shortweierstrass_affine as aff +import constantine/math/elliptic/ec_shortweierstrass_projective as prj +import constantine/math/elliptic/ec_scalar_mul_vartime as scl + +import groth16/bn128 +import groth16/bn128/curves +import groth16/math/domain + +#------------------------------------------------------------------------------- + +func forward_FFT_worker( m: int + , srcStride: int + , gpows: seq[Fr[BN254_Snarks]] + , src: seq[ProjG1] , srcOfs: int + , buf: var seq[ProjG1] , bufOfs: int + , tgt: var seq[ProjG1] , tgtOfs: int ) = + case m + + of 0: + tgt[tgtOfs] = src[srcOfs] + + of 1: + tgt[tgtOfs ] = src[srcOfs] ; tgt[tgtOfs ] += src[srcOfs+srcStride] + tgt[tgtOfs+1] = src[srcOfs] ; tgt[tgtOfs+1] -= src[srcOfs+srcStride] + + else: + let N : int = 1 shl m + let halfN : int = 1 shl (m-1) + forward_FFT_worker( m-1 + , srcStride shl 1 + , gpows + , src , srcOfs + , buf , bufOfs + N + , buf , bufOfs ) + forward_FFT_worker( m-1 + , srcStride shl 1 + , gpows + , src , srcOfs + srcStride + , buf , bufOfs + N + , buf , bufOfs + halfN ) + for j in 0..