Skip to content

Commit

Permalink
feat(bench-zkalc): polish and output json bench file
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jul 12, 2024
1 parent c4b540f commit 868c6bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ Cargo.lock
# -----------------------------------------------------------------------------------------
perf.data
perf.data.old
*.bench.json
50 changes: 27 additions & 23 deletions benchmarks/zkalc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ proc benchFrIP(rng: var RngState, curve: static Algebra): ZkalcBenchDetails =

proc benchEcAdd(rng: var RngState, EC: typedesc, useVartime: bool): ZkalcBenchDetails =
const G =
when EC.G == G1: "𝔾1"
else: "𝔾2"
when EC.G == G1: "𝔾₁"
else: "𝔾₂"
const curve = EC.getName()

var r {.noInit.}: EC
Expand All @@ -250,8 +250,8 @@ proc benchEcAdd(rng: var RngState, EC: typedesc, useVartime: bool): ZkalcBenchDe

proc benchEcMul(rng: var RngState, EC: typedesc, useVartime: bool): ZkalcBenchDetails =
const G =
when EC.G == G1: "𝔾1"
else: "𝔾2"
when EC.G == G1: "𝔾₁"
else: "𝔾₂"
const curve = EC.getName()

var r {.noInit.}: EC
Expand Down Expand Up @@ -327,8 +327,8 @@ proc createBenchMsmContext*(rng: var RngState, EC: typedesc, maxNumInputs: int):

proc benchEcMsm[EC](ctx: BenchMsmContext[EC]): ZkalcBenchDetails =
const G =
when EC.G == G1: "𝔾1"
else: "𝔾2"
when EC.G == G1: "𝔾₁"
else: "𝔾₂"
const curve = EC.getName()

let tp = Threadpool.new()
Expand All @@ -353,8 +353,8 @@ proc benchEcMsm[EC](ctx: BenchMsmContext[EC]): ZkalcBenchDetails =

proc benchEcIsInSubgroup(rng: var RngState, EC: type): ZkalcBenchDetails =
const G =
when EC.G == G1: "𝔾1"
else: "𝔾2"
when EC.G == G1: "𝔾₁"
else: "𝔾₂"
const curve = EC.getName()

var P = rng.random_unsafe(EC)
Expand All @@ -369,15 +369,14 @@ proc benchEcIsInSubgroup(rng: var RngState, EC: type): ZkalcBenchDetails =

proc benchEcHashToCurve(rng: var RngState, EC: type): ZkalcBenchDetails =
const G =
when EC.G == G1: "𝔾1"
else: "𝔾2"
when EC.G == G1: "𝔾₁"
else: "𝔾₂"
const curve = EC.getName()

const dst = "Constantine_Zkalc_Bench_HashToCurve"
# Gnark uses a message of size 54, probably to not spill over padding with SHA256
let msg = "Privacy is necessary for an open society [...]"

type EC = EC_ShortW_Jac[Fp[curve], G1]
var P {.noInit.}: EC

let stats = bench():
Expand Down Expand Up @@ -424,7 +423,7 @@ proc benchMultiPairing*(rng: var RngState, curve: static Algebra, maxNumInputs:
Ps = newSeq[EC_ShortW_Aff[Fp[curve], G1]](maxNumInputs)
Qs = newSeq[EC_ShortW_Aff[Fp2[curve], G2]](maxNumInputs)

stdout.write &"Generating {maxNumInputs} (𝔾1, 𝔾2) pairs ... "
stdout.write &"Generating {maxNumInputs} (𝔾₁, 𝔾₂) pairs ... "
stdout.flushFile()

let start = getMonotime()
Expand All @@ -451,7 +450,7 @@ proc benchMultiPairing*(rng: var RngState, curve: static Algebra, maxNumInputs:
# Run benches
# -------------------------------------------------------------------------------------

proc runBenches(curve: static Algebra, useVartime: bool) =
proc runBenches(curve: static Algebra, useVartime: bool): ZkalcBenchResult =
var rng: RngState
rng.seed(42)

Expand Down Expand Up @@ -512,18 +511,23 @@ proc runBenches(curve: static Algebra, useVartime: bool) =
zkalc.multipairing = rng.benchMultiPairing(curve, maxNumInputs = 1024)
separator()

return zkalc

proc main() =
let cmd = commandLineParams()
cmd.getOpt (curve: BN254_Snarks, vartime: true)

case curve
of BN254_Snarks: BN254_Snarks.runBenches(vartime)
of Pallas: Pallas .runBenches(vartime)
of Vesta: Vesta .runBenches(vartime)
of BLS12_377: BLS12_377 .runBenches(vartime)
of BLS12_381: BLS12_381 .runBenches(vartime)
else:
echo "This curve '" & $curve & "' is not configured for benchmarking at the moment."
cmd.getOpt (curve: BN254_Snarks, vartime: true, o: "constantine-bench-zkalc-" & $curve & "-" & now().format("yyyy-MM-dd--HH-mm-ss") & ".bench.json")

let results =
case curve
of BN254_Snarks: BN254_Snarks.runBenches(vartime)
of Pallas: Pallas .runBenches(vartime)
of Vesta: Vesta .runBenches(vartime)
of BLS12_377: BLS12_377 .runBenches(vartime)
of BLS12_381: BLS12_381 .runBenches(vartime)
else:
raise newException(ValueError, "This curve '" & $curve & "' is not configured for benchmarking at the moment.")

writeFile(o, results.toJSON())

when isMainModule:
main()

0 comments on commit 868c6bd

Please sign in to comment.