nim-dagger/benchmarks/run_benchmarks.nim

103 lines
2.8 KiB
Nim
Raw Normal View History

2024-05-02 01:07:36 +03:00
import std/[sequtils, strutils, strformat, os, options, importutils, times, os, strutils, terminal]
2024-04-24 16:50:32 +03:00
2024-04-23 20:21:39 +03:00
import pkg/questionable
import pkg/questionable/results
import pkg/datastore
2024-05-02 01:09:44 +03: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 20:21:39 +03:00
2024-05-02 15:56:05 +03:00
import ./utils
2024-05-02 01:09:50 +03:00
import ./create_circuits
2024-05-02 00:12:24 +03:00
type CircuitFiles* = object
r1cs*: string
wasm*: string
zkey*: string
inputs*: string
2024-05-02 15:47:12 +03:00
proc runArkCircom(args: CircuitArgs, files: CircuitFiles) =
2024-04-24 15:20:46 +03:00
echo "Loading sample proof..."
2024-04-24 15:17:16 +03:00
var
2024-05-02 00:12:24 +03:00
inputData = files.inputs.readFile()
2024-04-24 15:17:16 +03:00
inputJson = !JsonNode.parse(inputData)
proofInputs = Poseidon2Hash.jsonToProofInput(inputJson)
2024-04-24 17:28:53 +03:00
circom = CircomCompat.init(
2024-05-02 00:12:24 +03:00
files.r1cs,
files.wasm,
files.zkey,
2024-04-24 17:28:53 +03:00
slotDepth = args.depth,
numSamples = args.nsamples,
)
2024-04-24 16:50:32 +03:00
defer:
2024-05-02 00:12:24 +03:00
circom.release() # this comes from the rust FFI
2024-04-24 15:17:16 +03:00
2024-04-24 15:20:46 +03:00
echo "Sample proof loaded..."
echo "Proving..."
2024-04-24 16:50:32 +03:00
var proof: CircomProof
2024-04-24 16:53:13 +03:00
benchmark fmt"prover":
2024-04-24 16:50:32 +03:00
proof = circom.prove(proofInputs).tryGet
2024-04-24 15:17:16 +03:00
2024-04-24 16:50:54 +03:00
var verRes: bool
2024-04-24 16:53:13 +03:00
benchmark fmt"verify":
2024-04-24 16:50:32 +03:00
verRes = circom.verify(proof, proofInputs).tryGet
2024-04-24 15:28:06 +03:00
echo "verify result: ", verRes
2024-05-02 15:47:12 +03:00
proc runRapidSnark(args: CircuitArgs, files: CircuitFiles) =
2024-05-02 00:12:24 +03:00
# time rapidsnark ${CIRCUIT_MAIN}.zkey witness.wtns proof.json public.json
2024-04-24 15:11:55 +03:00
2024-05-02 00:12:24 +03:00
echo "generating the witness..."
2024-05-02 15:56:24 +03:00
## TODO
2024-05-02 00:12:24 +03:00
2024-05-02 15:48:49 +03:00
proc runBenchmark(args: CircuitArgs, env: CircuitEnv) =
2024-05-02 00:12:24 +03:00
## execute benchmarks given a set of args
## will create a folder in `benchmarks/circuit_bench_$(args)`
##
2024-05-02 15:48:49 +03:00
let env = createCircuit(args, env)
2024-05-02 00:12:24 +03: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)
proc startBenchmarks*() =
2024-04-24 16:50:32 +03:00
echo "Running benchmark"
# setup()
2024-05-02 15:48:49 +03:00
var env = CircuitEnv.default()
env.check()
2024-05-02 15:47:12 +03:00
var args = CircuitArgs(
2024-04-24 16:50:32 +03: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 12:53:34 +03:00
nsamples: 1, # number of samples to prove
2024-04-24 16:50:32 +03: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 15:48:49 +03:00
for i in 1 .. 2:
2024-04-24 16:53:13 +03:00
args.nsamples = i
2024-04-29 12:53:34 +03:00
stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
2024-05-02 15:49:54 +03:00
runBenchmark(args, env)
2024-04-29 12:53:34 +03:00
2024-05-02 00:12:24 +03:00
# for i in 1..16:
# args.nsamples = 10*i
# stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
# args.runBenchmark()
when isMainModule:
startBenchmarks()