Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use bellpepper's new conditional selects #355

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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