nim-dagger/benchmarks/ark_prover_bench.nim

133 lines
3.5 KiB
Nim
Raw Normal View History

2024-05-01 22:07:36 +00:00
import std/[sequtils, strutils, strformat, os, options, importutils, times, os, strutils, terminal]
2024-04-24 13:50:32 +00:00
2024-04-23 17:21:39 +00:00
import pkg/questionable
import pkg/questionable/results
import pkg/datastore
import pkg/codex/rng
import pkg/codex/stores
import pkg/codex/merkletree
import pkg/codex/utils/json
import pkg/codex/codextypes
import pkg/codex/slots
import pkg/codex/slots/builder
import pkg/codex/utils/poseidon2digest
import pkg/codex/slots/sampler/utils
import pkg/constantine/math/arithmetic
import pkg/constantine/math/io/io_bigints
import pkg/constantine/math/io/io_fields
import codex/slots/backends/helpers
import create_circuits
2024-05-01 21:12:24 +00:00
type CircuitFiles* = object
r1cs*: string
wasm*: string
zkey*: string
inputs*: string
2024-04-24 13:50:32 +00:00
template benchmark(benchmarkName: string, blk: untyped) =
2024-04-25 14:25:27 +00:00
let nn = 5
var vals = newSeqOfCap[float](nn)
2024-05-01 21:12:24 +00:00
for i in 1 .. nn:
2024-04-24 13:50:32 +00:00
block:
let t0 = epochTime()
2024-04-29 12:16:57 +00:00
`blk`
2024-04-24 13:50:32 +00:00
let elapsed = epochTime() - t0
2024-04-25 14:25:27 +00:00
vals.add elapsed
2024-05-01 21:12:24 +00:00
2024-04-25 14:25:27 +00:00
var elapsedStr = ""
for v in vals:
elapsedStr &= ", " & v.formatFloat(format = ffDecimal, precision = 3)
2024-05-01 21:12:24 +00:00
stdout.styledWriteLine(
fgGreen, "CPU Time [", benchmarkName, "] ", "avg(", $nn, "): ", elapsedStr, " s"
)
2024-04-24 13:50:32 +00:00
2024-05-01 21:12:24 +00:00
proc runArkCircom(args: CircArgs, files: CircuitFiles) =
2024-04-24 12:20:46 +00:00
echo "Loading sample proof..."
2024-04-24 12:17:16 +00:00
var
2024-05-01 21:12:24 +00:00
inputData = files.inputs.readFile()
2024-04-24 12:17:16 +00:00
inputJson = !JsonNode.parse(inputData)
proofInputs = Poseidon2Hash.jsonToProofInput(inputJson)
2024-04-24 14:28:53 +00:00
circom = CircomCompat.init(
2024-05-01 21:12:24 +00:00
files.r1cs,
files.wasm,
files.zkey,
2024-04-24 14:28:53 +00:00
slotDepth = args.depth,
numSamples = args.nsamples,
)
2024-04-24 13:50:32 +00:00
defer:
2024-05-01 21:12:24 +00:00
circom.release() # this comes from the rust FFI
2024-04-24 12:17:16 +00:00
2024-04-24 12:20:46 +00:00
echo "Sample proof loaded..."
echo "Proving..."
2024-04-24 13:50:32 +00:00
var proof: CircomProof
2024-04-24 13:53:13 +00:00
benchmark fmt"prover":
2024-04-24 13:50:32 +00:00
proof = circom.prove(proofInputs).tryGet
2024-04-24 12:17:16 +00:00
2024-04-24 13:50:54 +00:00
var verRes: bool
2024-04-24 13:53:13 +00:00
benchmark fmt"verify":
2024-04-24 13:50:32 +00:00
verRes = circom.verify(proof, proofInputs).tryGet
2024-04-24 12:28:06 +00:00
echo "verify result: ", verRes
2024-04-24 13:50:32 +00:00
when false:
2024-04-24 12:28:06 +00:00
proofInputs.slotIndex = 1 # change slot index
let proof = circom.prove(proofInputs).tryGet
echo "verify bad result: ", circom.verify(proof, proofInputs).tryGet
2024-04-24 12:17:16 +00:00
2024-05-01 21:12:24 +00:00
proc runRapidSnark(args: CircArgs, files: CircuitFiles) =
# time rapidsnark ${CIRCUIT_MAIN}.zkey witness.wtns proof.json public.json
2024-04-24 12:11:55 +00:00
2024-05-01 21:12:24 +00:00
echo "generating the witness..."
proc runBenchmark(args: CircArgs) =
## execute benchmarks given a set of args
## will create a folder in `benchmarks/circuit_bench_$(args)`
##
let env = createCircuit(args)
## TODO: copy over testcircomcompat proving
let files = CircuitFiles(
r1cs: env.dir / fmt"{env.name}.r1cs",
wasm: env.dir / fmt"{env.name}.wasm",
zkey: env.dir / fmt"{env.name}.zkey",
inputs: env.dir / fmt"input.json",
)
runArkCircom(args, files)
proc startBenchmarks*() =
2024-04-24 13:50:32 +00:00
echo "Running benchmark"
# setup()
checkEnv()
var args = CircArgs(
depth: 32, # maximum depth of the slot tree
maxslots: 256, # maximum number of slots
cellsize: 2048, # cell size in bytes
blocksize: 65536, # block size in bytes
2024-04-29 09:53:34 +00:00
nsamples: 1, # number of samples to prove
2024-04-24 13:50:32 +00:00
entropy: 1234567, # external randomness
seed: 12345, # seed for creating fake data
nslots: 11, # number of slots in the dataset
index: 3, # which slot we prove (0..NSLOTS-1)
ncells: 512, # number of cells in this slot
)
2024-05-01 21:12:24 +00:00
for i in 1 .. 9:
2024-04-24 13:53:13 +00:00
args.nsamples = i
2024-04-29 09:53:34 +00:00
stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
args.runBenchmark()
2024-05-01 21:12:24 +00:00
# for i in 1..16:
# args.nsamples = 10*i
# stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
# args.runBenchmark()
when isMainModule:
startBenchmarks()