2024-05-02 13:26:31 +00:00
|
|
|
import std/[sequtils, strformat, os, options, importutils]
|
2024-05-02 14:37:05 +00:00
|
|
|
import std/[times, os, strutils, terminal, parseopt]
|
2024-05-02 13:26:31 +00:00
|
|
|
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/datastore
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
import ./utils
|
|
|
|
import ./create_circuits
|
|
|
|
|
|
|
|
type CircuitFiles* = object
|
|
|
|
r1cs*: string
|
|
|
|
wasm*: string
|
|
|
|
zkey*: string
|
|
|
|
inputs*: string
|
2024-05-02 14:37:05 +00:00
|
|
|
dir*: string
|
|
|
|
circName*: string
|
2024-05-02 13:26:31 +00:00
|
|
|
|
2024-05-02 19:39:11 +00:00
|
|
|
proc runArkCircom(
|
|
|
|
args: CircuitArgs, files: CircuitFiles, proofInputs: ProofInputs[Poseidon2Hash]
|
|
|
|
) =
|
2024-05-02 13:26:31 +00:00
|
|
|
echo "Loading sample proof..."
|
2024-05-02 19:39:11 +00:00
|
|
|
var circom = CircomCompat.init(
|
|
|
|
files.r1cs,
|
|
|
|
files.wasm,
|
|
|
|
files.zkey,
|
|
|
|
slotDepth = args.depth,
|
|
|
|
numSamples = args.nsamples,
|
|
|
|
)
|
2024-05-02 13:26:31 +00:00
|
|
|
defer:
|
|
|
|
circom.release() # this comes from the rust FFI
|
|
|
|
|
|
|
|
echo "Sample proof loaded..."
|
|
|
|
echo "Proving..."
|
|
|
|
|
2024-05-02 15:18:39 +00:00
|
|
|
var proof: CircomProof = circom.prove(proofInputs).tryGet
|
2024-05-02 13:26:31 +00:00
|
|
|
|
2024-05-02 15:18:39 +00:00
|
|
|
var verRes: bool = circom.verify(proof, proofInputs).tryGet
|
|
|
|
if not verRes:
|
|
|
|
echo "verification failed"
|
|
|
|
quit 100
|
2024-05-02 13:26:31 +00:00
|
|
|
|
2024-05-02 14:14:52 +00:00
|
|
|
proc printHelp() =
|
|
|
|
echo "usage:"
|
2024-05-02 19:28:16 +00:00
|
|
|
echo " ./codex_ark_prover_cli [options] "
|
2024-05-02 14:14:52 +00:00
|
|
|
echo ""
|
|
|
|
echo "available options:"
|
|
|
|
echo " -h, --help : print this help"
|
|
|
|
echo " -v, --verbose : verbose output (print the actual parameters)"
|
2024-05-24 12:45:37 +00:00
|
|
|
echo " --r1cs:$FILE : r1cs file path"
|
|
|
|
echo " --wasm:$FILE : wasm file path"
|
|
|
|
echo " --zkey:$FILE : zkey file path"
|
|
|
|
echo " --inputs:$FILE : inputs.json file path"
|
2024-05-02 15:10:41 +00:00
|
|
|
# echo " -S, --seed = <seed> : seed to generate the fake data (eg. 12345)"
|
2024-05-02 14:14:52 +00:00
|
|
|
echo ""
|
2024-05-02 19:35:23 +00:00
|
|
|
echo "Must provide files options. Use either:"
|
|
|
|
echo " --dir:$CIRCUIT_DIR --name:$CIRCUIT_NAME"
|
|
|
|
echo "or:"
|
|
|
|
echo " --r1cs:$R1CS --wasm:$WASM --zkey:$ZKEY"
|
|
|
|
echo ""
|
2024-05-02 14:14:52 +00:00
|
|
|
|
2024-05-02 14:37:05 +00:00
|
|
|
quit(1)
|
2024-05-02 14:14:52 +00:00
|
|
|
|
2024-05-02 14:37:05 +00:00
|
|
|
proc parseCliOptions(args: var CircuitArgs, files: var CircuitFiles) =
|
2024-05-02 14:14:52 +00:00
|
|
|
var argCtr: int = 0
|
2024-05-02 19:35:23 +00:00
|
|
|
template expectPath(val: string): string =
|
|
|
|
if val == "":
|
|
|
|
echo "ERROR: expected path a but got empty for: ", key
|
|
|
|
printHelp()
|
|
|
|
val.absolutePath
|
2024-05-02 14:14:52 +00:00
|
|
|
|
|
|
|
for kind, key, value in getOpt():
|
|
|
|
case kind
|
|
|
|
|
|
|
|
# Positional arguments
|
|
|
|
of cmdArgument:
|
2024-05-02 19:35:23 +00:00
|
|
|
echo "\nERROR: got unexpected arg: ", key, "\n"
|
|
|
|
printHelp()
|
2024-05-02 14:14:52 +00:00
|
|
|
|
|
|
|
# Switches
|
|
|
|
of cmdLongOption, cmdShortOption:
|
|
|
|
case key
|
2024-05-02 19:39:11 +00:00
|
|
|
of "h", "help":
|
|
|
|
printHelp()
|
|
|
|
of "d", "depth":
|
|
|
|
args.depth = parseInt(value)
|
|
|
|
of "r1cs":
|
|
|
|
files.r1cs = value.expectPath()
|
|
|
|
of "wasm":
|
|
|
|
files.wasm = value.expectPath()
|
|
|
|
of "zkey":
|
|
|
|
files.zkey = value.expectPath()
|
|
|
|
of "inputs":
|
|
|
|
files.inputs = value.expectPath()
|
|
|
|
of "dir":
|
|
|
|
files.dir = value.expectPath()
|
|
|
|
of "name":
|
|
|
|
files.circName = value
|
2024-05-02 14:14:52 +00:00
|
|
|
else:
|
|
|
|
echo "Unknown option: ", key
|
|
|
|
echo "use --help to get a list of options"
|
|
|
|
quit()
|
|
|
|
of cmdEnd:
|
2024-05-02 19:39:11 +00:00
|
|
|
discard
|
2024-05-02 14:14:52 +00:00
|
|
|
|
2024-05-02 13:26:31 +00:00
|
|
|
proc run*() =
|
2024-05-02 19:36:43 +00:00
|
|
|
## Run Codex Ark/Circom based prover
|
|
|
|
##
|
|
|
|
echo "Running prover"
|
2024-05-02 14:37:05 +00:00
|
|
|
|
|
|
|
# prove wasm ${CIRCUIT_MAIN}.zkey witness.wtns proof.json public.json
|
2024-05-02 13:26:31 +00:00
|
|
|
|
|
|
|
var
|
2024-05-02 14:37:05 +00:00
|
|
|
args = CircuitArgs()
|
|
|
|
files = CircuitFiles()
|
|
|
|
|
|
|
|
parseCliOptions(args, files)
|
|
|
|
|
2024-05-02 19:39:11 +00:00
|
|
|
let dir =
|
|
|
|
if files.dir != "":
|
|
|
|
files.dir
|
|
|
|
else:
|
|
|
|
getCurrentDir()
|
2024-05-02 14:46:51 +00:00
|
|
|
if files.circName != "":
|
2024-05-02 19:39:11 +00:00
|
|
|
if files.r1cs == "":
|
|
|
|
files.r1cs = dir / fmt"{files.circName}.r1cs"
|
|
|
|
if files.wasm == "":
|
|
|
|
files.wasm = dir / fmt"{files.circName}.wasm"
|
|
|
|
if files.zkey == "":
|
|
|
|
files.zkey = dir / fmt"{files.circName}.zkey"
|
2024-05-02 14:46:51 +00:00
|
|
|
|
2024-05-02 19:39:11 +00:00
|
|
|
if files.inputs == "":
|
|
|
|
files.inputs = dir / fmt"input.json"
|
2024-05-02 14:14:52 +00:00
|
|
|
|
2024-05-02 15:18:39 +00:00
|
|
|
echo "Got file args: ", files
|
|
|
|
|
2024-05-02 15:16:00 +00:00
|
|
|
var fileErrors = false
|
|
|
|
template checkFile(file, name: untyped) =
|
|
|
|
if file == "" or not file.fileExists():
|
|
|
|
echo "\nERROR: must provide `" & name & "` file"
|
|
|
|
fileErrors = true
|
|
|
|
|
|
|
|
checkFile files.inputs, "inputs.json"
|
|
|
|
checkFile files.r1cs, "r1cs"
|
|
|
|
checkFile files.wasm, "wasm"
|
|
|
|
checkFile files.zkey, "zkey"
|
|
|
|
|
|
|
|
if fileErrors:
|
2024-05-02 19:35:23 +00:00
|
|
|
echo "ERROR: couldn't find all files"
|
|
|
|
printHelp()
|
2024-05-02 15:16:00 +00:00
|
|
|
|
2024-05-02 14:37:05 +00:00
|
|
|
var
|
|
|
|
inputData = files.inputs.readFile()
|
2024-05-02 15:03:06 +00:00
|
|
|
inputs: JsonNode = !JsonNode.parse(inputData)
|
2024-05-02 13:26:31 +00:00
|
|
|
|
2024-05-02 19:36:43 +00:00
|
|
|
# sets default values for these args
|
2024-05-02 19:39:11 +00:00
|
|
|
if args.depth == 0:
|
|
|
|
args.depth = codextypes.DefaultMaxSlotDepth
|
|
|
|
# maximum depth of the slot tree
|
|
|
|
if args.maxslots == 0:
|
|
|
|
args.maxslots = 256
|
|
|
|
# maximum number of slots
|
2024-05-02 15:03:06 +00:00
|
|
|
|
2024-05-02 19:36:43 +00:00
|
|
|
# sets number of samples to take
|
2024-05-02 19:39:11 +00:00
|
|
|
if args.nsamples == 0:
|
|
|
|
args.nsamples = 1
|
|
|
|
# number of samples to prove
|
2024-05-02 15:03:06 +00:00
|
|
|
|
2024-05-02 19:36:43 +00:00
|
|
|
# overrides the input.json params
|
2024-05-02 19:39:11 +00:00
|
|
|
if args.entropy != 0:
|
|
|
|
inputs["entropy"] = %($args.entropy)
|
|
|
|
if args.nslots != 0:
|
|
|
|
inputs["nSlotsPerDataSet"] = %args.nslots
|
|
|
|
if args.index != 0:
|
|
|
|
inputs["slotIndex"] = %args.index
|
|
|
|
if args.ncells != 0:
|
|
|
|
inputs["nCellsPerSlot"] = %args.ncells
|
|
|
|
|
|
|
|
var proofInputs = Poseidon2Hash.jsonToProofInput(inputs)
|
2024-05-02 14:46:51 +00:00
|
|
|
|
2024-05-02 14:37:05 +00:00
|
|
|
echo "Got args: ", args
|
2024-05-02 15:18:39 +00:00
|
|
|
runArkCircom(args, files, proofInputs)
|
2024-05-02 13:26:31 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
run()
|