Forgot a random access check

This commit is contained in:
wborgeaud 2021-10-18 17:23:39 +02:00
parent 5b81006e9a
commit dda14011c5
4 changed files with 31 additions and 36 deletions

View File

@ -60,17 +60,17 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// isn't required -- without it we'd get errors elsewhere in the stack -- but just gives more
/// helpful errors.
fn check_config(&self, arity: usize) {
// let random_access = RandomAccessGate::<F, D>::new(arity);
// TODO: It would be nice to remove the hardcoded 8 here and replace it with the maximum arity bits
// used in FRI.
let random_access = RandomAccessGate::<F, D>::new_from_config(&self.config, 8);
let interpolation_gate = InterpolationGate::<F, D>::new(arity);
// let min_wires = random_access
// .num_wires()
// .max(interpolation_gate.num_wires());
let min_wires = interpolation_gate.num_wires();
// let min_routed_wires = random_access
// .num_routed_wires()
// .max(interpolation_gate.num_routed_wires());
let min_routed_wires = interpolation_gate.num_routed_wires();
let min_wires = random_access
.num_wires()
.max(interpolation_gate.num_wires());
let min_routed_wires = random_access
.num_routed_wires()
.max(interpolation_gate.num_routed_wires());
assert!(
self.config.num_wires >= min_wires,

View File

@ -36,6 +36,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
(gate, i)
}
/// Checks that a `Target` matches a vector at a non-deterministic index.
/// Note: `access_index` is not range-checked.
pub fn random_access(&mut self, access_index: Target, claimed_element: Target, v: Vec<Target>) {

View File

@ -298,23 +298,6 @@ mod tests {
use crate::hash::hash_types::HashOut;
use crate::plonk::vars::EvaluationVars;
// #[test]
// fn wire_indices() {
// let gate = RandomAccessGate::<CrandallField, 4> {
// vec_size: 3,
// _phantom: PhantomData,
// };
//
// assert_eq!(gate.wire_access_index(), 0);
// assert_eq!(gate.wires_claimed_element(), 1..5);
// assert_eq!(gate.wires_list_item(0), 5..9);
// assert_eq!(gate.wires_list_item(2), 13..17);
// assert_eq!(gate.wire_equality_dummy_for_index(0), 17);
// assert_eq!(gate.wire_equality_dummy_for_index(2), 19);
// assert_eq!(gate.wire_index_matches_for_index(0), 20);
// assert_eq!(gate.wire_index_matches_for_index(2), 22);
// }
#[test]
fn low_degree() {
test_low_degree::<CrandallField, _, 4>(RandomAccessGate::new(4, 4));
@ -331,8 +314,8 @@ mod tests {
type FF = QuarticExtension<CrandallField>;
const D: usize = 4;
/// Returns the local wires for a random access gate given the vector, element to compare,
/// and index.
/// Returns the local wires for a random access gate given the vectors, elements to compare,
/// and indices.
fn get_wires(
lists: Vec<Vec<F>>,
access_indices: Vec<usize>,
@ -367,16 +350,21 @@ mod tests {
}
v.extend(equality_dummy_vals);
v.extend(index_matches_vals);
v.iter().map(|&x| x.into()).collect::<Vec<_>>()
}
let lists = (0..4).map(|_| F::rand_vec(3)).collect::<Vec<_>>();
let access_indices = (0..4)
.map(|_| thread_rng().gen_range(0..3))
let vec_size = 3;
let num_copies = 4;
let lists = (0..num_copies)
.map(|_| F::rand_vec(vec_size))
.collect::<Vec<_>>();
let access_indices = (0..num_copies)
.map(|_| thread_rng().gen_range(0..vec_size))
.collect::<Vec<_>>();
let gate = RandomAccessGate::<F, D> {
vec_size: 3,
num_copies: 4,
vec_size,
num_copies,
_phantom: PhantomData,
};

View File

@ -55,7 +55,6 @@ pub(crate) fn verify_merkle_proof<F: RichField>(
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Verifies that the given leaf data is present at the given index in the Merkle tree with the
/// given cap. The index is given by it's little-endian bits.
/// Note: Works only for D=4.
pub(crate) fn verify_merkle_proof(
&mut self,
leaf_data: Vec<Target>,
@ -75,10 +74,17 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
let index = self.le_sum(leaf_index_bits[proof.siblings.len()..].to_vec().into_iter());
for i in 0..4 {
self.random_access(
index,
state.elements[i],
merkle_cap.0.iter().map(|h| h.elements[i]).collect(),
);
}
}
/// Same a `verify_merkle_proof` but with the final "cap index" as extra parameter.
/// Note: Works only for D=4.
/// Same as `verify_merkle_proof` but with the final "cap index" as extra parameter.
pub(crate) fn verify_merkle_proof_with_cap_index(
&mut self,
leaf_data: Vec<Target>,