mirror of https://github.com/zcash/orchard.git
Simplify `gen_const_array` implementation
Also includes a performance improvement to `i2lebsp_k`.
This commit is contained in:
parent
0e9726ae69
commit
d8f2af8983
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue