From bdb6cfe9274523ea28ff0df28a8de5808f795005 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 1 Jul 2021 13:11:34 -0700 Subject: [PATCH] skeleton --- src/gates/insertion.rs | 99 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/gates/insertion.rs diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs new file mode 100644 index 00000000..9977c6ac --- /dev/null +++ b/src/gates/insertion.rs @@ -0,0 +1,99 @@ +use crate::circuit_builder::CircuitBuilder; +use crate::field::extension_field::target::ExtensionTarget; +use crate::field::extension_field::Extendable; +use crate::field::field::Field; +use crate::gates::gate::{Gate, GateRef}; +use crate::generator::{SimpleGenerator, WitnessGenerator}; +use crate::target::Target; +use crate::vars::{EvaluationTargets, EvaluationVars}; +use crate::wire::Wire; +use crate::witness::PartialWitness; + +/// A gate for inserting a value into a list at a non-deterministic location. +pub struct InsertionGate; + +impl InsertionGate { + pub fn get, const D: usize>() -> GateRef { + + GateRef::new(InsertionGate) + } + + pub const CONST_INPUT: usize = 0; + + pub const WIRE_OUTPUT: usize = 0; +} + +impl, const D: usize> Gate for InsertionGate { + fn id(&self) -> String { + format!("{:?}", self, D) + } + + fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { + let input = vars.local_constants[Self::CONST_INPUT]; + let output = vars.local_wires[Self::WIRE_OUTPUT]; + vec![output - input] + } + + fn eval_unfiltered_recursively( + &self, + builder: &mut CircuitBuilder, + vars: EvaluationTargets, + ) -> Vec> { + let input = vars.local_constants[Self::CONST_INPUT]; + let output = vars.local_wires[Self::WIRE_OUTPUT]; + vec![builder.sub_extension(output, input)] + } + + fn generators( + &self, + gate_index: usize, + local_constants: &[F], + ) -> Vec>> { + let gen = ConstantGenerator { + gate_index, + constant: local_constants[0], + }; + vec![Box::new(gen)] + } + + fn num_wires(&self) -> usize { + 1 + } + + fn num_constants(&self) -> usize { + 1 + } + + fn degree(&self) -> usize { + 1 + } + + fn num_constraints(&self) -> usize { + 1 + } +} + +#[derive(Debug)] +struct InsertionGenerator { + gate_index: usize, + +} + +impl SimpleGenerator for InsertionGenerator { + fn dependencies(&self) -> Vec { + Vec::new() + } + + fn run_once(&self, _witness: &PartialWitness) -> PartialWitness { + let wire = Wire { + gate: self.gate_index, + input: ConstantGate::WIRE_OUTPUT, + }; + PartialWitness::singleton_target(Target::Wire(wire), self.constant) + } +} + +#[cfg(test)] +mod tests { + +}