diff --git a/README.md b/README.md index e433d34..abae8ea 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,13 @@ at your choice. ### TODO +- [ ] find and fix the _second_ totally surreal bug - [ ] clean up the code +- [ ] make it compatible with the latest constantine and also Nim 2.0.x - [x] make it a nimble package -- [/] refactor `bn128.nim` into smaller files -- [/] proper MSM implementation (at first I couldn't make constantine's one to work) -- [x] compare `.r1cs` to the "coeffs" section of `.zkey` +- [ ] compare `.r1cs` to the "coeffs" section of `.zkey` - [x] generate fake circuit-specific setup ourselves +- [x] make a CLI interface - [ ] multithreading support (MSM, and possibly also FFT) - [ ] add Groth16 notes - [ ] document the `snarkjs` circuit-specific setup `H` points convention diff --git a/cli/cli_main.nim b/cli/cli_main.nim index 1ebff17..8514a5d 100644 --- a/cli/cli_main.nim +++ b/cli/cli_main.nim @@ -4,7 +4,7 @@ import std/strutils import std/sequtils import std/os import std/parseopt -import std/[times,os] +import std/times import std/options import strformat @@ -26,6 +26,7 @@ proc printHelp() = echo "available options:" echo " -h, --help : print this help" echo " -v, --verbose : verbose output" + echo " -d, --debug : debug output" echo " -t, --time : print time measurements" echo " -p, --prove : create a proof" echo " -y, --verify : verify a proof" @@ -45,6 +46,7 @@ type Config = object output_file: string io_file: string verbose: bool + debug: bool measure_time: bool do_prove: bool do_verify: bool @@ -95,6 +97,7 @@ proc parseCliOptions(): Config = of "h", "help" : printHelp() of "v", "verbose" : cfg.verbose = true + of "d", "debug" : cfg.debug = true of "t", "time" : cfg.measure_time = true of "p", "prove" : cfg.do_prove = true of "y", "verify" : cfg.do_verify = true @@ -190,6 +193,10 @@ proc cliMain(cfg: Config) = let elapsed = cpuTime() - start if cfg.measure_time: echo("fake setup took ",seconds(elapsed)) + if cfg.debug: + printGrothHeader(zkey.header) + # debugPrintCoeffs(zkey.coeffs) + if cfg.do_prove: if (cfg.wtns_file=="") or (cfg.zkey_file=="" and cfg.do_setup==false): echo("cannot prove: missing witness and/or zkey file!") diff --git a/groth16/prover.nim b/groth16/prover.nim index 0b03da1..5b0577b 100644 --- a/groth16/prover.nim +++ b/groth16/prover.nim @@ -36,32 +36,34 @@ type curve* : string #------------------------------------------------------------------------------- -# A, B, C column vectors +# Az, Bz, Cz column vectors # type ABC = object - valuesA : seq[Fr] - valuesB : seq[Fr] - valuesC : seq[Fr] + valuesAz : seq[Fr] + valuesBz : seq[Fr] + valuesCz : seq[Fr] +# computes the vectors A*z, B*z, C*z where z is the witness func buildABC( zkey: ZKey, witness: seq[Fr] ): ABC = let hdr: GrothHeader = zkey.header let domSize = hdr.domainSize - var valuesA : seq[Fr] = newSeq[Fr](domSize) - var valuesB : seq[Fr] = newSeq[Fr](domSize) + var valuesAz : seq[Fr] = newSeq[Fr](domSize) + var valuesBz : seq[Fr] = newSeq[Fr](domSize) + for entry in zkey.coeffs: case entry.matrix - of MatrixA: valuesA[entry.row] += entry.coeff * witness[entry.col] - of MatrixB: valuesB[entry.row] += entry.coeff * witness[entry.col] + of MatrixA: valuesAz[entry.row] += entry.coeff * witness[entry.col] + of MatrixB: valuesBz[entry.row] += entry.coeff * witness[entry.col] else: raise newException(AssertionDefect, "fatal error") - var valuesC : seq[Fr] = newSeq[Fr](domSize) + var valuesCz : seq[Fr] = newSeq[Fr](domSize) for i in 0.. # func computeSnarkjsScalarCoeffs( abc: ABC ): seq[Fr] = - let n = abc.valuesA.len - assert( abc.valuesB.len == n ) - assert( abc.valuesC.len == n ) + let n = abc.valuesAz.len + assert( abc.valuesBz.len == n ) + assert( abc.valuesCz.len == n ) let D = createDomain(n) let eta = createDomain(2*n).domainGen - let A1 = shiftEvalDomain( abc.valuesA, D, eta ) - let B1 = shiftEvalDomain( abc.valuesB, D, eta ) - let C1 = shiftEvalDomain( abc.valuesC, D, eta ) + let A1 = shiftEvalDomain( abc.valuesAz, D, eta ) + let B1 = shiftEvalDomain( abc.valuesBz, D, eta ) + let C1 = shiftEvalDomain( abc.valuesCz, D, eta ) var ys : seq[Fr] = newSeq[Fr]( n ) for j in 0..