From e4c098950fccfe923aaa3504fa8943fff8358b2d Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Sun, 3 Sep 2023 23:36:08 +0200 Subject: [PATCH] finishing touches, renam to fft_vartime --- .../math/elliptic/ec_scalar_mul_vartime.nim | 2 +- constantine/math/polynomials/fft.nim | 10 +++--- research/kzg/fft_g1.nim | 31 ++++++++++--------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/constantine/math/elliptic/ec_scalar_mul_vartime.nim b/constantine/math/elliptic/ec_scalar_mul_vartime.nim index 9a470d58..ebe46e78 100644 --- a/constantine/math/elliptic/ec_scalar_mul_vartime.nim +++ b/constantine/math/elliptic/ec_scalar_mul_vartime.nim @@ -360,7 +360,7 @@ func scalarMul_vartime*[scalBits; EC]( let usedBits = scalar.limbs.getBits_vartime() - when scalBits == EC.F.C.getCurveOrderBitwidth and + when scalBits == EC.F.C.getCurveOrderBitwidth() and EC.F.C.hasEndomorphismAcceleration(): if usedBits >= L: when EC.F is Fp: diff --git a/constantine/math/polynomials/fft.nim b/constantine/math/polynomials/fft.nim index 632ea8ab..f86b11ae 100644 --- a/constantine/math/polynomials/fft.nim +++ b/constantine/math/polynomials/fft.nim @@ -104,7 +104,7 @@ func fft_internal[EC; bits: static int]( output[i+half] .diff_vartime(output[i], y_times_root) output[i] .sum_vartime(output[i], y_times_root) -func fft*[EC]( +func fft_vartime*[EC]( desc: ECFFT_Descriptor[EC], output: var openarray[EC], vals: openarray[EC]): FFT_Status = @@ -121,7 +121,7 @@ func fft*[EC]( fft_internal(voutput, vals.toStridedView(), rootz) return FFTS_Success -func ifft*[EC]( +func ifft_vartime*[EC]( desc: ECFFT_Descriptor[EC], output: var openarray[EC], vals: openarray[EC]): FFT_Status = @@ -360,12 +360,12 @@ when isMainModule: data[i].madd(data[i-1], BLS12_381.getGenerator("G1")) var coefs = newSeq[EC_G1](data.len) - let fftOk = fft(fftDesc, coefs, data) + let fftOk = fft_vartime(fftDesc, coefs, data) doAssert fftOk == FFTS_Success # display("coefs", 0, coefs) var res = newSeq[EC_G1](data.len) - let ifftOk = ifft(fftDesc, res, coefs) + let ifftOk = ifft_vartime(fftDesc, res, coefs) doAssert ifftOk == FFTS_Success # display("res", 0, res) @@ -415,7 +415,7 @@ when isMainModule: # Bench let start = getMonotime() for i in 0 ..< NumIters: - let status = fftDesc.fft(coefsOut, data) + let status = fftDesc.fft_vartime(coefsOut, data) doAssert status == FFTS_Success let stop = getMonotime() diff --git a/research/kzg/fft_g1.nim b/research/kzg/fft_g1.nim index b64757b3..c642bce3 100644 --- a/research/kzg/fft_g1.nim +++ b/research/kzg/fft_g1.nim @@ -96,28 +96,29 @@ func expandRootOfUnity[F](rootOfUnity: F): auto {.noInit.} = func simpleFT[EC; bits: static int]( output: var View[EC], vals: View[EC], - rootsOfUnity: View[BigInt[bits]] - ) = + rootsOfUnity: View[BigInt[bits]]) = # FFT is a recursive algorithm # This is the base-case using a O(n²) algorithm let L = output.len var last {.noInit.}, v {.noInit.}: EC + var v0w0 {.noInit.} = vals[0] + v0w0.scalarMul_vartime(rootsOfUnity[0]) + for i in 0 ..< L: - last = vals[0] - last.scalarMul_vartime(rootsOfUnity[0]) + last = v0w0 for j in 1 ..< L: v = vals[j] + v.scalarMul_vartime(rootsOfUnity[(i*j) mod L]) - last += v + last.sum_vartime(last, v) output[i] = last func fft_internal[EC; bits: static int]( output: var View[EC], vals: View[EC], - rootsOfUnity: View[BigInt[bits]] - ) = + rootsOfUnity: View[BigInt[bits]]) = if output.len <= 4: simpleFT(output, vals, rootsOfUnity) return @@ -137,10 +138,10 @@ func fft_internal[EC; bits: static int]( # FFT Butterfly y_times_root = output[i+half] y_times_root .scalarMul_vartime(rootsOfUnity[i]) - output[i+half] .diff(output[i], y_times_root) - output[i] += y_times_root + output[i+half] .diff_vartime(output[i], y_times_root) + output[i] .sum_vartime(output[i], y_times_root) -func fft*[EC]( +func fft_vartime*[EC]( desc: FFTDescriptor[EC], output: var openarray[EC], vals: openarray[EC]): FFT_Status = @@ -157,7 +158,7 @@ func fft*[EC]( fft_internal(voutput, vals.toView(), rootz) return FFTS_Success -func ifft*[EC]( +func ifft_vartime*[EC]( desc: FFTDescriptor[EC], output: var openarray[EC], vals: openarray[EC]): FFT_Status = @@ -180,7 +181,7 @@ func ifft*[EC]( invLen.inv_vartime() let inv = invLen.toBig() - for i in 0..< output.len: + for i in 0 ..< output.len: output[i].scalarMul_vartime(inv) return FFTS_Success @@ -222,12 +223,12 @@ when isMainModule: data[i].madd(data[i-1], Generator1) var coefs = newSeq[EC_G1](data.len) - let fftOk = fft(fftDesc, coefs, data) + let fftOk = fft_vartime(fftDesc, coefs, data) doAssert fftOk == FFTS_Success # display("coefs", 0, coefs) var res = newSeq[EC_G1](data.len) - let ifftOk = ifft(fftDesc, res, coefs) + let ifftOk = ifft_vartime(fftDesc, res, coefs) doAssert ifftOk == FFTS_Success # display("res", 0, res) @@ -277,7 +278,7 @@ when isMainModule: # Bench let start = getMonotime() for i in 0 ..< NumIters: - let status = desc.fft(coefsOut, data) + let status = desc.fft_vartime(coefsOut, data) doAssert status == FFTS_Success let stop = getMonotime()