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
167
168
169
170
use bellperson::{ConstraintSystem, SynthesisError};
use fil_sapling_crypto::circuit::boolean::Boolean;
use fil_sapling_crypto::circuit::{num, pedersen_hash};
use fil_sapling_crypto::jubjub::JubjubEngine;
use crate::crypto::pedersen::PEDERSEN_BLOCK_SIZE;
pub fn pedersen_md_no_padding<E, CS>(
mut cs: CS,
params: &E::Params,
data: &[Boolean],
) -> Result<num::AllocatedNum<E>, SynthesisError>
where
E: JubjubEngine,
CS: ConstraintSystem<E>,
{
assert!(
data.len() >= 2 * PEDERSEN_BLOCK_SIZE,
"must be at least 2 block sizes long"
);
assert_eq!(
data.len() % PEDERSEN_BLOCK_SIZE,
0,
"data must be a multiple of the block size"
);
let mut chunks = data.chunks(PEDERSEN_BLOCK_SIZE);
let mut cur: Vec<Boolean> = chunks.nth(0).unwrap().to_vec();
let chunks_len = chunks.len();
for (i, block) in chunks.enumerate() {
let mut cs = cs.namespace(|| format!("block {}", i));
for b in block {
cur.push(b.clone());
}
if i == chunks_len - 1 {
} else {
cur = pedersen_compression(cs.namespace(|| "hash"), params, &cur)?;
}
}
pedersen_compression_num(cs.namespace(|| "last hash"), params, &cur)
}
pub fn pedersen_compression_num<E: JubjubEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
params: &E::Params,
bits: &[Boolean],
) -> Result<num::AllocatedNum<E>, SynthesisError> {
Ok(pedersen_hash::pedersen_hash(
cs.namespace(|| "inner hash"),
pedersen_hash::Personalization::NoteCommitment,
&bits,
params,
)?
.get_x()
.clone())
}
pub fn pedersen_compression<E: JubjubEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
params: &E::Params,
bits: &[Boolean],
) -> Result<Vec<Boolean>, SynthesisError> {
let h = pedersen_compression_num(cs.namespace(|| "compression"), params, bits)?;
let mut out = h.into_bits_le(cs.namespace(|| "h into bits"))?;
while out.len() < PEDERSEN_BLOCK_SIZE {
out.push(Boolean::Constant(false));
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::circuit::test::TestConstraintSystem;
use crate::crypto;
use crate::util::bytes_into_boolean_vec;
use bellperson::ConstraintSystem;
use fil_sapling_crypto::circuit::boolean::Boolean;
use fil_sapling_crypto::jubjub::JubjubBls12;
use paired::bls12_381::Bls12;
use rand::{Rng, SeedableRng, XorShiftRng};
#[test]
fn test_pedersen_single_input_circut() {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let cases = [(32, 697), (64, 1384)];
for (bytes, constraints) in &cases {
let mut cs = TestConstraintSystem::<Bls12>::new();
let data: Vec<u8> = (0..*bytes).map(|_| rng.gen()).collect();
let params = &JubjubBls12::new();
let data_bits: Vec<Boolean> = {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len()).unwrap()
};
let out = pedersen_compression_num(&mut cs, params, &data_bits)
.expect("pedersen hashing failed");
assert!(cs.is_satisfied(), "constraints not satisfied");
assert_eq!(
cs.num_constraints(),
*constraints,
"constraint size changed for {} bytes",
*bytes
);
let expected = crypto::pedersen::pedersen(data.as_slice());
assert_eq!(
expected,
out.get_value().unwrap(),
"circuit and non circuit do not match"
);
}
}
#[test]
fn test_pedersen_md_input_circut() {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let cases = [
(64, 1384),
(96, 2767),
(128, 4150),
(160, 5533),
(512, 20746),
];
for (bytes, constraints) in &cases {
let mut cs = TestConstraintSystem::<Bls12>::new();
let data: Vec<u8> = (0..*bytes).map(|_| rng.gen()).collect();
let params = &JubjubBls12::new();
let data_bits: Vec<Boolean> = {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len()).unwrap()
};
let out = pedersen_md_no_padding(cs.namespace(|| "pedersen"), params, &data_bits)
.expect("pedersen hashing failed");
assert!(cs.is_satisfied(), "constraints not satisfied");
assert_eq!(
cs.num_constraints(),
*constraints,
"constraint size changed {}",
bytes
);
let expected = crypto::pedersen::pedersen_md_no_padding(data.as_slice());
assert_eq!(
expected,
out.get_value().unwrap(),
"circuit and non circuit do not match"
);
}
}
}