This commit is contained in:
Jaremy Creechley 2024-05-02 00:12:24 +03:00
parent b0a39912f5
commit 6352392d87
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 78 additions and 58 deletions

View File

@ -7,7 +7,6 @@ import std/importutils
import std/[times, os, strutils]
import std/terminal
import pkg/questionable
import pkg/questionable/results
import pkg/datastore
@ -30,24 +29,30 @@ import codex/slots/backends/helpers
import create_circuits
type CircuitFiles* = object
r1cs*: string
wasm*: string
zkey*: string
inputs*: string
template benchmark(benchmarkName: string, blk: untyped) =
let nn = 5
var vals = newSeqOfCap[float](nn)
for i in 1..nn:
for i in 1 .. nn:
block:
let t0 = epochTime()
`blk`
let elapsed = epochTime() - t0
vals.add elapsed
var elapsedStr = ""
for v in vals:
elapsedStr &= ", " & v.formatFloat(format = ffDecimal, precision = 3)
stdout.styledWriteLine(fgGreen, "CPU Time [", benchmarkName, "] ", "avg(", $nn, "): ", elapsedStr, " s")
stdout.styledWriteLine(
fgGreen, "CPU Time [", benchmarkName, "] ", "avg(", $nn, "): ", elapsedStr, " s"
)
proc setup(
circuitDir: string, name: string,
) =
proc setup(circuitDir: string, name: string) =
let
inputData = readFile("tests/circuits/fixtures/input.json")
inputJson: JsonNode = !JsonNode.parse(inputData)
@ -60,31 +65,21 @@ proc setup(
let ver = datasetProof.verify(proofInput.slotRoot, proofInput.datasetRoot).tryGet
echo "ver: ", ver
proc runBenchmark(args: CircArgs) =
let env = createCircuit(args)
## TODO: copy over testcircomcompat proving
let
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"
proc runArkCircom(args: CircArgs, files: CircuitFiles) =
echo "Loading sample proof..."
var
inputData = inputs.readFile()
inputData = files.inputs.readFile()
inputJson = !JsonNode.parse(inputData)
proofInputs = Poseidon2Hash.jsonToProofInput(inputJson)
circom = CircomCompat.init(
r1cs,
wasm,
zkey,
files.r1cs,
files.wasm,
files.zkey,
slotDepth = args.depth,
numSamples = args.nsamples,
)
defer:
circom.release() # this comes from the rust FFI
circom.release() # this comes from the rust FFI
echo "Sample proof loaded..."
echo "Proving..."
@ -104,8 +99,29 @@ proc runBenchmark(args: CircArgs) =
let proof = circom.prove(proofInputs).tryGet
echo "verify bad result: ", circom.verify(proof, proofInputs).tryGet
proc runRapidSnark(args: CircArgs, files: CircuitFiles) =
# time rapidsnark ${CIRCUIT_MAIN}.zkey witness.wtns proof.json public.json
when isMainModule:
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*() =
echo "Running benchmark"
# setup()
checkEnv()
@ -122,12 +138,15 @@ when isMainModule:
ncells: 512, # number of cells in this slot
)
for i in 1..9:
for i in 1 .. 9:
args.nsamples = i
stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
args.runBenchmark()
for i in 1..16:
args.nsamples = 10*i
stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
args.runBenchmark()
# for i in 1..16:
# args.nsamples = 10*i
# stdout.styledWriteLine(fgYellow, "\nbenchmarking args: ", $args)
# args.runBenchmark()
when isMainModule:
startBenchmarks()

View File

@ -1,9 +1,4 @@
import std/hashes
import std/json
import std/strutils
import std/strformat
import std/os
import std/osproc
import std/[hashes, json, strutils, strformat, os, osproc]
template withDir(dir: string, blk: untyped) =
let prev = getCurrentDir()
@ -24,16 +19,27 @@ proc setProjDir(prev = getCurrentDir()): string =
else:
getCurrentDir()
var
nimCircuitCli =
"vendor" / "codex-storage-proofs-circuits" / "reference" / "nim" / "proof_input" /
"cli"
circuitDirIncludes = "vendor" / "codex-storage-proofs-circuits" / "circuit"
ptauDefPath = "benchmarks" / "ceremony" / "powersOfTau28_hez_final_23.ptau"
ptauDefUrl =
# "https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_21.ptau"
"https://storage.googleapis.com/zkevm/ptau/"
codexProjDir = ""
type
CircuitEnv* = object
nimCircuitCli =
"vendor" / "codex-storage-proofs-circuits" / "reference" / "nim" / "proof_input" /
"cli"
circuitDirIncludes = "vendor" / "codex-storage-proofs-circuits" / "circuit"
ptauDefPath = "benchmarks" / "ceremony" / "powersOfTau28_hez_final_23.ptau"
ptauDefUrl = "https://storage.googleapis.com/zkevm/ptau/"
codexProjDir = ""
CircArgs* = object
depth*: int
maxslots*: int
cellsize*: int
blocksize*: int
nsamples*: int
entropy*: int
seed*: int
nslots*: int
ncells*: int
index*: int
proc checkEnv*() =
codexProjDir = setProjDir()
@ -79,18 +85,6 @@ proc checkEnv*() =
ptauDefPath = ptauDefPath.absolutePath
echo "Found PTAU file: ", ptauDefPath
type CircArgs* = object
depth*: int
maxslots*: int
cellsize*: int
blocksize*: int
nsamples*: int
entropy*: int
seed*: int
nslots*: int
ncells*: int
index*: int
proc downloadPtau*(ptauPath, ptauUrl: string) =
if not ptauPath.fileExists:
echo "Ceremony file not found, downloading..."
@ -135,6 +129,7 @@ proc createCircuit*(
ptauPath = ptauDefPath,
ptauUrl = ptauDefUrl & ptauPath.splitPath.tail,
someEntropy = "some_entropy_75289v3b7rcawcsyiur",
doGenerateWitness = false,
): tuple[dir: string, name: string] =
## Generates all the files needed for to run a proof circuit. Downloads the PTAU file if needed.
##
@ -147,9 +142,11 @@ proc createCircuit*(
withDir circdir:
writeFile("circuit_params.json", pretty(%*args))
let
inputs = circdir / "input.json"
zkey = circdir / fmt"{name}.zkey"
wasm = circdir / fmt"{name}.wasm"
r1cs = circdir / fmt"{name}.r1cs"
wtns = circdir / fmt"{name}.wtns"
generateCircomAndSamples(args, name)
@ -183,6 +180,10 @@ proc createCircuit*(
moveFile(fmt"{name}_0001.zkey", fmt"{name}.zkey")
removeFile(fmt"{name}_0000.zkey")
if not wtns.fileExists and doGenerateWitness:
let cmd = fmt"node generate_witness.js {wtns} ../input.json ../witness.wtns"
execShellCmd(cmd)
return (circdir, name)
when isMainModule:
@ -201,4 +202,4 @@ when isMainModule:
ncells: 512, # number of cells in this slot
)
let benchenv = createCircuit(args)
echo "\nBench dir:\n", benchenv
echo "\nBench dir:\n", benchenv