2021-07-02 14:45:05 +02:00
|
|
|
use std::collections::HashMap;
|
2021-07-02 14:13:57 +02:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use std::hash::Hash;
|
2021-04-25 17:05:27 -07:00
|
|
|
|
|
|
|
|
use rayon::prelude::*;
|
|
|
|
|
|
|
|
|
|
use crate::field::field::Field;
|
|
|
|
|
use crate::polynomial::polynomial::PolynomialValues;
|
|
|
|
|
use crate::target::Target;
|
|
|
|
|
use crate::wire::Wire;
|
|
|
|
|
|
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)]
|
|
|
|
|
pub struct ForestNode<T: Debug + Copy + Eq + PartialEq> {
|
|
|
|
|
t: T,
|
|
|
|
|
parent: usize,
|
|
|
|
|
size: usize,
|
|
|
|
|
index: usize,
|
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-07-02 14:13:57 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-02 14:37:07 +02:00
|
|
|
pub struct TargetPartition<T: Debug + Copy + Eq + PartialEq + Hash, F: Fn(T) -> usize> {
|
2021-07-02 14:13:57 +02:00
|
|
|
forest: Vec<ForestNode<T>>,
|
2021-07-02 14:42:40 +02:00
|
|
|
/// Function to compute a node's index in the forest.
|
2021-07-02 14:37:07 +02:00
|
|
|
indices: F,
|
2021-07-02 14:13:57 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 14:37:07 +02:00
|
|
|
impl<T: Debug + Copy + Eq + PartialEq + Hash, F: Fn(T) -> usize> TargetPartition<T, F> {
|
|
|
|
|
pub fn new(f: F) -> Self {
|
2021-04-25 17:05:27 -07:00
|
|
|
Self {
|
2021-07-02 14:13:57 +02:00
|
|
|
forest: Vec::new(),
|
2021-07-02 14:37:07 +02:00
|
|
|
indices: f,
|
2021-04-25 17:05:27 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// Add a new partition with a single member.
|
2021-07-02 14:13:57 +02:00
|
|
|
pub fn add(&mut self, t: T) {
|
|
|
|
|
let index = self.forest.len();
|
2021-07-02 14:42:40 +02:00
|
|
|
debug_assert_eq!((self.indices)(t), index);
|
2021-07-02 14:26:49 +02:00
|
|
|
self.forest.push(ForestNode {
|
|
|
|
|
t,
|
|
|
|
|
parent: index,
|
|
|
|
|
size: 1,
|
|
|
|
|
index,
|
|
|
|
|
});
|
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-07-15 07:34:46 -07:00
|
|
|
pub fn find(&mut self, x: ForestNode<T>) -> ForestNode<T> {
|
2021-07-02 15:44:50 +02:00
|
|
|
if x.parent != x.index {
|
|
|
|
|
let root = self.find(self.forest[x.parent]);
|
|
|
|
|
self.forest[x.index].parent = root.index;
|
|
|
|
|
root
|
|
|
|
|
} else {
|
|
|
|
|
x
|
2021-04-25 17:05:27 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 14:42:40 +02:00
|
|
|
/// Merge two sets.
|
2021-07-02 14:13:57 +02:00
|
|
|
pub fn merge(&mut self, tx: T, ty: T) {
|
2021-07-02 15:34:23 +02:00
|
|
|
let mut x = self.forest[(self.indices)(tx)];
|
|
|
|
|
let mut y = self.forest[(self.indices)(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-07-02 15:34:23 +02:00
|
|
|
self.forest[x.index] = x;
|
|
|
|
|
self.forest[y.index] = y;
|
2021-07-02 14:13:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-07-02 14:37:07 +02:00
|
|
|
impl<F: Fn(Target) -> usize> TargetPartition<Target, F> {
|
2021-07-02 14:45:05 +02:00
|
|
|
pub fn wire_partition(&mut self) -> WirePartitions {
|
2021-07-02 14:13:57 +02:00
|
|
|
let mut partition = HashMap::<_, Vec<_>>::new();
|
|
|
|
|
let nodes = self.forest.clone();
|
|
|
|
|
for x in nodes {
|
|
|
|
|
let v = partition.entry(self.find(x).t).or_default();
|
|
|
|
|
v.push(x.t);
|
2021-04-25 17:05:27 -07:00
|
|
|
}
|
2021-07-02 14:13:57 +02:00
|
|
|
|
|
|
|
|
let mut indices = HashMap::new();
|
2021-07-21 08:26:41 -07:00
|
|
|
// Here we keep just the Wire targets, filtering out everything else.
|
2021-07-02 14:13:57 +02:00
|
|
|
let partition = partition
|
|
|
|
|
.into_values()
|
|
|
|
|
.map(|v| {
|
|
|
|
|
v.into_iter()
|
|
|
|
|
.filter_map(|t| match t {
|
|
|
|
|
Target::Wire(w) => Some(w),
|
|
|
|
|
_ => None,
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
partition.iter().enumerate().for_each(|(i, v)| {
|
|
|
|
|
v.iter().for_each(|t| {
|
|
|
|
|
indices.insert(*t, i);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-19 19:42:39 +02:00
|
|
|
WirePartitions { partition }
|
2021-04-25 17:05:27 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WirePartitions {
|
2021-07-02 14:13:57 +02:00
|
|
|
partition: Vec<Vec<Wire>>,
|
2021-04-25 17:05:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WirePartitions {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|