Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groth16 prover buggy draft #463

Draft
wants to merge 35 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
787cd97
fix scalarMul_vartime for tiny multiple 5
Vindaar Jul 12, 2024
adbfb79
WIP commit of our groth16 work
Vindaar Aug 5, 2024
7a049b3
add partially manual Groth16 proof file
Vindaar Aug 13, 2024
9c4ed12
minor comments, cleanup, TODOs
Vindaar Aug 13, 2024
43f857e
add note about typed R1CS
Vindaar Aug 13, 2024
e3f0c06
add 'typed' variant of Zkey
Vindaar Aug 13, 2024
f648d56
add 'typed' variant of Witness file data
Vindaar Aug 13, 2024
030f53c
move `asEC*`, `randomFieldElement` to utils
Vindaar Aug 13, 2024
e963b27
use 'typed' form of zkey / witnessss data objects
Vindaar Aug 13, 2024
9c2eea0
[tests] add test file for finite field FFT
Vindaar Aug 14, 2024
0eb92c7
[groth16] whitespace
Vindaar Aug 14, 2024
df3235f
clean up FFT for finite fields
Vindaar Aug 14, 2024
7454619
remove old FFT finite field test file
Vindaar Aug 14, 2024
713ec2a
remove old TODO from FFT
Vindaar Aug 14, 2024
c11c375
move groth16 files one dir up (proof_systems)
Vindaar Aug 14, 2024
282c138
add notes about binary zkey, wtns file format / parser
Vindaar Aug 14, 2024
fdca197
fixup code for changed path, minor cleanup
Vindaar Aug 14, 2024
a45e722
bind sysrand in utils
Vindaar Aug 15, 2024
42f2b5b
export some modules, export prover & prove
Vindaar Aug 15, 2024
9e06129
bind two identifiers in `sumImpl`
Vindaar Aug 15, 2024
c968b10
[example] add Groth16 prover example
Vindaar Aug 15, 2024
c203284
[examples] clean up outputs of code blocks
Vindaar Aug 15, 2024
22c85ea
remove left over `:END:`
Vindaar Aug 15, 2024
f1585a7
remove old echoes
Vindaar Aug 15, 2024
1ddefb0
export EC related modules from Groth16
Vindaar Aug 15, 2024
2387443
update tangled example Nim code
Vindaar Aug 15, 2024
30243f5
[io_fields] annotate `fromDecimal` with raises, pop `raises: []` before
Vindaar Aug 15, 2024
fe1608c
[tests] add groth16 prover test case, binary files for test
Vindaar Aug 15, 2024
24358cd
export wtns field of Witness section
Vindaar Aug 15, 2024
916548f
[tests] add test case for Witness binary parser
Vindaar Aug 15, 2024
dddceba
[tests] add `.zkey` parser test case
Vindaar Aug 20, 2024
56dfdf6
[tests] add expected zkey test files as JSON data
Vindaar Aug 20, 2024
05a8612
[nimble] add finite field FFT, Groth16 related tests to nimble file
Vindaar Aug 20, 2024
a4abd39
add booldefine variables to choose what is Montgomery encoded
Vindaar Aug 22, 2024
d48f0a0
add a bunch more echo outputs for SnarkJS comparison
Vindaar Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions constantine.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ const testDesc: seq[tuple[path: string, useGMP: bool]] = @[
# Polynomials
# ----------------------------------------------------------
("tests/math_polynomials/t_polynomials.nim", false),
("tests/math_polynomials/t_finite_field_fft.nim", false),

# Protocols
# ----------------------------------------------------------
Expand All @@ -571,6 +572,9 @@ const testDesc: seq[tuple[path: string, useGMP: bool]] = @[
# Proof systems
# ----------------------------------------------------------
("tests/proof_systems/t_r1cs_parser.nim", false),
("tests/proof_systems/t_wtns_parser.nim", false),
("tests/proof_systems/t_zkey_parser.nim", false),
("tests/proof_systems/t_groth16_prover.nim", false),
("tests/interactive_proofs/t_multilinear_extensions.nim", false),
]

Expand Down
13 changes: 13 additions & 0 deletions constantine/math/arithmetic/finite_fields.nim
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,19 @@ func pow_vartime*(a: var FF, exponent: openarray[byte]) =
FF.getSpareBits()
)

func pow_vartime*(a: var FF, exponent: FF) =
## Exponentiation modulo p
## ``a``: a field element to be exponentiated
## ``exponent``: a field element
##
## Warning ⚠️ :
## This is an optimization for public exponent
## Otherwise bits of the exponent can be retrieved with:
## - memory access analysis
## - power analysis
## - timing analysis
a.pow_vartime(toBig exponent)

func pow_squareMultiply_vartime(a: var FF, exponent: SomeUnsignedInt) {.tags:[VarTime], meter.} =
## **Variable-time** Exponentiation
##
Expand Down
6 changes: 3 additions & 3 deletions constantine/math/arithmetic/finite_fields_square_root.nim
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ func invsqrt_if_square_vartime*[Name](r: var Fp[Name], a: Fp[Name]): SecretBool
# Legendre symbol / Euler's Criterion / Kronecker's symbol
# ------------------------------------------------------------

func isSquare*(a: Fp): SecretBool =
func isSquare*(a: FF): SecretBool =
## Returns true if ``a`` is a square (quadratic residue) in 𝔽p
##
## Assumes that the prime modulus ``p`` is public.
var aa {.noInit.}: Fp.getBigInt()
var aa {.noInit.}: FF.getBigInt()
aa.fromField(a)
let symbol = legendre(aa.limbs, Fp.getModulus().limbs, aa.bits)
let symbol = legendre(aa.limbs, FF.getModulus().limbs, aa.bits)
return not(symbol == MaxWord)

{.pop.} # inline
Expand Down
2 changes: 1 addition & 1 deletion constantine/math/elliptic/ec_scalar_mul_vartime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func scalarMul_addchain_4bit_vartime[EC](P: var EC, scalar: BigInt) {.tags:[VarT
of 5:
var t {.noInit.}: EC
t.double(P)
t.double(P)
t.double()
P ~+= t
of 6:
var t {.noInit.}: EC
Expand Down
4 changes: 3 additions & 1 deletion constantine/math/elliptic/ec_shortweierstrass_jacobian.nim
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ template sumImpl[F; G: static Subgroup](

# if P or R were infinity points they would have spread 0 with Z₁Z₂
block: # Infinity points
bind isNeutral
bind ccopy
o.ccopy(Q, P.isNeutral())
o.ccopy(P, Q.isNeutral())

Expand Down Expand Up @@ -1028,7 +1030,7 @@ func `~-`*(a: EC_ShortW_Jac, b: EC_ShortW_Aff): EC_ShortW_Jac {.noInit, inline.}
## This MUST NOT be used with secret data.
##
## This is highly VULNERABLE to timing attacks and power analysis attacks.]
##
##
## Out-of-place functions SHOULD NOT be used in performance-critical subroutines as compilers
## tend to generate useless memory moves or have difficulties to minimize stack allocation
## and our types might be large (Fp12 ...)
Expand Down
25 changes: 25 additions & 0 deletions constantine/math/io/io_ec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ func toHex*[EC: EC_ShortW_Prj or EC_ShortW_Jac or EC_ShortW_Aff or EC_ShortW_Jac
result.appendHex(aff.y)
result &= "\n" & sp & ")"

func toDecimal*[EC: EC_ShortW_Prj or EC_ShortW_Jac or EC_ShortW_Aff or EC_ShortW_JacExt](P: EC, indent: static int = 0): string =
## Stringify an elliptic curve point to Hex
## Note. Leading zeros are not removed.
## Output as decimal.
##
## WARNING: NOT constant time!
##
## This proc output may change format in the future

var aff {.noInit.}: EC_ShortW_Aff[EC.F, EC.G]
when EC isnot EC_ShortW_Aff:
aff.affine(P)
else:
aff = P

const sp = spaces(indent)

result = sp & $EC & "(\n" & sp & " x: "
result.add toDecimal(aff.x)
result &= ",\n" & sp & " y: "
result.add toDecimal(aff.y)
result &= "\n" & sp & ")"



func toHex*[EC: EC_TwEdw_Aff or EC_TwEdw_Prj](P: EC, indent: static int = 0): string =
## Stringify an elliptic curve point to Hex for Twisted Edwards Curve
## Note, leading zeros are not removed.
Expand Down
34 changes: 34 additions & 0 deletions constantine/math/io/io_extfields.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,40 @@ func toHex*(f: ExtensionField, indent = 0, order: static Endianness = bigEndian)
## - no leaks
result.appendHex(f, indent, order)

func appendDecimal*(accum: var string, f: Fp, indent = 0, order: static Endianness = bigEndian) =
accum.add toDecimal(f)

func appendDecimal*(accum: var string, f: ExtensionField, indent = 0, order: static Endianness = bigEndian) =
## Stringify a tower field element to hex.
## Note. Leading zeros are not removed.
## Result is prefixed with 0x
##
## Output will be padded with 0s to maintain constant-time.
##
## CT:
## - no leaks
accum.add static($f.typeof.genericHead() & '(')
staticFor i, 0, f.coords.len:
when i != 0:
accum.add ", "
accum.add "\n" & spaces(indent+2) & "c" & $i & ": "
when f is Fp2:
accum.appendDecimal(f.coords[i], order = order)
else:
accum.appendDecimal(f.coords[i], indent+2, order)
accum.add ")"

func toDecimal*(f: ExtensionField, indent = 0, order: static Endianness = bigEndian): string =
## Stringify a tower field element to hex.
## Note. Leading zeros are not removed.
## Result is prefixed with 0x
##
## Output will be padded with 0s to maintain constant-time.
##
## CT:
## - no leaks
result.appendDecimal(f, indent, order)

func fromHex*(dst: var Fp2, c0, c1: string) =
## Convert 2 coordinates to an element of 𝔽p2
## with dst = c0 + β * c1
Expand Down
6 changes: 4 additions & 2 deletions constantine/math/io/io_fields.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func toDecimal*(f: FF): string =
## This function is NOT constant-time at the moment.
f.toBig().toDecimal()

func fromDecimal*(dst: var FF, decimalString: string) =
{.pop.} # `fromDecimal` can raise `ValueError`

func fromDecimal*(dst: var FF, decimalString: string) {.raises: [ValueError].} =
## Convert a decimal string. The input must be packed
## with no spaces or underscores.
## This assumes that bits and decimal length are **public.**
Expand All @@ -136,7 +138,7 @@ func fromDecimal*(dst: var FF, decimalString: string) =
let raw {.noinit.} = fromDecimal(dst.mres.typeof, decimalString)
dst.fromBig(raw)

func fromDecimal*(T: type FF, hexString: string): T {.noInit.}=
func fromDecimal*(T: type FF, hexString: string): T {.raises: [ValueError], noInit.}=
## Convert a decimal string. The input must be packed
## with no spaces or underscores.
## This assumes that bits and decimal length are **public.**
Expand Down
2 changes: 1 addition & 1 deletion constantine/math/polynomials/fft.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func fft_internal[EC; bits: static int](

for i in 0 ..< half:
# FFT Butterfly
y_times_root .scalarMul_vartime(output[i+half], rootsOfUnity[i])
y_times_root .scalarMul_vartime(rootsOfUnity[i], output[i+half])
output[i+half] .diff_vartime(output[i], y_times_root)
output[i] .sum_vartime(output[i], y_times_root)

Expand Down
Loading
Loading