From fae471f9d881b5ae0389f2296d1cfe078f3db56a Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Thu, 31 Mar 2022 15:33:31 +0200 Subject: [PATCH] Working --- plonky2/src/gates/constant.rs | 84 ++++++++++++++++------------ plonky2/src/gates/gate.rs | 4 +- plonky2/src/gates/random_access.rs | 48 ++++++++++------ plonky2/src/iop/generator.rs | 36 ++++++++++++ plonky2/src/plonk/circuit_builder.rs | 26 +++++---- 5 files changed, 131 insertions(+), 67 deletions(-) diff --git a/plonky2/src/gates/constant.rs b/plonky2/src/gates/constant.rs index 3f69ceb1..a4bc9731 100644 --- a/plonky2/src/gates/constant.rs +++ b/plonky2/src/gates/constant.rs @@ -9,7 +9,9 @@ use crate::gates::packed_util::PackedEvaluableBase; use crate::gates::util::StridedConstraintConsumer; use crate::hash::hash_types::RichField; use crate::iop::ext_target::ExtensionTarget; -use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator}; +use crate::iop::generator::{ + ConstantGenerator, GeneratedValues, SimpleGenerator, WitnessGenerator, +}; use crate::iop::target::Target; use crate::iop::wire::Wire; use crate::iop::witness::PartitionWitness; @@ -82,20 +84,21 @@ impl, const D: usize> Gate for ConstantGate { gate_index: usize, local_constants: &[F], ) -> Vec>> { - (0..self.num_consts) - .map(|i| { - let g: Box> = Box::new( - ConstantGenerator { - gate_index, - gate: *self, - i, - constant: local_constants[self.const_input(i)], - } - .adapter(), - ); - g - }) - .collect() + // (0..self.num_consts) + // .map(|i| { + // let g: Box> = Box::new( + // ConstantGenerator { + // gate_index, + // gate: *self, + // i, + // constant: local_constants[self.const_input(i)], + // } + // .adapter(), + // ); + // g + // }) + // .collect() + vec![] } fn num_wires(&self) -> usize { @@ -114,9 +117,16 @@ impl, const D: usize> Gate for ConstantGate { self.num_consts } - fn extra_constants(&self) -> Vec<(usize, usize)> { + fn extra_constants(&self, gate_index: usize) -> Vec> { (0..self.num_consts) - .map(|i| (self.const_input(i), self.wire_output(i))) + .map(|i| { + ConstantGenerator::new( + gate_index, + self.const_input(i), + self.wire_output(i), + F::ZERO, + ) + }) .collect() } } @@ -133,27 +143,27 @@ impl, const D: usize> PackedEvaluableBase for } } -#[derive(Debug)] -struct ConstantGenerator { - gate_index: usize, - gate: ConstantGate, - i: usize, - constant: F, -} +// #[derive(Debug)] +// struct ConstantGenerator { +// gate_index: usize, +// gate: ConstantGate, +// i: usize, +// constant: F, +// } -impl SimpleGenerator for ConstantGenerator { - fn dependencies(&self) -> Vec { - Vec::new() - } - - fn run_once(&self, _witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { - let wire = Wire { - gate: self.gate_index, - input: self.gate.wire_output(self.i), - }; - out_buffer.set_wire(wire, self.constant); - } -} +// impl SimpleGenerator for ConstantGenerator { +// fn dependencies(&self) -> Vec { +// Vec::new() +// } +// +// fn run_once(&self, _witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { +// let wire = Wire { +// gate: self.gate_index, +// input: self.gate.wire_output(self.i), +// }; +// out_buffer.set_wire(wire, self.constant); +// } +// } #[cfg(test)] mod tests { diff --git a/plonky2/src/gates/gate.rs b/plonky2/src/gates/gate.rs index 9d1e4874..d41a1721 100644 --- a/plonky2/src/gates/gate.rs +++ b/plonky2/src/gates/gate.rs @@ -12,7 +12,7 @@ use crate::gates::selectors::UNUSED_SELECTOR; use crate::gates::util::StridedConstraintConsumer; use crate::hash::hash_types::RichField; use crate::iop::ext_target::ExtensionTarget; -use crate::iop::generator::WitnessGenerator; +use crate::iop::generator::{ConstantGenerator, WitnessGenerator}; use crate::plonk::circuit_builder::CircuitBuilder; use crate::plonk::vars::{ EvaluationTargets, EvaluationVars, EvaluationVarsBase, EvaluationVarsBaseBatch, @@ -181,7 +181,7 @@ pub trait Gate, const D: usize>: 'static + Send + S .len() } - fn extra_constants(&self) -> Vec<(usize, usize)> { + fn extra_constants(&self, _gate_index: usize) -> Vec> { vec![] } } diff --git a/plonky2/src/gates/random_access.rs b/plonky2/src/gates/random_access.rs index 33d76a54..687e5e53 100644 --- a/plonky2/src/gates/random_access.rs +++ b/plonky2/src/gates/random_access.rs @@ -11,7 +11,9 @@ use crate::gates::packed_util::PackedEvaluableBase; use crate::gates::util::StridedConstraintConsumer; use crate::hash::hash_types::RichField; use crate::iop::ext_target::ExtensionTarget; -use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator}; +use crate::iop::generator::{ + ConstantGenerator, GeneratedValues, SimpleGenerator, WitnessGenerator, +}; use crate::iop::target::Target; use crate::iop::wire::Wire; use crate::iop::witness::{PartitionWitness, Witness}; @@ -230,18 +232,18 @@ impl, const D: usize> Gate for RandomAccessGa ); g }) - .chain((0..self.num_extra_constants).map(|i| { - let g: Box> = Box::new( - RandomAccessExtraConstantsGenerator { - gate_index, - gate: *self, - i, - constant: local_constants[i], - } - .adapter(), - ); - g - })) + // .chain((0..self.num_extra_constants).map(|i| { + // let g: Box> = Box::new( + // RandomAccessExtraConstantsGenerator { + // gate_index, + // gate: *self, + // i, + // constant: local_constants[i], + // } + // .adapter(), + // ); + // g + // })) .collect() } @@ -266,9 +268,9 @@ impl, const D: usize> Gate for RandomAccessGa self.num_copies } - fn extra_constants(&self) -> Vec<(usize, usize)> { + fn extra_constants(&self, gate_index: usize) -> Vec> { (0..self.num_extra_constants) - .map(|i| (i, self.wire_extra_constant(i))) + .map(|i| ConstantGenerator::new(gate_index, i, self.wire_extra_constant(i), F::ZERO)) .collect() } } @@ -440,6 +442,7 @@ mod tests { lists: Vec>, access_indices: Vec, claimed_elements: Vec, + constants: &[F], ) -> Vec { let num_copies = lists.len(); let vec_size = lists[0].len(); @@ -458,6 +461,7 @@ mod tests { bit_vals.push(F::from_bool(((access_index >> i) & 1) != 0)); } } + v.extend(constants); v.extend(bit_vals); v.iter().map(|&x| x.into()).collect() @@ -478,6 +482,7 @@ mod tests { num_extra_constants: 1, _phantom: PhantomData, }; + let constants = F::rand_vec(gate.num_constants()); let good_claimed_elements = lists .iter() @@ -485,19 +490,26 @@ mod tests { .map(|(l, &i)| l[i]) .collect(); let good_vars = EvaluationVars { - local_constants: &[], + local_constants: &constants.iter().map(|&x| x.into()).collect::>(), local_wires: &get_wires( bits, lists.clone(), access_indices.clone(), good_claimed_elements, + &constants, ), public_inputs_hash: &HashOut::rand(), }; let bad_claimed_elements = F::rand_vec(4); let bad_vars = EvaluationVars { - local_constants: &[], - local_wires: &get_wires(bits, lists, access_indices, bad_claimed_elements), + local_constants: &constants.iter().map(|&x| x.into()).collect::>(), + local_wires: &get_wires( + bits, + lists, + access_indices, + bad_claimed_elements, + &constants, + ), public_inputs_hash: &HashOut::rand(), }; diff --git a/plonky2/src/iop/generator.rs b/plonky2/src/iop/generator.rs index 1569e889..f38fc65c 100644 --- a/plonky2/src/iop/generator.rs +++ b/plonky2/src/iop/generator.rs @@ -8,6 +8,7 @@ use plonky2_field::field_types::{Field, PrimeField}; use crate::gadgets::arithmetic_u32::U32Target; use crate::gadgets::biguint::BigUintTarget; use crate::gadgets::nonnative::NonNativeTarget; +use crate::gates::gate::Gate; use crate::hash::hash_types::{HashOut, HashOutTarget, RichField}; use crate::iop::ext_target::ExtensionTarget; use crate::iop::target::{BoolTarget, Target}; @@ -325,3 +326,38 @@ impl SimpleGenerator for NonzeroTestGenerator { out_buffer.set_target(self.dummy, dummy_value); } } + +#[derive(Debug, Clone)] +pub struct ConstantGenerator { + pub gate_index: usize, + pub constant_index: usize, + pub target_index: usize, + pub constant: F, +} + +impl ConstantGenerator { + pub fn new(gate_index: usize, constant_index: usize, target_index: usize, constant: F) -> Self { + ConstantGenerator { + gate_index, + constant_index, + target_index, + constant, + } + } + + pub fn set_constant(&mut self, c: F) { + self.constant = c; + } +} +impl SimpleGenerator for ConstantGenerator { + fn dependencies(&self) -> Vec { + vec![] + } + + fn run_once(&self, _witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { + out_buffer.set_target( + Target::wire(self.gate_index, self.target_index), + self.constant, + ); + } +} diff --git a/plonky2/src/plonk/circuit_builder.rs b/plonky2/src/plonk/circuit_builder.rs index 218a836e..45e51aa0 100644 --- a/plonky2/src/plonk/circuit_builder.rs +++ b/plonky2/src/plonk/circuit_builder.rs @@ -28,7 +28,7 @@ use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField}; use crate::hash::merkle_proofs::MerkleProofTarget; use crate::iop::ext_target::ExtensionTarget; use crate::iop::generator::{ - CopyGenerator, RandomValueGenerator, SimpleGenerator, WitnessGenerator, + ConstantGenerator, CopyGenerator, RandomValueGenerator, SimpleGenerator, WitnessGenerator, }; use crate::iop::target::{BoolTarget, Target}; use crate::iop::wire::Wire; @@ -85,8 +85,8 @@ pub struct CircuitBuilder, const D: usize> { /// Map between gate type and the current gate of this type with available slots. current_slots: HashMap, CurrentSlot>, - /// gate_index, constant_index, target_index - constants_slots: Vec<(usize, usize, usize)>, + /// gate_index, constant_index, target_index, ConstantGen + constants_slots: Vec>, } impl, const D: usize> CircuitBuilder { @@ -224,9 +224,11 @@ impl, const D: usize> CircuitBuilder { let index = self.gate_instances.len(); - for (ci, ti) in gate_type.extra_constants() { - self.constants_slots.push((index, ci, ti)); - } + self.constants_slots + .extend(gate_type.extra_constants(index)); + // for const_gen in gate_type.extra_constants() { + // self.constants_slots.push((index, ci, ti, gen)); + // } // Note that we can't immediately add this gate's generators, because the list of constants // could be modified later, i.e. in the case of `ConstantGate`. We will add them later in @@ -664,7 +666,6 @@ impl, const D: usize> CircuitBuilder { // Fill constants while self.constants_to_targets.len() > self.constants_slots.len() { - dbg!("lfg"); self.add_gate( ConstantGate { num_consts: self.config.num_constants, @@ -674,14 +675,19 @@ impl, const D: usize> CircuitBuilder { } dbg!(self.constants_to_targets.len(), self.constants_slots.len()); - for ((c, t), (gate_index, const_wire, target_wire)) in self + for ((c, t), mut const_gen) in self .constants_to_targets .clone() .into_iter() .zip(self.constants_slots.clone()) { - self.gate_instances[gate_index].constants[const_wire] = c; - self.generate_copy(Target::wire(gate_index, target_wire), t); + self.gate_instances[const_gen.gate_index].constants[const_gen.constant_index] = c; + self.generate_copy( + Target::wire(const_gen.gate_index, const_gen.target_index), + t, + ); + const_gen.set_constant(c); + self.add_simple_generator(const_gen); } info!(