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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::{
algorithms::crh::BHPCRHGadget,
integers::uint::UInt8,
traits::{
algorithms::CommitmentGadget,
alloc::AllocGadget,
curves::CompressedGroupGadget,
integers::integer::Integer,
},
ToBitsLEGadget,
ToBytesGadget,
};
use snarkvm_algorithms::{commitment::BHPCommitment, CommitmentScheme};
use snarkvm_curves::ProjectiveCurve;
use snarkvm_fields::PrimeField;
use snarkvm_r1cs::{errors::SynthesisError, ConstraintSystem};
use snarkvm_utilities::{to_bytes_le, ToBytes};
use std::{borrow::Borrow, marker::PhantomData};
#[derive(Clone, Debug)]
pub struct BHPRandomnessGadget<G: ProjectiveCurve>(pub Vec<UInt8>, PhantomData<G>);
impl<G: ProjectiveCurve, F: PrimeField> AllocGadget<G::ScalarField, F> for BHPRandomnessGadget<G> {
fn alloc<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<G::ScalarField>, CS: ConstraintSystem<F>>(
cs: CS,
value_gen: Fn,
) -> Result<Self, SynthesisError> {
let randomness = to_bytes_le![value_gen()?.borrow()].unwrap();
Ok(Self(UInt8::alloc_vec(cs, &randomness)?, PhantomData))
}
fn alloc_input<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<G::ScalarField>, CS: ConstraintSystem<F>>(
cs: CS,
value_gen: Fn,
) -> Result<Self, SynthesisError> {
let randomness = to_bytes_le![value_gen()?.borrow()].unwrap();
Ok(Self(UInt8::alloc_input_vec_le(cs, &randomness)?, PhantomData))
}
}
impl<G: ProjectiveCurve, F: PrimeField> ToBytesGadget<F> for BHPRandomnessGadget<G> {
fn to_bytes<CS: ConstraintSystem<F>>(&self, _: CS) -> Result<Vec<UInt8>, SynthesisError> {
Ok(self.0.clone())
}
fn to_bytes_strict<CS: ConstraintSystem<F>>(&self, _: CS) -> Result<Vec<UInt8>, SynthesisError> {
Ok(self.0.clone())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BHPCommitmentGadget<
G: ProjectiveCurve,
F: PrimeField,
GG: CompressedGroupGadget<G, F>,
const NUM_WINDOWS: usize,
const WINDOW_SIZE: usize,
> {
bhp_crh_gadget: BHPCRHGadget<G, F, GG, NUM_WINDOWS, WINDOW_SIZE>,
random_base: Vec<G>,
}
impl<
G: ProjectiveCurve,
F: PrimeField,
GG: CompressedGroupGadget<G, F>,
const NUM_WINDOWS: usize,
const WINDOW_SIZE: usize,
> AllocGadget<BHPCommitment<G, NUM_WINDOWS, WINDOW_SIZE>, F>
for BHPCommitmentGadget<G, F, GG, NUM_WINDOWS, WINDOW_SIZE>
{
fn alloc_constant<
Fn: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<BHPCommitment<G, NUM_WINDOWS, WINDOW_SIZE>>,
CS: ConstraintSystem<F>,
>(
cs: CS,
value_gen: Fn,
) -> Result<Self, SynthesisError> {
let bhp: BHPCommitment<G, NUM_WINDOWS, WINDOW_SIZE> = value_gen()?.borrow().parameters().into();
Ok(Self {
bhp_crh_gadget: BHPCRHGadget::alloc_constant(cs, || Ok(bhp.bhp_crh.clone()))?,
random_base: bhp.random_base,
})
}
fn alloc<
Fn: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<BHPCommitment<G, NUM_WINDOWS, WINDOW_SIZE>>,
CS: ConstraintSystem<F>,
>(
_cs: CS,
_value_gen: Fn,
) -> Result<Self, SynthesisError> {
unimplemented!()
}
fn alloc_input<
Fn: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<BHPCommitment<G, NUM_WINDOWS, WINDOW_SIZE>>,
CS: ConstraintSystem<F>,
>(
_cs: CS,
_value_gen: Fn,
) -> Result<Self, SynthesisError> {
unimplemented!()
}
}
impl<
F: PrimeField,
G: ProjectiveCurve,
GG: CompressedGroupGadget<G, F>,
const NUM_WINDOWS: usize,
const WINDOW_SIZE: usize,
> CommitmentGadget<BHPCommitment<G, NUM_WINDOWS, WINDOW_SIZE>, F>
for BHPCommitmentGadget<G, F, GG, NUM_WINDOWS, WINDOW_SIZE>
{
type OutputGadget = GG::BaseFieldGadget;
type RandomnessGadget = BHPRandomnessGadget<G>;
fn randomness_from_bytes<CS: ConstraintSystem<F>>(
_cs: CS,
bytes: &[UInt8],
) -> Result<Self::RandomnessGadget, SynthesisError> {
Ok(BHPRandomnessGadget(bytes.to_vec(), PhantomData))
}
fn check_commitment_gadget<CS: ConstraintSystem<F>>(
&self,
mut cs: CS,
input: &[UInt8],
randomness: &Self::RandomnessGadget,
) -> Result<Self::OutputGadget, SynthesisError> {
assert!((input.len() * 8) <= (WINDOW_SIZE * NUM_WINDOWS));
let input = input.to_vec().to_bits_le(cs.ns(|| "to_bits"))?;
let mut result = self
.bhp_crh_gadget
.check_evaluation_gadget_on_bits_inner(cs.ns(|| "BHP hash"), input)?;
let rand_bits = randomness.0.iter().flat_map(|byte| byte.to_bits_le());
result.scalar_multiplication(cs.ns(|| "randomizer"), rand_bits.zip(&self.random_base))?;
Ok(result.to_x_coordinate())
}
}