Repository navigation
Use dynamic prime field with modulus sampled at runtime - #141
frozenspider wants to merge 12 commits into
Conversation
422fe57 to
b9767a0
Compare
# Conflicts: # crates/circuit/src/constraints.rs
b9767a0 to
9796a7d
Compare
zkfriendly
left a comment
There was a problem hiding this comment.
The PR looks well structured overall. Cross-checked everything with the PoC repo and left some comments regarding correctness and soundness. Other stuff looks good (Prime candidate sampling, width selection, and verification look correct and compatible with bounds we have. Wide mul and Barret reduction look correct too.)
| let sum = unsafe { self.reduced_value.unchecked_add(rhs.reduced_value) }; | ||
| self.reduced_value = if sum >= modulus { sum - modulus } else { sum }; |
There was a problem hiding this comment.
This may not actually be safe. Main reason is the global modulus can change (through prove and verify for example) and then doing arithmetic on values that remain accessible could result to an overflow:
For example, x = 100 is valid modulo 101. After changing the modulus to 3:
x += x: 100 + 100 - 3 = 197
x += x: 197 + 197 - 3 = 391
x += x: 391 + 391 - 3 = 779
Eventually, x could overflow!
| } | ||
| let terminal = verify_spartan_proof(&mut transcript, &self.matrices, &proof.spartan) | ||
| let prime = transcript.squeeze_prime(self.prime_bits); | ||
| let (params, matrices) = self.under_prime(prime, &prime_lock, &mut transcript)?; |
There was a problem hiding this comment.
The PoC explicitly validates the case that a value from a larger prime fits. Worth having that check here too: https://github.com/albert-garreta/BitZ-PoC/blob/8ced3b985a188e9ffb03d2550737cbb4a7345f2b/src/piop/spartan/piop.rs#L1254
| transcript.public_message(&(self.claim_shape.log_rows() as u64)); | ||
| transcript.public_message(&(self.claim_shape.log_columns() as u64)); | ||
| transcript.public_message(&self.generator); |
There was a problem hiding this comment.
These binds are not enough; My test changed integer coefficients while preserving earlier transcript inputs; the sampled prime stayed unchanged.
This PR currently follows:
Commitment + public bytes → choose prime q → absorb matrix digest → Spartan
which means we sample the prime before binding integer coefficients, instead as in the PoC repo we need to do this:
Commitment + public bytes + matrix digest → choose prime q → Spartan
Basically here we need to add this too:
transcript.public_message(self.matrices.digest());
| let values = [&self.values[0], &self.values[1], &self.values[2]] | ||
| .map(|values| values.par_iter().map(&map).collect()); |
There was a problem hiding this comment.
Here, similar to PoC we could only project distinct coeffs, for example, our 32-block SHA fixture contains 1,785,960 entries but only 133 distinct coefficients. we could defer this to optimisations
Resolves #136
Introduce$\le 126$ bit modulus that is squeezed from Fiat-Shamir transcript after the commitment and statement are bound. Number of bits to use in this prime depend on the claim shape.
DynField, a field withA lot of code is shared with
Fq, this shared code has been moved to a newhelpersmodule.This field relies on a globally shared modulus, and on an assumption that nobody would change that while field elements still exists. This convention is sadly not enforced by the type system.
[Remark: We could avoid this problem completely by passing around
FieldConfig, but that's a bit cumbersome]To sample a random prime, we use small prime numbers table sieve, followed by Miller-Rabin primality test, based on the PoC repo implementation.
E2E performance is quite close to static field, and can be optimized further. This is notable given that P/V can no longer do prime projection at the setup phase (setup got a bit faster).
Notable tricks used:
Vec<_>, withLayoutspecifying the sparse matrix layout.BigInttoFieldprojection has been optimized foru128-wide cases (in practice - pretty much all of them)Performance
To measure performance, I used a circuit
sha256-2kbRUSTFLAGS="-C target-cpu=native" cargo run --release -p bitz-cli -- circuit-e2e --circuit sha256-2kbsha256-2kbNotes:
mainbranch we normally useFq<Q100>, a 100-bit prime. To make it comparable, I usedFq<2230695117703373733444658220249201783>