From 30740a86b5378f606fe75cd360eec1b543424499 Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Fri, 15 Dec 2023 14:52:26 +0100 Subject: [PATCH] add generating the main component to the Nim proof input generator --- reference/nim/proof_input/README.md | 35 ++++++++++++++ reference/nim/proof_input/src/cli.nim | 69 ++++++++++++++++++++------- 2 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 reference/nim/proof_input/README.md diff --git a/reference/nim/proof_input/README.md b/reference/nim/proof_input/README.md new file mode 100644 index 0000000..9503c0f --- /dev/null +++ b/reference/nim/proof_input/README.md @@ -0,0 +1,35 @@ + +Proof circuit input generator +----------------------------- + +This is a Nim program to generate inputs for the proof circuit, and also the +main component of the circuit (it needs some global parameters). + +Note: this is only for testing purposes, it's not optimized, and does not +use real data. + +### Usage + + $ ./cli [options] --output=proof_input.json --circom=proof_main.circom + + available options: + -d, --depth = : maximum depth of the slot tree (eg. 32) + -N, --maxslots = : maximum number of slots (eg. 256) + -c, --cellSize = : cell size in bytes (eg. 2048) + -b, --blockSize = : block size in bytes (eg. 65536) + -s, --nslots = : number of slots in the dataset (eg. 13) + -n, --nsamples = : number of samples we prove (eg. 100) + -e, --entropy = : external randomness (eg. 1234567) + -S, --seed = : seed to generate the fake data (eg. 12345) + -f, --file = : slot data file + -i, --index = : index of the slot (within the dataset) we prove + -k, --log2ncells = : log2 of the number of cells inside this slot (eg. 10) + -K, --ncells = : number of cells inside this slot (eg. 1024; must be a power of two) + -o, --output = : the JSON file into which we write the proof input + -C, --circom = : the circom main component to create with these parameters + +You can generate only the `.json` or only the `.circom` file, or both at the same +time. + +There are some default values for all the rest of the parameters. + diff --git a/reference/nim/proof_input/src/cli.nim b/reference/nim/proof_input/src/cli.nim index aa2cfea..adca220 100644 --- a/reference/nim/proof_input/src/cli.nim +++ b/reference/nim/proof_input/src/cli.nim @@ -28,14 +28,15 @@ type FullConfig = object globCfg: GlobalConfig dsetCfg: DataSetConfig slotIndex: int - outFile: string entropy: int + outFile: string + circomFile: string const defGlobCfg = GlobalConfig( maxDepth: 32 , maxLog2NSlots: 8 , cellSize: 2048 - , blockSize: 4096 + , blockSize: 65536 ) const defDSetCfg = @@ -46,18 +47,19 @@ const defDSetCfg = ) const defFullCfg = - FullConfig( globCfg: defGlobCfg - , dsetCfg: defDSetCfg - , slotIndex: 0 - , outFile: "out.json" - , entropy: 1234567 + FullConfig( globCfg: defGlobCfg + , dsetCfg: defDSetCfg + , slotIndex: 0 + , outFile: "" + , circomFile: "" + , entropy: 1234567 ) #------------------------------------------------------------------------------- proc printHelp() = echo "usage:" - echo "$ ./cli [options] " + echo "$ ./cli [options] --output=proof_input.json --circom=proof_main.circom" echo "" echo "available options:" echo " -d, --depth = : maximum depth of the slot tree (eg. 32)" @@ -68,10 +70,12 @@ proc printHelp() = echo " -n, --nsamples = : number of samples we prove (eg. 100)" echo " -e, --entropy = : external randomness (eg. 1234567)" echo " -S, --seed = : seed to generate the fake data (eg. 12345)" - echo " -f, --file = : slot data file" + echo " -f, --file = : slot data file" echo " -i, --index = : index of the slot (within the dataset) we prove" echo " -k, --log2ncells = : log2 of the number of cells inside this slot (eg. 10)" echo " -K, --ncells = : number of cells inside this slot (eg. 1024; must be a power of two)" + echo " -o, --output = : the JSON file into which we write the proof input" + echo " -C, --circom = : the circom main component to create with these parameters" echo "" quit() @@ -92,7 +96,6 @@ proc parseCliOptions(): FullConfig = # Positional arguments of cmdArgument: # echo ("arg #" & $argCtr & " = " & key) - if argCtr == 0: fullCfg.outFile = key argCtr += 1 # Switches @@ -112,6 +115,8 @@ proc parseCliOptions(): FullConfig = of "i", "index" : fullCfg.slotIndex = parseInt(value) of "k", "log2ncells": dsetCfg.ncells = pow2(parseInt(value)) of "K", "ncells" : dsetCfg.ncells = checkPowerOfTwo(parseInt(value),"nCells") + of "o", "output" : fullCfg.outFile = value + of "C", "circom" : fullCfg.circomFile = value else: echo "Unknown option: ", key echo "use --help to get a list of options" @@ -120,11 +125,6 @@ proc parseCliOptions(): FullConfig = of cmdEnd: discard - if argctr != 1: - echo "expecting exactly 1 position argument, the name of the output JSON file" - echo "use --help for getting a list of options" - quit() - fullCfg.globCfg = globCfg fullCfg.dsetCfg = dsetCfg @@ -132,10 +132,45 @@ proc parseCliOptions(): FullConfig = #------------------------------------------------------------------------------- +proc writeCircomMainComponent(fullCfg: FullConfig, fname: string) = + let blockTreeDepth = exactLog2(fullCfg.globCfg.blockSize div fullCfg.globCfg.cellSize) + let nFieldElemsPerCell = (fullCfg.globCfg.cellSize + 30) div 31 + + let params: (int,int,int,int,int) = + ( fullCfg.globCfg.maxDepth + , fullCfg.globCfg.maxLog2NSlots + , blockTreeDepth + , nFieldElemsPerCell + , fullCfg.dsetCfg.nSamples + ) + + let f = open(fname, fmWrite) + defer: f.close() + + f.writeLine("pragma circom 2.0.0;") + f.writeLine("include \"sample_cells.circom\";") + f.writeLine("// SampleAndProven( maxDepth, maxLog2NSlots, blockTreeDepth, nFieldElemsPerCell, nSamples )") + f.writeLine("component main {public [entropy,dataSetRoot,slotIndex]} = SampleAndProve" & ($params) & ");") + +#------------------------------------------------------------------------------- + when isMainModule: let fullCfg = parseCliOptions() # echo fullCfg - let prfInput = generateProofInput( fullCfg.globCfg, fullCfg.dsetCfg, fullCfg.slotIndex, toF(fullCfg.entropy) ) - exportProofInput( fullCfg.outFile , prfInput ) + if fullCfg.circomFile == "" and fullCfg.outFile == "": + echo "nothing do!" + echo "use --help for getting a list of options" + quit() + + if fullCfg.circomFile != "": + echo "writing circom main component into `" & fullCfg.circomFile & "`" + writeCircomMainComponent(fullCfg, fullCfg.circomFile) + + if fullCfg.outFile != "": + echo "writing proof input into `" & fullCfg.outFile & "`..." + let prfInput = generateProofInput( fullCfg.globCfg, fullCfg.dsetCfg, fullCfg.slotIndex, toF(fullCfg.entropy) ) + exportProofInput( fullCfg.outFile , prfInput ) + + echo "done"