2021-06-25 16:31:10 +02:00
|
|
|
use std::ops::Range;
|
|
|
|
|
|
2021-05-30 13:25:53 -07:00
|
|
|
use crate::field::extension_field::target::ExtensionTarget;
|
2021-07-22 23:48:03 -07:00
|
|
|
use crate::field::extension_field::Extendable;
|
|
|
|
|
use crate::field::extension_field::FieldExtension;
|
|
|
|
|
use crate::gates::gate::Gate;
|
2021-08-19 14:54:11 +02:00
|
|
|
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator, Yo};
|
2021-07-29 22:00:29 -07:00
|
|
|
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
|
|
|
|
2021-08-16 10:18:10 +02:00
|
|
|
/// Number of arithmetic operations performed by an arithmetic gate.
|
|
|
|
|
pub const NUM_ARITHMETIC_OPS: usize = 4;
|
|
|
|
|
|
2021-06-25 16:31:10 +02: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)]
|
2021-06-25 16:31:10 +02:00
|
|
|
pub struct ArithmeticExtensionGate<const D: usize>;
|
2021-04-02 15:29:21 -07:00
|
|
|
|
2021-06-25 16:31:10 +02:00
|
|
|
impl<const D: usize> ArithmeticExtensionGate<D> {
|
2021-08-16 10:18:10 +02:00
|
|
|
pub fn wires_ith_multiplicand_0(i: usize) -> Range<usize> {
|
|
|
|
|
4 * D * i..4 * D * i + D
|
2021-08-13 14:28:05 +02:00
|
|
|
}
|
2021-08-16 10:18:10 +02:00
|
|
|
pub fn wires_ith_multiplicand_1(i: usize) -> Range<usize> {
|
|
|
|
|
4 * D * i + D..4 * D * i + 2 * D
|
2021-08-13 14:28:05 +02:00
|
|
|
}
|
2021-08-16 10:18:10 +02:00
|
|
|
pub fn wires_ith_addend(i: usize) -> Range<usize> {
|
|
|
|
|
4 * D * i + 2 * D..4 * D * i + 3 * D
|
2021-08-13 14:28:05 +02:00
|
|
|
}
|
2021-08-16 10:18:10 +02:00
|
|
|
pub fn wires_ith_output(i: usize) -> Range<usize> {
|
|
|
|
|
4 * D * i + 3 * D..4 * D * i + 4 * D
|
2021-08-13 14:28:05 +02:00
|
|
|
}
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:31:10 +02: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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 13:25:53 -07:00
|
|
|
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];
|
2021-06-25 16:31:10 +02:00
|
|
|
|
2021-08-16 10:18:10 +02:00
|
|
|
let mut constraints = Vec::new();
|
|
|
|
|
for i in 0..NUM_ARITHMETIC_OPS {
|
|
|
|
|
let multiplicand_0 = vars.get_local_ext_algebra(Self::wires_ith_multiplicand_0(i));
|
|
|
|
|
let multiplicand_1 = vars.get_local_ext_algebra(Self::wires_ith_multiplicand_1(i));
|
|
|
|
|
let addend = vars.get_local_ext_algebra(Self::wires_ith_addend(i));
|
|
|
|
|
let output = vars.get_local_ext_algebra(Self::wires_ith_output(i));
|
|
|
|
|
let computed_output =
|
2021-08-17 10:26:31 +02:00
|
|
|
(multiplicand_0 * multiplicand_1).scalar_mul(const_0) + addend.scalar_mul(const_1);
|
2021-08-16 10:18:10 +02:00
|
|
|
|
|
|
|
|
constraints.extend((output - computed_output).to_basefield_array());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:31:10 +02:00
|
|
|
constraints
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 14:00:55 +02: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];
|
|
|
|
|
|
2021-08-16 10:18:10 +02:00
|
|
|
let mut constraints = Vec::new();
|
|
|
|
|
for i in 0..NUM_ARITHMETIC_OPS {
|
|
|
|
|
let multiplicand_0 = vars.get_local_ext(Self::wires_ith_multiplicand_0(i));
|
|
|
|
|
let multiplicand_1 = vars.get_local_ext(Self::wires_ith_multiplicand_1(i));
|
|
|
|
|
let addend = vars.get_local_ext(Self::wires_ith_addend(i));
|
|
|
|
|
let output = vars.get_local_ext(Self::wires_ith_output(i));
|
|
|
|
|
let computed_output =
|
2021-08-17 00:49:01 -07:00
|
|
|
(multiplicand_0 * multiplicand_1).scalar_mul(const_0) + addend.scalar_mul(const_1);
|
2021-08-16 10:18:10 +02:00
|
|
|
|
|
|
|
|
constraints.extend((output - computed_output).to_basefield_array());
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 14:00:55 +02:00
|
|
|
constraints
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 15:29:21 -07:00
|
|
|
fn eval_unfiltered_recursively(
|
|
|
|
|
&self,
|
2021-05-30 13:25:53 -07:00
|
|
|
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];
|
|
|
|
|
|
2021-08-16 10:18:10 +02:00
|
|
|
let mut constraints = Vec::new();
|
|
|
|
|
for i in 0..NUM_ARITHMETIC_OPS {
|
|
|
|
|
let multiplicand_0 = vars.get_local_ext_algebra(Self::wires_ith_multiplicand_0(i));
|
|
|
|
|
let multiplicand_1 = vars.get_local_ext_algebra(Self::wires_ith_multiplicand_1(i));
|
|
|
|
|
let addend = vars.get_local_ext_algebra(Self::wires_ith_addend(i));
|
|
|
|
|
let output = vars.get_local_ext_algebra(Self::wires_ith_output(i));
|
|
|
|
|
let computed_output = {
|
|
|
|
|
let mul = builder.mul_ext_algebra(multiplicand_0, multiplicand_1);
|
|
|
|
|
let scaled_mul = builder.scalar_mul_ext_algebra(const_0, mul);
|
|
|
|
|
let scaled_addend = builder.scalar_mul_ext_algebra(const_1, addend);
|
|
|
|
|
builder.add_ext_algebra(scaled_mul, scaled_addend)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let diff = builder.sub_ext_algebra(output, computed_output);
|
|
|
|
|
constraints.extend(diff.to_ext_target_array());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:31:10 +02:00
|
|
|
constraints
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generators(
|
|
|
|
|
&self,
|
|
|
|
|
gate_index: usize,
|
|
|
|
|
local_constants: &[F],
|
|
|
|
|
) -> Vec<Box<dyn WitnessGenerator<F>>> {
|
2021-08-16 11:08:26 +02:00
|
|
|
(0..NUM_ARITHMETIC_OPS)
|
2021-08-16 10:18:10 +02:00
|
|
|
.map(|i| {
|
|
|
|
|
let g: Box<dyn WitnessGenerator<F>> = Box::new(ArithmeticExtensionGenerator {
|
|
|
|
|
gate_index,
|
|
|
|
|
const_0: local_constants[0],
|
|
|
|
|
const_1: local_constants[1],
|
|
|
|
|
i,
|
|
|
|
|
});
|
|
|
|
|
g
|
2021-08-13 14:28:05 +02:00
|
|
|
})
|
2021-08-16 10:18:10 +02:00
|
|
|
.collect::<Vec<_>>()
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_wires(&self) -> usize {
|
2021-08-16 10:18:10 +02:00
|
|
|
NUM_ARITHMETIC_OPS * 4 * 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 {
|
2021-08-16 10:18:10 +02:00
|
|
|
NUM_ARITHMETIC_OPS * D
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 14:54:11 +02:00
|
|
|
#[derive(Debug)]
|
2021-08-13 14:28:05 +02:00
|
|
|
struct ArithmeticExtensionGenerator<F: Extendable<D>, const D: usize> {
|
2021-06-29 12:33:11 -07:00
|
|
|
gate_index: usize,
|
|
|
|
|
const_0: F,
|
|
|
|
|
const_1: F,
|
2021-08-13 14:28:05 +02:00
|
|
|
i: usize,
|
2021-06-29 12:33:11 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-13 14:28:05 +02:00
|
|
|
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator<F, D> {
|
2021-06-29 12:33:11 -07:00
|
|
|
fn dependencies(&self) -> Vec<Target> {
|
2021-08-16 10:18:10 +02:00
|
|
|
ArithmeticExtensionGate::<D>::wires_ith_multiplicand_0(self.i)
|
|
|
|
|
.chain(ArithmeticExtensionGate::<D>::wires_ith_multiplicand_1(
|
|
|
|
|
self.i,
|
|
|
|
|
))
|
|
|
|
|
.chain(ArithmeticExtensionGate::<D>::wires_ith_addend(self.i))
|
2021-06-29 12:33:11 -07:00
|
|
|
.map(|i| Target::wire(self.gate_index, i))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 14:54:11 +02:00
|
|
|
fn run_once(&self, witness: &Yo<F>, out_buffer: &mut GeneratedValues<F>) {
|
2021-06-29 12:33:11 -07:00
|
|
|
let extract_extension = |range: Range<usize>| -> F::Extension {
|
|
|
|
|
let t = ExtensionTarget::from_range(self.gate_index, range);
|
|
|
|
|
witness.get_extension_target(t)
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-16 10:18:10 +02:00
|
|
|
let multiplicand_0 = extract_extension(
|
|
|
|
|
ArithmeticExtensionGate::<D>::wires_ith_multiplicand_0(self.i),
|
|
|
|
|
);
|
|
|
|
|
let multiplicand_1 = extract_extension(
|
|
|
|
|
ArithmeticExtensionGate::<D>::wires_ith_multiplicand_1(self.i),
|
|
|
|
|
);
|
|
|
|
|
let addend = extract_extension(ArithmeticExtensionGate::<D>::wires_ith_addend(self.i));
|
|
|
|
|
|
|
|
|
|
let output_target = ExtensionTarget::from_range(
|
|
|
|
|
self.gate_index,
|
|
|
|
|
ArithmeticExtensionGate::<D>::wires_ith_output(self.i),
|
|
|
|
|
);
|
2021-06-25 16:31:10 +02:00
|
|
|
|
2021-08-17 10:26:31 +02:00
|
|
|
let computed_output = (multiplicand_0 * multiplicand_1).scalar_mul(self.const_0)
|
|
|
|
|
+ addend.scalar_mul(self.const_1);
|
2021-06-25 16:31:10 +02:00
|
|
|
|
2021-08-02 10:55:10 -07:00
|
|
|
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;
|
2021-06-25 16:31:10 +02:00
|
|
|
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() {
|
2021-07-22 23:48:03 -07:00
|
|
|
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
|
|
|
}
|