plonky2/src/gates/exponentiation.rs

371 lines
12 KiB
Rust
Raw Normal View History

2021-07-23 15:08:47 -07:00
use std::marker::PhantomData;
use crate::field::extension_field::target::ExtensionTarget;
2021-07-23 20:06:00 -07:00
use crate::field::extension_field::Extendable;
use crate::field::field_types::Field;
2021-07-23 20:06:00 -07:00
use crate::gates::gate::Gate;
2021-08-20 09:55:49 +02:00
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
use crate::iop::target::Target;
use crate::iop::wire::Wire;
2021-08-20 11:13:40 +02:00
use crate::iop::witness::{PartitionWitness, Witness};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase};
2021-07-23 15:08:47 -07:00
2021-07-28 09:20:20 -07:00
/// A gate for raising a value to a power.
2021-07-23 15:08:47 -07:00
#[derive(Clone, Debug)]
pub(crate) struct ExponentiationGate<F: Extendable<D>, const D: usize> {
pub num_power_bits: usize,
pub _phantom: PhantomData<F>,
}
impl<F: Extendable<D>, const D: usize> ExponentiationGate<F, D> {
pub fn new(config: CircuitConfig) -> Self {
let num_power_bits = Self::max_power_bits(config.num_wires, config.num_routed_wires);
2021-07-23 20:06:00 -07:00
Self {
2021-07-27 12:34:47 -07:00
num_power_bits,
2021-07-23 15:08:47 -07:00
_phantom: PhantomData,
2021-07-23 20:06:00 -07:00
}
2021-07-23 15:08:47 -07:00
}
2021-07-29 15:31:04 -07:00
fn max_power_bits(num_wires: usize, num_routed_wires: usize) -> usize {
2021-08-02 13:12:50 +02:00
// 2 wires are reserved for the base and output.
let max_for_routed_wires = num_routed_wires - 2;
let max_for_wires = (num_wires - 2) / 2;
2021-07-28 10:09:35 -07:00
max_for_routed_wires.min(max_for_wires)
2021-07-28 09:20:20 -07:00
}
pub fn wire_base(&self) -> usize {
2021-07-23 15:08:47 -07:00
0
}
2021-07-27 12:48:52 -07:00
/// The `i`th bit of the exponent, in little-endian order.
2021-07-28 09:20:20 -07:00
pub fn wire_power_bit(&self, i: usize) -> usize {
2021-07-23 15:08:47 -07:00
debug_assert!(i < self.num_power_bits);
2021-07-29 12:58:44 -07:00
1 + i
2021-07-23 15:08:47 -07:00
}
2021-07-28 09:20:20 -07:00
pub fn wire_output(&self) -> usize {
2021-07-29 12:58:44 -07:00
1 + self.num_power_bits
2021-07-27 13:09:15 -07:00
}
2021-07-28 09:20:20 -07:00
pub fn wire_intermediate_value(&self, i: usize) -> usize {
2021-07-23 15:08:47 -07:00
debug_assert!(i < self.num_power_bits);
2021-07-29 12:58:44 -07:00
2 + self.num_power_bits + i
2021-07-23 15:08:47 -07:00
}
}
impl<F: Extendable<D>, const D: usize> Gate<F, D> for ExponentiationGate<F, D> {
fn id(&self) -> String {
format!("{:?}<D={}>", self, D)
}
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
2021-07-28 09:20:20 -07:00
let base = vars.local_wires[self.wire_base()];
2021-07-23 15:08:47 -07:00
2021-07-23 15:11:53 -07:00
let power_bits: Vec<_> = (0..self.num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| vars.local_wires[self.wire_power_bit(i)])
2021-07-23 15:11:53 -07:00
.collect();
let intermediate_values: Vec<_> = (0..self.num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| vars.local_wires[self.wire_intermediate_value(i)])
2021-07-23 15:11:53 -07:00
.collect();
2021-07-23 15:08:47 -07:00
2021-07-28 09:20:20 -07:00
let output = vars.local_wires[self.wire_output()];
2021-07-27 13:09:15 -07:00
2021-08-12 13:32:49 -07:00
let mut constraints = Vec::with_capacity(self.num_constraints());
2021-07-23 15:11:53 -07:00
2021-07-23 15:08:47 -07:00
for i in 0..self.num_power_bits {
2021-07-27 12:44:10 -07:00
let prev_intermediate_value = if i == 0 {
2021-07-27 11:04:16 -07:00
F::Extension::ONE
} else {
2021-07-27 12:44:10 -07:00
intermediate_values[i - 1].square()
2021-07-27 11:04:16 -07:00
};
2021-07-27 12:44:10 -07:00
// power_bits is in LE order, but we accumulate in BE order.
let cur_bit = power_bits[self.num_power_bits - i - 1];
2021-07-27 10:46:10 -07:00
let not_cur_bit = F::Extension::ONE - cur_bit;
let computed_intermediate_value =
2021-07-27 12:44:10 -07:00
prev_intermediate_value * (cur_bit * base + not_cur_bit);
2021-07-23 15:08:47 -07:00
constraints.push(computed_intermediate_value - intermediate_values[i]);
}
2021-07-27 13:09:15 -07:00
constraints.push(output - intermediate_values[self.num_power_bits - 1]);
2021-07-23 15:08:47 -07:00
constraints
}
2021-07-27 13:18:42 -07:00
fn eval_unfiltered_base(&self, vars: EvaluationVarsBase<F>) -> Vec<F> {
2021-07-28 09:20:20 -07:00
let base = vars.local_wires[self.wire_base()];
2021-07-27 13:18:42 -07:00
let power_bits: Vec<_> = (0..self.num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| vars.local_wires[self.wire_power_bit(i)])
2021-07-27 13:18:42 -07:00
.collect();
let intermediate_values: Vec<_> = (0..self.num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| vars.local_wires[self.wire_intermediate_value(i)])
2021-07-27 13:18:42 -07:00
.collect();
2021-07-28 09:20:20 -07:00
let output = vars.local_wires[self.wire_output()];
2021-07-27 13:18:42 -07:00
2021-08-12 13:32:49 -07:00
let mut constraints = Vec::with_capacity(self.num_constraints());
2021-07-27 13:18:42 -07:00
for i in 0..self.num_power_bits {
let prev_intermediate_value = if i == 0 {
F::ONE
} else {
intermediate_values[i - 1].square()
};
// power_bits is in LE order, but we accumulate in BE order.
let cur_bit = power_bits[self.num_power_bits - i - 1];
let not_cur_bit = F::ONE - cur_bit;
let computed_intermediate_value =
prev_intermediate_value * (cur_bit * base + not_cur_bit);
constraints.push(computed_intermediate_value - intermediate_values[i]);
}
constraints.push(output - intermediate_values[self.num_power_bits - 1]);
constraints
}
2021-07-23 15:08:47 -07:00
fn eval_unfiltered_recursively(
&self,
builder: &mut CircuitBuilder<F, D>,
vars: EvaluationTargets<D>,
) -> Vec<ExtensionTarget<D>> {
2021-07-28 09:20:20 -07:00
let base = vars.local_wires[self.wire_base()];
2021-07-27 13:29:57 -07:00
let power_bits: Vec<_> = (0..self.num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| vars.local_wires[self.wire_power_bit(i)])
2021-07-27 13:29:57 -07:00
.collect();
let intermediate_values: Vec<_> = (0..self.num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| vars.local_wires[self.wire_intermediate_value(i)])
2021-07-27 13:29:57 -07:00
.collect();
2021-07-28 09:20:20 -07:00
let output = vars.local_wires[self.wire_output()];
2021-07-27 13:29:57 -07:00
2021-08-12 13:32:49 -07:00
let mut constraints = Vec::with_capacity(self.num_constraints());
2021-07-27 13:29:57 -07:00
2021-08-02 13:12:50 +02:00
let one = builder.one_extension();
2021-07-27 13:29:57 -07:00
for i in 0..self.num_power_bits {
let prev_intermediate_value = if i == 0 {
one
} else {
builder.square_extension(intermediate_values[i - 1])
};
// power_bits is in LE order, but we accumulate in BE order.
let cur_bit = power_bits[self.num_power_bits - i - 1];
let mul_by = builder.select_ext_generalized(cur_bit, base, one);
2021-07-27 15:58:19 -07:00
let intermediate_value_diff =
builder.mul_sub_extension(prev_intermediate_value, mul_by, intermediate_values[i]);
2021-07-27 13:29:57 -07:00
constraints.push(intermediate_value_diff);
}
2021-07-27 15:58:19 -07:00
let output_diff =
builder.sub_extension(output, intermediate_values[self.num_power_bits - 1]);
2021-07-27 13:29:57 -07:00
constraints.push(output_diff);
constraints
2021-07-23 15:08:47 -07:00
}
fn generators(
&self,
gate_index: usize,
_local_constants: &[F],
) -> Vec<Box<dyn WitnessGenerator<F>>> {
let gen = ExponentiationGenerator::<F, D> {
gate_index,
gate: self.clone(),
};
vec![Box::new(gen)]
}
fn num_wires(&self) -> usize {
2021-07-28 09:20:20 -07:00
self.wire_intermediate_value(self.num_power_bits - 1) + 1
2021-07-23 15:08:47 -07:00
}
fn num_constants(&self) -> usize {
0
}
fn degree(&self) -> usize {
2021-07-23 15:56:14 -07:00
4
2021-07-23 15:08:47 -07:00
}
fn num_constraints(&self) -> usize {
2021-07-29 12:58:44 -07:00
self.num_power_bits + 1
2021-07-23 15:08:47 -07:00
}
}
#[derive(Debug)]
struct ExponentiationGenerator<F: Extendable<D>, const D: usize> {
gate_index: usize,
gate: ExponentiationGate<F, D>,
}
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ExponentiationGenerator<F, D> {
fn dependencies(&self) -> Vec<Target> {
let local_target = |input| Target::wire(self.gate_index, input);
2021-07-29 14:17:45 -07:00
let mut deps = Vec::with_capacity(self.gate.num_power_bits + 1);
2021-07-28 09:20:20 -07:00
deps.push(local_target(self.gate.wire_base()));
2021-07-23 15:08:47 -07:00
for i in 0..self.gate.num_power_bits {
2021-07-28 09:20:20 -07:00
deps.push(local_target(self.gate.wire_power_bit(i)));
2021-07-23 15:08:47 -07:00
}
deps
}
2021-08-20 09:55:49 +02:00
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
2021-07-23 15:08:47 -07:00
let local_wire = |input| Wire {
gate: self.gate_index,
input,
};
let get_local_wire = |input| witness.get_wire(local_wire(input));
let num_power_bits = self.gate.num_power_bits;
2021-07-28 09:20:20 -07:00
let base = get_local_wire(self.gate.wire_base());
2021-07-27 13:09:15 -07:00
2021-07-23 15:08:47 -07:00
let power_bits = (0..num_power_bits)
2021-07-28 09:20:20 -07:00
.map(|i| get_local_wire(self.gate.wire_power_bit(i)))
2021-07-23 15:08:47 -07:00
.collect::<Vec<_>>();
2021-07-27 10:19:46 -07:00
let mut intermediate_values = Vec::new();
2021-07-27 13:09:15 -07:00
2021-07-27 10:19:46 -07:00
let mut current_intermediate_value = F::ONE;
2021-07-23 15:08:47 -07:00
for i in 0..num_power_bits {
2021-07-27 15:58:19 -07:00
if power_bits[num_power_bits - i - 1] == F::ONE {
2021-07-27 10:19:46 -07:00
current_intermediate_value *= base;
}
intermediate_values.push(current_intermediate_value);
current_intermediate_value *= current_intermediate_value;
2021-07-23 15:08:47 -07:00
}
2021-07-27 15:58:19 -07:00
for i in 0..num_power_bits {
2021-07-28 09:20:20 -07:00
let intermediate_value_wire = local_wire(self.gate.wire_intermediate_value(i));
out_buffer.set_wire(intermediate_value_wire, intermediate_values[i]);
2021-07-23 15:08:47 -07:00
}
2021-07-28 09:20:20 -07:00
let output_wire = local_wire(self.gate.wire_output());
out_buffer.set_wire(output_wire, intermediate_values[num_power_bits - 1]);
2021-07-23 15:08:47 -07:00
}
}
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
2021-08-02 14:17:42 +02:00
use anyhow::Result;
2021-07-29 11:45:58 -07:00
use rand::Rng;
2021-07-23 15:47:03 -07:00
2021-07-23 15:08:47 -07:00
use crate::field::crandall_field::CrandallField;
use crate::field::extension_field::quartic::QuarticCrandallField;
use crate::field::field_types::Field;
2021-07-28 09:20:20 -07:00
use crate::gates::exponentiation::ExponentiationGate;
2021-07-23 15:08:47 -07:00
use crate::gates::gate::Gate;
2021-08-02 14:17:42 +02:00
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
use crate::hash::hash_types::HashOut;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::vars::EvaluationVars;
2021-07-23 15:46:52 -07:00
use crate::util::log2_ceil;
2021-07-23 15:08:47 -07:00
2021-07-28 09:20:20 -07:00
const MAX_POWER_BITS: usize = 17;
2021-07-23 15:08:47 -07:00
#[test]
fn wire_indices() {
let gate = ExponentiationGate::<CrandallField, 4> {
2021-07-23 15:11:42 -07:00
num_power_bits: 5,
2021-07-23 15:08:47 -07:00
_phantom: PhantomData,
};
2021-07-28 09:20:20 -07:00
assert_eq!(gate.wire_base(), 0);
2021-07-29 12:58:44 -07:00
assert_eq!(gate.wire_power_bit(0), 1);
assert_eq!(gate.wire_power_bit(4), 5);
assert_eq!(gate.wire_output(), 6);
assert_eq!(gate.wire_intermediate_value(0), 7);
assert_eq!(gate.wire_intermediate_value(4), 11);
2021-07-23 15:08:47 -07:00
}
#[test]
fn low_degree() {
let config = CircuitConfig {
num_wires: 120,
num_routed_wires: 30,
2021-07-29 15:31:04 -07:00
..CircuitConfig::large_config()
};
test_low_degree::<CrandallField, _, 4>(ExponentiationGate::new(config));
2021-07-23 15:08:47 -07:00
}
2021-08-02 14:17:42 +02:00
#[test]
fn eval_fns() -> Result<()> {
test_eval_fns::<CrandallField, _, 4>(ExponentiationGate::new(CircuitConfig::large_config()))
}
2021-07-23 15:08:47 -07:00
#[test]
fn test_gate_constraint() {
type F = CrandallField;
type FF = QuarticCrandallField;
const D: usize = 4;
2021-07-23 15:11:42 -07:00
/// Returns the local wires for an exponentiation gate given the base, power, and power bit
/// values.
2021-07-23 15:46:52 -07:00
fn get_wires(base: F, power: u64) -> Vec<FF> {
let mut power_bits = Vec::new();
let mut cur_power = power;
while cur_power > 0 {
power_bits.push(cur_power % 2);
cur_power /= 2;
2021-07-23 15:08:47 -07:00
}
2021-07-23 15:46:52 -07:00
let num_power_bits = power_bits.len();
2021-07-23 15:47:03 -07:00
2021-08-14 08:47:03 -07:00
let power_bits_f: Vec<_> = power_bits
2021-07-23 15:47:03 -07:00
.iter()
.map(|b| F::from_canonical_u64(*b))
.collect();
2021-07-23 15:08:47 -07:00
2021-07-23 15:46:52 -07:00
let mut v = Vec::new();
v.push(base);
2021-08-14 08:47:03 -07:00
v.extend(power_bits_f.clone());
2021-07-23 15:47:03 -07:00
2021-07-23 15:46:52 -07:00
let mut intermediate_values = Vec::new();
2021-07-27 10:19:46 -07:00
let mut current_intermediate_value = F::ONE;
2021-07-23 15:46:52 -07:00
for i in 0..num_power_bits {
2021-07-27 15:58:19 -07:00
if power_bits[num_power_bits - i - 1] == 1 {
2021-07-27 10:19:46 -07:00
current_intermediate_value *= base;
}
2021-07-23 15:46:52 -07:00
intermediate_values.push(current_intermediate_value);
2021-07-27 10:19:46 -07:00
current_intermediate_value *= current_intermediate_value;
2021-07-23 15:08:47 -07:00
}
2021-07-27 13:09:15 -07:00
let output_value = intermediate_values[num_power_bits - 1];
v.push(output_value);
2021-07-23 15:46:52 -07:00
v.extend(intermediate_values);
2021-07-23 15:08:47 -07:00
v.iter().map(|&x| x.into()).collect::<Vec<_>>()
}
2021-07-23 15:46:52 -07:00
let mut rng = rand::thread_rng();
2021-07-27 10:19:46 -07:00
let base = F::TWO;
2021-07-23 15:46:52 -07:00
let power = rng.gen::<usize>() % (1 << MAX_POWER_BITS);
2021-07-27 10:19:46 -07:00
let num_power_bits = log2_ceil(power + 1);
2021-07-23 15:08:47 -07:00
let gate = ExponentiationGate::<F, D> {
2021-07-23 15:46:52 -07:00
num_power_bits,
2021-07-23 15:08:47 -07:00
_phantom: PhantomData,
};
2021-07-23 15:56:25 -07:00
2021-07-27 10:19:46 -07:00
let vars = EvaluationVars {
2021-07-23 15:08:47 -07:00
local_constants: &[],
2021-07-23 15:46:52 -07:00
local_wires: &get_wires(base, power as u64),
public_inputs_hash: &HashOut::rand(),
2021-07-23 15:08:47 -07:00
};
assert!(
2021-07-27 10:19:46 -07:00
gate.eval_unfiltered(vars).iter().all(|x| x.is_zero()),
2021-07-23 15:08:47 -07:00
"Gate constraints are not satisfied."
);
}
}