Skip to content

Commit

Permalink
chores: better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jan 25, 2024
1 parent 4b109c3 commit 9f4be89
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 101 deletions.
84 changes: 38 additions & 46 deletions src/gadgets/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,31 +275,31 @@ impl<E: Engine> LookupTrace<E> {
where
F: Ord,
{
let (alpha, gamma) = challenges;
let (r, gamma) = challenges;
// update R
let gamma_square = gamma.mul(cs.namespace(|| "gamme^2"), gamma)?;
// read_value_term = gamma * value
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 / (r + (addr + gamma * value + gamma^2 * counter))
let new_R = AllocatedNum::alloc(cs.namespace(|| "new_R"), || {
prev_R
.get_value()
.zip(alpha.get_value())
.zip(r.get_value())
.zip(addr.get_value())
.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))
.map(|((((R, r), addr), value_term), counter_term)| {
R + (r + (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()
+ r.get_variable()
+ addr.get_variable()
+ read_value_term.get_variable()
+ read_counter_term.get_variable();
Expand Down Expand Up @@ -346,24 +346,24 @@ impl<E: Engine> LookupTrace<E> {
let new_W = AllocatedNum::alloc(cs.namespace(|| "new_W"), || {
prev_W
.get_value()
.zip(alpha.get_value())
.zip(r.get_value())
.zip(addr.get_value())
.zip(write_value_term.get_value())
.zip(write_counter_term.get_value())
.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, r), addr), value_term), write_counter_term), gamma_square)| {
W + (r + (addr + value_term + write_counter_term + gamma_square))
.invert()
.expect("invert failed due to write term is 0") // negilible probability for invert failed
},
)
.ok_or(SynthesisError::AssignmentMissing)
})?;
// new_W = W + 1 / (alpha - (addr + gamma * value + gamma^2 * counter))
// new_W = W + 1 / (r - (addr + gamma * value + gamma^2 * counter))
let mut w_blc = LinearCombination::<F>::zero();
w_blc = w_blc
+ alpha.get_variable()
+ r.get_variable()
+ addr.get_variable()
+ write_value_term.get_variable()
+ write_counter_term.get_variable()
Expand Down Expand Up @@ -536,11 +536,11 @@ impl<'a, E: Engine> LookupTraceBuilder<'a, E> {
comm_final_counter.absorb_in_ro(&mut hasher);
let gamma = hasher.squeeze(NUM_CHALLENGE_BITS);

// alpha
// r
let mut hasher = <E as Engine>::RO::new(ro_consts, 1);
hasher.absorb(scalar_as_base::<E>(gamma));
let alpha = hasher.squeeze(NUM_CHALLENGE_BITS);
(alpha, gamma)
let r = hasher.squeeze(NUM_CHALLENGE_BITS);
(r, gamma)
}
}

Expand Down Expand Up @@ -789,16 +789,13 @@ mod test {
Lookup::<<E1 as Engine>::Scalar>::new(1024, TableType::ReadOnly, initial_table);
let mut lookup_trace_builder = LookupTraceBuilder::<E1>::new(&mut lookup);
let challenges = (
AllocatedNum::alloc(cs.namespace(|| "alpha"), || {
Ok(<E1 as Engine>::Scalar::from(5))
})
.unwrap(),
AllocatedNum::alloc(cs.namespace(|| "r"), || Ok(<E1 as Engine>::Scalar::from(5))).unwrap(),
AllocatedNum::alloc(cs.namespace(|| "gamma"), || {
Ok(<E1 as Engine>::Scalar::from(7))
})
.unwrap(),
);
let (alpha, gamma) = &challenges;
let (r, gamma) = &challenges;
let zero = alloc_zero(cs.namespace(|| "zero"));
let one = alloc_one(cs.namespace(|| "one"));
let prev_intermediate_gamma = &one;
Expand Down Expand Up @@ -850,15 +847,15 @@ mod test {
next_R.get_value(),
prev_R
.get_value()
.zip(alpha.get_value())
.zip(r.get_value())
.zip(gamma.get_value())
.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))
.map(|((((prev_R, r), gamma), addr), read_value)| prev_R
+ (r + (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))
+ (r + (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ONE))
.invert()
.unwrap())
);
Expand All @@ -867,19 +864,18 @@ mod test {
next_W.get_value(),
prev_W
.get_value()
.zip(alpha.get_value())
.zip(r.get_value())
.zip(gamma.get_value())
.zip(addr.get_value())
.zip(read_value.get_value())
.map(|((((prev_W, alpha), gamma), addr), read_value)| {
.map(|((((prev_W, r), gamma), addr), read_value)| {
prev_W
+ (alpha + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
+ (r + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
.invert()
.unwrap()
+ (r + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
.invert()
.unwrap()
+ (alpha
+ (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
.invert()
.unwrap()
}),
);

Expand Down Expand Up @@ -915,16 +911,13 @@ mod test {
Lookup::<<E1 as Engine>::Scalar>::new(1024, TableType::ReadWrite, initial_table);
let mut lookup_trace_builder = LookupTraceBuilder::<E1>::new(&mut lookup);
let challenges = (
AllocatedNum::alloc(cs.namespace(|| "alpha"), || {
Ok(<E1 as Engine>::Scalar::from(5))
})
.unwrap(),
AllocatedNum::alloc(cs.namespace(|| "r"), || Ok(<E1 as Engine>::Scalar::from(5))).unwrap(),
AllocatedNum::alloc(cs.namespace(|| "gamma"), || {
Ok(<E1 as Engine>::Scalar::from(7))
})
.unwrap(),
);
let (alpha, gamma) = &challenges;
let (r, gamma) = &challenges;
let zero = alloc_zero(cs.namespace(|| "zero"));
let one = alloc_one(cs.namespace(|| "one"));
let prev_intermediate_gamma = &one;
Expand Down Expand Up @@ -976,18 +969,18 @@ mod test {
next_R.get_value(),
prev_R
.get_value()
.zip(alpha.get_value())
.zip(r.get_value())
.zip(gamma.get_value())
.zip(addr.get_value())
.zip(read_value.get_value())
.map(|((((prev_R, alpha), gamma), addr), read_value)| prev_R
+ (alpha
.map(|((((prev_R, r), gamma), addr), read_value)| prev_R
+ (r
+ (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))
+ (r + (addr + gamma * read_value + gamma * gamma * <E1 as Engine>::Scalar::ONE))
.invert()
.unwrap())
);
Expand All @@ -996,19 +989,18 @@ mod test {
next_W.get_value(),
prev_W
.get_value()
.zip(alpha.get_value())
.zip(r.get_value())
.zip(gamma.get_value())
.zip(addr.get_value())
.zip(read_value.get_value())
.map(|((((prev_W, alpha), gamma), addr), read_value)| {
.map(|((((prev_W, r), gamma), addr), read_value)| {
prev_W
+ (alpha + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
+ (r + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::ONE)))
.invert()
.unwrap()
+ (r + (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
.invert()
.unwrap()
+ (alpha
+ (addr + gamma * read_value + gamma * gamma * (<E1 as Engine>::Scalar::from(2))))
.invert()
.unwrap()
}),
);

Expand Down
32 changes: 16 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,8 @@ where
pk: &ProverKeyV2<E1, E2, C1, C2, S1, S2>,
recursive_snark: &RecursiveSNARK<E1, E2, C1, C2>,
challenges: (E1::Scalar, E1::Scalar),
read_row: E1::Scalar,
write_row: E1::Scalar,
R_acc: E1::Scalar,
W_acc: E1::Scalar,
initial_table: &Lookup<E1::Scalar>,
final_table: &Lookup<E1::Scalar>,
) -> Result<Self, NovaError> {
Expand All @@ -1120,8 +1120,8 @@ where
&recursive_snark.r_U_primary,
&recursive_snark.r_W_primary,
challenges,
read_row,
write_row,
R_acc,
W_acc,
initial_table.clone(),
final_table.clone(),
)
Expand Down Expand Up @@ -1160,9 +1160,9 @@ where
num_steps: usize,
z0_primary: &[E1::Scalar],
z0_secondary: &[E2::Scalar],
fingerprint_intermediate_gamma: E1::Scalar,
read_row: E1::Scalar,
write_row: E1::Scalar,
lookup_intermediate_gamma: E1::Scalar,
R_acc: E1::Scalar,
W_acc: E1::Scalar,
challenges: (E1::Scalar, E1::Scalar),
) -> Result<(Vec<E1::Scalar>, Vec<E2::Scalar>), NovaError> {
// the number of steps cannot be zero
Expand Down Expand Up @@ -1235,9 +1235,9 @@ where
self.r_W_snark_primary.verify::<E2>(
&vk.vk_primary,
&self.r_U_primary,
fingerprint_intermediate_gamma,
read_row,
write_row,
lookup_intermediate_gamma,
R_acc,
W_acc,
challenges.clone(),
)
},
Expand Down Expand Up @@ -2419,8 +2419,8 @@ mod tests {
let intermediate_gamma = zn_primary[0];
let alpha = zn_primary[1];
let gamma = zn_primary[2];
let read_row = zn_primary[3];
let write_row = zn_primary[4];
let R_acc = zn_primary[3];
let W_acc = zn_primary[4];
assert_eq!(
expected_intermediate_gamma, intermediate_gamma,
"expected_intermediate_gamma != intermediate_gamma"
Expand All @@ -2432,8 +2432,8 @@ mod tests {
&pk,
&recursive_snark,
(alpha, gamma),
read_row,
write_row,
R_acc,
W_acc,
&initial_table,
&final_table,
);
Expand All @@ -2447,8 +2447,8 @@ mod tests {
&z0_primary,
&z0_secondary,
expected_intermediate_gamma,
read_row,
write_row,
R_acc,
W_acc,
(alpha, gamma),
);
assert!(res.is_ok());
Expand Down
Loading

0 comments on commit 9f4be89

Please sign in to comment.