mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-25 18:42:26 +03:00
perf: reduce allocations in the Neg impl for Vec<Boolean>
Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
parent
1e25738890
commit
55d0f2e340
@ -22,6 +22,8 @@ use snarkos_models::{
|
||||
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
|
||||
};
|
||||
|
||||
use std::iter;
|
||||
|
||||
/// Returns a negated representation of `self` in the constraint system.
|
||||
pub trait Neg<F: Field>
|
||||
where
|
||||
@ -40,8 +42,9 @@ impl<F: Field> Neg<F> for Vec<Boolean> {
|
||||
let flipped: Self = self.iter().map(|bit| bit.not()).collect();
|
||||
|
||||
// add one
|
||||
let mut one = vec![Boolean::constant(true)];
|
||||
one.append(&mut vec![Boolean::Constant(false); self.len() - 1]);
|
||||
let mut one = Vec::with_capacity(self.len());
|
||||
one.push(Boolean::constant(true));
|
||||
one.extend(iter::repeat(Boolean::Constant(false)).take(self.len() - 1));
|
||||
|
||||
let mut bits = flipped.add_bits(cs.ns(|| "add one"), &one)?;
|
||||
let _carry = bits.pop(); // we already accounted for overflow above
|
||||
|
Loading…
Reference in New Issue
Block a user