plonky2/src/plonk/permutation_argument.rs

161 lines
5.0 KiB
Rust
Raw Normal View History

2021-07-02 14:45:05 +02:00
use std::collections::HashMap;
2021-07-02 14:13:57 +02:00
use std::fmt::Debug;
2021-04-25 17:05:27 -07:00
use rayon::prelude::*;
use crate::field::field_types::Field;
use crate::iop::target::Target;
use crate::iop::wire::Wire;
2021-08-20 10:44:19 +02:00
use crate::iop::witness::PartitionWitness;
2021-04-25 17:05:27 -07:00
use crate::polynomial::polynomial::PolynomialValues;
2021-07-02 14:42:40 +02:00
/// Node in the Disjoint Set Forest.
2021-07-02 14:13:57 +02:00
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2021-08-19 14:54:11 +02:00
pub struct ForestNode<T: Debug + Copy + Eq + PartialEq, V: Field> {
pub t: T,
pub parent: usize,
pub size: usize,
pub index: usize,
pub value: Option<V>,
2021-04-25 17:05:27 -07:00
}
2021-07-02 14:42:40 +02:00
/// Disjoint Set Forest data-structure following https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
2021-08-20 10:44:19 +02:00
impl<F: Field> PartitionWitness<F> {
2021-08-22 10:36:44 +02:00
pub fn new(
num_wires: usize,
num_routed_wires: usize,
degree: usize,
num_virtual_targets: usize,
) -> Self {
2021-04-25 17:05:27 -07:00
Self {
2021-08-22 10:36:44 +02:00
forest: Vec::with_capacity(degree * num_wires + num_virtual_targets),
2021-08-20 10:44:19 +02:00
num_wires,
num_routed_wires,
degree,
2021-04-25 17:05:27 -07:00
}
}
2021-08-20 10:44:19 +02:00
2021-04-25 17:05:27 -07:00
/// Add a new partition with a single member.
2021-08-20 10:44:19 +02:00
pub fn add(&mut self, t: Target) {
2021-08-20 12:55:59 +02:00
let index = self.forest.len();
2021-08-20 10:44:19 +02:00
debug_assert_eq!(self.target_index(t), index);
2021-08-20 12:55:59 +02:00
self.forest.push(ForestNode {
2021-07-02 14:26:49 +02:00
t,
parent: index,
size: 1,
index,
2021-08-19 14:54:11 +02:00
value: None,
2021-07-02 14:26:49 +02:00
});
2021-04-25 17:05:27 -07:00
}
2021-07-02 15:44:50 +02:00
/// Path compression method, see https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives.
2021-08-20 10:44:19 +02:00
pub fn find(&mut self, x: ForestNode<Target, F>) -> ForestNode<Target, F> {
2021-07-02 15:44:50 +02:00
if x.parent != x.index {
2021-08-20 12:55:59 +02:00
let root = self.find(self.forest[x.parent]);
self.forest[x.index].parent = root.index;
2021-07-02 15:44:50 +02:00
root
} else {
x
2021-04-25 17:05:27 -07:00
}
}
2021-07-02 14:42:40 +02:00
/// Merge two sets.
2021-08-20 10:44:19 +02:00
pub fn merge(&mut self, tx: Target, ty: Target) {
2021-08-20 12:55:59 +02:00
let mut x = self.forest[self.target_index(tx)];
let mut y = self.forest[self.target_index(ty)];
2021-04-25 17:05:27 -07:00
2021-07-02 15:34:23 +02:00
x = self.find(x);
y = self.find(y);
2021-07-02 14:13:57 +02:00
if x == y {
return;
2021-04-25 17:05:27 -07:00
}
2021-07-02 15:34:23 +02:00
if x.size >= y.size {
y.parent = x.index;
x.size += y.size;
} else {
x.parent = y.index;
y.size += x.size;
2021-04-25 17:05:27 -07:00
}
2021-08-20 12:55:59 +02:00
self.forest[x.index] = x;
self.forest[y.index] = y;
2021-07-02 14:13:57 +02:00
}
2021-08-23 11:06:33 +02:00
2021-08-20 13:06:07 +02:00
pub fn wire_partition(&mut self) -> WirePartition {
2021-07-02 14:13:57 +02:00
let mut partition = HashMap::<_, Vec<_>>::new();
2021-08-20 10:44:19 +02:00
for gate in 0..self.degree {
for input in 0..self.num_routed_wires {
let w = Wire { gate, input };
let t = Target::Wire(w);
2021-08-20 12:55:59 +02:00
let x = self.forest[self.target_index(t)];
2021-08-20 10:44:19 +02:00
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.
2021-08-20 12:55:59 +02:00
for index in 0..self.forest.len() - self.degree * self.num_wires {
2021-08-20 10:44:19 +02:00
let t = Target::VirtualTarget { index };
2021-08-20 12:55:59 +02:00
let x = self.forest[self.target_index(t)];
2021-08-20 10:44:19 +02:00
self.find(x);
2021-04-25 17:05:27 -07:00
}
2021-07-02 14:13:57 +02:00
// Here we keep just the Wire targets, filtering out everything else.
2021-08-20 10:44:19 +02:00
let partition = partition.into_values().collect::<Vec<_>>();
2021-08-20 13:06:07 +02:00
WirePartition { partition }
2021-04-25 17:05:27 -07:00
}
}
2021-08-20 13:06:07 +02:00
pub struct WirePartition {
2021-07-02 14:13:57 +02:00
partition: Vec<Vec<Wire>>,
2021-04-25 17:05:27 -07:00
}
2021-08-20 13:06:07 +02:00
impl WirePartition {
2021-04-25 17:05:27 -07:00
pub(crate) fn get_sigma_polys<F: Field>(
&self,
degree_log: usize,
k_is: &[F],
2021-06-16 17:43:41 +02:00
subgroup: &[F],
2021-04-25 17:05:27 -07:00
) -> Vec<PolynomialValues<F>> {
let degree = 1 << degree_log;
2021-07-19 19:42:39 +02:00
let sigma = self.get_sigma_map(degree, k_is.len());
2021-04-25 17:05:27 -07:00
sigma
.chunks(degree)
.map(|chunk| {
let values = chunk
.par_iter()
2021-06-16 17:43:41 +02:00
.map(|&x| k_is[x / degree] * subgroup[x % degree])
2021-04-25 17:05:27 -07:00
.collect::<Vec<_>>();
PolynomialValues::new(values)
})
.collect()
}
/// Generates sigma in the context of Plonk, which is a map from `[kn]` to `[kn]`, where `k` is
/// the number of routed wires and `n` is the number of gates.
2021-07-19 19:42:39 +02:00
fn get_sigma_map(&self, degree: usize, num_routed_wires: usize) -> Vec<usize> {
2021-07-15 10:39:57 +02:00
// Find a wire's "neighbor" in the context of Plonk's "extended copy constraints" check. In
// other words, find the next wire in the given wire's partition. If the given wire is last in
// its partition, this will loop around. If the given wire has a partition all to itself, it
// is considered its own neighbor.
2021-07-14 22:34:16 +02:00
let mut neighbors = HashMap::new();
for subset in &self.partition {
for n in 0..subset.len() {
neighbors.insert(subset[n], subset[(n + 1) % subset.len()]);
}
}
2021-04-25 17:05:27 -07:00
let mut sigma = Vec::new();
for input in 0..num_routed_wires {
for gate in 0..degree {
let wire = Wire { gate, input };
2021-07-14 22:34:16 +02:00
let neighbor = neighbors[&wire];
2021-04-25 17:05:27 -07:00
sigma.push(neighbor.input * degree + neighbor.gate);
}
}
sigma
}
}