1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::utilities::{
alloc::AllocGadget,
eq::{ConditionalEqGadget, EqGadget},
select::CondSelectGadget,
uint::{UInt, UInt8},
ToBytesGadget,
};
use snarkvm_algorithms::traits::CRH;
use snarkvm_fields::{Field, PrimeField};
use snarkvm_r1cs::{errors::SynthesisError, ConstraintSystem};
use std::fmt::Debug;
pub trait CRHGadget<H: CRH, F: Field>: Sized + Clone {
type ParametersGadget: AllocGadget<H::Parameters, F> + Clone;
type OutputGadget: ConditionalEqGadget<F>
+ EqGadget<F>
+ ToBytesGadget<F>
+ CondSelectGadget<F>
+ AllocGadget<H::Output, F>
+ Debug
+ Clone
+ Sized;
fn check_evaluation_gadget<CS: ConstraintSystem<F>>(
cs: CS,
parameters: &Self::ParametersGadget,
input: Vec<UInt8>,
) -> Result<Self::OutputGadget, SynthesisError>;
}
pub trait MaskedCRHGadget<H: CRH, F: PrimeField>: CRHGadget<H, F> {
fn extend_mask<CS: ConstraintSystem<F>>(_: CS, mask: &[UInt8]) -> Result<Vec<UInt8>, SynthesisError> {
let extended_mask = mask
.iter()
.flat_map(|m| {
m.to_bits_le()
.chunks(4)
.map(|c| {
let new_byte = c.iter().flat_map(|b| vec![*b, b.not()]).collect::<Vec<_>>();
UInt8::from_bits_le(&new_byte)
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
Ok(extended_mask)
}
fn check_evaluation_gadget_masked<CS: ConstraintSystem<F>>(
cs: CS,
parameters: &Self::ParametersGadget,
input: Vec<UInt8>,
mask_parameters: &Self::ParametersGadget,
mask: Vec<UInt8>,
) -> Result<Self::OutputGadget, SynthesisError>;
}