mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-07 00:03:10 +00:00
Fill random access gates to make sure all generators are run
This commit is contained in:
parent
3f0b5ab9d3
commit
5b81006e9a
@ -9,7 +9,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
/// Finds the last available random access gate with the given `vec_size` or add one if there aren't any.
|
||||
/// Returns `(g,i)` such that there is a random access gate with the given `vec_size` at index
|
||||
/// `g` and the gate's `i`-th random access is available.
|
||||
fn find_random_acces_gate(&mut self, vec_size: usize) -> (usize, usize) {
|
||||
fn find_random_access_gate(&mut self, vec_size: usize) -> (usize, usize) {
|
||||
let (gate, i) = self
|
||||
.free_random_access
|
||||
.get(&vec_size)
|
||||
@ -36,7 +36,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
|
||||
(gate, i)
|
||||
}
|
||||
/// Checks that an `ExtensionTarget` matches a vector at a non-deterministic index.
|
||||
/// 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>) {
|
||||
let vec_size = v.len();
|
||||
@ -44,7 +44,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
if vec_size == 1 {
|
||||
return self.connect(claimed_element, v[0]);
|
||||
}
|
||||
let (gate_index, copy) = self.find_random_acces_gate(vec_size);
|
||||
let (gate_index, copy) = self.find_random_access_gate(vec_size);
|
||||
let dummy_gate = RandomAccessGate::<F, D>::new_from_config(&self.config, vec_size);
|
||||
|
||||
v.iter().enumerate().for_each(|(i, &val)| {
|
||||
|
||||
@ -212,7 +212,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGa
|
||||
}
|
||||
|
||||
fn num_constraints(&self) -> usize {
|
||||
self.num_copies * self.vec_size * 3
|
||||
3 * self.num_copies * self.vec_size
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ use crate::gates::gate::{Gate, GateInstance, GateRef, PrefixedGate};
|
||||
use crate::gates::gate_tree::Tree;
|
||||
use crate::gates::noop::NoopGate;
|
||||
use crate::gates::public_input::PublicInputGate;
|
||||
use crate::gates::random_access::RandomAccessGate;
|
||||
use crate::gates::switch::SwitchGate;
|
||||
use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget};
|
||||
use crate::hash::hashing::hash_n_to_hash;
|
||||
@ -535,6 +536,27 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill the remaining unused random access operations with zeros, so that all
|
||||
/// `RandomAccessGenerator`s are run.
|
||||
fn fill_random_access_gates(&mut self) {
|
||||
let zero = self.zero();
|
||||
let remaining_random_access_gates = self
|
||||
.free_random_access
|
||||
.clone()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
for (vec_size, (_, i)) in remaining_random_access_gates {
|
||||
let max_copies = RandomAccessGate::<F, D>::max_num_copies(
|
||||
self.config.num_routed_wires,
|
||||
self.config.num_wires,
|
||||
vec_size,
|
||||
);
|
||||
for _ in i..max_copies {
|
||||
self.random_access(zero, zero, vec![zero; vec_size]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill the remaining unused switch gates with dummy values, so that all
|
||||
/// `SwitchGenerator` are run.
|
||||
fn fill_switch_gates(&mut self) {
|
||||
@ -574,6 +596,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
let start = Instant::now();
|
||||
|
||||
self.fill_arithmetic_gates();
|
||||
self.fill_random_access_gates();
|
||||
self.fill_switch_gates();
|
||||
|
||||
// Hash the public inputs, and route them to a `PublicInputGate` which will enforce that
|
||||
|
||||
@ -398,7 +398,7 @@ mod tests {
|
||||
fn test_size_optimized_recursion() -> Result<()> {
|
||||
init_logger();
|
||||
type F = GoldilocksField;
|
||||
const D: usize = 4;
|
||||
const D: usize = 2;
|
||||
|
||||
let normal_config = CircuitConfig::standard_recursion_config();
|
||||
let final_config = CircuitConfig {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user