constants::util.rs: Factor out gen_fixed_array() method.

This commit is contained in:
therealyingtong 2021-06-10 00:44:30 +08:00
parent 7818291118
commit 18535894d6
3 changed files with 28 additions and 17 deletions

View File

@ -40,6 +40,22 @@ pub fn evaluate<C: CurveAffine>(x: u8, coeffs: &[C::Base]) -> C::Base {
.fold(C::Base::default(), |acc, coeff| acc * x + coeff)
}
/// 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,
) -> [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)))
{
*bit = val;
}
ret
}
#[cfg(test)]
mod tests {
use super::decompose_scalar_fixed;

View File

@ -4,6 +4,7 @@ use halo2::arithmetic::CurveExt;
use pasta_curves::pallas;
use subtle::CtOption;
use crate::constants::util::gen_const_array;
use crate::spec::extract_p_bottom;
mod addition;
@ -24,15 +25,9 @@ fn lebs2ip_k(bits: &[bool]) -> u32 {
/// up to `2^K` - 1.
pub fn i2lebsp_k(int: usize) -> [bool; K] {
assert!(int < (1 << K));
let mut ret = [false; K];
for (bit, val) in ret
.iter_mut()
.zip((0..K).map(|mask| ((int & (1 << mask)) >> mask) == 1))
{
*bit = val;
}
ret
gen_const_array(int, |int: &mut usize, mask: usize| {
((*int & (1 << mask)) >> mask) == 1
})
}
/// Pads the given iterator (which MUST have length $\leq K * C$) with zero-bits to a

View File

@ -1,13 +1,13 @@
use crate::{
constants::{MERKLE_CRH_PERSONALIZATION, MERKLE_DEPTH_ORCHARD},
constants::{util::gen_const_array, MERKLE_CRH_PERSONALIZATION, MERKLE_DEPTH_ORCHARD},
note::commitment::ExtractedNoteCommitment,
primitives::sinsemilla::{i2lebsp_k, HashDomain, K},
};
use pasta_curves::{arithmetic::FieldExt, pallas};
use ff::{PrimeField, PrimeFieldBits};
use ff::{Field, PrimeField, PrimeFieldBits};
use rand::RngCore;
use std::{convert::TryInto, iter};
use std::iter;
/// The root of an Orchard commitment tree.
#[derive(Eq, PartialEq, Clone, Debug)]
@ -28,13 +28,13 @@ 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)
}
MerklePath {
position: rng.next_u32(),
auth_path: (0..MERKLE_DEPTH_ORCHARD)
.map(|_| pallas::Base::rand())
.collect::<Vec<_>>()
.try_into()
.unwrap(),
auth_path: gen_const_array(rng, dummy_inner),
}
}