2021-05-19 12:06:42 -07:00
|
|
|
use std::convert::TryInto;
|
2021-05-17 09:06:17 -07:00
|
|
|
use std::marker::PhantomData;
|
2021-05-19 12:06:42 -07:00
|
|
|
use std::ops::Range;
|
2021-05-17 09:06:17 -07:00
|
|
|
|
|
|
|
|
use crate::circuit_builder::CircuitBuilder;
|
2021-05-19 12:06:42 -07:00
|
|
|
use crate::field::extension_field::{Extendable, FieldExtension};
|
|
|
|
|
use crate::field::field::Field;
|
2021-05-17 09:06:17 -07:00
|
|
|
use crate::gates::gate::{Gate, GateRef};
|
|
|
|
|
use crate::generator::{SimpleGenerator, WitnessGenerator};
|
|
|
|
|
use crate::target::Target;
|
|
|
|
|
use crate::vars::{EvaluationTargets, EvaluationVars};
|
|
|
|
|
use crate::witness::PartialWitness;
|
|
|
|
|
|
|
|
|
|
/// The size of the field extension, in terms of number of base elements per extension element.
|
|
|
|
|
const EXT_SIZE: usize = 4;
|
|
|
|
|
|
|
|
|
|
/// Evaluates the interpolant of some given elements from a quartic field extension.
|
|
|
|
|
///
|
|
|
|
|
/// In particular, this gate takes as inputs `num_points` points, `num_points` values, and the point
|
|
|
|
|
/// to evaluate the interpolant at. It computes the interpolant and outputs its evaluation at the
|
|
|
|
|
/// given point.
|
|
|
|
|
#[derive(Debug)]
|
2021-05-19 12:06:42 -07:00
|
|
|
pub(crate) struct QuarticInterpolationGate<F: Field + Extendable<D>, const D: usize> {
|
2021-05-17 09:06:17 -07:00
|
|
|
num_points: usize,
|
2021-05-19 12:06:42 -07:00
|
|
|
_phantom: PhantomData<F>,
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
impl<F: Field + Extendable<D>, const D: usize> QuarticInterpolationGate<F, D> {
|
|
|
|
|
pub fn new(num_points: usize) -> GateRef<F> {
|
2021-05-17 09:06:17 -07:00
|
|
|
let gate = Self {
|
|
|
|
|
num_points,
|
|
|
|
|
_phantom: PhantomData,
|
|
|
|
|
};
|
|
|
|
|
GateRef::new(gate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_points(&self) -> usize {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wire indices of the `i`th interpolant point.
|
|
|
|
|
pub fn wire_point(&self, i: usize) -> usize {
|
|
|
|
|
debug_assert!(i < self.num_points);
|
|
|
|
|
self.start_points() + i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_values(&self) -> usize {
|
|
|
|
|
self.start_points() + self.num_points
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wire indices of the `i`th interpolant value.
|
2021-05-19 12:06:42 -07:00
|
|
|
pub fn wires_value(&self, i: usize) -> Range<usize> {
|
2021-05-17 09:06:17 -07:00
|
|
|
debug_assert!(i < self.num_points);
|
2021-05-19 12:06:42 -07:00
|
|
|
let start = self.start_values() + i * EXT_SIZE;
|
|
|
|
|
start..start + EXT_SIZE
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
fn start_evaluation_point(&self) -> usize {
|
2021-05-17 09:06:17 -07:00
|
|
|
self.start_values() + self.num_points * EXT_SIZE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wire indices of the point to evaluate the interpolant at.
|
2021-05-19 12:06:42 -07:00
|
|
|
pub fn wires_evaluation_point(&self) -> Range<usize> {
|
|
|
|
|
let start = self.start_evaluation_point();
|
|
|
|
|
start..start + EXT_SIZE
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
fn start_evaluation_value(&self) -> usize {
|
|
|
|
|
self.start_evaluation_point() + EXT_SIZE
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wire indices of the interpolated value.
|
2021-05-19 12:06:42 -07:00
|
|
|
pub fn wires_evaluation_value(&self) -> Range<usize> {
|
|
|
|
|
let start = self.start_evaluation_value();
|
|
|
|
|
start..start + EXT_SIZE
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_coeffs(&self) -> usize {
|
2021-05-19 12:06:42 -07:00
|
|
|
self.start_evaluation_value() + EXT_SIZE
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wire indices of the interpolant's `i`th coefficient.
|
2021-05-19 12:06:42 -07:00
|
|
|
pub fn wires_coeff(&self, i: usize) -> Range<usize> {
|
2021-05-17 09:06:17 -07:00
|
|
|
debug_assert!(i < self.num_points);
|
2021-05-19 12:06:42 -07:00
|
|
|
let start = self.start_coeffs() + i * EXT_SIZE;
|
|
|
|
|
start..start + EXT_SIZE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn end(&self) -> usize {
|
|
|
|
|
self.start_coeffs() + self.num_points * EXT_SIZE
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
impl<F: Field + Extendable<D>, const D: usize> Gate<F> for QuarticInterpolationGate<F, D> {
|
2021-05-17 09:06:17 -07:00
|
|
|
fn id(&self) -> String {
|
2021-05-19 12:06:42 -07:00
|
|
|
let qfe_name = std::any::type_name::<F::Extension>();
|
2021-05-17 09:06:17 -07:00
|
|
|
format!("{} {:?}", qfe_name, self)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
fn eval_unfiltered(&self, vars: EvaluationVars<F>) -> Vec<F> {
|
|
|
|
|
let mut constraints = Vec::with_capacity(self.num_constraints());
|
|
|
|
|
|
|
|
|
|
let x_eval = F::Extension::from_basefield_array(
|
|
|
|
|
vars.local_wires[self.wires_evaluation_point()].try_into().unwrap());
|
|
|
|
|
let x_eval_powers = x_eval.powers().take(self.num_points);
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
|
|
constraints
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eval_unfiltered_recursively(
|
|
|
|
|
&self,
|
2021-05-19 12:06:42 -07:00
|
|
|
builder: &mut CircuitBuilder<F>,
|
2021-05-17 09:06:17 -07:00
|
|
|
vars: EvaluationTargets,
|
|
|
|
|
) -> Vec<Target> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generators(
|
|
|
|
|
&self,
|
|
|
|
|
gate_index: usize,
|
2021-05-19 12:06:42 -07:00
|
|
|
_local_constants: &[F],
|
|
|
|
|
) -> Vec<Box<dyn WitnessGenerator<F>>> {
|
|
|
|
|
let gen = QuarticInterpolationGenerator::<F, D> {
|
2021-05-17 11:47:33 -07:00
|
|
|
gate_index,
|
|
|
|
|
num_points: self.num_points,
|
|
|
|
|
_phantom: PhantomData,
|
|
|
|
|
};
|
|
|
|
|
vec![Box::new(gen)]
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_wires(&self) -> usize {
|
2021-05-19 12:06:42 -07:00
|
|
|
self.end()
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_constants(&self) -> usize {
|
2021-05-17 11:47:33 -07:00
|
|
|
0
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn degree(&self) -> usize {
|
2021-05-17 11:47:33 -07:00
|
|
|
self.num_points - 1
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn num_constraints(&self) -> usize {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
struct QuarticInterpolationGenerator<F: Field + Extendable<D>, const D: usize> {
|
2021-05-17 11:47:33 -07:00
|
|
|
gate_index: usize,
|
|
|
|
|
num_points: usize,
|
2021-05-19 12:06:42 -07:00
|
|
|
_phantom: PhantomData<F>,
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
impl<F: Field + Extendable<D>, const D: usize> SimpleGenerator<F>
|
|
|
|
|
for QuarticInterpolationGenerator<F, D>
|
2021-05-17 09:06:17 -07:00
|
|
|
{
|
|
|
|
|
fn dependencies(&self) -> Vec<Target> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
fn run_once(&self, witness: &PartialWitness<F>) -> PartialWitness<F> {
|
2021-05-17 09:06:17 -07:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
2021-05-19 12:06:42 -07:00
|
|
|
use crate::field::crandall_field::CrandallField;
|
2021-05-17 11:47:33 -07:00
|
|
|
use crate::gates::gate::Gate;
|
|
|
|
|
use crate::gates::interpolation_quartic::QuarticInterpolationGate;
|
2021-05-17 09:06:17 -07:00
|
|
|
|
|
|
|
|
#[test]
|
2021-05-19 12:06:42 -07:00
|
|
|
fn wire_indices_2_points() {
|
|
|
|
|
let gate = QuarticInterpolationGate::<CrandallField, 4> {
|
2021-05-17 11:47:44 -07:00
|
|
|
num_points: 2,
|
|
|
|
|
_phantom: PhantomData,
|
|
|
|
|
};
|
2021-05-17 09:06:17 -07:00
|
|
|
// The exact indices aren't really important, but we want to make sure we don't have any
|
|
|
|
|
// overlaps or gaps.
|
|
|
|
|
assert_eq!(gate.wire_point(0), 0);
|
|
|
|
|
assert_eq!(gate.wire_point(1), 1);
|
2021-05-19 12:06:42 -07:00
|
|
|
assert_eq!(gate.wires_value(0), 2..6);
|
|
|
|
|
assert_eq!(gate.wires_value(1), 6..10);
|
|
|
|
|
assert_eq!(gate.wires_evaluation_point(), 10..14);
|
|
|
|
|
assert_eq!(gate.wires_evaluation_value(), 14..18);
|
|
|
|
|
assert_eq!(gate.wires_coeff(0), 18..22);
|
|
|
|
|
assert_eq!(gate.wires_coeff(1), 22..26);
|
2021-05-17 11:47:33 -07:00
|
|
|
assert_eq!(gate.num_wires(), 26);
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|
2021-05-19 12:06:42 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn wire_indices_4_points() {
|
|
|
|
|
let gate = QuarticInterpolationGate::<CrandallField, 4> {
|
|
|
|
|
num_points: 4,
|
|
|
|
|
_phantom: PhantomData,
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(gate.num_wires(), 44);
|
|
|
|
|
}
|
2021-05-17 09:06:17 -07:00
|
|
|
}
|