plonky2/src/gates/base_sum.rs

176 lines
5.6 KiB
Rust
Raw Normal View History

2021-06-15 19:13:15 +02:00
use std::ops::Range;
2021-06-04 15:40:54 +02:00
use crate::field::extension_field::target::ExtensionTarget;
use crate::field::extension_field::Extendable;
use crate::field::field_types::Field;
2021-07-29 11:45:58 -07:00
use crate::gates::gate::Gate;
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
use crate::iop::target::Target;
use crate::iop::witness::PartialWitness;
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::plonk_common::{reduce_with_powers, reduce_with_powers_ext_recursive};
use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase};
2021-06-04 15:40:54 +02:00
2021-08-05 13:48:37 +02:00
/// A gate which can decompose a number into base B little-endian limbs.
#[derive(Clone, Debug)]
2021-06-04 15:40:54 +02:00
pub struct BaseSumGate<const B: usize> {
num_limbs: usize,
}
impl<const B: usize> BaseSumGate<B> {
pub fn new(num_limbs: usize) -> Self {
Self { num_limbs }
2021-06-04 15:40:54 +02:00
}
pub const WIRE_SUM: usize = 0;
2021-08-05 13:47:33 +02:00
pub const START_LIMBS: usize = 1;
2021-06-04 15:40:54 +02:00
/// Returns the index of the `i`th limb wire.
pub fn limbs(&self) -> Range<usize> {
2021-06-15 19:13:15 +02:00
Self::START_LIMBS..Self::START_LIMBS + self.num_limbs
2021-06-04 15:40:54 +02:00
}
}
impl<F: Extendable<D>, const D: usize, const B: usize> Gate<F, D> for BaseSumGate<B> {
fn id(&self) -> String {
2021-06-04 16:02:48 +02:00
format!("{:?} + Base: {}", self, B)
2021-06-04 15:40:54 +02:00
}
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
let sum = vars.local_wires[Self::WIRE_SUM];
let mut limbs = vars.local_wires[self.limbs()].to_vec();
2021-06-04 15:40:54 +02:00
let computed_sum = reduce_with_powers(&limbs, F::Extension::from_canonical_usize(B));
2021-08-05 13:47:33 +02:00
let mut constraints = vec![computed_sum - sum];
2021-06-04 15:40:54 +02:00
for limb in limbs {
constraints.push(
(0..B)
.map(|i| limb - F::Extension::from_canonical_usize(i))
.product(),
);
}
constraints
}
fn eval_unfiltered_base(&self, vars: EvaluationVarsBase<F>) -> Vec<F> {
let sum = vars.local_wires[Self::WIRE_SUM];
let mut limbs = vars.local_wires[self.limbs()].to_vec();
let computed_sum = reduce_with_powers(&limbs, F::from_canonical_usize(B));
2021-08-05 13:47:33 +02:00
let mut constraints = vec![computed_sum - sum];
for limb in limbs {
constraints.push((0..B).map(|i| limb - F::from_canonical_usize(i)).product());
}
constraints
}
2021-06-04 15:40:54 +02:00
fn eval_unfiltered_recursively(
&self,
builder: &mut CircuitBuilder<F, D>,
vars: EvaluationTargets<D>,
) -> Vec<ExtensionTarget<D>> {
let base = builder.constant(F::from_canonical_usize(B));
let sum = vars.local_wires[Self::WIRE_SUM];
let mut limbs = vars.local_wires[self.limbs()].to_vec();
2021-07-28 13:38:41 -07:00
let computed_sum = reduce_with_powers_ext_recursive(builder, &limbs, base);
2021-08-05 13:47:33 +02:00
let mut constraints = vec![builder.sub_extension(computed_sum, sum)];
2021-06-04 15:40:54 +02:00
for limb in limbs {
constraints.push({
let mut acc = builder.one_extension();
(0..B).for_each(|i| {
let it = builder.constant_extension(F::from_canonical_usize(i).into());
let diff = builder.sub_extension(limb, it);
2021-06-09 21:12:15 +02:00
acc = builder.mul_extension(acc, diff);
2021-06-04 15:40:54 +02:00
});
acc
});
}
constraints
}
fn generators(
&self,
gate_index: usize,
_local_constants: &[F],
) -> Vec<Box<dyn WitnessGenerator<F>>> {
let gen = BaseSplitGenerator::<B> {
gate_index,
num_limbs: self.num_limbs,
};
vec![Box::new(gen)]
}
2021-08-05 13:47:33 +02:00
// 1 for the sum then `num_limbs` for the limbs.
2021-06-04 15:40:54 +02:00
fn num_wires(&self) -> usize {
2021-08-05 13:51:26 +02:00
1 + self.num_limbs
2021-06-04 15:40:54 +02:00
}
fn num_constants(&self) -> usize {
0
}
// Bounded by the range-check (x-0)*(x-1)*...*(x-B+1).
2021-06-04 15:40:54 +02:00
fn degree(&self) -> usize {
B
}
2021-08-05 13:47:33 +02:00
// 1 for checking the sum then `num_limbs` for range-checking the limbs.
2021-06-04 15:40:54 +02:00
fn num_constraints(&self) -> usize {
2021-08-05 13:47:33 +02:00
1 + self.num_limbs
2021-06-04 15:40:54 +02:00
}
}
#[derive(Debug)]
pub struct BaseSplitGenerator<const B: usize> {
gate_index: usize,
num_limbs: usize,
}
impl<F: Field, const B: usize> SimpleGenerator<F> for BaseSplitGenerator<B> {
fn dependencies(&self) -> Vec<Target> {
2021-06-10 15:55:29 +02:00
vec![Target::wire(self.gate_index, BaseSumGate::<B>::WIRE_SUM)]
2021-06-04 15:40:54 +02:00
}
fn run_once(&self, witness: &PartialWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let sum_value = witness
.get_target(Target::wire(self.gate_index, BaseSumGate::<B>::WIRE_SUM))
2021-06-04 15:40:54 +02:00
.to_canonical_u64() as usize;
2021-06-10 16:48:05 +02:00
debug_assert_eq!(
(0..self.num_limbs).fold(sum_value, |acc, _| acc / B),
0,
"Integer too large to fit in given number of limbs"
);
2021-06-15 19:13:15 +02:00
let limbs = (BaseSumGate::<B>::START_LIMBS..BaseSumGate::<B>::START_LIMBS + self.num_limbs)
.map(|i| Target::wire(self.gate_index, i));
let limbs_value = (0..self.num_limbs)
.scan(sum_value, |acc, _| {
let tmp = *acc % B;
*acc /= B;
Some(F::from_canonical_usize(tmp))
})
.collect::<Vec<_>>();
for (b, b_value) in limbs.zip(limbs_value) {
out_buffer.set_target(b, b_value);
2021-06-04 15:40:54 +02:00
}
}
}
#[cfg(test)]
mod tests {
2021-08-02 14:17:42 +02:00
use anyhow::Result;
2021-06-04 15:40:54 +02:00
use crate::field::crandall_field::CrandallField;
use crate::gates::base_sum::BaseSumGate;
2021-08-02 14:17:42 +02:00
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
2021-06-04 15:40:54 +02:00
#[test]
fn low_degree() {
test_low_degree::<CrandallField, _, 4>(BaseSumGate::<6>::new(11))
2021-06-04 15:40:54 +02:00
}
2021-08-02 14:17:42 +02:00
#[test]
fn eval_fns() -> Result<()> {
test_eval_fns::<CrandallField, _, 4>(BaseSumGate::<6>::new(11))
}
2021-06-04 15:40:54 +02:00
}