From f9af4fa84f84d2130b2ae727c29c66c86d8a80e0 Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Fri, 15 Dec 2023 13:13:49 +0100 Subject: [PATCH] add Nim CLI application to generate proof inputs --- reference/nim/proof_input/.gitignore | 1 + reference/nim/proof_input/proof_input.nimble | 2 +- reference/nim/proof_input/src/cli.nim | 141 +++++++++++++++++++ reference/nim/proof_input/src/dataset.nim | 8 +- reference/nim/proof_input/src/merkle.nim | 2 +- reference/nim/proof_input/src/misc.nim | 10 +- reference/nim/proof_input/src/sample.nim | 2 +- reference/nim/proof_input/src/slot.nim | 2 +- 8 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 reference/nim/proof_input/src/cli.nim diff --git a/reference/nim/proof_input/.gitignore b/reference/nim/proof_input/.gitignore index 737146a..e2cf927 100644 --- a/reference/nim/proof_input/.gitignore +++ b/reference/nim/proof_input/.gitignore @@ -1,4 +1,5 @@ .DS_Store testmain +cli *.json json/ \ No newline at end of file diff --git a/reference/nim/proof_input/proof_input.nimble b/reference/nim/proof_input/proof_input.nimble index 325f5e4..263f639 100644 --- a/reference/nim/proof_input/proof_input.nimble +++ b/reference/nim/proof_input/proof_input.nimble @@ -4,7 +4,7 @@ author = "Balazs Komuves" description = "reference implementation for generating the proof inputs" license = "MIT or Apache-2.0" srcDir = "src" -bin = @["testmain"] +bin = @["cli","testmain"] requires "nim >= 1.6.0" requires "https://github.com/mratsim/constantine" diff --git a/reference/nim/proof_input/src/cli.nim b/reference/nim/proof_input/src/cli.nim new file mode 100644 index 0000000..aa2cfea --- /dev/null +++ b/reference/nim/proof_input/src/cli.nim @@ -0,0 +1,141 @@ + +{. warning[UnusedImport]:off .} + +import sugar +import std/strutils +import std/sequtils +import std/os +import std/parseopt + +import constantine/math/arithmetic + +import poseidon2/types +import poseidon2/merkle + +import types +import blocks +import slot +import dataset +import sample +import merkle +import gen_input +import json +import misc + +#------------------------------------------------------------------------------- + +type FullConfig = object + globCfg: GlobalConfig + dsetCfg: DataSetConfig + slotIndex: int + outFile: string + entropy: int + +const defGlobCfg = + GlobalConfig( maxDepth: 32 + , maxLog2NSlots: 8 + , cellSize: 2048 + , blockSize: 4096 + ) + +const defDSetCfg = + DataSetConfig( nCells: 256 + , nSamples: 5 + , nSlots: 11 + , dataSrc: DataSource(kind: FakeData, seed: 12345) + ) + +const defFullCfg = + FullConfig( globCfg: defGlobCfg + , dsetCfg: defDSetCfg + , slotIndex: 0 + , outFile: "out.json" + , entropy: 1234567 + ) + +#------------------------------------------------------------------------------- + +proc printHelp() = + echo "usage:" + echo "$ ./cli [options] " + echo "" + echo "available options:" + echo " -d, --depth = : maximum depth of the slot tree (eg. 32)" + echo " -N, --maxslots = : maximum number of slots (eg. 256)" + echo " -c, --cellSize = : cell size in bytes (eg. 2048)" + echo " -b, --blockSize = : block size in bytes (eg. 65536)" + echo " -s, --nslots = : number of slots in the dataset (eg. 13)" + 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 " -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 "" + + quit() + +#------------------------------------------------------------------------------- + +proc parseCliOptions(): FullConfig = + + var argCtr: int = 0 + + var globCfg = defGlobCfg + var dsetCfg = defDSetCfg + var fullCfg = defFullCfg + + for kind, key, value in getOpt(): + case kind + + # Positional arguments + of cmdArgument: + # echo ("arg #" & $argCtr & " = " & key) + if argCtr == 0: fullCfg.outFile = key + argCtr += 1 + + # Switches + of cmdLongOption, cmdShortOption: + case key + + of "h", "help" : printHelp() + of "d", "depth" : globCfg.maxDepth = parseInt(value) + of "N", "maxslots" : globCfg.maxLog2NSlots = ceilingLog2(parseInt(value)) + of "c", "cellSize" : globCfg.cellSize = checkPowerOfTwo(parseInt(value),"cellSize") + of "b", "blockSize" : globCfg.blockSize = checkPowerOfTwo(parseInt(value),"blockSize") + of "s", "nslots" : dsetCfg.nSlots = parseInt(value) + of "n", "nsamples" : dsetCfg.nsamples = parseInt(value) + of "e", "entropy" : fullCfg.entropy = parseInt(value) + of "S", "seed" : dsetCfg.dataSrc = DataSource(kind: FakeData, seed: uint64(parseInt(value))) + of "f", "file" : dsetCfg.dataSrc = DataSource(kind: SlotFile, filename: value) + of "i", "index" : fullCfg.slotIndex = parseInt(value) + of "k", "log2ncells": dsetCfg.ncells = pow2(parseInt(value)) + of "K", "ncells" : dsetCfg.ncells = checkPowerOfTwo(parseInt(value),"nCells") + else: + echo "Unknown option: ", key + echo "use --help to get a list of options" + quit() + + 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 + + return fullCfg + +#------------------------------------------------------------------------------- + +when isMainModule: + + let fullCfg = parseCliOptions() + # echo fullCfg + + let prfInput = generateProofInput( fullCfg.globCfg, fullCfg.dsetCfg, fullCfg.slotIndex, toF(fullCfg.entropy) ) + exportProofInput( fullCfg.outFile , prfInput ) diff --git a/reference/nim/proof_input/src/dataset.nim b/reference/nim/proof_input/src/dataset.nim index 75e9097..ab80674 100644 --- a/reference/nim/proof_input/src/dataset.nim +++ b/reference/nim/proof_input/src/dataset.nim @@ -1,12 +1,12 @@ -import sugar +#import sugar -import std/streams -import std/sequtils +#import std/streams +#import std/sequtils import types -import blocks import slot +#import blocks #------------------------------------------------------------------------------- # Example slot configuration diff --git a/reference/nim/proof_input/src/merkle.nim b/reference/nim/proof_input/src/merkle.nim index fcacb88..4914fc7 100644 --- a/reference/nim/proof_input/src/merkle.nim +++ b/reference/nim/proof_input/src/merkle.nim @@ -6,8 +6,8 @@ import constantine/math/arithmetic import constantine/math/io/io_fields import poseidon2/types -import poseidon2/merkle import poseidon2/compress +#import poseidon2/merkle import types diff --git a/reference/nim/proof_input/src/misc.nim b/reference/nim/proof_input/src/misc.nim index b00c412..92dff1b 100644 --- a/reference/nim/proof_input/src/misc.nim +++ b/reference/nim/proof_input/src/misc.nim @@ -21,9 +21,17 @@ func ceilingLog2* (x: int): int = else: return (floorLog2(x-1) + 1) -func exactLog2( x: int): int = +func exactLog2*(x: int): int = let k = ceilingLog2(x) assert( x == 2^k, "exactLog2: not a power of two" ) return k +func checkPowerOfTwo*(x: int , what: string): int = + let k = ceilingLog2(x) + assert( x == 2^k, ("`" & what & "` is expected to be a power of 2") ) + return x + +# wtf Nim, serously +func pow2*(k: int): int = 2^k + #------------------------------------------------------------------------------- diff --git a/reference/nim/proof_input/src/sample.nim b/reference/nim/proof_input/src/sample.nim index 2f8d67a..897dd40 100644 --- a/reference/nim/proof_input/src/sample.nim +++ b/reference/nim/proof_input/src/sample.nim @@ -5,8 +5,8 @@ import std/bitops import constantine/math/arithmetic import poseidon2/types -import poseidon2/io import poseidon2/sponge +#import poseidon2/io import types import misc diff --git a/reference/nim/proof_input/src/slot.nim b/reference/nim/proof_input/src/slot.nim index a16015f..08d13d0 100644 --- a/reference/nim/proof_input/src/slot.nim +++ b/reference/nim/proof_input/src/slot.nim @@ -5,7 +5,7 @@ import std/streams import std/sequtils import types -import blocks +#import blocks #------------------------------------------------------------------------------- # Example slot configuration