Simplify `gen_const_array` implementation

Also includes a performance improvement to `i2lebsp_k`.
This commit is contained in:
str4d 2021-06-11 20:20:41 +01:00 committed by Jack Grigg
parent 0e9726ae69
commit d8f2af8983
3 changed files with 6 additions and 16 deletions

View File

@ -42,15 +42,11 @@ pub fn evaluate<C: CurveAffine>(x: u8, coeffs: &[C::Base]) -> C::Base {
/// Takes in an FnMut closure and returns a constant-length array with elements of
/// type `Output`.
pub fn gen_const_array<Input, Output: Copy + Default, const LEN: usize>(
mut input: Input,
mut closure: impl FnMut(&mut Input, usize) -> Output,
pub fn gen_const_array<Output: Copy + Default, const LEN: usize>(
mut closure: impl FnMut(usize) -> Output,
) -> [Output; LEN] {
let mut ret: [Output; LEN] = [Default::default(); LEN];
for (bit, val) in ret
.iter_mut()
.zip((0..LEN).map(|idx| closure(&mut input, idx)))
{
for (bit, val) in ret.iter_mut().zip((0..LEN).map(|idx| closure(idx))) {
*bit = val;
}
ret

View File

@ -25,9 +25,7 @@ fn lebs2ip_k(bits: &[bool]) -> u32 {
/// up to `2^K` - 1.
pub fn i2lebsp_k(int: usize) -> [bool; K] {
assert!(int < (1 << K));
gen_const_array(int, |int: &mut usize, mask: usize| {
((*int & (1 << mask)) >> mask) == 1
})
gen_const_array(|mask: usize| (int & (1 << mask)) != 0)
}
/// Pads the given iterator (which MUST have length $\leq K * C$) with zero-bits to a

View File

@ -29,14 +29,10 @@ pub struct MerklePath {
impl MerklePath {
/// Generates a dummy Merkle path for use in dummy spent notes.
pub(crate) fn dummy(rng: &mut impl RngCore) -> Self {
fn dummy_inner(rng: &mut impl RngCore, _idx: usize) -> pallas::Base {
pallas::Base::random(rng)
}
pub(crate) fn dummy(mut rng: &mut impl RngCore) -> Self {
MerklePath {
position: rng.next_u32(),
auth_path: gen_const_array(rng, dummy_inner),
auth_path: gen_const_array(|_| pallas::Base::random(&mut rng)),
}
}