plonky2/src/gates/arithmetic.rs

268 lines
11 KiB
Rust
Raw Normal View History

use std::ops::Range;
use crate::field::extension_field::target::ExtensionTarget;
use crate::field::extension_field::Extendable;
use crate::field::extension_field::FieldExtension;
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::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase};
2021-04-02 15:29:21 -07:00
/// A gate which can a linear combination `c0*x*y+c1*z` twice with the same `x`.
2021-04-02 15:29:21 -07:00
#[derive(Debug)]
pub struct ArithmeticExtensionGate<const D: usize>;
2021-04-02 15:29:21 -07:00
impl<const D: usize> ArithmeticExtensionGate<D> {
pub fn wires_first_multiplicand_0() -> Range<usize> {
0..D
}
pub fn wires_first_multiplicand_1() -> Range<usize> {
D..2 * D
}
pub fn wires_first_addend() -> Range<usize> {
2 * D..3 * D
}
2021-08-13 10:40:31 +02:00
pub fn wires_first_output() -> Range<usize> {
3 * D..4 * D
}
2021-08-13 10:40:31 +02:00
pub fn wires_second_multiplicand_0() -> Range<usize> {
4 * D..5 * D
}
2021-08-13 10:40:31 +02:00
pub fn wires_second_multiplicand_1() -> Range<usize> {
5 * D..6 * D
}
2021-08-13 10:40:31 +02:00
pub fn wires_second_addend() -> Range<usize> {
6 * D..7 * D
}
pub fn wires_second_output() -> Range<usize> {
7 * D..8 * D
}
2021-04-02 15:29:21 -07:00
}
impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D> {
2021-04-02 15:29:21 -07:00
fn id(&self) -> String {
format!("{:?}", self)
}
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
2021-04-02 15:29:21 -07:00
let const_0 = vars.local_constants[0];
let const_1 = vars.local_constants[1];
let first_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_first_multiplicand_0());
let first_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_first_multiplicand_1());
let first_addend = vars.get_local_ext_algebra(Self::wires_first_addend());
let second_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_0());
let second_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_1());
let second_addend = vars.get_local_ext_algebra(Self::wires_second_addend());
let first_output = vars.get_local_ext_algebra(Self::wires_first_output());
let second_output = vars.get_local_ext_algebra(Self::wires_second_output());
let first_computed_output = first_multiplicand_0 * first_multiplicand_1 * const_0.into()
+ first_addend * const_1.into();
let second_computed_output = second_multiplicand_0 * second_multiplicand_1 * const_0.into()
+ second_addend * const_1.into();
let mut constraints = (first_output - first_computed_output)
.to_basefield_array()
.to_vec();
constraints.extend((second_output - second_computed_output).to_basefield_array());
constraints
2021-04-02 15:29:21 -07:00
}
fn eval_unfiltered_base(&self, vars: EvaluationVarsBase<F>) -> Vec<F> {
let const_0 = vars.local_constants[0];
let const_1 = vars.local_constants[1];
let first_multiplicand_0 = vars.get_local_ext(Self::wires_first_multiplicand_0());
let first_multiplicand_1 = vars.get_local_ext(Self::wires_first_multiplicand_1());
let first_addend = vars.get_local_ext(Self::wires_first_addend());
let second_multiplicand_0 = vars.get_local_ext(Self::wires_second_multiplicand_0());
let second_multiplicand_1 = vars.get_local_ext(Self::wires_second_multiplicand_1());
let second_addend = vars.get_local_ext(Self::wires_second_addend());
let first_output = vars.get_local_ext(Self::wires_first_output());
let second_output = vars.get_local_ext(Self::wires_second_output());
let first_computed_output = first_multiplicand_0 * first_multiplicand_1 * const_0.into()
+ first_addend * const_1.into();
let second_computed_output = second_multiplicand_0 * second_multiplicand_1 * const_0.into()
+ second_addend * const_1.into();
let mut constraints = (first_output - first_computed_output)
.to_basefield_array()
.to_vec();
constraints.extend((second_output - second_computed_output).to_basefield_array());
constraints
}
2021-04-02 15:29:21 -07:00
fn eval_unfiltered_recursively(
&self,
builder: &mut CircuitBuilder<F, D>,
vars: EvaluationTargets<D>,
) -> Vec<ExtensionTarget<D>> {
2021-04-02 15:29:21 -07:00
let const_0 = vars.local_constants[0];
let const_1 = vars.local_constants[1];
let first_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_first_multiplicand_0());
let first_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_first_multiplicand_1());
let first_addend = vars.get_local_ext_algebra(Self::wires_first_addend());
let second_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_0());
let second_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_1());
let second_addend = vars.get_local_ext_algebra(Self::wires_second_addend());
let first_output = vars.get_local_ext_algebra(Self::wires_first_output());
let second_output = vars.get_local_ext_algebra(Self::wires_second_output());
let first_computed_output =
builder.mul_ext_algebra(first_multiplicand_0, first_multiplicand_1);
let first_computed_output = builder.scalar_mul_ext_algebra(const_0, first_computed_output);
let first_scaled_addend = builder.scalar_mul_ext_algebra(const_1, first_addend);
let first_computed_output =
builder.add_ext_algebra(first_computed_output, first_scaled_addend);
let second_computed_output =
builder.mul_ext_algebra(second_multiplicand_0, second_multiplicand_1);
let second_computed_output =
builder.scalar_mul_ext_algebra(const_0, second_computed_output);
let second_scaled_addend = builder.scalar_mul_ext_algebra(const_1, second_addend);
let second_computed_output =
builder.add_ext_algebra(second_computed_output, second_scaled_addend);
let diff_0 = builder.sub_ext_algebra(first_output, first_computed_output);
let diff_1 = builder.sub_ext_algebra(second_output, second_computed_output);
let mut constraints = diff_0.to_ext_target_array().to_vec();
constraints.extend(diff_1.to_ext_target_array());
constraints
2021-04-02 15:29:21 -07:00
}
fn generators(
&self,
gate_index: usize,
local_constants: &[F],
) -> Vec<Box<dyn WitnessGenerator<F>>> {
let gen0 = ArithmeticExtensionGenerator0 {
2021-04-02 15:29:21 -07:00
gate_index,
const_0: local_constants[0],
const_1: local_constants[1],
};
let gen1 = ArithmeticExtensionGenerator1 {
gate_index,
const_0: local_constants[0],
const_1: local_constants[1],
};
vec![Box::new(gen0), Box::new(gen1)]
2021-04-02 15:29:21 -07:00
}
fn num_wires(&self) -> usize {
8 * D
2021-04-02 15:29:21 -07:00
}
fn num_constants(&self) -> usize {
2
}
fn degree(&self) -> usize {
3
}
fn num_constraints(&self) -> usize {
2 * D
2021-04-02 15:29:21 -07:00
}
}
struct ArithmeticExtensionGenerator0<F: Extendable<D>, const D: usize> {
2021-04-02 15:29:21 -07:00
gate_index: usize,
const_0: F,
const_1: F,
}
struct ArithmeticExtensionGenerator1<F: Extendable<D>, const D: usize> {
gate_index: usize,
const_0: F,
const_1: F,
}
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator0<F, D> {
2021-04-02 15:29:21 -07:00
fn dependencies(&self) -> Vec<Target> {
ArithmeticExtensionGate::<D>::wires_first_multiplicand_0()
.chain(ArithmeticExtensionGate::<D>::wires_first_multiplicand_1())
.chain(ArithmeticExtensionGate::<D>::wires_first_addend())
.map(|i| Target::wire(self.gate_index, i))
.collect()
2021-04-02 15:29:21 -07:00
}
fn run_once(&self, witness: &PartialWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let extract_extension = |range: Range<usize>| -> F::Extension {
let t = ExtensionTarget::from_range(self.gate_index, range);
witness.get_extension_target(t)
2021-04-02 15:29:21 -07:00
};
let multiplicand_0 =
extract_extension(ArithmeticExtensionGate::<D>::wires_first_multiplicand_0());
let multiplicand_1 =
extract_extension(ArithmeticExtensionGate::<D>::wires_first_multiplicand_1());
let addend = extract_extension(ArithmeticExtensionGate::<D>::wires_first_addend());
let output_target = ExtensionTarget::from_range(
self.gate_index,
ArithmeticExtensionGate::<D>::wires_first_output(),
);
let computed_output =
multiplicand_0 * multiplicand_1 * self.const_0.into() + addend * self.const_1.into();
out_buffer.set_extension_target(output_target, computed_output)
}
}
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator1<F, D> {
fn dependencies(&self) -> Vec<Target> {
ArithmeticExtensionGate::<D>::wires_second_multiplicand_0()
.chain(ArithmeticExtensionGate::<D>::wires_second_multiplicand_1())
.chain(ArithmeticExtensionGate::<D>::wires_second_addend())
.map(|i| Target::wire(self.gate_index, i))
.collect()
}
fn run_once(&self, witness: &PartialWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let extract_extension = |range: Range<usize>| -> F::Extension {
let t = ExtensionTarget::from_range(self.gate_index, range);
witness.get_extension_target(t)
};
let multiplicand_0 =
extract_extension(ArithmeticExtensionGate::<D>::wires_second_multiplicand_0());
let multiplicand_1 =
extract_extension(ArithmeticExtensionGate::<D>::wires_second_multiplicand_1());
let addend = extract_extension(ArithmeticExtensionGate::<D>::wires_second_addend());
let output_target = ExtensionTarget::from_range(
self.gate_index,
ArithmeticExtensionGate::<D>::wires_second_output(),
);
let computed_output =
multiplicand_0 * multiplicand_1 * self.const_0.into() + addend * self.const_1.into();
out_buffer.set_extension_target(output_target, computed_output)
2021-04-02 15:29:21 -07:00
}
}
2021-05-20 05:27:56 -07:00
#[cfg(test)]
mod tests {
2021-08-02 14:17:42 +02:00
use anyhow::Result;
2021-05-20 05:27:56 -07:00
use crate::field::crandall_field::CrandallField;
use crate::gates::arithmetic::ArithmeticExtensionGate;
2021-08-02 14:17:42 +02:00
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
2021-05-20 05:27:56 -07:00
#[test]
fn low_degree() {
test_low_degree::<CrandallField, _, 4>(ArithmeticExtensionGate)
2021-05-20 05:27:56 -07:00
}
2021-08-02 14:17:42 +02:00
#[test]
fn eval_fns() -> Result<()> {
test_eval_fns::<CrandallField, _, 4>(ArithmeticExtensionGate)
}
2021-05-20 05:27:56 -07:00
}