From a4fc35f3eb2eaf98808e708d70767470690ef214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Thu, 29 Feb 2024 18:11:19 -0500 Subject: [PATCH] Refactor to use bellpepper's new conditional selects - Function `conditionally_select_vec` is removed and replaced with `conditionally_select_slice` across multiple files for consistency. - Duplicate functions `conditionally_select` and `conditionally_select_vec` are removed from various modules. - Unused import, `itertools::Itertools`, removed from `src/gadgets/utils.rs`. --- src/circuit.rs | 8 ++++---- src/gadgets/ecc.rs | 8 ++++---- src/gadgets/mod.rs | 3 +-- src/gadgets/r1cs.rs | 8 +++++--- src/gadgets/utils.rs | 44 ---------------------------------------- src/supernova/circuit.rs | 8 ++++---- 6 files changed, 18 insertions(+), 61 deletions(-) diff --git a/src/circuit.rs b/src/circuit.rs index cfeb027f1..b5f4473b0 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -7,8 +7,8 @@ use crate::{ constants::{NIO_NOVA_FOLD, NUM_FE_WITHOUT_IO_FOR_CRHF, NUM_HASH_BITS}, gadgets::{ - alloc_num_equals, alloc_scalar_as_base, alloc_zero, conditionally_select_vec, le_bits_to_num, - AllocatedPoint, AllocatedR1CSInstance, AllocatedRelaxedR1CSInstance, + alloc_num_equals, alloc_scalar_as_base, alloc_zero, le_bits_to_num, AllocatedPoint, + AllocatedR1CSInstance, AllocatedRelaxedR1CSInstance, }, r1cs::{R1CSInstance, RelaxedR1CSInstance}, traits::{ @@ -17,7 +17,7 @@ use crate::{ Commitment, }; use abomonation_derive::Abomonation; -use bellpepper::gadgets::Assignment; +use bellpepper::gadgets::{boolean_utils::conditionally_select_slice, Assignment}; use bellpepper_core::{ boolean::{AllocatedBit, Boolean}, num::AllocatedNum, @@ -318,7 +318,7 @@ impl<'a, E: Engine, SC: StepCircuit> NovaAugmentedCircuit<'a, E, SC> { ); // Compute z_{i+1} - let z_input = conditionally_select_vec( + let z_input = conditionally_select_slice( cs.namespace(|| "select input to F"), &z_0, &z_i, diff --git a/src/gadgets/ecc.rs b/src/gadgets/ecc.rs index 0ebef4c4b..5d1e5d250 100644 --- a/src/gadgets/ecc.rs +++ b/src/gadgets/ecc.rs @@ -2,13 +2,13 @@ #![allow(non_snake_case)] use crate::{ gadgets::utils::{ - alloc_num_equals, alloc_one, alloc_zero, conditionally_select, conditionally_select2, - select_num_or_one, select_num_or_zero, select_num_or_zero2, select_one_or_diff2, - select_one_or_num2, select_zero_or_num2, + alloc_num_equals, alloc_one, alloc_zero, conditionally_select2, select_num_or_one, + select_num_or_zero, select_num_or_zero2, select_one_or_diff2, select_one_or_num2, + select_zero_or_num2, }, traits::Group, }; -use bellpepper::gadgets::Assignment; +use bellpepper::gadgets::{boolean_utils::conditionally_select, Assignment}; use bellpepper_core::{ boolean::{AllocatedBit, Boolean}, num::AllocatedNum, diff --git a/src/gadgets/mod.rs b/src/gadgets/mod.rs index 926d1ba46..7022edcf2 100644 --- a/src/gadgets/mod.rs +++ b/src/gadgets/mod.rs @@ -15,6 +15,5 @@ mod utils; #[cfg(test)] pub(crate) use utils::alloc_one; pub(crate) use utils::{ - alloc_num_equals, alloc_scalar_as_base, alloc_zero, conditionally_select_vec, le_bits_to_num, - scalar_as_base, + alloc_num_equals, alloc_scalar_as_base, alloc_zero, le_bits_to_num, scalar_as_base, }; diff --git a/src/gadgets/r1cs.rs b/src/gadgets/r1cs.rs index a93a6e474..85b87f7c2 100644 --- a/src/gadgets/r1cs.rs +++ b/src/gadgets/r1cs.rs @@ -8,14 +8,16 @@ use crate::{ gadgets::{ ecc::AllocatedPoint, utils::{ - alloc_bignat_constant, alloc_one, alloc_scalar_as_base, conditionally_select, - conditionally_select_bignat, le_bits_to_num, + alloc_bignat_constant, alloc_one, alloc_scalar_as_base, conditionally_select_bignat, + le_bits_to_num, }, }, r1cs::{R1CSInstance, RelaxedR1CSInstance}, traits::{commitment::CommitmentTrait, Engine, Group, ROCircuitTrait, ROConstantsCircuit}, }; -use bellpepper::gadgets::{boolean::Boolean, num::AllocatedNum, Assignment}; +use bellpepper::gadgets::{ + boolean::Boolean, boolean_utils::conditionally_select, num::AllocatedNum, Assignment, +}; use bellpepper_core::{ConstraintSystem, SynthesisError}; use ff::Field; use itertools::Itertools as _; diff --git a/src/gadgets/utils.rs b/src/gadgets/utils.rs index b768a8dd9..1b256c9d7 100644 --- a/src/gadgets/utils.rs +++ b/src/gadgets/utils.rs @@ -8,7 +8,6 @@ use bellpepper_core::{ ConstraintSystem, LinearCombination, SynthesisError, }; use ff::{Field, PrimeField, PrimeFieldBits}; -use itertools::Itertools as _; use num_bigint::BigInt; /// Gets as input the little indian representation of a number and spits out the number @@ -170,49 +169,6 @@ pub fn alloc_num_equals>( Ok(r) } -/// If condition return a otherwise b -pub fn conditionally_select>( - mut cs: CS, - a: &AllocatedNum, - b: &AllocatedNum, - condition: &Boolean, -) -> Result, SynthesisError> { - let c = AllocatedNum::alloc(cs.namespace(|| "conditional select result"), || { - if *condition.get_value().get()? { - Ok(*a.get_value().get()?) - } else { - Ok(*b.get_value().get()?) - } - })?; - - // a * condition + b*(1-condition) = c -> - // a * condition - b*condition = c - b - cs.enforce( - || "conditional select constraint", - |lc| lc + a.get_variable() - b.get_variable(), - |_| condition.lc(CS::one(), F::ONE), - |lc| lc + c.get_variable() - b.get_variable(), - ); - - Ok(c) -} - -/// If condition return a otherwise b -pub fn conditionally_select_vec>( - mut cs: CS, - a: &[AllocatedNum], - b: &[AllocatedNum], - condition: &Boolean, -) -> Result>, SynthesisError> { - a.iter() - .zip_eq(b.iter()) - .enumerate() - .map(|(i, (a, b))| { - conditionally_select(cs.namespace(|| format!("select_{i}")), a, b, condition) - }) - .collect::>, SynthesisError>>() -} - /// If condition return a otherwise b where a and b are `BigNats` pub fn conditionally_select_bignat>( mut cs: CS, diff --git a/src/supernova/circuit.rs b/src/supernova/circuit.rs index d287bf3d2..b0cff4743 100644 --- a/src/supernova/circuit.rs +++ b/src/supernova/circuit.rs @@ -15,8 +15,8 @@ use crate::{ constants::{NIO_NOVA_FOLD, NUM_HASH_BITS}, gadgets::{ alloc_num_equals, alloc_scalar_as_base, alloc_zero, conditionally_select_alloc_relaxed_r1cs, - conditionally_select_vec, conditionally_select_vec_allocated_relaxed_r1cs_instance, - le_bits_to_num, AllocatedPoint, AllocatedR1CSInstance, AllocatedRelaxedR1CSInstance, + conditionally_select_vec_allocated_relaxed_r1cs_instance, le_bits_to_num, AllocatedPoint, + AllocatedR1CSInstance, AllocatedRelaxedR1CSInstance, }, r1cs::{R1CSInstance, RelaxedR1CSInstance}, traits::{commitment::CommitmentTrait, Engine, ROCircuitTrait, ROConstantsCircuit}, @@ -28,7 +28,7 @@ use bellpepper_core::{ ConstraintSystem, SynthesisError, }; -use bellpepper::gadgets::Assignment; +use bellpepper::gadgets::{boolean_utils::conditionally_select_slice, Assignment}; use abomonation_derive::Abomonation; use ff::{Field, PrimeField}; @@ -619,7 +619,7 @@ impl<'a, E: Engine, SC: EnforcingStepCircuit> SuperNovaAugmentedCircuit ); // Compute z_{i+1} - let z_input = conditionally_select_vec( + let z_input = conditionally_select_slice( cs.namespace(|| "select input to F"), &z_0, &z_i,