From bdb6cfe9274523ea28ff0df28a8de5808f795005 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 1 Jul 2021 13:11:34 -0700 Subject: [PATCH 01/30] 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 { + +} From 2bee1c67219fe5df4b4c407e3a462e84549dbe12 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 6 Jul 2021 10:28:39 -0700 Subject: [PATCH 02/30] current progress --- src/gates/insertion.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 9977c6ac..7cdd0242 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -10,9 +10,21 @@ 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; +#[derive(Clone, Debug)] +pub(crate) struct InsertionGate, const D: usize> { + pub vec_size: usize, + pub _phantom: PhantomData, +} impl InsertionGate { + pub fn new(vec_size: usize) -> GateRef { + let gate = Self { + vec_size, + _phantom: PhantomData, + }; + GateRef::new(gate) + } + pub fn get, const D: usize>() -> GateRef { GateRef::new(InsertionGate) @@ -49,9 +61,10 @@ impl, const D: usize> Gate for InsertionGate { gate_index: usize, local_constants: &[F], ) -> Vec>> { - let gen = ConstantGenerator { + let gen = InsertionGenerator:: { gate_index, - constant: local_constants[0], + gate: self.clone(), + _phantom: PhantomData, }; vec![Box::new(gen)] } @@ -76,7 +89,8 @@ impl, const D: usize> Gate for InsertionGate { #[derive(Debug)] struct InsertionGenerator { gate_index: usize, - + gate: InterpolationGate, + _phantom: PhantomData, } impl SimpleGenerator for InsertionGenerator { From fbcfbf2d231a39f23d3789878b7aac928b7d3ab1 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 7 Jul 2021 14:32:27 -0700 Subject: [PATCH 03/30] insertion gate progress --- src/gates/insertion.rs | 105 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 13 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 7cdd0242..ea1d37a7 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -25,14 +25,73 @@ impl InsertionGate { GateRef::new(gate) } - pub fn get, const D: usize>() -> GateRef { - - GateRef::new(InsertionGate) + pub fn wires_insertion_index() -> Range { + 0..D } - pub const CONST_INPUT: usize = 0; + pub fn wires_list_item(i: usize) -> Range { + let start = (i + 1) * D; + start..start + D + } - pub const WIRE_OUTPUT: usize = 0; + fn start_of_intermediate_wires() -> usize { + (i + 2) * D + } + + fn wires_per_round() -> { + // D wires needed for each of cur_index, insert_here, equality_dummy, new_item, new_item_plus_old_item, + // already_inserted, and not_already_inserted + 7 * D + } + + /// The wires corresponding to the "cur_index" variable in the gadget (non-gate) insert function. + pub fn cur_index_for_round_r(r: usize) -> Range { + let intermediate_index = 0; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } + + /// The wires corresponding to the "insert_here" variable in the gadget (non-gate) insert function. + pub fn insert_here_for_round_r(r: usize) -> Range { + let intermediate_index = 1; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } + + /// The wires corresponding to the "equality_dummy" variable in the gadget (non-gate) insert function. + pub fn equality_dummy_for_round_r(r: usize) -> Range { + let intermediate_index = 1; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } + + /// The wires corresponding to the "new_item" variable in the gadget (non-gate) insert function. + pub fn new_item_for_round_r(r: usize) -> Range { + let intermediate_index = 2; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } + + /// The wires corresponding to the "new_item_plus_old_item" variable in the gadget (non-gate) insert function. + pub fn new_item_plus_old_item_for_round_r(r: usize) -> Range { + let intermediate_index = 3; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } + + /// The wires corresponding to the "already_inserted" variable in the gadget (non-gate) insert function. + pub fn already_inserted_for_round_r(r: usize) -> Range { + let intermediate_index = 4; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } + + /// The wires corresponding to the "not_already_inserted" variable in the gadget (non-gate) insert function. + pub fn not_already_inserted_for_round_r(r: usize) -> Range { + let intermediate_index = 5; + let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + start..start + D + } } impl, const D: usize> Gate for InsertionGate { @@ -41,9 +100,28 @@ impl, const D: usize> Gate for InsertionGate { } 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] + let insertion_index = vars.get_local_ext_algebra(Self::wires_insertion_index()); + let mut list_items = Vec::new(); + for i in 0..self::vec_size { + list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); + } + + let mut constraints = Vec::new(); + + for r in 0..self::vec_size { + let this_round_cur_index = vars.get_local_ext_algebra(Self::cur_index_for_round_r(r)); + // TODO: set value of cur_index + + let this_round_insert_here = vars.get_local_ext_algebra(Self::insert_here_for_round_r(r)); + // enforce "insert_here = is_equal(cur_index, insertion_index)" + let this_round_equality_dummy = vars.get_local_ext_algebra(Self::equality_dummy_for_round_r(r)); + let computed_insert_here = (this_round_cur_index - insertion_index) * this_round_equality_dummy; + constraints.extend(computed_insert_here - this_round_insert_here).to_basefield_array()); + + + } + + constraints } fn eval_unfiltered_recursively( @@ -51,9 +129,7 @@ impl, const D: usize> Gate for InsertionGate { 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)] + todo!() } fn generators( @@ -70,11 +146,14 @@ impl, const D: usize> Gate for InsertionGate { } fn num_wires(&self) -> usize { - 1 + let num_input_wires = self.vec_size + 1; // the original vector, and the insertion index + let num_output_wires = self.vec_size + 1; // the final vector, with the inserted element + let num_intermediate_wires = 6 * self.vec_size; // six intermediate variables needed for each element of the vector + self.vec_size + 1 } fn num_constants(&self) -> usize { - 1 + 0 } fn degree(&self) -> usize { From 662d62d8b439cc4fd86459b7e042c7c547f6be98 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 8 Jul 2021 15:20:26 -0700 Subject: [PATCH 04/30] progress on eval_unfiltered --- src/gates/insertion.rs | 74 +++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index ea1d37a7..b7bdb9d6 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -1,4 +1,5 @@ use crate::circuit_builder::CircuitBuilder; +use crate::field::extension_field::algebra::ExtensionAlgebra; use crate::field::extension_field::target::ExtensionTarget; use crate::field::extension_field::Extendable; use crate::field::field::Field; @@ -25,42 +26,40 @@ impl InsertionGate { GateRef::new(gate) } - pub fn wires_insertion_index() -> Range { - 0..D + pub fn wires_insertion_index() -> usize { + 0 + } + + pub fn wires_element_to_insert() -> Range { + 1..D+1 } pub fn wires_list_item(i: usize) -> Range { - let start = (i + 1) * D; + let start = (i + 1) * D + 1; start..start + D } - fn start_of_intermediate_wires() -> usize { - (i + 2) * D + fn start_of_output_wires(&self) -> usize { + (self::vec_size + 1) * D + 1 } - fn wires_per_round() -> { - // D wires needed for each of cur_index, insert_here, equality_dummy, new_item, new_item_plus_old_item, + pub fn wires_output_list_item(&self, i: usize) -> Range { + let start = self::start_of_output_wires() + i * D; + start..start + D + } + + fn start_of_intermediate_wires(&self) -> usize { + self::start_of_output_wires() + self::vec_size * D + } + + fn intermediate_wires_per_round() -> { + // D wires needed for each of insert_here, equality_dummy, new_item, new_item_plus_old_item, // already_inserted, and not_already_inserted - 7 * D - } - - /// The wires corresponding to the "cur_index" variable in the gadget (non-gate) insert function. - pub fn cur_index_for_round_r(r: usize) -> Range { - let intermediate_index = 0; - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; - start..start + D - } - - /// The wires corresponding to the "insert_here" variable in the gadget (non-gate) insert function. - pub fn insert_here_for_round_r(r: usize) -> Range { - let intermediate_index = 1; - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; - start..start + D + 6 * D } /// The wires corresponding to the "equality_dummy" variable in the gadget (non-gate) insert function. pub fn equality_dummy_for_round_r(r: usize) -> Range { - let intermediate_index = 1; let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; start..start + D } @@ -106,19 +105,34 @@ impl, const D: usize> Gate for InsertionGate { list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); } + let element_to_insert = vars.get_local_ext_algebra(Self::wires_element_to_insert()); + let mut constraints = Vec::new(); + let mut already_inserted = F::zero(); for r in 0..self::vec_size { - let this_round_cur_index = vars.get_local_ext_algebra(Self::cur_index_for_round_r(r)); - // TODO: set value of cur_index + let cur_index = F::Extension::from_canonical_usize(r); + + let equality_dummy = vars.get_local_ext_algebra(Self::equality_dummy_for_round_r(r)); - let this_round_insert_here = vars.get_local_ext_algebra(Self::insert_here_for_round_r(r)); - // enforce "insert_here = is_equal(cur_index, insertion_index)" - let this_round_equality_dummy = vars.get_local_ext_algebra(Self::equality_dummy_for_round_r(r)); - let computed_insert_here = (this_round_cur_index - insertion_index) * this_round_equality_dummy; - constraints.extend(computed_insert_here - this_round_insert_here).to_basefield_array()); + let difference = cur_index - insertion_index; + let insert_here = if difference == F::ZERO { + F::ZERO + } else { + F::ONE + }; + + // The two equality constraints: + constraints.extend(difference * equality_dummy - insert_here); + constraints.extend((1 - insert_here) * difference); + let mut new_item = insert_here * element_to_insert + already_inserted; + if r > 0 { + new_item += already_inserted * list_items[i - 1]; + } + already_inserted += insert_here; + new_item += (F::ONE - already_inserted) * list_items[i]; } constraints From 751e61647fc3d12741652277c131f459a0b1d4c6 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 8 Jul 2021 17:05:04 -0700 Subject: [PATCH 05/30] cleanup and progress --- src/gates/insertion.rs | 82 ++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index b7bdb9d6..92e69311 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -44,51 +44,17 @@ impl InsertionGate { } pub fn wires_output_list_item(&self, i: usize) -> Range { - let start = self::start_of_output_wires() + i * D; + let start = self.start_of_output_wires() + i * D; start..start + D } fn start_of_intermediate_wires(&self) -> usize { - self::start_of_output_wires() + self::vec_size * D - } - - fn intermediate_wires_per_round() -> { - // D wires needed for each of insert_here, equality_dummy, new_item, new_item_plus_old_item, - // already_inserted, and not_already_inserted - 6 * D + self.start_of_output_wires() + self.vec_size * D } /// The wires corresponding to the "equality_dummy" variable in the gadget (non-gate) insert function. pub fn equality_dummy_for_round_r(r: usize) -> Range { - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; - start..start + D - } - - /// The wires corresponding to the "new_item" variable in the gadget (non-gate) insert function. - pub fn new_item_for_round_r(r: usize) -> Range { - let intermediate_index = 2; - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; - start..start + D - } - - /// The wires corresponding to the "new_item_plus_old_item" variable in the gadget (non-gate) insert function. - pub fn new_item_plus_old_item_for_round_r(r: usize) -> Range { - let intermediate_index = 3; - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; - start..start + D - } - - /// The wires corresponding to the "already_inserted" variable in the gadget (non-gate) insert function. - pub fn already_inserted_for_round_r(r: usize) -> Range { - let intermediate_index = 4; - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; - start..start + D - } - - /// The wires corresponding to the "not_already_inserted" variable in the gadget (non-gate) insert function. - pub fn not_already_inserted_for_round_r(r: usize) -> Range { - let intermediate_index = 5; - let start = start_of_intermediate_wires() + r * wires_per_round() + D * intermediate_index; + let start = start_of_intermediate_wires() + D * r; start..start + D } } @@ -101,7 +67,7 @@ impl, const D: usize> Gate for InsertionGate { fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { let insertion_index = vars.get_local_ext_algebra(Self::wires_insertion_index()); let mut list_items = Vec::new(); - for i in 0..self::vec_size { + for i in 0..self.vec_size { list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); } @@ -110,7 +76,7 @@ impl, const D: usize> Gate for InsertionGate { let mut constraints = Vec::new(); let mut already_inserted = F::zero(); - for r in 0..self::vec_size { + for r in 0..self.vec_size { let cur_index = F::Extension::from_canonical_usize(r); let equality_dummy = vars.get_local_ext_algebra(Self::equality_dummy_for_round_r(r)); @@ -182,21 +148,49 @@ impl, const D: usize> Gate for InsertionGate { #[derive(Debug)] struct InsertionGenerator { gate_index: usize, - gate: InterpolationGate, + gate: InsertionGate, _phantom: PhantomData, } impl SimpleGenerator for InsertionGenerator { fn dependencies(&self) -> Vec { - Vec::new() + let local_target = |input| { + Target::Wire(Wire { + gate: self.gate_index, + input, + }) + }; + + let local_targets = |inputs: Range| inputs.map(local_target); + + let mut deps = Vec::new(); + deps.extend(local_targets(self.gate.wires_insertion_index())); + deps.extend(local_targets(self.gate.wires_element_to_insert())); + for i in 0..self.gate.vec_size { + deps.push(local_target(self.gate.wires_list_item(i))); + } + deps } fn run_once(&self, _witness: &PartialWitness) -> PartialWitness { - let wire = Wire { + let n = self.gate.num_points; + + let local_wire = |input| Wire { gate: self.gate_index, - input: ConstantGate::WIRE_OUTPUT, + input, }; - PartialWitness::singleton_target(Target::Wire(wire), self.constant) + + let get_local_wire = |input| witness.get_wire(local_wire(input)); + + let get_local_ext = |wire_range: Range| { + debug_assert_eq!(wire_range.len(), D); + let values = wire_range.map(get_local_wire).collect::>(); + let arr = values.try_into().unwrap(); + F::Extension::from_basefield_array(arr) + }; + + // Compute the new vector, and the equality dummy values. + todo!() } } From bec189b5983dc03dc8ed09c2fbc642091e8417f3 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Fri, 9 Jul 2021 16:31:19 -0700 Subject: [PATCH 06/30] fixes --- src/gates/insertion.rs | 50 +++++++++++++++++++++++------------------- src/gates/mod.rs | 1 + 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 92e69311..81874a7e 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -1,3 +1,7 @@ +use std::convert::TryInto; +use std::ops::Range; +use std::marker::PhantomData; + use crate::circuit_builder::CircuitBuilder; use crate::field::extension_field::algebra::ExtensionAlgebra; use crate::field::extension_field::target::ExtensionTarget; @@ -17,7 +21,7 @@ pub(crate) struct InsertionGate, const D: usize> { pub _phantom: PhantomData, } -impl InsertionGate { +impl, const D: usize> InsertionGate { pub fn new(vec_size: usize) -> GateRef { let gate = Self { vec_size, @@ -40,7 +44,7 @@ impl InsertionGate { } fn start_of_output_wires(&self) -> usize { - (self::vec_size + 1) * D + 1 + (self.vec_size + 1) * D + 1 } pub fn wires_output_list_item(&self, i: usize) -> Range { @@ -53,19 +57,19 @@ impl InsertionGate { } /// The wires corresponding to the "equality_dummy" variable in the gadget (non-gate) insert function. - pub fn equality_dummy_for_round_r(r: usize) -> Range { - let start = start_of_intermediate_wires() + D * r; + pub fn equality_dummy_for_round_r(&self, r: usize) -> Range { + let start = self.start_of_intermediate_wires() + D * r; start..start + D } } -impl, const D: usize> Gate for InsertionGate { +impl, const D: usize> Gate for InsertionGate { fn id(&self) -> String { format!("{:?}", self, D) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { - let insertion_index = vars.get_local_ext_algebra(Self::wires_insertion_index()); + let insertion_index = vars.local_wires[Self::wires_insertion_index()]; let mut list_items = Vec::new(); for i in 0..self.vec_size { list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); @@ -75,30 +79,32 @@ impl, const D: usize> Gate for InsertionGate { let mut constraints = Vec::new(); - let mut already_inserted = F::zero(); + let mut already_inserted : ExtensionAlgebra = F::Extension::ZERO.into(); for r in 0..self.vec_size { let cur_index = F::Extension::from_canonical_usize(r); - let equality_dummy = vars.get_local_ext_algebra(Self::equality_dummy_for_round_r(r)); + let equality_dummy = vars.get_local_ext_algebra(self.equality_dummy_for_round_r(r)); let difference = cur_index - insertion_index; - let insert_here = if difference == F::ZERO { - F::ZERO + let insert_here : ExtensionAlgebra = if difference == F::Extension::ZERO { + F::Extension::ZERO.into() } else { - F::ONE + F::Extension::ONE.into() }; // The two equality constraints: - constraints.extend(difference * equality_dummy - insert_here); - constraints.extend((1 - insert_here) * difference); + let equality_dummy_constraint : ExtensionAlgebra = difference.into() * equality_dummy - insert_here; + constraints.extend(equality_dummy_constraint.to_basefield_array()); + let mul_to_zero_constraint : ExtensionAlgebra = (F::Extension::ONE.into() - insert_here) * difference; + constraints.extend(mul_to_zero_constraint.to_basefield_array()); let mut new_item = insert_here * element_to_insert + already_inserted; if r > 0 { - new_item += already_inserted * list_items[i - 1]; + new_item += already_inserted * list_items[r - 1]; } already_inserted += insert_here; - new_item += (F::ONE - already_inserted) * list_items[i]; + new_item += (F::Extension::ONE.into() - already_inserted) * list_items[r]; } constraints @@ -146,13 +152,13 @@ impl, const D: usize> Gate for InsertionGate { } #[derive(Debug)] -struct InsertionGenerator { +struct InsertionGenerator, const D: usize> { gate_index: usize, gate: InsertionGate, _phantom: PhantomData, } -impl SimpleGenerator for InsertionGenerator { +impl, const D: usize> SimpleGenerator for InsertionGenerator { fn dependencies(&self) -> Vec { let local_target = |input| { Target::Wire(Wire { @@ -164,17 +170,15 @@ impl SimpleGenerator for InsertionGenerator { let local_targets = |inputs: Range| inputs.map(local_target); let mut deps = Vec::new(); - deps.extend(local_targets(self.gate.wires_insertion_index())); - deps.extend(local_targets(self.gate.wires_element_to_insert())); + deps.push(local_target(InsertionGate::::wires_insertion_index())); + deps.extend(local_targets(InsertionGate::::wires_element_to_insert())); for i in 0..self.gate.vec_size { - deps.push(local_target(self.gate.wires_list_item(i))); + deps.extend(local_targets(InsertionGate::::wires_list_item(i))); } deps } - fn run_once(&self, _witness: &PartialWitness) -> PartialWitness { - let n = self.gate.num_points; - + fn run_once(&self, witness: &PartialWitness) -> PartialWitness { let local_wire = |input| Wire { gate: self.gate_index, input, diff --git a/src/gates/mod.rs b/src/gates/mod.rs index fa23b273..f09b5335 100644 --- a/src/gates/mod.rs +++ b/src/gates/mod.rs @@ -4,6 +4,7 @@ pub mod constant; pub(crate) mod gate; pub mod gate_tree; pub mod gmimc; +pub mod insertion; pub mod interpolation; pub(crate) mod noop; From 66e4f7c3cf1261e8d44cd5f09f2f6c73499e4f0b Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 12:14:14 -0700 Subject: [PATCH 07/30] added output constraint --- src/gates/insertion.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 81874a7e..5ddc9168 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -5,7 +5,7 @@ use std::marker::PhantomData; use crate::circuit_builder::CircuitBuilder; use crate::field::extension_field::algebra::ExtensionAlgebra; use crate::field::extension_field::target::ExtensionTarget; -use crate::field::extension_field::Extendable; +use crate::field::extension_field::{FieldExtension, Extendable}; use crate::field::field::Field; use crate::gates::gate::{Gate, GateRef}; use crate::generator::{SimpleGenerator, WitnessGenerator}; @@ -70,17 +70,25 @@ impl, const D: usize> Gate for InsertionGate { fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { let insertion_index = vars.local_wires[Self::wires_insertion_index()]; + let mut list_items = Vec::new(); for i in 0..self.vec_size { list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); } + let dummy_value : ExtensionAlgebra = F::Extension::ZERO.into(); // will never be reached + list_items.push(dummy_value); + + let mut output_list_items = Vec::new(); + for i in 0..self.vec_size+1 { + output_list_items.push(vars.get_local_ext_algebra(self.wires_output_list_item(i))); + } let element_to_insert = vars.get_local_ext_algebra(Self::wires_element_to_insert()); let mut constraints = Vec::new(); let mut already_inserted : ExtensionAlgebra = F::Extension::ZERO.into(); - for r in 0..self.vec_size { + for r in 0..self.vec_size + 1 { let cur_index = F::Extension::from_canonical_usize(r); let equality_dummy = vars.get_local_ext_algebra(self.equality_dummy_for_round_r(r)); @@ -93,7 +101,8 @@ impl, const D: usize> Gate for InsertionGate { }; // The two equality constraints: - let equality_dummy_constraint : ExtensionAlgebra = difference.into() * equality_dummy - insert_here; + let difference_algebra : ExtensionAlgebra = difference.into(); + let equality_dummy_constraint : ExtensionAlgebra = difference_algebra * equality_dummy - insert_here; constraints.extend(equality_dummy_constraint.to_basefield_array()); let mul_to_zero_constraint : ExtensionAlgebra = (F::Extension::ONE.into() - insert_here) * difference; constraints.extend(mul_to_zero_constraint.to_basefield_array()); @@ -105,6 +114,8 @@ impl, const D: usize> Gate for InsertionGate { already_inserted += insert_here; new_item += (F::Extension::ONE.into() - already_inserted) * list_items[r]; + + constraints.extend((new_item - output_list_items[r]).to_basefield_array()); } constraints From 6090f6d6ce5fef086d7564f11dbea5cdadfffd51 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 15:31:12 -0700 Subject: [PATCH 08/30] changed insert_here to a wire, and fixes --- src/gates/insertion.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 5ddc9168..3633c712 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -56,10 +56,12 @@ impl, const D: usize> InsertionGate { self.start_of_output_wires() + self.vec_size * D } - /// The wires corresponding to the "equality_dummy" variable in the gadget (non-gate) insert function. - pub fn equality_dummy_for_round_r(&self, r: usize) -> Range { - let start = self.start_of_intermediate_wires() + D * r; - start..start + D + pub fn equality_dummy_for_round_r(&self, r: usize) -> usize { + self.start_of_intermediate_wires() + r + } + + pub fn insert_here_for_round_r(&self, r: usize) -> usize { + self.start_of_intermediate_wires() + (self.vec_size + 1) + r } } @@ -87,33 +89,29 @@ impl, const D: usize> Gate for InsertionGate { let mut constraints = Vec::new(); - let mut already_inserted : ExtensionAlgebra = F::Extension::ZERO.into(); + let mut already_inserted = F::Extension::ZERO; for r in 0..self.vec_size + 1 { let cur_index = F::Extension::from_canonical_usize(r); - let equality_dummy = vars.get_local_ext_algebra(self.equality_dummy_for_round_r(r)); + let equality_dummy = vars.local_wires[self.equality_dummy_for_round_r(r)]; let difference = cur_index - insertion_index; - let insert_here : ExtensionAlgebra = if difference == F::Extension::ZERO { - F::Extension::ZERO.into() - } else { - F::Extension::ONE.into() - }; + let insert_here = vars.local_wires[self.insert_here_for_round_r(r)]; - // The two equality constraints: + // The two equality constraints. let difference_algebra : ExtensionAlgebra = difference.into(); - let equality_dummy_constraint : ExtensionAlgebra = difference_algebra * equality_dummy - insert_here; + let equality_dummy_constraint : ExtensionAlgebra = difference_algebra * equality_dummy.into() - insert_here.into(); constraints.extend(equality_dummy_constraint.to_basefield_array()); - let mul_to_zero_constraint : ExtensionAlgebra = (F::Extension::ONE.into() - insert_here) * difference; + let mul_to_zero_constraint : ExtensionAlgebra = ((F::Extension::ONE - insert_here) * difference).into(); constraints.extend(mul_to_zero_constraint.to_basefield_array()); - let mut new_item = insert_here * element_to_insert + already_inserted; + let mut new_item = element_to_insert * insert_here.into() + already_inserted.into(); if r > 0 { - new_item += already_inserted * list_items[r - 1]; + new_item += list_items[r - 1] * already_inserted.into(); } already_inserted += insert_here; - new_item += (F::Extension::ONE.into() - already_inserted) * list_items[r]; + new_item += list_items[r] * (F::Extension::ONE - already_inserted).into(); constraints.extend((new_item - output_list_items[r]).to_basefield_array()); } @@ -204,7 +202,7 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator F::Extension::from_basefield_array(arr) }; - // Compute the new vector, and the equality dummy values. + // Compute the new vector and the values for equality_dummy and insert_here todo!() } } From cbffb854cc50156c8dedf275a860024e4692c607 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 15:31:26 -0700 Subject: [PATCH 09/30] cargo fmt --- src/gates/insertion.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 3633c712..28f74b80 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -1,11 +1,11 @@ use std::convert::TryInto; -use std::ops::Range; use std::marker::PhantomData; +use std::ops::Range; use crate::circuit_builder::CircuitBuilder; use crate::field::extension_field::algebra::ExtensionAlgebra; use crate::field::extension_field::target::ExtensionTarget; -use crate::field::extension_field::{FieldExtension, Extendable}; +use crate::field::extension_field::{Extendable, FieldExtension}; use crate::field::field::Field; use crate::gates::gate::{Gate, GateRef}; use crate::generator::{SimpleGenerator, WitnessGenerator}; @@ -35,7 +35,7 @@ impl, const D: usize> InsertionGate { } pub fn wires_element_to_insert() -> Range { - 1..D+1 + 1..D + 1 } pub fn wires_list_item(i: usize) -> Range { @@ -72,16 +72,16 @@ impl, const D: usize> Gate for InsertionGate { fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { let insertion_index = vars.local_wires[Self::wires_insertion_index()]; - + let mut list_items = Vec::new(); for i in 0..self.vec_size { list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); } - let dummy_value : ExtensionAlgebra = F::Extension::ZERO.into(); // will never be reached + let dummy_value: ExtensionAlgebra = F::Extension::ZERO.into(); // will never be reached list_items.push(dummy_value); let mut output_list_items = Vec::new(); - for i in 0..self.vec_size+1 { + for i in 0..self.vec_size + 1 { output_list_items.push(vars.get_local_ext_algebra(self.wires_output_list_item(i))); } @@ -92,17 +92,19 @@ impl, const D: usize> Gate for InsertionGate { let mut already_inserted = F::Extension::ZERO; for r in 0..self.vec_size + 1 { let cur_index = F::Extension::from_canonical_usize(r); - + let equality_dummy = vars.local_wires[self.equality_dummy_for_round_r(r)]; let difference = cur_index - insertion_index; let insert_here = vars.local_wires[self.insert_here_for_round_r(r)]; - + // The two equality constraints. - let difference_algebra : ExtensionAlgebra = difference.into(); - let equality_dummy_constraint : ExtensionAlgebra = difference_algebra * equality_dummy.into() - insert_here.into(); + let difference_algebra: ExtensionAlgebra = difference.into(); + let equality_dummy_constraint: ExtensionAlgebra = + difference_algebra * equality_dummy.into() - insert_here.into(); constraints.extend(equality_dummy_constraint.to_basefield_array()); - let mul_to_zero_constraint : ExtensionAlgebra = ((F::Extension::ONE - insert_here) * difference).into(); + let mul_to_zero_constraint: ExtensionAlgebra = + ((F::Extension::ONE - insert_here) * difference).into(); constraints.extend(mul_to_zero_constraint.to_basefield_array()); let mut new_item = element_to_insert * insert_here.into() + already_inserted.into(); @@ -180,7 +182,9 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator let mut deps = Vec::new(); deps.push(local_target(InsertionGate::::wires_insertion_index())); - deps.extend(local_targets(InsertionGate::::wires_element_to_insert())); + deps.extend(local_targets( + InsertionGate::::wires_element_to_insert(), + )); for i in 0..self.gate.vec_size { deps.extend(local_targets(InsertionGate::::wires_list_item(i))); } @@ -208,6 +212,4 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator } #[cfg(test)] -mod tests { - -} +mod tests {} From 185117ed4313b234c148df837b168b49f6903cbb Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 15:39:35 -0700 Subject: [PATCH 10/30] params --- src/gates/insertion.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 28f74b80..8697946e 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -154,11 +154,11 @@ impl, const D: usize> Gate for InsertionGate { } fn degree(&self) -> usize { - 1 + 2 } fn num_constraints(&self) -> usize { - 1 + (self.vec_size + 1) * 3 } } From 4bac34646c218dae875e19557c4cdc0f0493713f Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 16:38:05 -0700 Subject: [PATCH 11/30] generator --- src/gates/insertion.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 8697946e..2329c101 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -99,9 +99,8 @@ impl, const D: usize> Gate for InsertionGate { let insert_here = vars.local_wires[self.insert_here_for_round_r(r)]; // The two equality constraints. - let difference_algebra: ExtensionAlgebra = difference.into(); let equality_dummy_constraint: ExtensionAlgebra = - difference_algebra * equality_dummy.into() - insert_here.into(); + (difference * equality_dummy - insert_here).into(); constraints.extend(equality_dummy_constraint.to_basefield_array()); let mul_to_zero_constraint: ExtensionAlgebra = ((F::Extension::ONE - insert_here) * difference).into(); @@ -207,7 +206,24 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator }; // Compute the new vector and the values for equality_dummy and insert_here - todo!() + let n = self.gate.vec_size; + let orig_vec = (0..n).map(|i| get_local_ext(InsertionGate::::wires_list_item(i))).collect::>(); + let to_insert = get_local_ext(InsertionGate::::wires_element_to_insert()); + let insertion_index_f = get_local_wire(InsertionGate::::wires_insertion_index()); + + let insertion_index = insertion_index_f.to_canonical_u64() as usize; + let mut new_vec = Vec::new(); + new_vec.extend(&orig_vec[..insertion_index]); + new_vec.push(to_insert); + new_vec.extend(&orig_vec[insertion_index..]); + + let mut result = PartialWitness::::new(); + for i in 0..=n { + let output_wires = self.gate.wires_output_list_item(i).map(local_wire); + result.set_ext_wires(output_wires, new_vec[i]); + } + + result } } From 1b83b4dd4b90df78e531504f0c13be3cadd7cf6d Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 16:45:59 -0700 Subject: [PATCH 12/30] cargo fmt --- src/gates/insertion.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 2329c101..2d60ea83 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -207,10 +207,12 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator // Compute the new vector and the values for equality_dummy and insert_here let n = self.gate.vec_size; - let orig_vec = (0..n).map(|i| get_local_ext(InsertionGate::::wires_list_item(i))).collect::>(); + let orig_vec = (0..n) + .map(|i| get_local_ext(InsertionGate::::wires_list_item(i))) + .collect::>(); let to_insert = get_local_ext(InsertionGate::::wires_element_to_insert()); let insertion_index_f = get_local_wire(InsertionGate::::wires_insertion_index()); - + let insertion_index = insertion_index_f.to_canonical_u64() as usize; let mut new_vec = Vec::new(); new_vec.extend(&orig_vec[..insertion_index]); From 82206fdc3e898696e64159cb1c41faa57da33ad5 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 16:46:53 -0700 Subject: [PATCH 13/30] num_wires --- src/gates/insertion.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 2d60ea83..b224edff 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -142,10 +142,7 @@ impl, const D: usize> Gate for InsertionGate { } fn num_wires(&self) -> usize { - let num_input_wires = self.vec_size + 1; // the original vector, and the insertion index - let num_output_wires = self.vec_size + 1; // the final vector, with the inserted element - let num_intermediate_wires = 6 * self.vec_size; // six intermediate variables needed for each element of the vector - self.vec_size + 1 + self.insert_here_for_round_r(self.vec_size - 1) + 1 } fn num_constants(&self) -> usize { From 57a39a173028b91b72ad3f099da694e7517f567d Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 12 Jul 2021 16:55:41 -0700 Subject: [PATCH 14/30] basic tests, and fixes --- src/gates/insertion.rs | 64 +++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index b224edff..bdcdec7c 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -30,15 +30,15 @@ impl, const D: usize> InsertionGate { GateRef::new(gate) } - pub fn wires_insertion_index() -> usize { + pub fn wires_insertion_index(&self) -> usize { 0 } - pub fn wires_element_to_insert() -> Range { + pub fn wires_element_to_insert(&self) -> Range { 1..D + 1 } - pub fn wires_list_item(i: usize) -> Range { + pub fn wires_list_item(&self, i: usize) -> Range { let start = (i + 1) * D + 1; start..start + D } @@ -53,7 +53,7 @@ impl, const D: usize> InsertionGate { } fn start_of_intermediate_wires(&self) -> usize { - self.start_of_output_wires() + self.vec_size * D + self.start_of_output_wires() + (self.vec_size + 1) * D } pub fn equality_dummy_for_round_r(&self, r: usize) -> usize { @@ -71,11 +71,11 @@ impl, const D: usize> Gate for InsertionGate { } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { - let insertion_index = vars.local_wires[Self::wires_insertion_index()]; + let insertion_index = vars.local_wires[self.wires_insertion_index()]; let mut list_items = Vec::new(); for i in 0..self.vec_size { - list_items.push(vars.get_local_ext_algebra(Self::wires_list_item(i))); + list_items.push(vars.get_local_ext_algebra(self.wires_list_item(i))); } let dummy_value: ExtensionAlgebra = F::Extension::ZERO.into(); // will never be reached list_items.push(dummy_value); @@ -85,7 +85,7 @@ impl, const D: usize> Gate for InsertionGate { output_list_items.push(vars.get_local_ext_algebra(self.wires_output_list_item(i))); } - let element_to_insert = vars.get_local_ext_algebra(Self::wires_element_to_insert()); + let element_to_insert = vars.get_local_ext_algebra(self.wires_element_to_insert()); let mut constraints = Vec::new(); @@ -177,12 +177,10 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator let local_targets = |inputs: Range| inputs.map(local_target); let mut deps = Vec::new(); - deps.push(local_target(InsertionGate::::wires_insertion_index())); - deps.extend(local_targets( - InsertionGate::::wires_element_to_insert(), - )); + deps.push(local_target(self.gate.wires_insertion_index())); + deps.extend(local_targets(self.gate.wires_element_to_insert())); for i in 0..self.gate.vec_size { - deps.extend(local_targets(InsertionGate::::wires_list_item(i))); + deps.extend(local_targets(self.gate.wires_list_item(i))); } deps } @@ -205,10 +203,10 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator // Compute the new vector and the values for equality_dummy and insert_here let n = self.gate.vec_size; let orig_vec = (0..n) - .map(|i| get_local_ext(InsertionGate::::wires_list_item(i))) + .map(|i| get_local_ext(self.gate.wires_list_item(i))) .collect::>(); - let to_insert = get_local_ext(InsertionGate::::wires_element_to_insert()); - let insertion_index_f = get_local_wire(InsertionGate::::wires_insertion_index()); + let to_insert = get_local_ext(self.gate.wires_element_to_insert()); + let insertion_index_f = get_local_wire(self.gate.wires_insertion_index()); let insertion_index = insertion_index_f.to_canonical_u64() as usize; let mut new_vec = Vec::new(); @@ -227,4 +225,38 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator } #[cfg(test)] -mod tests {} +mod tests { + use std::marker::PhantomData; + + use crate::field::crandall_field::CrandallField; + use crate::gates::gate_testing::test_low_degree; + use crate::gates::insertion::InsertionGate; + + #[test] + fn wire_indices() { + let gate = InsertionGate:: { + vec_size: 3, + _phantom: PhantomData, + }; + + assert_eq!(gate.wires_insertion_index(), 0); + assert_eq!(gate.wires_element_to_insert(), 1..5); + assert_eq!(gate.wires_list_item(0), 5..9); + assert_eq!(gate.wires_list_item(2), 13..17); + assert_eq!(gate.wires_output_list_item(0), 17..21); + assert_eq!(gate.wires_output_list_item(3), 29..33); + assert_eq!(gate.equality_dummy_for_round_r(0), 33); + assert_eq!(gate.equality_dummy_for_round_r(3), 36); + assert_eq!(gate.insert_here_for_round_r(0), 37); + assert_eq!(gate.insert_here_for_round_r(3), 40); + } + + #[test] + fn low_degree() { + type F = CrandallField; + test_low_degree(InsertionGate::::new(4)); + } + + #[test] + fn test_gate_constraint() {} +} From e0c767f3c161e42bc6de80b15bea866b0291b460 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:01:26 -0700 Subject: [PATCH 15/30] added intermediate wires to generator; and test (does not pass) --- src/gates/insertion.rs | 115 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 11 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index bdcdec7c..479c0b45 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -56,11 +56,11 @@ impl, const D: usize> InsertionGate { self.start_of_output_wires() + (self.vec_size + 1) * D } - pub fn equality_dummy_for_round_r(&self, r: usize) -> usize { + pub fn wires_equality_dummy_for_round_r(&self, r: usize) -> usize { self.start_of_intermediate_wires() + r } - pub fn insert_here_for_round_r(&self, r: usize) -> usize { + pub fn wires_insert_here_for_round_r(&self, r: usize) -> usize { self.start_of_intermediate_wires() + (self.vec_size + 1) + r } } @@ -93,10 +93,10 @@ impl, const D: usize> Gate for InsertionGate { for r in 0..self.vec_size + 1 { let cur_index = F::Extension::from_canonical_usize(r); - let equality_dummy = vars.local_wires[self.equality_dummy_for_round_r(r)]; + let equality_dummy = vars.local_wires[self.wires_equality_dummy_for_round_r(r)]; let difference = cur_index - insertion_index; - let insert_here = vars.local_wires[self.insert_here_for_round_r(r)]; + let insert_here = vars.local_wires[self.wires_insert_here_for_round_r(r)]; // The two equality constraints. let equality_dummy_constraint: ExtensionAlgebra = @@ -142,7 +142,7 @@ impl, const D: usize> Gate for InsertionGate { } fn num_wires(&self) -> usize { - self.insert_here_for_round_r(self.vec_size - 1) + 1 + self.wires_insert_here_for_round_r(self.vec_size - 1) + 1 } fn num_constants(&self) -> usize { @@ -214,10 +214,31 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator new_vec.push(to_insert); new_vec.extend(&orig_vec[insertion_index..]); + let mut equality_dummy_vals = Vec::new(); + for i in 0..n+1 { + if i != insertion_index { + let diff = if i > insertion_index { + F::from_canonical_usize(i - insertion_index) + } else { + F::ZERO - F::from_canonical_usize(insertion_index - i) + }; + equality_dummy_vals.push(diff.inverse()); + } else { + equality_dummy_vals.push(F::ONE); + } + } + + let mut insert_here_vals = vec![F::ZERO; n]; + insert_here_vals.insert(insertion_index, F::ONE); + let mut result = PartialWitness::::new(); - for i in 0..=n { + for i in 0..n+1 { let output_wires = self.gate.wires_output_list_item(i).map(local_wire); result.set_ext_wires(output_wires, new_vec[i]); + let equality_dummy_wire = local_wire(self.gate.wires_equality_dummy_for_round_r(i)); + result.set_wire(equality_dummy_wire, equality_dummy_vals[i]); + let insert_here_wire = local_wire(self.gate.wires_insert_here_for_round_r(i)); + result.set_wire(insert_here_wire, insert_here_vals[i]); } result @@ -229,8 +250,13 @@ mod tests { use std::marker::PhantomData; use crate::field::crandall_field::CrandallField; + use crate::field::extension_field::quartic::QuarticCrandallField; + use crate::field::extension_field::FieldExtension; + use crate::field::field::Field; + use crate::gates::gate::Gate; use crate::gates::gate_testing::test_low_degree; use crate::gates::insertion::InsertionGate; + use crate::vars::EvaluationVars; #[test] fn wire_indices() { @@ -245,10 +271,10 @@ mod tests { assert_eq!(gate.wires_list_item(2), 13..17); assert_eq!(gate.wires_output_list_item(0), 17..21); assert_eq!(gate.wires_output_list_item(3), 29..33); - assert_eq!(gate.equality_dummy_for_round_r(0), 33); - assert_eq!(gate.equality_dummy_for_round_r(3), 36); - assert_eq!(gate.insert_here_for_round_r(0), 37); - assert_eq!(gate.insert_here_for_round_r(3), 40); + assert_eq!(gate.wires_equality_dummy_for_round_r(0), 33); + assert_eq!(gate.wires_equality_dummy_for_round_r(3), 36); + assert_eq!(gate.wires_insert_here_for_round_r(0), 37); + assert_eq!(gate.wires_insert_here_for_round_r(3), 40); } #[test] @@ -258,5 +284,72 @@ mod tests { } #[test] - fn test_gate_constraint() {} + fn test_gate_constraint() { + type F = CrandallField; + type FF = QuarticCrandallField; + const D: usize = 4; + + /// Returns the local wires for an interpolation gate for given coeffs, points and eval point. + fn get_wires( + vec_size: usize, + orig_vec: Vec, + insertion_index: usize, + element_to_insert: FF, + ) -> Vec { + let mut v = vec![F::ZERO; 2 * (vec_size + 1) * (D + 1) + 1]; + v[0] = F::from_canonical_usize(insertion_index as usize); + for i in 0..D { + v[1 + i] = >::to_basefield_array(&element_to_insert)[i]; + } + for j in 0..vec_size { + for i in 0..D { + v[(j + 1) * D + 1 + i] = >::to_basefield_array(&orig_vec[j])[i]; + } + } + + let mut new_vec = orig_vec.clone(); + new_vec.insert(insertion_index, element_to_insert); + let mut equality_dummy_vals = Vec::new(); + for i in 0..vec_size+1 { + if i != insertion_index { + let diff = if i > insertion_index { + F::from_canonical_usize(i - insertion_index) + } else { + F::ZERO - F::from_canonical_usize(insertion_index - i) + }; + equality_dummy_vals.push(diff.inverse()); + } else { + equality_dummy_vals.push(F::ONE); + } + } + let mut insert_here_vals = vec![F::ZERO; vec_size]; + insert_here_vals.insert(insertion_index, F::ONE); + + for j in 0..vec_size+1 { + for i in 0..D { + v[(vec_size + j + 1) * D + 1 + i] = >::to_basefield_array(&new_vec[j])[i]; + } + v[(2 * vec_size + 2) * D + 1 + j] = equality_dummy_vals[j]; + v[(2 * vec_size + 2) * D + 1 + (vec_size + 1) + j] = insert_here_vals[j]; + } + + v.iter().map(|&x| x.into()).collect::>() + } + + let orig_vec = vec![FF::rand(); 3]; + let insertion_index = 1; + let element_to_insert = FF::rand(); + let gate = InsertionGate:: { + vec_size: 3, + _phantom: PhantomData, + }; + let vars = EvaluationVars { + local_constants: &[], + local_wires: &get_wires(3, orig_vec, insertion_index, element_to_insert), + }; + assert!( + gate.eval_unfiltered(vars).iter().all(|x| x.is_zero()), + "Gate constraints are not satisfied." + ); + } } From 1ecc234644e8f3d2cfb1820364b4070d8daf1b9f Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:02:55 -0700 Subject: [PATCH 16/30] fix --- src/gates/insertion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 479c0b45..adf213df 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -142,7 +142,7 @@ impl, const D: usize> Gate for InsertionGate { } fn num_wires(&self) -> usize { - self.wires_insert_here_for_round_r(self.vec_size - 1) + 1 + self.wires_insert_here_for_round_r(self.vec_size) + 1 } fn num_constants(&self) -> usize { From f0ae72ba74b2af5478451b151b20690b2270b43c Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:15:56 -0700 Subject: [PATCH 17/30] constraints in base field (fixes low degree test) --- src/gates/insertion.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index adf213df..c5b7f067 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -99,12 +99,10 @@ impl, const D: usize> Gate for InsertionGate { let insert_here = vars.local_wires[self.wires_insert_here_for_round_r(r)]; // The two equality constraints. - let equality_dummy_constraint: ExtensionAlgebra = - (difference * equality_dummy - insert_here).into(); - constraints.extend(equality_dummy_constraint.to_basefield_array()); - let mul_to_zero_constraint: ExtensionAlgebra = - ((F::Extension::ONE - insert_here) * difference).into(); - constraints.extend(mul_to_zero_constraint.to_basefield_array()); + let equality_dummy_constraint = difference * equality_dummy - insert_here; + constraints.push(equality_dummy_constraint); + let mul_to_zero_constraint = (F::Extension::ONE - insert_here) * difference; + constraints.push(mul_to_zero_constraint); let mut new_item = element_to_insert * insert_here.into() + already_inserted.into(); if r > 0 { From ca944d5892e1e8237f5b6ffad212df1ce538781f Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:48:24 -0700 Subject: [PATCH 18/30] fixes; and in-progress test debugging --- src/gates/insertion.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index c5b7f067..0812f3e3 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -99,10 +99,8 @@ impl, const D: usize> Gate for InsertionGate { let insert_here = vars.local_wires[self.wires_insert_here_for_round_r(r)]; // The two equality constraints. - let equality_dummy_constraint = difference * equality_dummy - insert_here; - constraints.push(equality_dummy_constraint); - let mul_to_zero_constraint = (F::Extension::ONE - insert_here) * difference; - constraints.push(mul_to_zero_constraint); + constraints.push(difference * equality_dummy - (F::Extension::ONE - insert_here)); + constraints.push((F::Extension::ONE - insert_here) * difference); let mut new_item = element_to_insert * insert_here.into() + already_inserted.into(); if r > 0 { @@ -345,9 +343,13 @@ mod tests { local_constants: &[], local_wires: &get_wires(3, orig_vec, insertion_index, element_to_insert), }; - assert!( - gate.eval_unfiltered(vars).iter().all(|x| x.is_zero()), - "Gate constraints are not satisfied." - ); + + let constraints = gate.eval_unfiltered(vars); + let _ = constraints.iter().zip(0..constraints.len()).all(|(x, i)| { + if !x.is_zero() { + println!("gate constraint {} is not satisfied", i); + } + true + }); } } From c6a33d0eff240b1305143a1fe713f7642f0bcb4c Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:49:22 -0700 Subject: [PATCH 19/30] more fixes --- src/gates/insertion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 0812f3e3..83d3b90e 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -100,7 +100,7 @@ impl, const D: usize> Gate for InsertionGate { // The two equality constraints. constraints.push(difference * equality_dummy - (F::Extension::ONE - insert_here)); - constraints.push((F::Extension::ONE - insert_here) * difference); + constraints.push(insert_here * difference); let mut new_item = element_to_insert * insert_here.into() + already_inserted.into(); if r > 0 { From 3a2ba05b278eabbf28a8a33edee05e0074fa16b6 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:50:48 -0700 Subject: [PATCH 20/30] last fix! --- src/gates/insertion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 83d3b90e..8fbda3d2 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -102,7 +102,7 @@ impl, const D: usize> Gate for InsertionGate { constraints.push(difference * equality_dummy - (F::Extension::ONE - insert_here)); constraints.push(insert_here * difference); - let mut new_item = element_to_insert * insert_here.into() + already_inserted.into(); + let mut new_item = element_to_insert * insert_here.into(); if r > 0 { new_item += list_items[r - 1] * already_inserted.into(); } From f4c7756f56cce97ff3e0b73e9465536d450ba825 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:51:45 -0700 Subject: [PATCH 21/30] removed test debugging --- src/gates/insertion.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 8fbda3d2..b37414b9 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -344,12 +344,9 @@ mod tests { local_wires: &get_wires(3, orig_vec, insertion_index, element_to_insert), }; - let constraints = gate.eval_unfiltered(vars); - let _ = constraints.iter().zip(0..constraints.len()).all(|(x, i)| { - if !x.is_zero() { - println!("gate constraint {} is not satisfied", i); - } - true - }); + assert!( + gate.eval_unfiltered(vars).iter().all(|x| x.is_zero()), + "Gate constraints are not satisfied." + ); } } From 0a32e0fdebdb4f70ba9bdbc130bb0730bf4438e1 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 13 Jul 2021 16:54:04 -0700 Subject: [PATCH 22/30] cargo fmt --- src/gadgets/insert.rs | 1 - src/gates/insertion.rs | 14 ++++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/gadgets/insert.rs b/src/gadgets/insert.rs index 6632eae7..7ddda1fb 100644 --- a/src/gadgets/insert.rs +++ b/src/gadgets/insert.rs @@ -68,7 +68,6 @@ impl, const D: usize> CircuitBuilder { new_list.push(new_item); } - new_list } } diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index b37414b9..e82b4b32 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -211,7 +211,7 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator new_vec.extend(&orig_vec[insertion_index..]); let mut equality_dummy_vals = Vec::new(); - for i in 0..n+1 { + for i in 0..n + 1 { if i != insertion_index { let diff = if i > insertion_index { F::from_canonical_usize(i - insertion_index) @@ -228,7 +228,7 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator insert_here_vals.insert(insertion_index, F::ONE); let mut result = PartialWitness::::new(); - for i in 0..n+1 { + for i in 0..n + 1 { let output_wires = self.gate.wires_output_list_item(i).map(local_wire); result.set_ext_wires(output_wires, new_vec[i]); let equality_dummy_wire = local_wire(self.gate.wires_equality_dummy_for_round_r(i)); @@ -299,14 +299,15 @@ mod tests { } for j in 0..vec_size { for i in 0..D { - v[(j + 1) * D + 1 + i] = >::to_basefield_array(&orig_vec[j])[i]; + v[(j + 1) * D + 1 + i] = + >::to_basefield_array(&orig_vec[j])[i]; } } let mut new_vec = orig_vec.clone(); new_vec.insert(insertion_index, element_to_insert); let mut equality_dummy_vals = Vec::new(); - for i in 0..vec_size+1 { + for i in 0..vec_size + 1 { if i != insertion_index { let diff = if i > insertion_index { F::from_canonical_usize(i - insertion_index) @@ -321,9 +322,10 @@ mod tests { let mut insert_here_vals = vec![F::ZERO; vec_size]; insert_here_vals.insert(insertion_index, F::ONE); - for j in 0..vec_size+1 { + for j in 0..vec_size + 1 { for i in 0..D { - v[(vec_size + j + 1) * D + 1 + i] = >::to_basefield_array(&new_vec[j])[i]; + v[(vec_size + j + 1) * D + 1 + i] = + >::to_basefield_array(&new_vec[j])[i]; } v[(2 * vec_size + 2) * D + 1 + j] = equality_dummy_vals[j]; v[(2 * vec_size + 2) * D + 1 + (vec_size + 1) + j] = insert_here_vals[j]; From 27c93f8f4bc452630412038f473d9ed7dc80a032 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 15:47:59 -0700 Subject: [PATCH 23/30] addressed comments --- src/gates/insertion.rs | 140 ++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 78 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index e82b4b32..37469237 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -38,7 +38,8 @@ impl, const D: usize> InsertionGate { 1..D + 1 } - pub fn wires_list_item(&self, i: usize) -> Range { + pub fn wires_original_list_item(&self, i: usize) -> Range { + debug_assert!(i < self.vec_size); let start = (i + 1) * D + 1; start..start + D } @@ -48,6 +49,7 @@ impl, const D: usize> InsertionGate { } pub fn wires_output_list_item(&self, i: usize) -> Range { + debug_assert!(i <= self.vec_size); let start = self.start_of_output_wires() + i * D; start..start + D } @@ -56,10 +58,15 @@ impl, const D: usize> InsertionGate { self.start_of_output_wires() + (self.vec_size + 1) * D } + /// An intermediate wire for a dummy variable used to show equality. + /// The prover sets this to 1/(x-y) if x != y, or to an arbitrary value if + /// x == y. pub fn wires_equality_dummy_for_round_r(&self, r: usize) -> usize { self.start_of_intermediate_wires() + r } + // An intermediate wire for the "insert_here" variable (1 if the current index is the index at + /// which to insert the new value, 0 otherwise). pub fn wires_insert_here_for_round_r(&self, r: usize) -> usize { self.start_of_intermediate_wires() + (self.vec_size + 1) + r } @@ -73,24 +80,20 @@ impl, const D: usize> Gate for InsertionGate { fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { let insertion_index = vars.local_wires[self.wires_insertion_index()]; - let mut list_items = Vec::new(); - for i in 0..self.vec_size { - list_items.push(vars.get_local_ext_algebra(self.wires_list_item(i))); - } - let dummy_value: ExtensionAlgebra = F::Extension::ZERO.into(); // will never be reached - list_items.push(dummy_value); + let list_items = (0..self.vec_size) + .map(|i| vars.get_local_ext_algebra(self.wires_original_list_item(i))) + .collect::>(); - let mut output_list_items = Vec::new(); - for i in 0..self.vec_size + 1 { - output_list_items.push(vars.get_local_ext_algebra(self.wires_output_list_item(i))); - } + let output_list_items = (0..=self.vec_size) + .map(|i| vars.get_local_ext_algebra(self.wires_output_list_item(i))) + .collect::>(); let element_to_insert = vars.get_local_ext_algebra(self.wires_element_to_insert()); let mut constraints = Vec::new(); let mut already_inserted = F::Extension::ZERO; - for r in 0..self.vec_size + 1 { + for r in 0..=self.vec_size { let cur_index = F::Extension::from_canonical_usize(r); let equality_dummy = vars.local_wires[self.wires_equality_dummy_for_round_r(r)]; @@ -108,7 +111,9 @@ impl, const D: usize> Gate for InsertionGate { } already_inserted += insert_here; - new_item += list_items[r] * (F::Extension::ONE - already_inserted).into(); + if r < self.vec_size { + new_item += list_items[r] * (F::Extension::ONE - already_inserted).into(); + } constraints.extend((new_item - output_list_items[r]).to_basefield_array()); } @@ -132,7 +137,6 @@ impl, const D: usize> Gate for InsertionGate { let gen = InsertionGenerator:: { gate_index, gate: self.clone(), - _phantom: PhantomData, }; vec![Box::new(gen)] } @@ -150,7 +154,7 @@ impl, const D: usize> Gate for InsertionGate { } fn num_constraints(&self) -> usize { - (self.vec_size + 1) * 3 + (self.vec_size + 1) * (2 + D) } } @@ -158,17 +162,11 @@ impl, const D: usize> Gate for InsertionGate { struct InsertionGenerator, const D: usize> { gate_index: usize, gate: InsertionGate, - _phantom: PhantomData, } impl, const D: usize> SimpleGenerator for InsertionGenerator { fn dependencies(&self) -> Vec { - let local_target = |input| { - Target::Wire(Wire { - gate: self.gate_index, - input, - }) - }; + let local_target = |input| Target::wire(self.gate_index, input); let local_targets = |inputs: Range| inputs.map(local_target); @@ -176,7 +174,7 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator deps.push(local_target(self.gate.wires_insertion_index())); deps.extend(local_targets(self.gate.wires_element_to_insert())); for i in 0..self.gate.vec_size { - deps.extend(local_targets(self.gate.wires_list_item(i))); + deps.extend(local_targets(self.gate.wires_original_list_item(i))); } deps } @@ -197,38 +195,38 @@ impl, const D: usize> SimpleGenerator for InsertionGenerator }; // Compute the new vector and the values for equality_dummy and insert_here - let n = self.gate.vec_size; - let orig_vec = (0..n) - .map(|i| get_local_ext(self.gate.wires_list_item(i))) + let vec_size = self.gate.vec_size; + let orig_vec = (0..vec_size) + .map(|i| get_local_ext(self.gate.wires_original_list_item(i))) .collect::>(); let to_insert = get_local_ext(self.gate.wires_element_to_insert()); let insertion_index_f = get_local_wire(self.gate.wires_insertion_index()); let insertion_index = insertion_index_f.to_canonical_u64() as usize; - let mut new_vec = Vec::new(); - new_vec.extend(&orig_vec[..insertion_index]); - new_vec.push(to_insert); - new_vec.extend(&orig_vec[insertion_index..]); + debug_assert!( + insertion_index <= vec_size, + "Insertion index {} is larger than the vector size {}", + insertion_index, + vec_size + ); + + let mut new_vec = orig_vec.clone(); + new_vec.insert(insertion_index, to_insert); let mut equality_dummy_vals = Vec::new(); - for i in 0..n + 1 { - if i != insertion_index { - let diff = if i > insertion_index { - F::from_canonical_usize(i - insertion_index) - } else { - F::ZERO - F::from_canonical_usize(insertion_index - i) - }; - equality_dummy_vals.push(diff.inverse()); + for i in 0..=vec_size { + equality_dummy_vals.push(if i == insertion_index { + F::ONE } else { - equality_dummy_vals.push(F::ONE); - } + (F::from_canonical_usize(i) - insertion_index_f).inverse() + }); } - let mut insert_here_vals = vec![F::ZERO; n]; + let mut insert_here_vals = vec![F::ZERO; vec_size]; insert_here_vals.insert(insertion_index, F::ONE); let mut result = PartialWitness::::new(); - for i in 0..n + 1 { + for i in 0..=vec_size { let output_wires = self.gate.wires_output_list_item(i).map(local_wire); result.set_ext_wires(output_wires, new_vec[i]); let equality_dummy_wire = local_wire(self.gate.wires_equality_dummy_for_round_r(i)); @@ -263,8 +261,8 @@ mod tests { assert_eq!(gate.wires_insertion_index(), 0); assert_eq!(gate.wires_element_to_insert(), 1..5); - assert_eq!(gate.wires_list_item(0), 5..9); - assert_eq!(gate.wires_list_item(2), 13..17); + assert_eq!(gate.wires_original_list_item(0), 5..9); + assert_eq!(gate.wires_original_list_item(2), 13..17); assert_eq!(gate.wires_output_list_item(0), 17..21); assert_eq!(gate.wires_output_list_item(3), 29..33); assert_eq!(gate.wires_equality_dummy_for_round_r(0), 33); @@ -285,51 +283,37 @@ mod tests { type FF = QuarticCrandallField; const D: usize = 4; - /// Returns the local wires for an interpolation gate for given coeffs, points and eval point. - fn get_wires( - vec_size: usize, - orig_vec: Vec, - insertion_index: usize, - element_to_insert: FF, - ) -> Vec { - let mut v = vec![F::ZERO; 2 * (vec_size + 1) * (D + 1) + 1]; - v[0] = F::from_canonical_usize(insertion_index as usize); - for i in 0..D { - v[1 + i] = >::to_basefield_array(&element_to_insert)[i]; - } + /// Returns the local wires for an insertion gate for given the original vector, element to + /// insert, and index. + fn get_wires(orig_vec: Vec, insertion_index: usize, element_to_insert: FF) -> Vec { + let vec_size = orig_vec.len(); + + let mut v = Vec::new(); + v.push(F::from_canonical_usize(insertion_index)); + v.extend(element_to_insert.0); for j in 0..vec_size { - for i in 0..D { - v[(j + 1) * D + 1 + i] = - >::to_basefield_array(&orig_vec[j])[i]; - } + v.extend(orig_vec[j].0); } let mut new_vec = orig_vec.clone(); new_vec.insert(insertion_index, element_to_insert); let mut equality_dummy_vals = Vec::new(); - for i in 0..vec_size + 1 { - if i != insertion_index { - let diff = if i > insertion_index { - F::from_canonical_usize(i - insertion_index) - } else { - F::ZERO - F::from_canonical_usize(insertion_index - i) - }; - equality_dummy_vals.push(diff.inverse()); + for i in 0..=vec_size { + equality_dummy_vals.push(if i == insertion_index { + F::ONE } else { - equality_dummy_vals.push(F::ONE); - } + (F::from_canonical_usize(i) - F::from_canonical_usize(insertion_index)) + .inverse() + }); } let mut insert_here_vals = vec![F::ZERO; vec_size]; insert_here_vals.insert(insertion_index, F::ONE); - for j in 0..vec_size + 1 { - for i in 0..D { - v[(vec_size + j + 1) * D + 1 + i] = - >::to_basefield_array(&new_vec[j])[i]; - } - v[(2 * vec_size + 2) * D + 1 + j] = equality_dummy_vals[j]; - v[(2 * vec_size + 2) * D + 1 + (vec_size + 1) + j] = insert_here_vals[j]; + for j in 0..=vec_size { + v.extend(new_vec[j].0); } + v.extend(equality_dummy_vals); + v.extend(insert_here_vals); v.iter().map(|&x| x.into()).collect::>() } @@ -343,7 +327,7 @@ mod tests { }; let vars = EvaluationVars { local_constants: &[], - local_wires: &get_wires(3, orig_vec, insertion_index, element_to_insert), + local_wires: &get_wires(orig_vec, insertion_index, element_to_insert), }; assert!( From 9f2f987f20a48ea168f6ca2cf19045c02bbe131f Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 15:57:35 -0700 Subject: [PATCH 24/30] minor fixes --- src/gates/insertion.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 37469237..04bc0422 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -132,7 +132,7 @@ impl, const D: usize> Gate for InsertionGate { fn generators( &self, gate_index: usize, - local_constants: &[F], + _local_constants: &[F], ) -> Vec>> { let gen = InsertionGenerator:: { gate_index, @@ -245,7 +245,6 @@ mod tests { use crate::field::crandall_field::CrandallField; use crate::field::extension_field::quartic::QuarticCrandallField; - use crate::field::extension_field::FieldExtension; use crate::field::field::Field; use crate::gates::gate::Gate; use crate::gates::gate_testing::test_low_degree; From 16ae68795a47aeee66a996ade09f2dd78c4717b6 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 16:55:18 -0700 Subject: [PATCH 25/30] cleanup --- src/gates/insertion.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 04bc0422..3dc8e5d1 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -79,26 +79,20 @@ impl, const D: usize> Gate for InsertionGate { fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { let insertion_index = vars.local_wires[self.wires_insertion_index()]; - let list_items = (0..self.vec_size) .map(|i| vars.get_local_ext_algebra(self.wires_original_list_item(i))) .collect::>(); - let output_list_items = (0..=self.vec_size) .map(|i| vars.get_local_ext_algebra(self.wires_output_list_item(i))) .collect::>(); - let element_to_insert = vars.get_local_ext_algebra(self.wires_element_to_insert()); let mut constraints = Vec::new(); - let mut already_inserted = F::Extension::ZERO; for r in 0..=self.vec_size { let cur_index = F::Extension::from_canonical_usize(r); - - let equality_dummy = vars.local_wires[self.wires_equality_dummy_for_round_r(r)]; - let difference = cur_index - insertion_index; + let equality_dummy = vars.local_wires[self.wires_equality_dummy_for_round_r(r)]; let insert_here = vars.local_wires[self.wires_insert_here_for_round_r(r)]; // The two equality constraints. @@ -109,12 +103,12 @@ impl, const D: usize> Gate for InsertionGate { if r > 0 { new_item += list_items[r - 1] * already_inserted.into(); } - already_inserted += insert_here; - if r < self.vec_size { new_item += list_items[r] * (F::Extension::ONE - already_inserted).into(); } + already_inserted += insert_here; + // Output constraint. constraints.extend((new_item - output_list_items[r]).to_basefield_array()); } From c708d7e428206df738abe83471073a8a1f9dabfd Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 17:17:45 -0700 Subject: [PATCH 26/30] eval_unfiltered_recursively --- src/gates/insertion.rs | 48 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 3dc8e5d1..8c91ced5 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -120,7 +120,53 @@ impl, const D: usize> Gate for InsertionGate { builder: &mut CircuitBuilder, vars: EvaluationTargets, ) -> Vec> { - todo!() + let insertion_index = vars.local_wires[self.wires_insertion_index()]; + let list_items = (0..self.vec_size) + .map(|i| vars.get_local_ext_algebra(self.wires_original_list_item(i))) + .collect::>(); + let output_list_items = (0..=self.vec_size) + .map(|i| vars.get_local_ext_algebra(self.wires_output_list_item(i))) + .collect::>(); + let element_to_insert = vars.get_local_ext_algebra(self.wires_element_to_insert()); + + let mut constraints = Vec::new(); + let mut already_inserted = builder.constant_extension(F::Extension::ZERO); + for r in 0..=self.vec_size { + let cur_index_ext = F::Extension::from_canonical_usize(r); + let cur_index = builder.constant_extension(cur_index_ext); + + let difference = builder.sub_extension(cur_index, insertion_index); + let equality_dummy = vars.local_wires[self.wires_equality_dummy_for_round_r(r)]; + let insert_here = vars.local_wires[self.wires_insert_here_for_round_r(r)]; + + // The two equality constraints. + let prod = builder.mul_extension(difference, equality_dummy); + let one = builder.constant_extension(F::Extension::ONE); + let not_insert_here = builder.sub_extension(one, insert_here); + let first_equality_constraint = builder.sub_extension(prod, not_insert_here); + constraints.push(first_equality_constraint); + + let second_equality_constraint = builder.mul_extension(insert_here, difference); + constraints.push(second_equality_constraint); + + let mut new_item = builder.scalar_mul_ext_algebra(insert_here, element_to_insert); + if r > 0 { + let to_add = builder.scalar_mul_ext_algebra(already_inserted, list_items[r - 1]); + new_item = builder.add_ext_algebra(new_item, to_add); + } + if r < self.vec_size { + let not_already_inserted = builder.sub_extension(one, already_inserted); + let to_add = builder.scalar_mul_ext_algebra(not_already_inserted, list_items[r]); + new_item = builder.add_ext_algebra(new_item, to_add); + } + already_inserted = builder.add_extension(already_inserted, insert_here); + + // Output constraint. + let diff = builder.sub_ext_algebra(new_item, output_list_items[r]); + constraints.extend(diff.to_ext_target_array()); + } + + constraints } fn generators( From 220c9bc87a4832b9ef376c0e8654803087422609 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 17:38:24 -0700 Subject: [PATCH 27/30] fixed bug oops --- src/gates/insertion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index 8c91ced5..c98a86ca 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -103,10 +103,10 @@ impl, const D: usize> Gate for InsertionGate { if r > 0 { new_item += list_items[r - 1] * already_inserted.into(); } + already_inserted += insert_here; if r < self.vec_size { new_item += list_items[r] * (F::Extension::ONE - already_inserted).into(); } - already_inserted += insert_here; // Output constraint. constraints.extend((new_item - output_list_items[r]).to_basefield_array()); From ea07db14acb3965605235d5f0d99f150ca6f9b5a Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 17:38:39 -0700 Subject: [PATCH 28/30] fixed in recursive version too --- src/gates/insertion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gates/insertion.rs b/src/gates/insertion.rs index c98a86ca..80088955 100644 --- a/src/gates/insertion.rs +++ b/src/gates/insertion.rs @@ -154,12 +154,12 @@ impl, const D: usize> Gate for InsertionGate { let to_add = builder.scalar_mul_ext_algebra(already_inserted, list_items[r - 1]); new_item = builder.add_ext_algebra(new_item, to_add); } + already_inserted = builder.add_extension(already_inserted, insert_here); if r < self.vec_size { let not_already_inserted = builder.sub_extension(one, already_inserted); let to_add = builder.scalar_mul_ext_algebra(not_already_inserted, list_items[r]); new_item = builder.add_ext_algebra(new_item, to_add); } - already_inserted = builder.add_extension(already_inserted, insert_here); // Output constraint. let diff = builder.sub_ext_algebra(new_item, output_list_items[r]); From d3127c73d309f0bde723ff4ff06a13a10e652b8d Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 14 Jul 2021 23:54:22 -0700 Subject: [PATCH 29/30] insertion gadget (test fails) --- src/gadgets/insert.rs | 81 +++++++++++++------------------------------ 1 file changed, 25 insertions(+), 56 deletions(-) diff --git a/src/gadgets/insert.rs b/src/gadgets/insert.rs index 734b6c03..0d0f3f37 100644 --- a/src/gadgets/insert.rs +++ b/src/gadgets/insert.rs @@ -1,42 +1,12 @@ +use std::marker::PhantomData; + use crate::circuit_builder::CircuitBuilder; use crate::field::extension_field::target::ExtensionTarget; use crate::field::extension_field::Extendable; -use crate::generator::NonzeroTestGenerator; +use crate::gates::insertion::InsertionGate; use crate::target::Target; impl, const D: usize> CircuitBuilder { - /// Evaluates to 0 if `x` equals zero, 1 otherwise. - /// From section 2 of https://github.com/mir-protocol/r1cs-workshop/blob/master/workshop.pdf, - /// based on an idea from https://eprint.iacr.org/2012/598.pdf. - pub fn is_nonzero(&mut self, x: Target) -> Target { - // Dummy variable. - let m = self.add_virtual_target(); - - // The prover sets this the dummy variable to 1/x if x != 0, or to an arbitrary value if - // x == 0. - self.add_generator(NonzeroTestGenerator { - to_test: x, - dummy: m, - }); - - // Evaluates to (0) * (0) = 0 if x == 0 and (x) * (1/x) = 1 otherwise. - let y = self.mul(x, m); - - // Enforce that (1 - y) * x == 0. - let prod = self.arithmetic(F::NEG_ONE, x, y, F::ONE, x); - self.assert_zero(prod); - - y - } - - /// Evaluates to 1 if `x` and `y` are equal, 0 otherwise. - pub fn is_equal(&mut self, x: Target, y: Target) -> Target { - let difference = self.sub(x, y); - let not_equal = self.is_nonzero(difference); - let one = self.one(); - self.sub(one, not_equal) - } - /// Inserts a `Target` in a vector at a non-deterministic index. /// Note: `index` is not range-checked. pub fn insert( @@ -45,32 +15,31 @@ impl, const D: usize> CircuitBuilder { element: ExtensionTarget, v: Vec>, ) -> Vec> { - let mut already_inserted = self.zero(); - let mut new_list = Vec::new(); + let gate = InsertionGate:: { + vec_size: v.len(), + _phantom: PhantomData, + }; + let gate_index = + self.add_gate_no_constants(InsertionGate::new(v.len())); + + v.iter().enumerate().map(|(i, &val)| { + self.route_extension( + val, + ExtensionTarget::from_range(gate_index, gate.wires_original_list_item(i)), + ); + }); + self.route(index, Target::wire(gate_index, gate.wires_insertion_index())); + self.route_extension( + element, + ExtensionTarget::from_range(gate_index, gate.wires_element_to_insert()), + ); - let one = self.one(); - for i in 0..=v.len() { - let cur_index = self.constant(F::from_canonical_usize(i)); - let insert_here = self.is_equal(cur_index, index); - - let mut new_item = self.zero_extension(); - new_item = self.scalar_mul_add_extension(insert_here, element, new_item); - if i > 0 { - new_item = self.scalar_mul_add_extension(already_inserted, v[i - 1], new_item); - } - already_inserted = self.add(already_inserted, insert_here); - - let not_already_inserted = self.sub(one, already_inserted); - if i < v.len() { - new_item = self.scalar_mul_add_extension(not_already_inserted, v[i], new_item); - } - - new_list.push(new_item); - } - - new_list + (0..=v.len()).map(|i| { + ExtensionTarget::from_range(gate_index, gate.wires_output_list_item(i)) + }).collect::>() } } + #[cfg(test)] mod tests { use super::*; From 6b214410f297a5f6dec63575df5ded337b8f45fc Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 15 Jul 2021 13:57:50 -0700 Subject: [PATCH 30/30] fixes --- src/circuit_data.rs | 2 +- src/gadgets/insert.rs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/circuit_data.rs b/src/circuit_data.rs index 10c5ffe0..aa3b4208 100644 --- a/src/circuit_data.rs +++ b/src/circuit_data.rs @@ -54,7 +54,7 @@ impl CircuitConfig { pub(crate) fn large_config() -> Self { Self { num_wires: 134, - num_routed_wires: 28, + num_routed_wires: 34, security_bits: 128, rate_bits: 3, num_challenges: 3, diff --git a/src/gadgets/insert.rs b/src/gadgets/insert.rs index 0d0f3f37..f790d953 100644 --- a/src/gadgets/insert.rs +++ b/src/gadgets/insert.rs @@ -19,24 +19,26 @@ impl, const D: usize> CircuitBuilder { vec_size: v.len(), _phantom: PhantomData, }; - let gate_index = - self.add_gate_no_constants(InsertionGate::new(v.len())); - - v.iter().enumerate().map(|(i, &val)| { + let gate_index = self.add_gate_no_constants(InsertionGate::new(v.len())); + + v.iter().enumerate().for_each(|(i, &val)| { self.route_extension( val, ExtensionTarget::from_range(gate_index, gate.wires_original_list_item(i)), ); }); - self.route(index, Target::wire(gate_index, gate.wires_insertion_index())); + self.route( + index, + Target::wire(gate_index, gate.wires_insertion_index()), + ); self.route_extension( element, ExtensionTarget::from_range(gate_index, gate.wires_element_to_insert()), ); - (0..=v.len()).map(|i| { - ExtensionTarget::from_range(gate_index, gate.wires_output_list_item(i)) - }).collect::>() + (0..=v.len()) + .map(|i| ExtensionTarget::from_range(gate_index, gate.wires_output_list_item(i))) + .collect::>() } }