Skip to content

Commit

Permalink
debugging sumcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jan 12, 2024
1 parent 5718bbf commit 9e8bc25
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 945 deletions.
42 changes: 22 additions & 20 deletions src/gadgets/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<E: Engine> LookupTrace<E> {
let read_value_term = gamma.mul(cs.namespace(|| "read_value_term"), read_value)?;
// counter_term = gamma^2 * counter
let read_counter_term = gamma_square.mul(cs.namespace(|| "read_counter_term"), read_counter)?;
// new_R = R + 1 / (alpha - (addr + gamma * value + gamma^2 * counter))
// new_R = R + 1 / (alpha + (addr + gamma * value + gamma^2 * counter))
let new_R = AllocatedNum::alloc(cs.namespace(|| "new_R"), || {
prev_R
.get_value()
Expand All @@ -291,17 +291,18 @@ impl<E: Engine> LookupTrace<E> {
.zip(read_value_term.get_value())
.zip(read_counter_term.get_value())
.map(|((((R, alpha), addr), value_term), counter_term)| {
R + (alpha - (addr + value_term + counter_term))
R + (alpha + (addr + value_term + counter_term))
.invert()
.expect("invert failed due to read term is 0") // negilible probability for invert failed
})
.ok_or(SynthesisError::AssignmentMissing)
})?;
let mut r_blc = LinearCombination::<F>::zero();
r_blc = r_blc + alpha.get_variable()
- addr.get_variable()
- read_value_term.get_variable()
- read_counter_term.get_variable();
r_blc = r_blc
+ alpha.get_variable()
+ addr.get_variable()
+ read_value_term.get_variable()
+ read_counter_term.get_variable();
cs.enforce(
|| "R update",
|lc| lc + new_R.get_variable() - prev_R.get_variable(),
Expand Down Expand Up @@ -352,7 +353,7 @@ impl<E: Engine> LookupTrace<E> {
.zip(gamma_square.get_value())
.map(
|(((((W, alpha), addr), value_term), write_counter_term), gamma_square)| {
W + (alpha - (addr + value_term + write_counter_term + gamma_square))
W + (alpha + (addr + value_term + write_counter_term + gamma_square))
.invert()
.expect("invert failed due to write term is 0") // negilible probability for invert failed
},
Expand All @@ -361,11 +362,12 @@ impl<E: Engine> LookupTrace<E> {
})?;
// new_W = W + 1 / (alpha - (addr + gamma * value + gamma^2 * counter))
let mut w_blc = LinearCombination::<F>::zero();
w_blc = w_blc + alpha.get_variable()
- addr.get_variable()
- write_value_term.get_variable()
- write_counter_term.get_variable()
- gamma_square.get_variable();
w_blc = w_blc
+ alpha.get_variable()
+ addr.get_variable()
+ write_value_term.get_variable()
+ write_counter_term.get_variable()
+ gamma_square.get_variable();
cs.enforce(
|| "W update",
|lc| lc + new_W.get_variable() - prev_W.get_variable(),
Expand Down Expand Up @@ -853,10 +855,10 @@ mod test {
.zip(addr.get_value())
.zip(read_value.get_value())
.map(|((((prev_R, alpha), gamma), addr), read_value)| prev_R
+ (alpha - (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ZERO))
+ (alpha + (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ZERO))
.invert()
.unwrap()
+ (alpha - (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ONE))
+ (alpha + (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ONE))
.invert()
.unwrap())
);
Expand All @@ -871,11 +873,11 @@ mod test {
.zip(read_value.get_value())
.map(|((((prev_W, alpha), gamma), addr), read_value)| {
prev_W
+ (alpha - (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
+ (alpha + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
.invert()
.unwrap()
+ (alpha
- (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
+ (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
.invert()
.unwrap()
}),
Expand Down Expand Up @@ -980,12 +982,12 @@ mod test {
.zip(read_value.get_value())
.map(|((((prev_R, alpha), gamma), addr), read_value)| prev_R
+ (alpha
- (addr
+ (addr
+ gamma * <E1 as Engine>::Scalar::ZERO
+ gamma * gamma * <E1 as Engine>::Scalar::ZERO))
.invert()
.unwrap()
+ (alpha - (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ONE))
+ (alpha + (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ONE))
.invert()
.unwrap())
);
Expand All @@ -1000,11 +1002,11 @@ mod test {
.zip(read_value.get_value())
.map(|((((prev_W, alpha), gamma), addr), read_value)| {
prev_W
+ (alpha - (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
+ (alpha + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
.invert()
.unwrap()
+ (alpha
- (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
+ (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
.invert()
.unwrap()
}),
Expand Down
20 changes: 12 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ where
/// let circuit2 = TrivialCircuit::<<E2 as Engine>::Scalar>::default();
/// // Only relevant for a SNARK using computation commitmnets, pass &(|_| 0)
/// // or &*nova_snark::traits::snark::default_ck_hint() otherwise.
/// let ck_hint1 = &*SPrime::<E1>::ck_floor();
/// let ck_hint2 = &*SPrime::<E2>::ck_floor();
/// let ck_hint1 = &*SPrime::<E1, EE<_>>::ck_floor();
/// let ck_hint2 = &*SPrime::<E2, EE<_>>::ck_floor();
///
/// let pp = PublicParams::setup(&circuit1, &circuit2, ck_hint1, ck_hint2);
/// ```
Expand Down Expand Up @@ -2343,13 +2343,16 @@ mod tests {

let circuit_secondary = TrivialCircuit::default();

let ck_hint1 = &*SPrime::<E1, EE<_>>::ck_floor();
let ck_hint2 = &*SPrime::<E2, EE<_>>::ck_floor();

// produce public parameters
let pp =
PublicParams::<E1, E2, HeapifyCircuit<E1, E2>, TrivialCircuit<<E2 as Engine>::Scalar>>::setup(
&circuit_primaries[0],
&circuit_secondary,
&*default_ck_hint(),
&*default_ck_hint(),
ck_hint1,
ck_hint2,
);

// produce the prover and verifier keys for compressed snark
Expand Down Expand Up @@ -2407,10 +2410,10 @@ mod tests {
zn_primary[5]
); // rw counter = number_of_iterated_nodes * (3r + 4w) operations

assert_eq!(pp.circuit_shape_primary.r1cs_shape.num_cons, 12599);
assert_eq!(pp.circuit_shape_primary.r1cs_shape.num_vars, 12607);
assert_eq!(pp.circuit_shape_secondary.r1cs_shape.num_cons, 10347);
assert_eq!(pp.circuit_shape_secondary.r1cs_shape.num_vars, 10329);
assert_eq!(pp.circuit_shape_primary.r1cs_shape.num_cons, 12609);
assert_eq!(pp.circuit_shape_primary.r1cs_shape.num_vars, 12615);
assert_eq!(pp.circuit_shape_secondary.r1cs_shape.num_cons, 10357);
assert_eq!(pp.circuit_shape_secondary.r1cs_shape.num_vars, 10337);

println!("zn_primary {:?}", zn_primary);

Expand Down Expand Up @@ -2449,6 +2452,7 @@ mod tests {
write_row,
(alpha, gamma),
);
println!("res: {:?}", res);
assert!(res.is_ok());
}
}
Loading

0 comments on commit 9e8bc25

Please sign in to comment.