From 507577b7ad253d6b98dc58b8573af132701b35fc Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Fri, 20 Aug 2021 12:55:59 +0200 Subject: [PATCH] Comments --- src/hash/merkle_proofs.rs | 2 +- src/iop/generator.rs | 2 +- src/iop/witness.rs | 13 +++++++------ src/plonk/permutation_argument.rs | 24 ++++++++++++------------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/hash/merkle_proofs.rs b/src/hash/merkle_proofs.rs index c0fc3afa..cb782f41 100644 --- a/src/hash/merkle_proofs.rs +++ b/src/hash/merkle_proofs.rs @@ -83,7 +83,7 @@ impl, const D: usize> CircuitBuilder { gate, input: swap_wire, }); - self.generate_copy(bit.target, swap_wire); + self.route(bit.target, swap_wire); let input_wires = (0..12) .map(|i| { diff --git a/src/iop/generator.rs b/src/iop/generator.rs index f71a1826..1d0227c8 100644 --- a/src/iop/generator.rs +++ b/src/iop/generator.rs @@ -17,7 +17,7 @@ pub(crate) fn generate_partial_witness( generators: &[Box>], timing: &mut TimingTree, ) { - let max_target_index = witness.nodes.len(); + let max_target_index = witness.forest.len(); // Index generator indices by their watched targets. let mut generator_indices_by_watches = vec![Vec::new(); max_target_index]; timed!(timing, "index generators by their watched targets", { diff --git a/src/iop/witness.rs b/src/iop/witness.rs index b1d69610..ad61dde5 100644 --- a/src/iop/witness.rs +++ b/src/iop/witness.rs @@ -11,6 +11,7 @@ use crate::iop::target::{BoolTarget, Target}; use crate::iop::wire::Wire; use crate::plonk::permutation_argument::ForestNode; +/// A witness holds information on the values of targets in a circuit. pub trait Witness { fn try_get_target(&self, target: Target) -> Option; @@ -155,8 +156,6 @@ impl MatrixWitness { #[derive(Clone, Debug)] pub struct PartialWitness { - // pub(crate) wire_values: Vec>>, - // pub(crate) virtual_target_values: Vec>, pub(crate) target_values: HashMap, } @@ -185,9 +184,11 @@ impl Witness for PartialWitness { } } +/// `PartitionWitness` holds a disjoint-set forest of the targets respecting a ciruit's copy constraints. +/// The value of a target is defined to be the value of its root in the forest. #[derive(Clone)] pub struct PartitionWitness { - pub nodes: Vec>, + pub forest: Vec>, pub num_wires: usize, pub num_routed_wires: usize, pub degree: usize, @@ -195,12 +196,12 @@ pub struct PartitionWitness { impl Witness for PartitionWitness { fn try_get_target(&self, target: Target) -> Option { - self.nodes[self.nodes[self.target_index(target)].parent].value + self.forest[self.forest[self.target_index(target)].parent].value } fn set_target(&mut self, target: Target, value: F) { - let i = self.nodes[self.target_index(target)].parent; - self.nodes[i].value = Some(value); + let i = self.forest[self.target_index(target)].parent; + self.forest[i].value = Some(value); } } diff --git a/src/plonk/permutation_argument.rs b/src/plonk/permutation_argument.rs index 9dca05c6..4fbbf0ec 100644 --- a/src/plonk/permutation_argument.rs +++ b/src/plonk/permutation_argument.rs @@ -23,7 +23,7 @@ pub struct ForestNode { impl PartitionWitness { pub fn new(num_wires: usize, num_routed_wires: usize, degree: usize) -> Self { Self { - nodes: vec![], + forest: vec![], num_wires, num_routed_wires, degree, @@ -32,9 +32,9 @@ impl PartitionWitness { /// Add a new partition with a single member. pub fn add(&mut self, t: Target) { - let index = self.nodes.len(); + let index = self.forest.len(); debug_assert_eq!(self.target_index(t), index); - self.nodes.push(ForestNode { + self.forest.push(ForestNode { t, parent: index, size: 1, @@ -46,8 +46,8 @@ impl PartitionWitness { /// Path compression method, see https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives. pub fn find(&mut self, x: ForestNode) -> ForestNode { if x.parent != x.index { - let root = self.find(self.nodes[x.parent]); - self.nodes[x.index].parent = root.index; + let root = self.find(self.forest[x.parent]); + self.forest[x.index].parent = root.index; root } else { x @@ -56,8 +56,8 @@ impl PartitionWitness { /// Merge two sets. pub fn merge(&mut self, tx: Target, ty: Target) { - let mut x = self.nodes[self.target_index(tx)]; - let mut y = self.nodes[self.target_index(ty)]; + let mut x = self.forest[self.target_index(tx)]; + let mut y = self.forest[self.target_index(ty)]; x = self.find(x); y = self.find(y); @@ -74,8 +74,8 @@ impl PartitionWitness { y.size += x.size; } - self.nodes[x.index] = x; - self.nodes[y.index] = y; + self.forest[x.index] = x; + self.forest[y.index] = y; } } impl PartitionWitness { @@ -85,14 +85,14 @@ impl PartitionWitness { for input in 0..self.num_routed_wires { let w = Wire { gate, input }; let t = Target::Wire(w); - let x = self.nodes[self.target_index(t)]; + let x = self.forest[self.target_index(t)]; partition.entry(self.find(x).t).or_default().push(w); } } // I'm not 100% sure this loop is needed, but I'm afraid removing it might lead to subtle bugs. - for index in 0..self.nodes.len() - self.degree * self.num_wires { + for index in 0..self.forest.len() - self.degree * self.num_wires { let t = Target::VirtualTarget { index }; - let x = self.nodes[self.target_index(t)]; + let x = self.forest[self.target_index(t)]; self.find(x); }