mirror of
https://github.com/logos-storage/nim-groth16.git
synced 2026-01-08 00:23:09 +00:00
nimbify package
This commit is contained in:
parent
5ce7926e92
commit
4b8487b0df
@ -1,6 +1,7 @@
|
||||
import pkg/groth16
|
||||
|
||||
import ../test_proof
|
||||
import ../export_json
|
||||
import ../tests/test_proof
|
||||
import ../src/export_json
|
||||
|
||||
let zkey_fname : string = "./build/product.zkey"
|
||||
let wtns_fname : string = "./build/product.wtns"
|
||||
@ -8,4 +9,3 @@ let proof = testProveAndVerify( zkey_fname, wtns_fname)
|
||||
|
||||
exportPublicIO( "./build/nim_public.json" , proof )
|
||||
exportProof( "./build/nim_proof.json" , proof )
|
||||
|
||||
|
||||
13
groth16.nimble
Normal file
13
groth16.nimble
Normal file
@ -0,0 +1,13 @@
|
||||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
author = "Dmitriy Ryajov"
|
||||
description = "A circom/snarkjs compatible groth16 implementation"
|
||||
license = "MIT"
|
||||
srcDir = "src"
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 1.6.14",
|
||||
"https://github.com/mratsim/constantine#5f7ba18"
|
||||
49
misc.nim
49
misc.nim
@ -1,49 +0,0 @@
|
||||
|
||||
#
|
||||
# miscellaneous routines
|
||||
#
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
func delta*(i, j: int) : int =
|
||||
return (if (i == j): 1 else: 0)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
func floorLog2* (x : int) : int =
|
||||
var k = -1
|
||||
var y = x
|
||||
while (y > 0):
|
||||
k += 1
|
||||
y = y shr 1
|
||||
return k
|
||||
|
||||
func ceilingLog2* (x : int) : int =
|
||||
if (x==0):
|
||||
return -1
|
||||
else:
|
||||
return (floorLog2(x-1) + 1)
|
||||
|
||||
#-------------------
|
||||
|
||||
#[
|
||||
import std/math
|
||||
|
||||
proc sanityCheckLog2* () =
|
||||
for i in 0..18:
|
||||
let x = float64(i)
|
||||
echo( i," | ",floorLog2(i),"=",floor(log2(x))," | ",ceilingLog2(i),"=",ceil(log2(x)) )
|
||||
]#
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#[
|
||||
func rotateSeq[T](xs: seq[T], ofs: int): seq[T] =
|
||||
let n = xs.len
|
||||
var ys : seq[T]
|
||||
for i in (0..<n):
|
||||
ys.add( xs[ (i+n+ofs) mod n ] )
|
||||
return ys
|
||||
]#
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
44
src/misc.nim
Normal file
44
src/misc.nim
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
#
|
||||
# miscellaneous routines
|
||||
#
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
func floorLog2* (x : int) : int =
|
||||
var k = -1
|
||||
var y = x
|
||||
while (y > 0):
|
||||
k += 1
|
||||
y = y shr 1
|
||||
return k
|
||||
|
||||
func ceilingLog2* (x : int) : int =
|
||||
if (x==0):
|
||||
return -1
|
||||
else:
|
||||
return (floorLog2(x-1) + 1)
|
||||
|
||||
#-------------------
|
||||
|
||||
when isMainModule:
|
||||
|
||||
import std/math
|
||||
|
||||
proc sanityCheckLog2* () =
|
||||
for i in 0..18:
|
||||
let x = float64(i)
|
||||
echo( i," | ",floorLog2(i),"=",floor(log2(x))," | ",ceilingLog2(i),"=",ceil(log2(x)) )
|
||||
|
||||
sanityCheckLog2()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
func rotateSeq[T](xs: seq[T], ofs: int): seq[T] =
|
||||
let n = xs.len
|
||||
var ys : seq[T]
|
||||
for i in (0..<n):
|
||||
ys.add( xs[ (i+n+ofs) mod n ] )
|
||||
return ys
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
@ -269,15 +269,19 @@ func polyInverseNTT*(ys: seq[Fr], D: Domain): Poly =
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#[
|
||||
|
||||
proc sanityCheckOneHalf*() =
|
||||
when isMainModule:
|
||||
proc sanityCheckOneHalf*() =
|
||||
let two = oneFr + oneFr
|
||||
let invTwo = oneHalfFr
|
||||
echo(toDecimalFr(two))
|
||||
echo(toDecimalFr(invTwo * two))
|
||||
echo(toHex(invTwo))
|
||||
|
||||
proc sanityCheckVanishing*() =
|
||||
var js : seq[int] = toSeq(101..112)
|
||||
let cs : seq[Fr] = map( js, intToFr )
|
||||
let P : Poly = Poly( coeffs:cs )
|
||||
|
||||
#-------------------
|
||||
|
||||
proc sanityCheckVanishing*() =
|
||||
@ -285,9 +289,6 @@ proc sanityCheckVanishing*() =
|
||||
let cs : seq[Fr] = map( js, intToFr )
|
||||
let P : Poly = Poly( coeffs:cs )
|
||||
|
||||
echo("degree of P = ",polyDegree(P))
|
||||
debugPrintFrSeq("xs", P.coeffs)
|
||||
|
||||
let n : int = 5
|
||||
let QR = polyQuotRemByVanishing(P, n)
|
||||
let Q = QR.quot
|
||||
@ -302,6 +303,20 @@ proc sanityCheckVanishing*() =
|
||||
debugPrintFrSeq("zs", S.coeffs)
|
||||
echo( polyIsEqual(P,S) )
|
||||
|
||||
proc sanityCheckNTT*() =
|
||||
var js : seq[int] = toSeq(101..108)
|
||||
let cs : seq[Fr] = map( js, intToFr )
|
||||
let P : Poly = Poly( coeffs:cs )
|
||||
let D : Domain = createDomain(8)
|
||||
let xs : seq[Fr] = D.enumerateDomain()
|
||||
let ys : seq[Fr] = collect( newSeq, (for x in xs: polyEvalAt(P,x)) )
|
||||
let zs : seq[Fr] = polyForwardNTT(P ,D)
|
||||
let Q : Poly = polyInverseNTT(zs,D)
|
||||
debugPrintFrSeq("xs", xs)
|
||||
debugPrintFrSeq("ys", ys)
|
||||
debugPrintFrSeq("zs", zs)
|
||||
debugPrintFrSeq("us", Q.coeffs)
|
||||
|
||||
#-------------------
|
||||
|
||||
proc sanityCheckNTT*() =
|
||||
@ -325,10 +340,6 @@ proc sanityCheckMulFFT*() =
|
||||
let cs : seq[Fr] = map( js, intToFr )
|
||||
let P : Poly = Poly( coeffs:cs )
|
||||
|
||||
var ks : seq[int] = toSeq(1001..1020)
|
||||
let ds : seq[Fr] = map( ks, intToFr )
|
||||
let Q : Poly = Poly( coeffs:ds )
|
||||
|
||||
let R1 : Poly = polyMulNaive( P , Q )
|
||||
let R2 : Poly = polyMulFFT( P , Q )
|
||||
|
||||
@ -337,6 +348,8 @@ proc sanityCheckMulFFT*() =
|
||||
|
||||
echo( "multiply test = ", polyIsEqual(R1,R2) )
|
||||
|
||||
echo( "multiply test = ", polyIsEqual(R1,R2) )
|
||||
|
||||
#-------------------
|
||||
|
||||
proc sanityCheckLagrangeBases*() =
|
||||
@ -39,7 +39,7 @@ proc parseSection1_header( stream: Stream, user: var Witness, sectionLen: int )
|
||||
# echo "\nparsing witness header"
|
||||
|
||||
let (n8r, r) = parsePrimeField( stream ) # size of the scalar field
|
||||
user.r = r;
|
||||
user.r = r
|
||||
|
||||
# echo("r = ",toDecimalBig(r))
|
||||
|
||||
@ -50,7 +50,7 @@ proc parseSection1_header( stream: Stream, user: var Witness, sectionLen: int )
|
||||
user.curve = "bn128"
|
||||
|
||||
let nvars = int( stream.readUint32() )
|
||||
user.nvars = nvars;
|
||||
user.nvars = nvars
|
||||
|
||||
# echo("nvars = ",nvars)
|
||||
|
||||
1
tests/config.nims
Normal file
1
tests/config.nims
Normal file
@ -0,0 +1 @@
|
||||
switch("path", "$projectDir/../src")
|
||||
@ -1,10 +1,8 @@
|
||||
|
||||
import ./groth16
|
||||
import ./witness
|
||||
import ./r1cs
|
||||
import ./zkey
|
||||
import ./zkey_types
|
||||
import ./fake_setup
|
||||
import pkg/groth16
|
||||
import pkg/witness
|
||||
import pkg/zkey
|
||||
import pkg/zkey_types
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user