Skip to content

Commit

Permalink
Refactor to use bellpepper's new conditional selects (#355)
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 authored Mar 4, 2024
1 parent 28ab1e6 commit 4cb69de
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

1 comment on commit 4cb69de

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Arecibo GPU benchmarks.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/arecibo/actions/runs/8135323280

Benchmark Results

RecursiveSNARK-NIVC-2

ref=28ab1e6 ref=4cb69de
Prove-NumCons-6540 44.32 ms (✅ 1.00x) 44.16 ms (✅ 1.00x faster)
Verify-NumCons-6540 34.55 ms (✅ 1.00x) 34.22 ms (✅ 1.01x faster)
Prove-NumCons-1028888 319.15 ms (✅ 1.00x) 318.95 ms (✅ 1.00x faster)
Verify-NumCons-1028888 249.07 ms (✅ 1.00x) 251.05 ms (✅ 1.01x slower)

CompressedSNARK-NIVC-Commitments-2

ref=28ab1e6 ref=4cb69de
Prove-NumCons-6540 10.43 s (✅ 1.00x) 10.56 s (✅ 1.01x slower)
Verify-NumCons-6540 50.51 ms (✅ 1.00x) 50.68 ms (✅ 1.00x slower)
Prove-NumCons-1028888 52.05 s (✅ 1.00x) 54.61 s (✅ 1.05x slower)
Verify-NumCons-1028888 50.53 ms (✅ 1.00x) 50.70 ms (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.