add Nim CLI application to generate proof inputs

This commit is contained in:
Balazs Komuves 2023-12-15 13:13:49 +01:00
parent fea3d520b4
commit f9af4fa84f
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
8 changed files with 159 additions and 9 deletions

View File

@ -1,4 +1,5 @@
.DS_Store
testmain
cli
*.json
json/

View File

@ -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"

View File

@ -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] <proof_input.json>"
echo ""
echo "available options:"
echo " -d, --depth = <maxdepth> : maximum depth of the slot tree (eg. 32)"
echo " -N, --maxslots = <maxslots> : maximum number of slots (eg. 256)"
echo " -c, --cellSize = <cellSize> : cell size in bytes (eg. 2048)"
echo " -b, --blockSize = <blockSize> : block size in bytes (eg. 65536)"
echo " -s, --nslots = <nslots> : number of slots in the dataset (eg. 13)"
echo " -n, --nsamples = <nsamples> : number of samples we prove (eg. 100)"
echo " -e, --entropy = <entropy> : external randomness (eg. 1234567)"
echo " -S, --seed = <seed> : seed to generate the fake data (eg. 12345)"
echo " -f, --file = <datafile> : slot data file"
echo " -i, --index = <slotIndex> : index of the slot (within the dataset) we prove"
echo " -k, --log2ncells = <log2(ncells)> : log2 of the number of cells inside this slot (eg. 10)"
echo " -K, --ncells = <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 )

View File

@ -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

View File

@ -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

View File

@ -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
#-------------------------------------------------------------------------------

View File

@ -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

View File

@ -5,7 +5,7 @@ import std/streams
import std/sequtils
import types
import blocks
#import blocks
#-------------------------------------------------------------------------------
# Example slot configuration