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-07-29 22:00:29 -07:00
|
|
|
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
|
|
|
|
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-07-21 17:20:08 +02:00
|
|
|
pub fn wires_first_multiplicand_0() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
0..D
|
|
|
|
|
}
|
2021-07-21 17:20:08 +02:00
|
|
|
pub fn wires_first_multiplicand_1() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
D..2 * D
|
|
|
|
|
}
|
2021-07-21 17:20:08 +02:00
|
|
|
pub fn wires_first_addend() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
2 * D..3 * D
|
|
|
|
|
}
|
2021-08-13 10:40:31 +02:00
|
|
|
pub fn wires_first_output() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
3 * D..4 * D
|
|
|
|
|
}
|
2021-08-13 10:40:31 +02:00
|
|
|
pub fn wires_second_multiplicand_0() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
4 * D..5 * D
|
|
|
|
|
}
|
2021-08-13 10:40:31 +02:00
|
|
|
pub fn wires_second_multiplicand_1() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
5 * D..6 * D
|
|
|
|
|
}
|
2021-08-13 10:40:31 +02:00
|
|
|
pub fn wires_second_addend() -> Range<usize> {
|
2021-06-25 16:31:10 +02:00
|
|
|
6 * D..7 * D
|
|
|
|
|
}
|
2021-07-21 17:20:08 +02:00
|
|
|
pub fn wires_second_output() -> Range<usize> {
|
|
|
|
|
7 * D..8 * D
|
|
|
|
|
}
|
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-07-21 17:20:08 +02:00
|
|
|
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());
|
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];
|
|
|
|
|
|
|
|
|
|
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,
|
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-07-21 17:20:08 +02:00
|
|
|
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);
|
2021-06-25 16:31:10 +02:00
|
|
|
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>>> {
|
2021-06-29 12:33:11 -07:00
|
|
|
let gen0 = ArithmeticExtensionGenerator0 {
|
2021-04-02 15:29:21 -07:00
|
|
|
gate_index,
|
|
|
|
|
const_0: local_constants[0],
|
|
|
|
|
const_1: local_constants[1],
|
|
|
|
|
};
|
2021-06-29 12:33:11 -07:00
|
|
|
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 {
|
2021-07-21 17:20:08 +02:00
|
|
|
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 {
|
2021-06-25 16:31:10 +02:00
|
|
|
2 * D
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 12:33:11 -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,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 12:33:11 -07:00
|
|
|
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> {
|
2021-07-21 17:20:08 +02:00
|
|
|
ArithmeticExtensionGate::<D>::wires_first_multiplicand_0()
|
|
|
|
|
.chain(ArithmeticExtensionGate::<D>::wires_first_multiplicand_1())
|
|
|
|
|
.chain(ArithmeticExtensionGate::<D>::wires_first_addend())
|
2021-06-25 16:31:10 +02:00
|
|
|
.map(|i| Target::wire(self.gate_index, i))
|
|
|
|
|
.collect()
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-02 10:55:10 -07:00
|
|
|
fn run_once(&self, witness: &PartialWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
2021-06-25 16:31:10 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2021-06-25 16:31:10 +02:00
|
|
|
let multiplicand_0 =
|
2021-07-21 17:20:08 +02:00
|
|
|
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());
|
2021-06-25 16:31:10 +02:00
|
|
|
|
2021-07-21 17:20:08 +02:00
|
|
|
let output_target = ExtensionTarget::from_range(
|
2021-06-25 16:31:10 +02:00
|
|
|
self.gate_index,
|
2021-07-21 17:20:08 +02:00
|
|
|
ArithmeticExtensionGate::<D>::wires_first_output(),
|
2021-06-25 16:31:10 +02:00
|
|
|
);
|
2021-06-29 12:33:11 -07:00
|
|
|
|
2021-07-21 17:20:08 +02:00
|
|
|
let computed_output =
|
|
|
|
|
multiplicand_0 * multiplicand_1 * self.const_0.into() + addend * self.const_1.into();
|
2021-06-29 12:33:11 -07:00
|
|
|
|
2021-08-02 10:55:10 -07:00
|
|
|
out_buffer.set_extension_target(output_target, computed_output)
|
2021-06-29 12:33:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator1<F, D> {
|
|
|
|
|
fn dependencies(&self) -> Vec<Target> {
|
2021-07-21 17:20:08 +02:00
|
|
|
ArithmeticExtensionGate::<D>::wires_second_multiplicand_0()
|
|
|
|
|
.chain(ArithmeticExtensionGate::<D>::wires_second_multiplicand_1())
|
|
|
|
|
.chain(ArithmeticExtensionGate::<D>::wires_second_addend())
|
2021-06-29 12:33:11 -07:00
|
|
|
.map(|i| Target::wire(self.gate_index, i))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 10:55:10 -07:00
|
|
|
fn run_once(&self, witness: &PartialWitness<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-07-21 17:20:08 +02:00
|
|
|
let multiplicand_0 =
|
|
|
|
|
extract_extension(ArithmeticExtensionGate::<D>::wires_second_multiplicand_0());
|
2021-06-29 12:33:11 -07:00
|
|
|
let multiplicand_1 =
|
2021-07-21 17:20:08 +02:00
|
|
|
extract_extension(ArithmeticExtensionGate::<D>::wires_second_multiplicand_1());
|
|
|
|
|
let addend = extract_extension(ArithmeticExtensionGate::<D>::wires_second_addend());
|
2021-06-29 12:33:11 -07:00
|
|
|
|
2021-07-21 17:20:08 +02:00
|
|
|
let output_target = ExtensionTarget::from_range(
|
2021-06-25 16:31:10 +02:00
|
|
|
self.gate_index,
|
2021-07-21 17:20:08 +02:00
|
|
|
ArithmeticExtensionGate::<D>::wires_second_output(),
|
2021-06-25 16:31:10 +02:00
|
|
|
);
|
|
|
|
|
|
2021-07-21 17:20:08 +02:00
|
|
|
let computed_output =
|
|
|
|
|
multiplicand_0 * multiplicand_1 * self.const_0.into() + addend * self.const_1.into();
|
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
|
|
|
}
|