From f17c0389b9c65369a98e91b319f20627131da0bd Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Sat, 13 Jun 2026 14:20:17 +0200 Subject: [PATCH] add some quick&dirty FFT benchmarks --- bench/.gitignore | 2 + bench/bench_fft.nim | 104 ++++++++++++++++++++++++++++++++++++++ bench/bench_fft.nimble | 13 +++++ bench/nim.cfg | 1 + bench/shared.nim | 23 +++++++++ groth16.nimble | 2 +- groth16/bn128/arrays.nim | 61 ++++++++++++++++++++++ tests/groth16/testFFT.nim | 54 +++----------------- 8 files changed, 213 insertions(+), 47 deletions(-) create mode 100644 bench/.gitignore create mode 100644 bench/bench_fft.nim create mode 100644 bench/bench_fft.nimble create mode 100644 bench/nim.cfg create mode 100644 bench/shared.nim create mode 100644 groth16/bn128/arrays.nim diff --git a/bench/.gitignore b/bench/.gitignore new file mode 100644 index 0000000..b302f53 --- /dev/null +++ b/bench/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +bench_fft diff --git a/bench/bench_fft.nim b/bench/bench_fft.nim new file mode 100644 index 0000000..e3d2d6c --- /dev/null +++ b/bench/bench_fft.nim @@ -0,0 +1,104 @@ + +# +# nimble build -d:release +# + +import strformat + +import taskpools + +import constantine/math/arithmetic +import constantine/named/properties_fields + +import groth16/bn128 +import groth16/bn128/msm +import groth16/bn128/arrays + +import groth16/math/domain +import groth16/math/ntt +import groth16/dynamic/group_fft + +import shared + +#------------------------------------------------------------------------------- + +when isMainModule: + + echo "quick & dirty FFT benchmarks" + + let nthreads: int = 8 + let N: int = 8192 + + let D = createDomain(N) + let xs : seq[Fr[BN254_Snarks]] = randFrSeq(N) + let gs : seq[AffG1] = randG1Seq(N) + + var ys, zs: seq[Fr[BN254_Snarks]] + var hs, rs: seq[AffG1] + + var pool = Taskpool.new(nthreads) + + #----------------------------------------------------------------------------- + + echo "\n----------------------------------------" + echo "*** scalar multiplications\n" + + hs = newSeq[AffG1](N) + withMeasureTime(true , fmt"{N} individual scalar multiplications "): + for i in 0..= 2.2.0" +requires "https://github.com/status-im/nim-taskpools >= 0.0.5" +requires "https://github.com/mratsim/constantine" +requires "groth16 >= 0.1.2" diff --git a/bench/nim.cfg b/bench/nim.cfg new file mode 100644 index 0000000..0f840a1 --- /dev/null +++ b/bench/nim.cfg @@ -0,0 +1 @@ +--path:".." diff --git a/bench/shared.nim b/bench/shared.nim new file mode 100644 index 0000000..ef568d1 --- /dev/null +++ b/bench/shared.nim @@ -0,0 +1,23 @@ + +import strformat +import times, strutils + +#------------------------------------------------------------------------------- + +func seconds*(x: float): string = fmt"{x:.4f} seconds" + +func quoted*(s: string): string = fmt"`{s:s}`" + +template withMeasureTime*(doPrint: bool, text: string, code: untyped) = + block: + if doPrint: + let t0 = epochTime() + code + let elapsed = epochTime() - t0 + let elapsedStr = elapsed.formatFloat(format = ffDecimal, precision = 4) + echo ( text & " took " & elapsedStr & " seconds" ) + else: + code + +#------------------------------------------------------------------------------- + diff --git a/groth16.nimble b/groth16.nimble index ec8762a..be06ac4 100644 --- a/groth16.nimble +++ b/groth16.nimble @@ -4,7 +4,7 @@ author = "Balazs Komuves" description = "Groth16 proof system" license = "MIT OR Apache-2.0" -skipDirs = @["groth16/example"] +skipDirs = @["groth16/example","bench"] binDir = "build" namedBin = {"cli/cli_main": "nim-groth16"}.toTable() installExt = @["nim"] diff --git a/groth16/bn128/arrays.nim b/groth16/bn128/arrays.nim new file mode 100644 index 0000000..0b65368 --- /dev/null +++ b/groth16/bn128/arrays.nim @@ -0,0 +1,61 @@ + +import constantine/math/arithmetic +import constantine/named/properties_fields + +import groth16/bn128/fields +import groth16/bn128/curves +import groth16/bn128/rnd + +#------------------------------------------------------------------------------- +# Fr arrays + +proc randFrSeq*(N: int) : seq[Fr[BN254_Snarks]] = + var xs : seq[Fr[BN254_Snarks]] = newSeq[Fr[BN254_Snarks]]( N ) + for i in 0..