From 1d77116ef3ff5186c6599047326ebae6563ed861 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Thu, 24 Mar 2022 07:19:48 +0100 Subject: [PATCH] Working --- plonky2/src/gadgets/biguint.rs | 2 ++ plonky2/src/gates/gate.rs | 7 +++++++ plonky2/src/gates/selectors.rs | 2 +- plonky2/src/plonk/circuit_builder.rs | 28 ++++++++++++++-------------- plonky2/src/plonk/circuit_data.rs | 2 +- plonky2/src/plonk/mod.rs | 1 + plonky2/src/plonk/vars.rs | 2 +- 7 files changed, 27 insertions(+), 17 deletions(-) diff --git a/plonky2/src/gadgets/biguint.rs b/plonky2/src/gadgets/biguint.rs index c9ad7280..b516bd70 100644 --- a/plonky2/src/gadgets/biguint.rs +++ b/plonky2/src/gadgets/biguint.rs @@ -262,6 +262,7 @@ mod tests { use crate::iop::witness::Witness; use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; + use crate::plonk::constest::check_constraints; use crate::{ iop::witness::PartialWitness, plonk::{circuit_builder::CircuitBuilder, circuit_data::CircuitConfig, verifier::verify}, @@ -352,6 +353,7 @@ mod tests { pw.set_biguint_target(&y, &y_value); pw.set_biguint_target(&expected_z, &expected_z_value); + // check_constraints::(builder, pw) let data = builder.build::(); let proof = data.prove(pw).unwrap(); verify(proof, &data.verifier_only, &data.common) diff --git a/plonky2/src/gates/gate.rs b/plonky2/src/gates/gate.rs index b5fc7772..ccc65512 100644 --- a/plonky2/src/gates/gate.rs +++ b/plonky2/src/gates/gate.rs @@ -93,6 +93,13 @@ pub trait Gate, const D: usize>: 'static + Send + S combination_range, vars.local_constants[selector_index], ); + // println!( + // "{} {:?} {} {}", + // self.id(), + // combination_range, + // gate_index, + // self.degree() + // ); vars.remove_prefix(num_selectors); self.eval_unfiltered(vars) .into_iter() diff --git a/plonky2/src/gates/selectors.rs b/plonky2/src/gates/selectors.rs index 3eb5282e..9d0145af 100644 --- a/plonky2/src/gates/selectors.rs +++ b/plonky2/src/gates/selectors.rs @@ -21,7 +21,7 @@ pub(crate) fn compute_selectors, const D: usize>( while pos < gates.len() { let mut i = 0; - while (pos + i < gates.len()) && (i + gates[pos + i].0.degree() <= max_degree) { + while (pos + i < gates.len()) && (i + gates[pos + i].0.degree() + 1 <= max_degree) { i += 1; } combinations.push((pos, pos + i)); diff --git a/plonky2/src/plonk/circuit_builder.rs b/plonky2/src/plonk/circuit_builder.rs index 49acaade..fd28f2ea 100644 --- a/plonky2/src/plonk/circuit_builder.rs +++ b/plonky2/src/plonk/circuit_builder.rs @@ -51,16 +51,16 @@ pub struct CircuitBuilder, const D: usize> { pub config: CircuitConfig, /// The types of gates used in this circuit. - gates: HashSet>, + pub(crate) gates: HashSet>, /// The concrete placement of each gate. pub(crate) gate_instances: Vec>, /// Targets to be made public. - public_inputs: Vec, + pub(crate) public_inputs: Vec, /// The next available index for a `VirtualTarget`. - virtual_target_index: usize, + pub(crate) virtual_target_index: usize, copy_constraints: Vec, @@ -68,10 +68,10 @@ pub struct CircuitBuilder, const D: usize> { context_log: ContextTree, /// A vector of marked targets. The values assigned to these targets will be displayed by the prover. - marked_targets: Vec>, + pub(crate) marked_targets: Vec>, /// Generators used to generate the witness. - generators: Vec>>, + pub(crate) generators: Vec>>, constants_to_targets: HashMap, targets_to_constants: HashMap, @@ -83,7 +83,7 @@ pub struct CircuitBuilder, const D: usize> { pub(crate) arithmetic_results: HashMap, ExtensionTarget>, /// Map between gate type and the current gate of this type with available slots. - current_slots: HashMap, CurrentSlot>, + pub(crate) current_slots: HashMap, CurrentSlot>, } impl, const D: usize> CircuitBuilder { @@ -424,7 +424,7 @@ impl, const D: usize> CircuitBuilder { (gate_idx, slot_idx) } - fn fri_params(&self, degree_bits: usize) -> FriParams { + pub(crate) fn fri_params(&self, degree_bits: usize) -> FriParams { self.config .fri_config .fri_params(degree_bits, self.config.zero_knowledge) @@ -493,7 +493,7 @@ impl, const D: usize> CircuitBuilder { } } - fn blind_and_pad(&mut self) { + pub(crate) fn blind_and_pad(&mut self) { if self.config.zero_knowledge { self.blind(); } @@ -580,7 +580,11 @@ impl, const D: usize> CircuitBuilder { .collect() } - fn sigma_vecs(&self, k_is: &[F], subgroup: &[F]) -> (Vec>, Forest) { + pub(crate) fn sigma_vecs( + &self, + k_is: &[F], + subgroup: &[F], + ) -> (Vec>, Forest) { let degree = self.gate_instances.len(); let degree_log = log2_strict(degree); let config = &self.config; @@ -637,9 +641,6 @@ impl, const D: usize> CircuitBuilder { where [(); C::Hasher::HASH_SIZE]:, { - for g in &self.gate_instances { - dbg!(&g.gate_ref.0.id()); - } let mut timing = TimingTree::new("preprocess", Level::Trace); let start = Instant::now(); let rate_bits = self.config.fri_config.rate_bits; @@ -675,14 +676,13 @@ impl, const D: usize> CircuitBuilder { let mut gates = self.gates.iter().cloned().collect::>(); gates.sort_unstable_by_key(|g| g.0.degree()); - dbg!(&gates); let (constant_vecs, selector_indices, combination_ranges, num_selectors) = compute_selectors( gates.clone(), &self.gate_instances, self.config.max_quotient_degree_factor + 1, ); - dbg!(&constant_vecs, &selector_indices, &combination_ranges); + // dbg!(&constant_vecs, &selector_indices, &combination_ranges); let num_constants = constant_vecs.len(); // let (gate_tree, max_filtered_constraint_degree, num_constants) = Tree::from_gates(gates); // let prefixed_gates = PrefixedGate::from_tree(gate_tree); diff --git a/plonky2/src/plonk/circuit_data.rs b/plonky2/src/plonk/circuit_data.rs index c889def9..c5910f0a 100644 --- a/plonky2/src/plonk/circuit_data.rs +++ b/plonky2/src/plonk/circuit_data.rs @@ -63,7 +63,7 @@ impl CircuitConfig { Self { num_wires: 135, num_routed_wires: 80, - constant_gate_size: 2, + constant_gate_size: 5, use_base_arithmetic_gate: true, security_bits: 100, num_challenges: 2, diff --git a/plonky2/src/plonk/mod.rs b/plonky2/src/plonk/mod.rs index 4f2fa4e1..fe025898 100644 --- a/plonky2/src/plonk/mod.rs +++ b/plonky2/src/plonk/mod.rs @@ -1,6 +1,7 @@ pub mod circuit_builder; pub mod circuit_data; pub mod config; +pub mod constest; pub(crate) mod copy_constraint; mod get_challenges; pub(crate) mod permutation_argument; diff --git a/plonky2/src/plonk/vars.rs b/plonky2/src/plonk/vars.rs index 8c7b71e3..91529b15 100644 --- a/plonky2/src/plonk/vars.rs +++ b/plonky2/src/plonk/vars.rs @@ -21,7 +21,7 @@ pub struct EvaluationVars<'a, F: RichField + Extendable, const D: usize> { /// evaluation points, then wire 1 for all points, and so on). #[derive(Debug, Copy, Clone)] pub struct EvaluationVarsBaseBatch<'a, F: Field> { - batch_size: usize, + pub(crate) batch_size: usize, pub local_constants: &'a [F], pub local_wires: &'a [F], pub public_inputs_hash: &'a HashOut,