2021-07-22 16:18:13 +02:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
|
|
2021-03-28 15:36:51 -07:00
|
|
|
use crate::circuit_builder::CircuitBuilder;
|
2021-06-25 16:49:29 +02:00
|
|
|
use crate::field::extension_field::Extendable;
|
2021-04-21 22:31:45 +02:00
|
|
|
use crate::target::Target;
|
2021-03-28 15:36:51 -07:00
|
|
|
|
2021-05-30 13:25:53 -07:00
|
|
|
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
2021-04-27 13:16:24 -07:00
|
|
|
/// Computes `-x`.
|
2021-04-02 15:29:21 -07:00
|
|
|
pub fn neg(&mut self, x: Target) -> Target {
|
|
|
|
|
let neg_one = self.neg_one();
|
|
|
|
|
self.mul(x, neg_one)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 13:16:24 -07:00
|
|
|
/// Computes `x^2`.
|
|
|
|
|
pub fn square(&mut self, x: Target) -> Target {
|
|
|
|
|
self.mul(x, x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes `x^3`.
|
|
|
|
|
pub fn cube(&mut self, x: Target) -> Target {
|
2021-07-21 17:41:22 +02:00
|
|
|
let xe = self.convert_to_ext(x);
|
|
|
|
|
self.mul_three_extension(xe, xe, xe).to_target_array()[0]
|
2021-04-27 13:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-21 11:47:18 -07:00
|
|
|
/// Computes `const_0 * multiplicand_0 * multiplicand_1 + const_1 * addend`.
|
|
|
|
|
pub fn arithmetic(
|
|
|
|
|
&mut self,
|
|
|
|
|
const_0: F,
|
|
|
|
|
multiplicand_0: Target,
|
|
|
|
|
multiplicand_1: Target,
|
|
|
|
|
const_1: F,
|
|
|
|
|
addend: Target,
|
|
|
|
|
) -> Target {
|
|
|
|
|
// See if we can determine the result without adding an `ArithmeticGate`.
|
2021-04-22 16:32:57 -07:00
|
|
|
if let Some(result) =
|
|
|
|
|
self.arithmetic_special_cases(const_0, multiplicand_0, multiplicand_1, const_1, addend)
|
|
|
|
|
{
|
2021-04-21 11:47:18 -07:00
|
|
|
return result;
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
2021-06-25 13:53:14 +02:00
|
|
|
let multiplicand_0_ext = self.convert_to_ext(multiplicand_0);
|
|
|
|
|
let multiplicand_1_ext = self.convert_to_ext(multiplicand_1);
|
|
|
|
|
let addend_ext = self.convert_to_ext(addend);
|
|
|
|
|
|
|
|
|
|
self.arithmetic_extension(
|
|
|
|
|
const_0,
|
|
|
|
|
const_1,
|
|
|
|
|
multiplicand_0_ext,
|
|
|
|
|
multiplicand_1_ext,
|
|
|
|
|
addend_ext,
|
|
|
|
|
)
|
|
|
|
|
.0[0]
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-21 11:47:18 -07:00
|
|
|
/// Checks for special cases where the value of
|
|
|
|
|
/// `const_0 * multiplicand_0 * multiplicand_1 + const_1 * addend`
|
|
|
|
|
/// can be determined without adding an `ArithmeticGate`.
|
|
|
|
|
fn arithmetic_special_cases(
|
|
|
|
|
&mut self,
|
|
|
|
|
const_0: F,
|
|
|
|
|
multiplicand_0: Target,
|
|
|
|
|
multiplicand_1: Target,
|
|
|
|
|
const_1: F,
|
|
|
|
|
addend: Target,
|
|
|
|
|
) -> Option<Target> {
|
|
|
|
|
let zero = self.zero();
|
|
|
|
|
|
|
|
|
|
let mul_0_const = self.target_as_constant(multiplicand_0);
|
|
|
|
|
let mul_1_const = self.target_as_constant(multiplicand_1);
|
|
|
|
|
let addend_const = self.target_as_constant(addend);
|
|
|
|
|
|
2021-04-22 16:32:57 -07:00
|
|
|
let first_term_zero =
|
|
|
|
|
const_0 == F::ZERO || multiplicand_0 == zero || multiplicand_1 == zero;
|
2021-04-21 11:47:18 -07:00
|
|
|
let second_term_zero = const_1 == F::ZERO || addend == zero;
|
|
|
|
|
|
|
|
|
|
// If both terms are constant, return their (constant) sum.
|
|
|
|
|
let first_term_const = if first_term_zero {
|
|
|
|
|
Some(F::ZERO)
|
|
|
|
|
} else if let (Some(x), Some(y)) = (mul_0_const, mul_1_const) {
|
|
|
|
|
Some(const_0 * x * y)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
let second_term_const = if second_term_zero {
|
|
|
|
|
Some(F::ZERO)
|
|
|
|
|
} else {
|
|
|
|
|
addend_const.map(|x| const_1 * x)
|
|
|
|
|
};
|
|
|
|
|
if let (Some(x), Some(y)) = (first_term_const, second_term_const) {
|
|
|
|
|
return Some(self.constant(x + y));
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 12:35:19 -07:00
|
|
|
if first_term_zero && const_1.is_one() {
|
|
|
|
|
return Some(addend);
|
2021-04-21 11:47:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if second_term_zero {
|
|
|
|
|
if let Some(x) = mul_0_const {
|
|
|
|
|
if (const_0 * x).is_one() {
|
|
|
|
|
return Some(multiplicand_1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(x) = mul_1_const {
|
|
|
|
|
if (const_1 * x).is_one() {
|
|
|
|
|
return Some(multiplicand_0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 13:16:24 -07:00
|
|
|
/// Computes `x * y + z`.
|
|
|
|
|
pub fn mul_add(&mut self, x: Target, y: Target, z: Target) -> Target {
|
|
|
|
|
self.arithmetic(F::ONE, x, y, F::ONE, z)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes `x * y - z`.
|
|
|
|
|
pub fn mul_sub(&mut self, x: Target, y: Target, z: Target) -> Target {
|
|
|
|
|
self.arithmetic(F::ONE, x, y, F::NEG_ONE, z)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes `x + y`.
|
2021-04-21 11:47:18 -07:00
|
|
|
pub fn add(&mut self, x: Target, y: Target) -> Target {
|
|
|
|
|
let one = self.one();
|
|
|
|
|
// x + y = 1 * x * 1 + 1 * y
|
|
|
|
|
self.arithmetic(F::ONE, x, one, F::ONE, y)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 17:41:22 +02:00
|
|
|
/// Add `n` `Target`s with `ceil(n/2) + 1` `ArithmeticExtensionGate`s.
|
2021-06-25 16:45:02 +02:00
|
|
|
// TODO: Can be made `2*D` times more efficient by using all wires of an `ArithmeticExtensionGate`.
|
2021-04-02 15:29:21 -07:00
|
|
|
pub fn add_many(&mut self, terms: &[Target]) -> Target {
|
2021-07-21 17:41:22 +02:00
|
|
|
let terms_ext = terms
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&t| self.convert_to_ext(t))
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
self.add_many_extension(&terms_ext).to_target_array()[0]
|
2021-03-28 15:36:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 13:16:24 -07:00
|
|
|
/// Computes `x - y`.
|
2021-03-28 15:36:51 -07:00
|
|
|
pub fn sub(&mut self, x: Target, y: Target) -> Target {
|
2021-04-21 11:47:18 -07:00
|
|
|
let one = self.one();
|
|
|
|
|
// x - y = 1 * x * 1 + (-1) * y
|
|
|
|
|
self.arithmetic(F::ONE, x, one, F::NEG_ONE, y)
|
2021-03-28 15:36:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 13:16:24 -07:00
|
|
|
/// Computes `x * y`.
|
2021-03-28 15:36:51 -07:00
|
|
|
pub fn mul(&mut self, x: Target, y: Target) -> Target {
|
2021-04-21 11:47:18 -07:00
|
|
|
// x * y = 1 * x * y + 0 * x
|
|
|
|
|
self.arithmetic(F::ONE, x, y, F::ZERO, x)
|
2021-03-28 15:36:51 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 17:41:22 +02:00
|
|
|
/// Multiply `n` `Target`s with `ceil(n/2) + 1` `ArithmeticExtensionGate`s.
|
2021-04-02 15:29:21 -07:00
|
|
|
pub fn mul_many(&mut self, terms: &[Target]) -> Target {
|
2021-07-21 17:41:22 +02:00
|
|
|
let terms_ext = terms
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&t| self.convert_to_ext(t))
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
self.mul_many_extension(&terms_ext).to_target_array()[0]
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-20 12:49:02 -07:00
|
|
|
/// Exponentiate `base` to the power of `2^power_log`.
|
|
|
|
|
// TODO: Test
|
|
|
|
|
pub fn exp_power_of_2(&mut self, mut base: Target, power_log: usize) -> Target {
|
|
|
|
|
for _ in 0..power_log {
|
|
|
|
|
base = self.square(base);
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
2021-07-20 12:49:02 -07:00
|
|
|
base
|
2021-04-02 15:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 17:39:45 +02:00
|
|
|
// TODO: Optimize this, maybe with a new gate.
|
2021-06-25 16:27:20 +02:00
|
|
|
// TODO: Test
|
2021-07-20 15:25:03 +02:00
|
|
|
/// Exponentiate `base` to the power of `exponent`, given by its little-endian bits.
|
2021-07-23 08:53:00 +02:00
|
|
|
pub fn exp_from_bits(
|
|
|
|
|
&mut self,
|
|
|
|
|
base: Target,
|
|
|
|
|
exponent_bits: impl Iterator<Item = impl Borrow<Target>>,
|
|
|
|
|
) -> Target {
|
2021-06-09 17:39:45 +02:00
|
|
|
let mut current = base;
|
2021-07-22 11:58:29 +02:00
|
|
|
let one = self.one();
|
|
|
|
|
let mut product = one;
|
2021-06-09 17:39:45 +02:00
|
|
|
|
2021-07-23 08:53:00 +02:00
|
|
|
for bit in exponent_bits {
|
|
|
|
|
let multiplicand = self.select(*bit.borrow(), current, one);
|
2021-07-22 11:58:29 +02:00
|
|
|
product = self.mul(product, multiplicand);
|
2021-06-09 17:39:45 +02:00
|
|
|
current = self.mul(current, current);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
product
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 15:25:03 +02:00
|
|
|
// TODO: Optimize this, maybe with a new gate.
|
|
|
|
|
// TODO: Test
|
2021-07-21 20:38:23 +02:00
|
|
|
/// Exponentiate `base` to the power of `2^bit_length-1-exponent`, given by its little-endian bits.
|
2021-07-22 16:18:13 +02:00
|
|
|
pub fn exp_from_complement_bits(
|
|
|
|
|
&mut self,
|
|
|
|
|
base: Target,
|
2021-07-23 08:15:13 +02:00
|
|
|
exponent_bits: impl Iterator<Item = impl Borrow<Target>>,
|
2021-07-22 16:18:13 +02:00
|
|
|
) -> Target {
|
2021-07-20 15:25:03 +02:00
|
|
|
let mut current = base;
|
2021-07-22 11:58:29 +02:00
|
|
|
let one = self.one();
|
|
|
|
|
let mut product = one;
|
2021-07-20 15:25:03 +02:00
|
|
|
|
2021-07-23 08:21:55 +02:00
|
|
|
for bit in exponent_bits {
|
2021-07-23 08:16:23 +02:00
|
|
|
let multiplicand = self.select(*bit.borrow(), one, current);
|
2021-07-22 11:58:29 +02:00
|
|
|
product = self.mul(product, multiplicand);
|
2021-07-20 15:25:03 +02:00
|
|
|
current = self.mul(current, current);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
product
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Optimize this, maybe with a new gate.
|
|
|
|
|
// TODO: Test
|
|
|
|
|
/// Exponentiate `base` to the power of `exponent`, where `exponent < 2^num_bits`.
|
|
|
|
|
pub fn exp(&mut self, base: Target, exponent: Target, num_bits: usize) -> Target {
|
|
|
|
|
let exponent_bits = self.split_le(exponent, num_bits);
|
2021-07-23 08:53:00 +02:00
|
|
|
self.exp_from_bits(base, exponent_bits.iter())
|
2021-07-20 15:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:27:20 +02:00
|
|
|
/// Exponentiate `base` to the power of a known `exponent`.
|
|
|
|
|
// TODO: Test
|
|
|
|
|
pub fn exp_u64(&mut self, base: Target, exponent: u64) -> Target {
|
2021-06-25 16:53:11 +02:00
|
|
|
let base_ext = self.convert_to_ext(base);
|
|
|
|
|
self.exp_u64_extension(base_ext, exponent).0[0]
|
2021-06-25 16:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-18 23:05:57 -07:00
|
|
|
/// Computes `x / y`. Results in an unsatisfiable instance if `y = 0`.
|
|
|
|
|
pub fn div(&mut self, x: Target, y: Target) -> Target {
|
|
|
|
|
let y_inv = self.inverse(y);
|
|
|
|
|
self.mul(x, y_inv)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 11:47:18 -07:00
|
|
|
/// Computes `q = x / y` by witnessing `q` and requiring that `q * y = x`. This can be unsafe in
|
|
|
|
|
/// some cases, as it allows `0 / 0 = <anything>`.
|
|
|
|
|
pub fn div_unsafe(&mut self, x: Target, y: Target) -> Target {
|
|
|
|
|
// Check for special cases where we can determine the result without an `ArithmeticGate`.
|
|
|
|
|
let zero = self.zero();
|
|
|
|
|
let one = self.one();
|
|
|
|
|
if x == zero {
|
|
|
|
|
return zero;
|
|
|
|
|
}
|
|
|
|
|
if y == one {
|
|
|
|
|
return x;
|
|
|
|
|
}
|
2021-04-22 16:32:57 -07:00
|
|
|
if let (Some(x_const), Some(y_const)) =
|
|
|
|
|
(self.target_as_constant(x), self.target_as_constant(y))
|
|
|
|
|
{
|
2021-04-21 11:47:18 -07:00
|
|
|
return self.constant(x_const / y_const);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 13:53:14 +02:00
|
|
|
let x_ext = self.convert_to_ext(x);
|
|
|
|
|
let y_ext = self.convert_to_ext(y);
|
|
|
|
|
self.div_unsafe_extension(x_ext, y_ext).0[0]
|
2021-04-21 11:47:18 -07:00
|
|
|
}
|
2021-07-18 23:05:57 -07:00
|
|
|
|
|
|
|
|
/// Computes `1 / x`. Results in an unsatisfiable instance if `x = 0`.
|
|
|
|
|
pub fn inverse(&mut self, x: Target) -> Target {
|
|
|
|
|
let x_ext = self.convert_to_ext(x);
|
|
|
|
|
self.inverse_extension(x_ext).0[0]
|
|
|
|
|
}
|
2021-06-07 17:55:27 +02:00
|
|
|
}
|