plonky2/src/gates/exponentiation.rs

281 lines
8.8 KiB
Rust
Raw Normal View History

2021-07-23 15:08:47 -07:00
use std::marker::PhantomData;
use crate::circuit_builder::CircuitBuilder;
use crate::field::extension_field::target::ExtensionTarget;
2021-07-23 20:06:00 -07:00
use crate::field::extension_field::Extendable;
2021-07-23 15:08:47 -07:00
use crate::field::field::Field;
2021-07-23 20:06:00 -07:00
use crate::gates::gate::Gate;
2021-07-23 15:08:47 -07:00
use crate::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
use crate::plonk_common::reduce_with_powers;
use crate::target::Target;
use crate::vars::{EvaluationTargets, EvaluationVars};
use crate::wire::Wire;
use crate::witness::PartialWitness;
const MAX_POWER_BITS: usize = 8;
/// A gate for inserting a value into a list at a non-deterministic location.
#[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> {
2021-07-23 20:06:00 -07:00
pub fn new(power_bits: usize) -> Self {
Self {
2021-07-23 15:08:47 -07:00
num_power_bits: power_bits,
_phantom: PhantomData,
2021-07-23 20:06:00 -07:00
}
2021-07-23 15:08:47 -07:00
}
pub fn wires_base(&self) -> usize {
0
}
pub fn wires_power(&self) -> usize {
1
}
pub fn wires_power_bit(&self, i: usize) -> usize {
debug_assert!(i < self.num_power_bits);
2 + i
}
pub fn wires_intermediate_value(&self, i: usize) -> usize {
debug_assert!(i < self.num_power_bits);
2 + self.num_power_bits + i
}
}
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> {
let base = vars.local_wires[self.wires_base()];
let power = vars.local_wires[self.wires_power()];
2021-07-27 10:19:46 -07:00
let computed_output = base.exp(power.to_canonical_u64());
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)
.map(|i| vars.local_wires[self.wires_power_bit(i)])
.collect();
let intermediate_values: Vec<_> = (0..self.num_power_bits)
.map(|i| vars.local_wires[self.wires_intermediate_value(i)])
.collect();
2021-07-23 15:08:47 -07:00
let mut constraints = Vec::new();
2021-07-23 15:11:53 -07:00
2021-07-27 10:19:46 -07:00
let power_bits_reversed = &power_bits.iter().cloned().rev().collect::<Vec<_>>()[..];
let computed_power = reduce_with_powers(power_bits_reversed, F::Extension::TWO);
2021-07-23 15:08:47 -07:00
constraints.push(power - computed_power);
2021-07-23 15:11:53 -07:00
2021-07-27 10:19:46 -07:00
let mut current_intermediate_value = F::Extension::ONE;
2021-07-23 15:08:47 -07:00
for i in 0..self.num_power_bits {
2021-07-27 10:19:46 -07:00
let computed_intermediate_value = if power_bits[i] == F::Extension::ONE {
current_intermediate_value * base
} else {
current_intermediate_value
};
2021-07-23 15:08:47 -07:00
constraints.push(computed_intermediate_value - intermediate_values[i]);
2021-07-27 10:19:46 -07:00
current_intermediate_value = computed_intermediate_value * computed_intermediate_value;
2021-07-23 15:08:47 -07:00
}
2021-07-27 10:19:46 -07:00
constraints.push(computed_output - intermediate_values[self.num_power_bits - 1]);
2021-07-23 15:08:47 -07:00
constraints
}
fn eval_unfiltered_recursively(
&self,
builder: &mut CircuitBuilder<F, D>,
vars: EvaluationTargets<D>,
) -> Vec<ExtensionTarget<D>> {
todo!()
}
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 {
self.wires_intermediate_value(self.num_power_bits - 1) + 1
}
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 {
self.num_power_bits + 2
}
}
#[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);
let mut deps = Vec::new();
deps.push(local_target(self.gate.wires_base()));
deps.push(local_target(self.gate.wires_power()));
for i in 0..self.gate.num_power_bits {
deps.push(local_target(self.gate.wires_power_bit(i)));
}
deps
}
fn run_once(&self, witness: &PartialWitness<F>) -> GeneratedValues<F> {
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;
let base = get_local_wire(self.gate.wires_base());
let power_bits = (0..num_power_bits)
.map(|i| get_local_wire(self.gate.wires_power_bit(i)))
.collect::<Vec<_>>();
2021-07-27 10:19:46 -07:00
let mut intermediate_values = Vec::new();
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 10:19:46 -07:00
if power_bits[i] == F::ONE {
current_intermediate_value *= base;
}
intermediate_values.push(current_intermediate_value);
current_intermediate_value *= current_intermediate_value;
2021-07-23 15:08:47 -07:00
}
let mut result = GeneratedValues::<F>::with_capacity(num_power_bits);
for i in 0..=num_power_bits {
let intermediate_value_wire = local_wire(self.gate.wires_intermediate_value(i));
result.set_wire(intermediate_value_wire, intermediate_values[i]);
}
result
}
}
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
2021-07-23 15:47:03 -07:00
use rand::{thread_rng, Rng};
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::Field;
2021-07-23 15:46:52 -07:00
use crate::gates::exponentiation::{ExponentiationGate, MAX_POWER_BITS};
2021-07-23 15:08:47 -07:00
use crate::gates::gate::Gate;
use crate::gates::gate_testing::test_low_degree;
use crate::proof::Hash;
2021-07-23 15:46:52 -07:00
use crate::util::log2_ceil;
2021-07-23 15:08:47 -07:00
use crate::vars::EvaluationVars;
#[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-23 15:11:42 -07:00
assert_eq!(gate.wires_base(), 0);
assert_eq!(gate.wires_power(), 1);
assert_eq!(gate.wires_power_bit(0), 2);
assert_eq!(gate.wires_power_bit(4), 6);
assert_eq!(gate.wires_intermediate_value(0), 7);
2021-07-23 15:52:37 -07:00
assert_eq!(gate.wires_intermediate_value(4), 11);
2021-07-23 15:08:47 -07:00
}
#[test]
fn low_degree() {
2021-07-23 20:06:00 -07:00
test_low_degree::<CrandallField, _, 4>(ExponentiationGate::new(5));
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-27 10:19:46 -07:00
power_bits = power_bits.iter().cloned().rev().collect::<Vec<_>>();
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-07-23 15:46:52 -07:00
let power_F = F::from_canonical_u64(power);
2021-07-23 15:47:03 -07:00
let power_bits_F: Vec<_> = power_bits
.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);
v.push(power_F);
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 10:19:46 -07:00
if power_bits[i] == 1 {
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-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),
2021-07-23 15:08:47 -07:00
public_inputs_hash: &Hash::rand(),
};
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."
);
}
}