Skip to content

Commit

Permalink
Refactor to use bellpepper's new conditional selects
Browse files Browse the repository at this point in the history
- 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`.
  • Loading branch information
huitseeker committed Mar 1, 2024
1 parent 28ab1e6 commit a4fc35f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 61 deletions.
8 changes: 4 additions & 4 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -318,7 +318,7 @@ impl<'a, E: Engine, SC: StepCircuit<E::Base>> 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,
Expand Down
8 changes: 4 additions & 4 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
8 changes: 5 additions & 3 deletions src/gadgets/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down
44 changes: 0 additions & 44 deletions src/gadgets/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,49 +169,6 @@ pub fn alloc_num_equals<F: PrimeField, CS: ConstraintSystem<F>>(
Ok(r)
}

/// If condition return a otherwise b
pub fn conditionally_select<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
a: &AllocatedNum<F>,
b: &AllocatedNum<F>,
condition: &Boolean,
) -> Result<AllocatedNum<F>, 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<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
a: &[AllocatedNum<F>],
b: &[AllocatedNum<F>],
condition: &Boolean,
) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
a.iter()
.zip_eq(b.iter())
.enumerate()
.map(|(i, (a, b))| {
conditionally_select(cs.namespace(|| format!("select_{i}")), a, b, condition)
})
.collect::<Result<Vec<AllocatedNum<F>>, SynthesisError>>()
}

/// If condition return a otherwise b where a and b are `BigNats`
pub fn conditionally_select_bignat<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
Expand Down
8 changes: 4 additions & 4 deletions src/supernova/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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};
Expand Down Expand Up @@ -619,7 +619,7 @@ impl<'a, E: Engine, SC: EnforcingStepCircuit<E::Base>> 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,
Expand Down

0 comments on commit a4fc35f

Please sign in to comment.