nim-dagger/benchmarks/run_benchmarks.nim

103 lines
2.8 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
2024-05-01 22:09:44 +00:00
import pkg/codex/[rng, stores, merkletree, codextypes, slots]
import pkg/codex/utils/[json, poseidon2digest]
import pkg/codex/slots/[builder, sampler/utils, backends/helpers]
import pkg/constantine/math/[arithmetic, io/io_bigints, io/io_fields]
2024-04-23 17:21:39 +00:00
2024-05-02 12:56:05 +00:00
import ./utils
2024-05-01 22:09:50 +00:00
import ./create_circuits
2024-05-01 21:12:24 +00:00
type CircuitFiles* = object
r1cs*: string
wasm*: string
zkey*: string
inputs*: string
2024-05-02 12:47:12 +00:00
proc runArkCircom(args: CircuitArgs, 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-05-02 12:47:12 +00:00
proc runRapidSnark(args: CircuitArgs, files: CircuitFiles) =
2024-05-01 21:12:24 +00:00
# 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..."
2024-05-02 12:56:24 +00:00
## TODO
2024-05-01 21:12:24 +00:00
2024-05-02 12:48:49 +00:00
proc runBenchmark(args: CircuitArgs, env: CircuitEnv) =
2024-05-01 21:12:24 +00:00
## execute benchmarks given a set of args
## will create a folder in `benchmarks/circuit_bench_$(args)`
##
2024-05-02 12:48:49 +00:00
let env = createCircuit(args, env)
2024-05-01 21:12:24 +00:00
## 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)
2024-05-02 13:04:58 +00:00
proc runAllBenchmarks*() =
2024-04-24 13:50:32 +00:00
echo "Running benchmark"
# setup()
2024-05-02 12:48:49 +00:00
var env = CircuitEnv.default()
env.check()
2024-05-02 12:47:12 +00:00
var args = CircuitArgs(
2024-04-24 13:50:32 +00:00
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-02 12:48:49 +00:00
for i in 1 .. 2:
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)
2024-05-02 12:49:54 +00:00
runBenchmark(args, env)
2024-04-29 09:53:34 +00:00
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:
2024-05-02 13:04:58 +00:00
runAllBenchmarks()