Skip to content

Commit

Permalink
fix: fix rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenciak committed Mar 7, 2024
1 parent 0fafbeb commit f912350
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/cyclefold/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use neptune::{circuit2::poseidon_hash_allocated, poseidon::PoseidonConstants};

use crate::{
constants::NUM_CHALLENGE_BITS,
gadgets::{alloc_zero, conditionally_select, le_bits_to_num, AllocatedPoint},
gadgets::{alloc_zero, le_bits_to_num, AllocatedPoint},
traits::{commitment::CommitmentTrait, Engine},
Commitment,
};
use bellpepper::gadgets::boolean_utils::conditionally_select;

/// A structure containing the CycleFold circuit inputs and implementing the synthesize function
pub struct CyclefoldCircuit<E: Engine> {
Expand Down
6 changes: 3 additions & 3 deletions src/cyclefold/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<E: Engine> AllocatedCycleFoldData<E> {
}

pub mod emulated {
use bellpepper::gadgets::Assignment;
use bellpepper::gadgets::{boolean_utils::conditionally_select, Assignment};
use bellpepper_core::{
boolean::{AllocatedBit, Boolean},
num::AllocatedNum,
Expand All @@ -230,8 +230,8 @@ pub mod emulated {
use crate::{
constants::{NUM_CHALLENGE_BITS, NUM_FE_IN_EMULATED_POINT},
gadgets::{
alloc_zero, conditionally_select, conditionally_select_allocated_bit,
conditionally_select_bignat, f_to_nat, le_bits_to_num, BigNat,
alloc_zero, conditionally_select_allocated_bit, conditionally_select_bignat, f_to_nat,
le_bits_to_num, BigNat,
},
traits::{commitment::CommitmentTrait, Engine, Group, ROCircuitTrait, ROConstantsCircuit},
RelaxedR1CSInstance,
Expand Down
8 changes: 5 additions & 3 deletions src/cyclefold/nova_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
constants::{BN_N_LIMBS, NIO_CYCLE_FOLD, NUM_FE_IN_EMULATED_POINT, NUM_HASH_BITS},
gadgets::{
alloc_num_equals, alloc_scalar_as_base, alloc_zero, conditionally_select_vec, le_bits_to_num,
alloc_num_equals, alloc_scalar_as_base, alloc_zero, le_bits_to_num,
AllocatedRelaxedR1CSInstance,
},
traits::{
Expand All @@ -13,7 +13,9 @@ use crate::{
};

use abomonation_derive::Abomonation;
use bellpepper::gadgets::{boolean::Boolean, num::AllocatedNum, Assignment};
use bellpepper::gadgets::{
boolean::Boolean, boolean_utils::conditionally_select_slice, num::AllocatedNum, Assignment,
};
use bellpepper_core::{boolean::AllocatedBit, ConstraintSystem, SynthesisError};
use ff::Field;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -460,7 +462,7 @@ where
);

// 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
3 changes: 2 additions & 1 deletion src/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ mod utils;
#[cfg(test)]
pub(crate) use utils::alloc_one;
pub(crate) use utils::{
alloc_num_equals, alloc_scalar_as_base, alloc_zero, le_bits_to_num, scalar_as_base,
alloc_bignat_constant, alloc_num_equals, alloc_scalar_as_base, alloc_zero,
conditionally_select_allocated_bit, conditionally_select_bignat, le_bits_to_num, scalar_as_base,
};
27 changes: 27 additions & 0 deletions src/gadgets/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,33 @@ pub fn alloc_num_equals<F: PrimeField, CS: ConstraintSystem<F>>(
Ok(r)
}

// TODO: Figure out if this can be done better
pub fn conditionally_select_allocated_bit<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
a: &AllocatedBit,
b: &AllocatedBit,
condition: &Boolean,
) -> Result<AllocatedBit, SynthesisError> {
let c = AllocatedBit::alloc(
cs.namespace(|| "conditionally select result"),
if condition.get_value().unwrap_or(false) {
a.get_value()
} else {
b.get_value()
},
)?;

// 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 where a and b are `BigNats`
pub fn conditionally_select_bignat<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
Expand Down

0 comments on commit f912350

Please sign in to comment.