Skip to content

Commit

Permalink
finishing touches, renam to fft_vartime
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Sep 3, 2023
1 parent 4661e82 commit e4c0989
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion constantine/math/elliptic/ec_scalar_mul_vartime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions constantine/math/polynomials/fft.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
31 changes: 16 additions & 15 deletions research/kzg/fft_g1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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 =
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit e4c0989

Please sign in to comment.