2021-08-02 17:01:51 -07:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
|
|
use crate::field::extension_field::target::ExtensionTarget;
|
2021-10-18 17:27:22 +02:00
|
|
|
use crate::field::extension_field::Extendable;
|
2021-09-07 18:28:28 -07:00
|
|
|
use crate::field::field_types::{Field, RichField};
|
2021-08-02 17:01:51 -07:00
|
|
|
use crate::gates::gate::Gate;
|
|
|
|
|
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
|
|
|
|
|
use crate::iop::target::Target;
|
|
|
|
|
use crate::iop::wire::Wire;
|
2021-08-20 11:13:40 +02:00
|
|
|
use crate::iop::witness::{PartitionWitness, Witness};
|
2021-08-02 17:01:51 -07:00
|
|
|
use crate::plonk::circuit_builder::CircuitBuilder;
|
2021-10-18 11:07:18 +02:00
|
|
|
use crate::plonk::circuit_data::CircuitConfig;
|
2021-08-02 17:01:51 -07:00
|
|
|
use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase};
|
|
|
|
|
|
2021-08-19 19:41:18 -07:00
|
|
|
/// A gate for checking that a particular element of a list matches a given value.
|
2021-10-18 21:48:40 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2021-09-07 18:28:28 -07:00
|
|
|
pub(crate) struct RandomAccessGate<F: RichField + Extendable<D>, const D: usize> {
|
2021-08-02 17:01:51 -07:00
|
|
|
pub vec_size: usize,
|
2021-10-18 11:07:18 +02:00
|
|
|
pub num_copies: usize,
|
2021-08-02 17:01:51 -07:00
|
|
|
_phantom: PhantomData<F>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 18:28:28 -07:00
|
|
|
impl<F: RichField + Extendable<D>, const D: usize> RandomAccessGate<F, D> {
|
2021-10-18 11:07:18 +02:00
|
|
|
pub fn new(num_copies: usize, vec_size: usize) -> Self {
|
2021-08-02 17:01:51 -07:00
|
|
|
Self {
|
|
|
|
|
vec_size,
|
2021-10-18 11:07:18 +02:00
|
|
|
num_copies,
|
2021-08-02 17:01:51 -07:00
|
|
|
_phantom: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 15:19:09 +02:00
|
|
|
pub fn new_from_config(config: &CircuitConfig, vec_size: usize) -> Self {
|
2021-10-18 16:48:21 +02:00
|
|
|
let num_copies = Self::max_num_copies(config.num_routed_wires, config.num_wires, vec_size);
|
2021-10-18 15:19:09 +02:00
|
|
|
Self::new(num_copies, vec_size)
|
2021-10-18 11:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 16:48:21 +02:00
|
|
|
pub fn max_num_copies(num_routed_wires: usize, num_wires: usize, vec_size: usize) -> usize {
|
|
|
|
|
// Need `(2 + vec_size) * num_copies` routed wires
|
|
|
|
|
(num_routed_wires / (2 + vec_size)).min(
|
2021-10-18 21:38:57 +02:00
|
|
|
// Need `(2 + 3*vec_size) * num_copies` wires
|
|
|
|
|
num_wires / (2 + 3 * vec_size),
|
2021-10-18 16:48:21 +02:00
|
|
|
)
|
2021-10-18 11:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 15:19:09 +02:00
|
|
|
pub fn wire_access_index(&self, copy: usize) -> usize {
|
|
|
|
|
debug_assert!(copy < self.num_copies);
|
|
|
|
|
(2 + self.vec_size) * copy
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 15:19:09 +02:00
|
|
|
pub fn wire_claimed_element(&self, copy: usize) -> usize {
|
|
|
|
|
debug_assert!(copy < self.num_copies);
|
|
|
|
|
(2 + self.vec_size) * copy + 1
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 15:19:09 +02:00
|
|
|
pub fn wire_list_item(&self, i: usize, copy: usize) -> usize {
|
2021-08-02 17:01:51 -07:00
|
|
|
debug_assert!(i < self.vec_size);
|
2021-10-18 15:19:09 +02:00
|
|
|
debug_assert!(copy < self.num_copies);
|
|
|
|
|
(2 + self.vec_size) * copy + 2 + i
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_of_intermediate_wires(&self) -> usize {
|
2021-10-18 15:19:09 +02:00
|
|
|
(2 + self.vec_size) * self.num_copies
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-05 23:28:04 -07:00
|
|
|
pub(crate) fn num_routed_wires(&self) -> usize {
|
|
|
|
|
self.start_of_intermediate_wires()
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 17:01:51 -07:00
|
|
|
/// An intermediate wire for a dummy variable used to show equality.
|
|
|
|
|
/// The prover sets this to 1/(x-y) if x != y, or to an arbitrary value if
|
|
|
|
|
/// x == y.
|
2021-10-18 15:19:09 +02:00
|
|
|
pub fn wire_equality_dummy_for_index(&self, i: usize, copy: usize) -> usize {
|
2021-08-02 17:01:51 -07:00
|
|
|
debug_assert!(i < self.vec_size);
|
2021-10-18 15:19:09 +02:00
|
|
|
debug_assert!(copy < self.num_copies);
|
|
|
|
|
self.start_of_intermediate_wires() + copy * self.vec_size + i
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-02 17:58:42 -07:00
|
|
|
/// An intermediate wire for the "index_matches" variable (1 if the current index is the index at
|
|
|
|
|
/// which to compare, 0 otherwise).
|
2021-10-18 15:19:09 +02:00
|
|
|
pub fn wire_index_matches_for_index(&self, i: usize, copy: usize) -> usize {
|
2021-08-02 17:01:51 -07:00
|
|
|
debug_assert!(i < self.vec_size);
|
2021-10-18 15:19:09 +02:00
|
|
|
debug_assert!(copy < self.num_copies);
|
|
|
|
|
self.start_of_intermediate_wires()
|
|
|
|
|
+ self.vec_size * self.num_copies
|
2021-10-18 16:48:21 +02:00
|
|
|
+ self.vec_size * copy
|
2021-10-18 15:19:09 +02:00
|
|
|
+ i
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 18:28:28 -07:00
|
|
|
impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGate<F, D> {
|
2021-08-02 17:01:51 -07:00
|
|
|
fn id(&self) -> String {
|
|
|
|
|
format!("{:?}<D={}>", self, D)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
|
2021-08-12 13:32:49 -07:00
|
|
|
let mut constraints = Vec::with_capacity(self.num_constraints());
|
2021-10-18 15:19:09 +02:00
|
|
|
|
|
|
|
|
for copy in 0..self.num_copies {
|
|
|
|
|
let access_index = vars.local_wires[self.wire_access_index(copy)];
|
|
|
|
|
let list_items = (0..self.vec_size)
|
|
|
|
|
.map(|i| vars.local_wires[self.wire_list_item(i, copy)])
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
let claimed_element = vars.local_wires[self.wire_claimed_element(copy)];
|
|
|
|
|
|
|
|
|
|
for i in 0..self.vec_size {
|
|
|
|
|
let cur_index = F::Extension::from_canonical_usize(i);
|
|
|
|
|
let difference = cur_index - access_index;
|
|
|
|
|
let equality_dummy = vars.local_wires[self.wire_equality_dummy_for_index(i, copy)];
|
|
|
|
|
let index_matches = vars.local_wires[self.wire_index_matches_for_index(i, copy)];
|
|
|
|
|
|
|
|
|
|
// The two index equality constraints.
|
|
|
|
|
constraints.push(difference * equality_dummy - (F::Extension::ONE - index_matches));
|
|
|
|
|
constraints.push(index_matches * difference);
|
|
|
|
|
// Value equality constraint.
|
2021-10-18 17:27:22 +02:00
|
|
|
constraints.push((list_items[i] - claimed_element) * index_matches);
|
2021-10-18 15:19:09 +02:00
|
|
|
}
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constraints
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eval_unfiltered_base(&self, vars: EvaluationVarsBase<F>) -> Vec<F> {
|
2021-08-12 13:32:49 -07:00
|
|
|
let mut constraints = Vec::with_capacity(self.num_constraints());
|
2021-10-18 15:19:09 +02:00
|
|
|
|
|
|
|
|
for copy in 0..self.num_copies {
|
|
|
|
|
let access_index = vars.local_wires[self.wire_access_index(copy)];
|
|
|
|
|
let list_items = (0..self.vec_size)
|
|
|
|
|
.map(|i| vars.local_wires[self.wire_list_item(i, copy)])
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
let claimed_element = vars.local_wires[self.wire_claimed_element(copy)];
|
|
|
|
|
|
|
|
|
|
for i in 0..self.vec_size {
|
|
|
|
|
let cur_index = F::from_canonical_usize(i);
|
|
|
|
|
let difference = cur_index - access_index;
|
|
|
|
|
let equality_dummy = vars.local_wires[self.wire_equality_dummy_for_index(i, copy)];
|
|
|
|
|
let index_matches = vars.local_wires[self.wire_index_matches_for_index(i, copy)];
|
|
|
|
|
|
|
|
|
|
// The two index equality constraints.
|
|
|
|
|
constraints.push(difference * equality_dummy - (F::ONE - index_matches));
|
|
|
|
|
constraints.push(index_matches * difference);
|
|
|
|
|
// Value equality constraint.
|
2021-10-18 17:27:22 +02:00
|
|
|
constraints.push((list_items[i] - claimed_element) * index_matches);
|
2021-10-18 15:19:09 +02:00
|
|
|
}
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constraints
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eval_unfiltered_recursively(
|
|
|
|
|
&self,
|
|
|
|
|
builder: &mut CircuitBuilder<F, D>,
|
|
|
|
|
vars: EvaluationTargets<D>,
|
|
|
|
|
) -> Vec<ExtensionTarget<D>> {
|
2021-08-12 13:32:49 -07:00
|
|
|
let mut constraints = Vec::with_capacity(self.num_constraints());
|
2021-10-18 15:19:09 +02:00
|
|
|
|
|
|
|
|
for copy in 0..self.num_copies {
|
|
|
|
|
let access_index = vars.local_wires[self.wire_access_index(copy)];
|
|
|
|
|
let list_items = (0..self.vec_size)
|
|
|
|
|
.map(|i| vars.local_wires[self.wire_list_item(i, copy)])
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
let claimed_element = vars.local_wires[self.wire_claimed_element(copy)];
|
|
|
|
|
|
|
|
|
|
for i in 0..self.vec_size {
|
|
|
|
|
let cur_index_ext = F::Extension::from_canonical_usize(i);
|
|
|
|
|
let cur_index = builder.constant_extension(cur_index_ext);
|
|
|
|
|
let difference = builder.sub_extension(cur_index, access_index);
|
|
|
|
|
let equality_dummy = vars.local_wires[self.wire_equality_dummy_for_index(i, copy)];
|
|
|
|
|
let index_matches = vars.local_wires[self.wire_index_matches_for_index(i, copy)];
|
|
|
|
|
|
|
|
|
|
let one = builder.one_extension();
|
|
|
|
|
let not_index_matches = builder.sub_extension(one, index_matches);
|
|
|
|
|
let first_equality_constraint =
|
|
|
|
|
builder.mul_sub_extension(difference, equality_dummy, not_index_matches);
|
|
|
|
|
constraints.push(first_equality_constraint);
|
|
|
|
|
|
|
|
|
|
let second_equality_constraint = builder.mul_extension(index_matches, difference);
|
|
|
|
|
constraints.push(second_equality_constraint);
|
|
|
|
|
|
|
|
|
|
// Output constraint.
|
|
|
|
|
let diff = builder.sub_extension(list_items[i], claimed_element);
|
|
|
|
|
let conditional_diff = builder.mul_extension(index_matches, diff);
|
|
|
|
|
constraints.push(conditional_diff);
|
|
|
|
|
}
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constraints
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generators(
|
|
|
|
|
&self,
|
|
|
|
|
gate_index: usize,
|
|
|
|
|
_local_constants: &[F],
|
|
|
|
|
) -> Vec<Box<dyn WitnessGenerator<F>>> {
|
2021-10-18 21:48:40 +02:00
|
|
|
(0..self.num_copies)
|
|
|
|
|
.map(|copy| {
|
|
|
|
|
let g: Box<dyn WitnessGenerator<F>> = Box::new(
|
|
|
|
|
RandomAccessGenerator {
|
|
|
|
|
gate_index,
|
|
|
|
|
gate: *self,
|
|
|
|
|
copy,
|
|
|
|
|
}
|
|
|
|
|
.adapter(),
|
|
|
|
|
);
|
|
|
|
|
g
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_wires(&self) -> usize {
|
2021-10-18 15:19:09 +02:00
|
|
|
self.wire_index_matches_for_index(self.vec_size - 1, self.num_copies - 1) + 1
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_constants(&self) -> usize {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn degree(&self) -> usize {
|
|
|
|
|
2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_constraints(&self) -> usize {
|
2021-10-18 17:11:59 +02:00
|
|
|
3 * self.num_copies * self.vec_size
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-09-07 18:28:28 -07:00
|
|
|
struct RandomAccessGenerator<F: RichField + Extendable<D>, const D: usize> {
|
2021-08-02 17:01:51 -07:00
|
|
|
gate_index: usize,
|
|
|
|
|
gate: RandomAccessGate<F, D>,
|
2021-10-18 21:48:40 +02:00
|
|
|
copy: usize,
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-07 18:28:28 -07:00
|
|
|
impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
|
2021-09-05 10:27:11 -07:00
|
|
|
for RandomAccessGenerator<F, D>
|
|
|
|
|
{
|
2021-08-02 17:01:51 -07:00
|
|
|
fn dependencies(&self) -> Vec<Target> {
|
|
|
|
|
let local_target = |input| Target::wire(self.gate_index, input);
|
|
|
|
|
|
|
|
|
|
let mut deps = Vec::new();
|
2021-10-18 21:48:40 +02:00
|
|
|
deps.push(local_target(self.gate.wire_access_index(self.copy)));
|
|
|
|
|
deps.push(local_target(self.gate.wire_claimed_element(self.copy)));
|
|
|
|
|
for i in 0..self.gate.vec_size {
|
|
|
|
|
deps.push(local_target(self.gate.wire_list_item(i, self.copy)));
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
deps
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:55:49 +02:00
|
|
|
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
2021-08-02 17:01:51 -07:00
|
|
|
let local_wire = |input| Wire {
|
|
|
|
|
gate: self.gate_index,
|
|
|
|
|
input,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let get_local_wire = |input| witness.get_wire(local_wire(input));
|
|
|
|
|
|
2021-08-02 17:58:42 -07:00
|
|
|
// Compute the new vector and the values for equality_dummy and index_matches
|
2021-08-02 17:01:51 -07:00
|
|
|
let vec_size = self.gate.vec_size;
|
2021-10-18 21:48:40 +02:00
|
|
|
let access_index_f = get_local_wire(self.gate.wire_access_index(self.copy));
|
|
|
|
|
|
|
|
|
|
let access_index = access_index_f.to_canonical_u64() as usize;
|
|
|
|
|
debug_assert!(
|
|
|
|
|
access_index < vec_size,
|
|
|
|
|
"Access index {} is larger than the vector size {}",
|
|
|
|
|
access_index,
|
|
|
|
|
vec_size
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for i in 0..vec_size {
|
|
|
|
|
let equality_dummy_wire =
|
|
|
|
|
local_wire(self.gate.wire_equality_dummy_for_index(i, self.copy));
|
|
|
|
|
let index_matches_wire =
|
|
|
|
|
local_wire(self.gate.wire_index_matches_for_index(i, self.copy));
|
|
|
|
|
|
|
|
|
|
if i == access_index {
|
|
|
|
|
out_buffer.set_wire(equality_dummy_wire, F::ONE);
|
|
|
|
|
out_buffer.set_wire(index_matches_wire, F::ONE);
|
|
|
|
|
} else {
|
|
|
|
|
out_buffer.set_wire(
|
|
|
|
|
equality_dummy_wire,
|
|
|
|
|
(F::from_canonical_usize(i) - F::from_canonical_usize(access_index)).inverse(),
|
|
|
|
|
);
|
|
|
|
|
out_buffer.set_wire(index_matches_wire, F::ZERO);
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
2021-08-03 08:59:25 -07:00
|
|
|
use anyhow::Result;
|
2021-10-18 15:19:09 +02:00
|
|
|
use rand::{thread_rng, Rng};
|
2021-08-03 08:59:25 -07:00
|
|
|
|
2021-09-13 11:45:17 -07:00
|
|
|
use crate::field::extension_field::quartic::QuarticExtension;
|
2021-08-02 17:01:51 -07:00
|
|
|
use crate::field::field_types::Field;
|
2021-11-02 12:04:42 -07:00
|
|
|
use crate::field::goldilocks_field::GoldilocksField;
|
2021-08-02 17:01:51 -07:00
|
|
|
use crate::gates::gate::Gate;
|
2021-08-03 08:59:25 -07:00
|
|
|
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
|
2021-08-02 17:01:51 -07:00
|
|
|
use crate::gates::random_access::RandomAccessGate;
|
|
|
|
|
use crate::hash::hash_types::HashOut;
|
|
|
|
|
use crate::plonk::vars::EvaluationVars;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn low_degree() {
|
2021-11-02 12:04:42 -07:00
|
|
|
test_low_degree::<GoldilocksField, _, 4>(RandomAccessGate::new(4, 4));
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-02 17:58:42 -07:00
|
|
|
#[test]
|
|
|
|
|
fn eval_fns() -> Result<()> {
|
2021-11-02 12:04:42 -07:00
|
|
|
test_eval_fns::<GoldilocksField, _, 4>(RandomAccessGate::new(4, 4))
|
2021-08-02 17:58:42 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-02 17:01:51 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_gate_constraint() {
|
2021-11-02 12:04:42 -07:00
|
|
|
type F = GoldilocksField;
|
|
|
|
|
type FF = QuarticExtension<GoldilocksField>;
|
2021-08-02 17:01:51 -07:00
|
|
|
const D: usize = 4;
|
|
|
|
|
|
2021-10-18 17:23:39 +02:00
|
|
|
/// Returns the local wires for a random access gate given the vectors, elements to compare,
|
|
|
|
|
/// and indices.
|
2021-10-18 15:19:09 +02:00
|
|
|
fn get_wires(
|
|
|
|
|
lists: Vec<Vec<F>>,
|
|
|
|
|
access_indices: Vec<usize>,
|
|
|
|
|
claimed_elements: Vec<F>,
|
|
|
|
|
) -> Vec<FF> {
|
|
|
|
|
let num_copies = lists.len();
|
|
|
|
|
let vec_size = lists[0].len();
|
2021-08-02 17:01:51 -07:00
|
|
|
|
|
|
|
|
let mut v = Vec::new();
|
|
|
|
|
let mut equality_dummy_vals = Vec::new();
|
2021-08-02 17:58:42 -07:00
|
|
|
let mut index_matches_vals = Vec::new();
|
2021-10-18 15:19:09 +02:00
|
|
|
for copy in 0..num_copies {
|
|
|
|
|
let access_index = access_indices[copy];
|
|
|
|
|
v.push(F::from_canonical_usize(access_index));
|
|
|
|
|
v.push(claimed_elements[copy]);
|
|
|
|
|
for j in 0..vec_size {
|
|
|
|
|
v.push(lists[copy][j]);
|
2021-08-02 17:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 15:19:09 +02:00
|
|
|
for i in 0..vec_size {
|
|
|
|
|
if i == access_index {
|
|
|
|
|
equality_dummy_vals.push(F::ONE);
|
|
|
|
|
index_matches_vals.push(F::ONE);
|
|
|
|
|
} else {
|
|
|
|
|
equality_dummy_vals.push(
|
|
|
|
|
(F::from_canonical_usize(i) - F::from_canonical_usize(access_index))
|
|
|
|
|
.inverse(),
|
|
|
|
|
);
|
|
|
|
|
index_matches_vals.push(F::ZERO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-02 17:01:51 -07:00
|
|
|
v.extend(equality_dummy_vals);
|
2021-08-02 17:58:42 -07:00
|
|
|
v.extend(index_matches_vals);
|
2021-10-18 17:23:39 +02:00
|
|
|
|
2021-08-02 17:01:51 -07:00
|
|
|
v.iter().map(|&x| x.into()).collect::<Vec<_>>()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 17:23:39 +02:00
|
|
|
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))
|
2021-10-18 15:19:09 +02:00
|
|
|
.collect::<Vec<_>>();
|
2021-08-02 17:01:51 -07:00
|
|
|
let gate = RandomAccessGate::<F, D> {
|
2021-10-18 17:23:39 +02:00
|
|
|
vec_size,
|
|
|
|
|
num_copies,
|
2021-08-02 17:01:51 -07:00
|
|
|
_phantom: PhantomData,
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-18 15:19:09 +02:00
|
|
|
let good_claimed_elements = lists
|
|
|
|
|
.iter()
|
|
|
|
|
.zip(&access_indices)
|
|
|
|
|
.map(|(l, &i)| l[i])
|
|
|
|
|
.collect();
|
2021-08-02 17:01:51 -07:00
|
|
|
let good_vars = EvaluationVars {
|
|
|
|
|
local_constants: &[],
|
2021-10-18 15:19:09 +02:00
|
|
|
local_wires: &get_wires(lists.clone(), access_indices.clone(), good_claimed_elements),
|
2021-08-02 17:01:51 -07:00
|
|
|
public_inputs_hash: &HashOut::rand(),
|
|
|
|
|
};
|
2021-10-18 15:19:09 +02:00
|
|
|
let bad_claimed_elements = F::rand_vec(4);
|
2021-08-02 17:01:51 -07:00
|
|
|
let bad_vars = EvaluationVars {
|
|
|
|
|
local_constants: &[],
|
2021-10-18 15:19:09 +02:00
|
|
|
local_wires: &get_wires(lists, access_indices, bad_claimed_elements),
|
2021-08-02 17:01:51 -07:00
|
|
|
public_inputs_hash: &HashOut::rand(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
|
gate.eval_unfiltered(good_vars).iter().all(|x| x.is_zero()),
|
|
|
|
|
"Gate constraints are not satisfied."
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
!gate.eval_unfiltered(bad_vars).iter().all(|x| x.is_zero()),
|
2021-10-18 15:19:09 +02:00
|
|
|
"Gate constraints are satisfied but should not be."
|
2021-08-02 17:01:51 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|