mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-26 11:06:00 +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},
|
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
/// Returns a negated representation of `self` in the constraint system.
|
/// Returns a negated representation of `self` in the constraint system.
|
||||||
pub trait Neg<F: Field>
|
pub trait Neg<F: Field>
|
||||||
where
|
where
|
||||||
@ -40,8 +42,9 @@ impl<F: Field> Neg<F> for Vec<Boolean> {
|
|||||||
let flipped: Self = self.iter().map(|bit| bit.not()).collect();
|
let flipped: Self = self.iter().map(|bit| bit.not()).collect();
|
||||||
|
|
||||||
// add one
|
// add one
|
||||||
let mut one = vec![Boolean::constant(true)];
|
let mut one = Vec::with_capacity(self.len());
|
||||||
one.append(&mut vec![Boolean::Constant(false); self.len() - 1]);
|
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 mut bits = flipped.add_bits(cs.ns(|| "add one"), &one)?;
|
||||||
let _carry = bits.pop(); // we already accounted for overflow above
|
let _carry = bits.pop(); // we already accounted for overflow above
|
||||||
|
Loading…
Reference in New Issue
Block a user