-
Notifications
You must be signed in to change notification settings - Fork 21
/
gadget_poseidon.rs
906 lines (740 loc) · 31 KB
/
gadget_poseidon.rs
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
#![allow(non_snake_case)]
extern crate rand;
extern crate curve25519_dalek;
extern crate merlin;
extern crate bulletproofs;
//extern crate spock;
use curve25519_dalek::scalar::Scalar;
use bulletproofs::r1cs::{ConstraintSystem, R1CSError, R1CSProof, Variable, Prover, Verifier};
use bulletproofs::{BulletproofGens, PedersenGens};
use merlin::Transcript;
use bulletproofs::r1cs::LinearCombination;
use crate::r1cs_utils::{AllocatedScalar, constrain_lc_with_scalar};
use crate::gadget_zero_nonzero::is_nonzero_gadget;
use crate::poseidon_constants::{MDS_ENTRIES, ROUND_CONSTS};
use crate::scalar_utils::get_scalar_from_hex;
use rand::SeedableRng;
use rand::rngs::StdRng;
use curve25519_dalek::ristretto::CompressedRistretto;
use std::mem;
use std::collections::HashMap;
// TODO: Add serialization with serde
pub struct PoseidonParams {
pub width: usize,
// Number of full SBox rounds in beginning
pub full_rounds_beginning: usize,
// Number of full SBox rounds in end
pub full_rounds_end: usize,
// Number of partial SBox rounds in beginning
pub partial_rounds: usize,
pub round_keys: Vec<Scalar>,
pub MDS_matrix: Vec<Vec<Scalar>>
}
impl PoseidonParams {
pub fn new(width: usize, full_rounds_beginning: usize, full_rounds_end: usize, partial_rounds: usize) -> PoseidonParams {
let total_rounds = full_rounds_beginning + partial_rounds + full_rounds_end;
let round_keys = Self::gen_round_keys(width, total_rounds);
let matrix_2 = Self::gen_MDS_matrix(width);
PoseidonParams {
width,
full_rounds_beginning,
full_rounds_end,
partial_rounds,
round_keys,
MDS_matrix: matrix_2
}
}
// TODO: Write logic to generate correct round keys.
fn gen_round_keys(width: usize, total_rounds: usize) -> Vec<Scalar> {
let cap = total_rounds * width;
/*let mut test_rng: StdRng = SeedableRng::from_seed([24u8; 32]);
vec![Scalar::random(&mut test_rng); cap]*/
if ROUND_CONSTS.len() < cap {
panic!("Not enough round constants, need {}, found {}", cap, ROUND_CONSTS.len());
}
let mut rc = vec![];
for i in 0..cap {
// TODO: Remove unwrap, handle error
let c = get_scalar_from_hex(ROUND_CONSTS[i]).unwrap();
rc.push(c);
}
rc
}
// TODO: Write logic to generate correct MDS matrix. Currently loading hardcoded constants.
fn gen_MDS_matrix(width: usize) -> Vec<Vec<Scalar>> {
/*let mut test_rng: StdRng = SeedableRng::from_seed([24u8; 32]);
vec![vec![Scalar::random(&mut test_rng); width]; width]*/
if MDS_ENTRIES.len() != width {
panic!("Incorrect width, only width {} is supported now", width);
}
let mut mds: Vec<Vec<Scalar>> = vec![vec![Scalar::zero(); width]; width];
for i in 0..width {
if MDS_ENTRIES[i].len() != width {
panic!("Incorrect width, only width {} is supported now", width);
}
for j in 0..width {
// TODO: Remove unwrap, handle error
mds[i][j] = get_scalar_from_hex(MDS_ENTRIES[i][j]).unwrap();
}
}
mds
}
pub fn get_total_rounds(&self) -> usize {
self.full_rounds_beginning + self.partial_rounds + self.full_rounds_end
}
}
/// Simplify linear combination by taking Variables common across terms and adding their corresponding scalars.
/// Useful when linear combinations become large. Takes ownership of linear combination as this function is useful
/// when memory is limited and the obvious action after this function call will be to free the memory held by the passed linear combination
fn simplify_lc(lc: LinearCombination) -> LinearCombination {
// TODO: Move this code to the fork of bulletproofs
let mut vars: HashMap<Variable, Scalar> = HashMap::new();
let terms = lc.get_terms();
for (var, val) in terms {
*vars.entry(var).or_insert(Scalar::zero()) += val;
}
let mut new_lc_terms = vec![];
for (var, val) in vars {
new_lc_terms.push((var, val));
}
new_lc_terms.iter().collect()
}
pub enum SboxType {
Cube,
Inverse
}
impl SboxType {
fn apply_sbox(&self, elem: &Scalar) -> Scalar {
match self {
SboxType::Cube => (elem * elem) * elem,
SboxType::Inverse => elem.invert()
}
}
fn synthesize_sbox<CS: ConstraintSystem>(
&self,
cs: &mut CS,
input_var: LinearCombination,
round_key: Scalar
) -> Result<Variable, R1CSError> {
match self {
SboxType::Cube => Self::synthesize_cube_sbox(cs, input_var, round_key),
SboxType::Inverse => Self::synthesize_inverse_sbox(cs, input_var, round_key),
_ => Err(R1CSError::GadgetError {description: String::from("Unknown Sbox type")})
}
}
// Allocate variables in circuit and enforce constraints when Sbox as cube
fn synthesize_cube_sbox<CS: ConstraintSystem>(
cs: &mut CS,
input_var: LinearCombination,
round_key: Scalar
) -> Result<Variable, R1CSError> {
let inp_plus_const: LinearCombination = input_var + round_key;
let (i, _, sqr) = cs.multiply(inp_plus_const.clone(), inp_plus_const);
let (_, _, cube) = cs.multiply(sqr.into(), i.into());
Ok(cube)
}
// Allocate variables in circuit and enforce constraints when Sbox as inverse
fn synthesize_inverse_sbox<CS: ConstraintSystem>(
cs: &mut CS,
input_var: LinearCombination,
round_key: Scalar
) -> Result<Variable, R1CSError> {
let inp_plus_const: LinearCombination = input_var + round_key;
let val_l = cs.evaluate_lc(&inp_plus_const);
let val_r = val_l.map(|l| {
l.invert()
});
let (var_l, _) = cs.allocate_single(val_l)?;
let (var_r, var_o) = cs.allocate_single(val_r)?;
// Ensure `inp_plus_const` is not zero. As a side effect, `is_nonzero_gadget` also ensures that arguments passes are inverse of each other
is_nonzero_gadget(
cs,
AllocatedScalar {
variable: var_l,
assignment: val_l
},
AllocatedScalar {
variable: var_r,
assignment: val_r
}
)?;
// Constrain product of `inp_plus_const` and its inverse to be 1.
constrain_lc_with_scalar::<CS>(cs, var_o.unwrap().into(), &Scalar::one());
Ok(var_r)
}
}
fn Poseidon_permutation(
input: &[Scalar],
params: &PoseidonParams,
sbox: &SboxType
) -> Vec<Scalar>
{
let width = params.width;
assert_eq!(input.len(), width);
let full_rounds_beginning = params.full_rounds_beginning;
let partial_rounds = params.partial_rounds;
let full_rounds_end = params.full_rounds_end;
let mut current_state = input.to_owned();
let mut current_state_temp = vec![Scalar::zero(); width];
let mut round_keys_offset = 0;
// full Sbox rounds
for _ in 0..full_rounds_beginning {
// Sbox layer
for i in 0..width {
current_state[i] += params.round_keys[round_keys_offset];
current_state[i] = sbox.apply_sbox(¤t_state[i]);
round_keys_offset += 1;
}
// linear layer
for j in 0..width {
for i in 0..width {
current_state_temp[i] += current_state[j] * params.MDS_matrix[i][j];
}
}
// Output of this round becomes input to next round
for i in 0..width {
current_state[i] = current_state_temp[i];
current_state_temp[i] = Scalar::zero();
}
}
// middle partial Sbox rounds
for _ in full_rounds_beginning..(full_rounds_beginning+partial_rounds) {
for i in 0..width {
current_state[i] += ¶ms.round_keys[round_keys_offset];
round_keys_offset += 1;
}
// partial Sbox layer, apply Sbox to only 1 element of the state.
// Here the last one is chosen but the choice is arbitrary.
current_state[width-1] = sbox.apply_sbox(¤t_state[width-1]);
// linear layer
for j in 0..width {
for i in 0..width {
current_state_temp[i] += current_state[j] * params.MDS_matrix[i][j];
}
}
// Output of this round becomes input to next round
for i in 0..width {
current_state[i] = current_state_temp[i];
current_state_temp[i] = Scalar::zero();
}
}
// last full Sbox rounds
for _ in full_rounds_beginning+partial_rounds..(full_rounds_beginning+partial_rounds+full_rounds_end) {
// Sbox layer
for i in 0..width {
current_state[i] += params.round_keys[round_keys_offset];
current_state[i] = sbox.apply_sbox(¤t_state[i]);
round_keys_offset += 1;
}
// linear layer
for j in 0..width {
for i in 0..width {
current_state_temp[i] += current_state[j] * params.MDS_matrix[i][j];
}
}
// Output of this round becomes input to next round
for i in 0..width {
current_state[i] = current_state_temp[i];
current_state_temp[i] = Scalar::zero();
}
}
// Finally the current_state becomes the output
current_state
}
pub fn Poseidon_permutation_constraints<'a, CS: ConstraintSystem>(
cs: &mut CS,
input: Vec<LinearCombination>,
params: &'a PoseidonParams,
sbox_type: &SboxType
) -> Result<Vec<LinearCombination>, R1CSError> {
let width = params.width;
assert_eq!(input.len(), width);
fn apply_linear_layer(
width: usize,
sbox_outs: Vec<LinearCombination>,
next_inputs: &mut Vec<LinearCombination>,
MDS_matrix: &Vec<Vec<Scalar>>,
) {
for j in 0..width {
for i in 0..width {
next_inputs[i] = next_inputs[i].clone() + sbox_outs[j].clone() * MDS_matrix[i][j];
}
}
}
let mut input_vars: Vec<LinearCombination> = input;
let mut round_keys_offset = 0;
let full_rounds_beginning = params.full_rounds_beginning;
let partial_rounds = params.partial_rounds;
let full_rounds_end = params.full_rounds_end;
// ------------ First rounds with full SBox begin --------------------
for k in 0..full_rounds_beginning {
let mut sbox_outputs: Vec<LinearCombination> = vec![LinearCombination::default(); width];
// Substitution (S-box) layer
for i in 0..width {
let round_key = params.round_keys[round_keys_offset];
sbox_outputs[i] = sbox_type.synthesize_sbox(cs, input_vars[i].clone(), round_key)?.into();
round_keys_offset += 1;
}
let mut next_input_vars: Vec<LinearCombination> = vec![LinearCombination::default(); width];
apply_linear_layer(width, sbox_outputs, &mut next_input_vars, ¶ms.MDS_matrix);
for i in 0..width {
// replace input_vars with next_input_vars
input_vars[i] = next_input_vars.remove(0);
}
}
// ------------ First rounds with full SBox begin --------------------
// ------------ Middle rounds with partial SBox begin --------------------
for k in full_rounds_beginning..(full_rounds_beginning+partial_rounds) {
let mut sbox_outputs: Vec<LinearCombination> = vec![LinearCombination::default(); width];
// Substitution (S-box) layer
for i in 0..width {
let round_key = params.round_keys[round_keys_offset];
// apply Sbox to only 1 element of the state.
// Here the last one is chosen but the choice is arbitrary.
if i == width-1 {
sbox_outputs[i] = sbox_type.synthesize_sbox(cs, input_vars[i].clone(), round_key)?.into();
} else {
sbox_outputs[i] = input_vars[i].clone() + LinearCombination::from(round_key);
}
round_keys_offset += 1;
}
// Linear layer
let mut next_input_vars: Vec<LinearCombination> = vec![LinearCombination::default(); width];
apply_linear_layer(width, sbox_outputs, &mut next_input_vars, ¶ms.MDS_matrix);
for i in 0..width {
// replace input_vars with simplified next_input_vars
input_vars[i] = simplify_lc(next_input_vars.remove(0));
}
}
// ------------ Middle rounds with partial SBox end --------------------
// ------------ Last rounds with full SBox begin --------------------
for k in (full_rounds_beginning+partial_rounds)..(full_rounds_beginning+partial_rounds+full_rounds_end) {
let mut sbox_outputs: Vec<LinearCombination> = vec![LinearCombination::default(); width];
// Substitution (S-box) layer
for i in 0..width {
let round_key = params.round_keys[round_keys_offset];
sbox_outputs[i] = sbox_type.synthesize_sbox(cs, input_vars[i].clone(), round_key)?.into();
round_keys_offset += 1;
}
// Linear layer
let mut next_input_vars: Vec<LinearCombination> = vec![LinearCombination::default(); width];
apply_linear_layer(width, sbox_outputs, &mut next_input_vars, ¶ms.MDS_matrix);
for i in 0..width {
// replace input_vars with next_input_vars
input_vars[i] = next_input_vars.remove(0);
}
}
// ------------ Last rounds with full SBox end --------------------
Ok(input_vars)
}
pub fn Poseidon_permutation_gadget<'a, CS: ConstraintSystem>(
cs: &mut CS,
input: Vec<AllocatedScalar>,
params: &'a PoseidonParams,
sbox_type: &SboxType,
output: &[Scalar]
) -> Result<(), R1CSError> {
let width = params.width;
assert_eq!(output.len(), width);
let input_vars: Vec<LinearCombination> = input.iter().map(|e| e.variable.into()).collect();
let permutation_output = Poseidon_permutation_constraints::<CS>(cs, input_vars, params, sbox_type)?;
for i in 0..width {
constrain_lc_with_scalar::<CS>(cs, permutation_output[i].to_owned(), &output[i]);
}
Ok(())
}
/// 2:1 (2 inputs, 1 output) hash from the permutation by passing the first input as zero, 2 of the next 4 as non-zero, a padding constant and rest zero. Choose one of the outputs.
// Choice is arbitrary
pub const PADDING_CONST: u64 = 101;
pub const ZERO_CONST: u64 = 0;
pub fn Poseidon_hash_2(xl: Scalar, xr: Scalar, params: &PoseidonParams, sbox: &SboxType) -> Scalar {
// Only 2 inputs to the permutation are set to the input of this hash function,
// one is set to the padding constant and rest are 0. Always keep the 1st input as 0
let input = vec![
Scalar::from(ZERO_CONST),
xl,
xr,
Scalar::from(PADDING_CONST),
Scalar::from(ZERO_CONST),
Scalar::from(ZERO_CONST)
];
// Never take the first output
Poseidon_permutation(&input, params, sbox)[1]
}
pub fn Poseidon_hash_2_constraints<'a, CS: ConstraintSystem>(
cs: &mut CS,
xl: LinearCombination,
xr: LinearCombination,
statics: Vec<LinearCombination>,
params: &'a PoseidonParams,
sbox_type: &SboxType,
) -> Result<LinearCombination, R1CSError> {
let width = params.width;
// Only 2 inputs to the permutation are set to the input of this hash function.
assert_eq!(statics.len(), width-2);
// Always keep the 1st input as 0
let mut inputs = vec![statics[0].to_owned()];
inputs.push(xl);
inputs.push(xr);
// statics correspond to committed variables with values as PADDING_CONST and 0s and randomness as 0
for i in 1..statics.len() {
inputs.push(statics[i].to_owned());
}
let permutation_output = Poseidon_permutation_constraints::<CS>(cs, inputs, params, sbox_type)?;
Ok(permutation_output[1].to_owned())
}
pub fn Poseidon_hash_2_gadget<'a, CS: ConstraintSystem>(
cs: &mut CS,
xl: AllocatedScalar,
xr: AllocatedScalar,
statics: Vec<AllocatedScalar>,
params: &'a PoseidonParams,
sbox_type: &SboxType,
output: &Scalar
) -> Result<(), R1CSError> {
let statics: Vec<LinearCombination> = statics.iter().map(|s| s.variable.into()).collect();
let hash = Poseidon_hash_2_constraints::<CS>(cs, xl.variable.into(), xr.variable.into(), statics, params, sbox_type)?;
constrain_lc_with_scalar::<CS>(cs, hash, output);
Ok(())
}
pub fn Poseidon_hash_4(inputs: [Scalar; 4], params: &PoseidonParams, sbox: &SboxType) -> Scalar {
// Only 4 inputs to the permutation are set to the input of this hash function,
// one is set to the padding constant and one is set to 0. Always keep the 1st input as 0
let input = vec![
Scalar::from(ZERO_CONST),
inputs[0],
inputs[1],
inputs[2],
inputs[3],
Scalar::from(PADDING_CONST)
];
// Never take the first output
Poseidon_permutation(&input, params, sbox)[1]
}
pub fn Poseidon_hash_4_constraints<'a, CS: ConstraintSystem>(
cs: &mut CS,
input: [LinearCombination; 4],
statics: Vec<LinearCombination>,
params: &'a PoseidonParams,
sbox_type: &SboxType,
) -> Result<LinearCombination, R1CSError> {
let width = params.width;
// Only 4 inputs to the permutation are set to the input of this hash function.
assert_eq!(statics.len(), width-4);
// Always keep the 1st input as 0
let mut inputs = vec![statics[0].to_owned()];
inputs.push(input[0].clone());
inputs.push(input[1].clone());
inputs.push(input[2].clone());
inputs.push(input[3].clone());
// statics correspond to committed variables with values as PADDING_CONST and 0s and randomness as 0
for i in 1..statics.len() {
inputs.push(statics[i].to_owned());
}
let permutation_output = Poseidon_permutation_constraints::<CS>(cs, inputs, params, sbox_type)?;
Ok(permutation_output[1].to_owned())
}
pub fn Poseidon_hash_4_gadget<'a, CS: ConstraintSystem>(
cs: &mut CS,
input: Vec<AllocatedScalar>,
statics: Vec<AllocatedScalar>,
params: &'a PoseidonParams,
sbox_type: &SboxType,
output: &Scalar
) -> Result<(), R1CSError> {
let statics: Vec<LinearCombination> = statics.iter().map(|s| s.variable.into()).collect();
let mut input_arr: [LinearCombination; 4] = [LinearCombination::default(), LinearCombination::default(), LinearCombination::default(), LinearCombination::default()];
for i in 0..input.len() {
input_arr[i] = input[i].variable.into();
}
let hash = Poseidon_hash_4_constraints::<CS>(cs, input_arr, statics, params, sbox_type)?;
constrain_lc_with_scalar::<CS>(cs, hash, output);
Ok(())
}
/// Allocate padding constant and zeroes for Prover
pub fn allocate_statics_for_prover(prover: &mut Prover, num_statics: usize) -> Vec<AllocatedScalar> {
let mut statics = vec![];
let (_, var) = prover.commit(Scalar::from(ZERO_CONST), Scalar::zero());
statics.push(AllocatedScalar {
variable: var,
assignment: Some(Scalar::from(ZERO_CONST)),
});
// Commitment to PADDING_CONST with blinding as 0
let (_, var) = prover.commit(Scalar::from(PADDING_CONST), Scalar::zero());
statics.push(AllocatedScalar {
variable: var,
assignment: Some(Scalar::from(PADDING_CONST)),
});
// Commit to 0 with randomness 0 for the rest of the elements of width
for _ in 2..num_statics {
let (_, var) = prover.commit(Scalar::from(ZERO_CONST), Scalar::zero());
statics.push(AllocatedScalar {
variable: var,
assignment: Some(Scalar::from(ZERO_CONST)),
});
}
statics
}
/// Allocate padding constant and zeroes for Verifier
pub fn allocate_statics_for_verifier(verifier: &mut Verifier, num_statics: usize, pc_gens: &PedersenGens) -> Vec<AllocatedScalar> {
let mut statics = vec![];
// Commitment to PADDING_CONST with blinding as 0
let pad_comm = pc_gens.commit(Scalar::from(PADDING_CONST), Scalar::zero()).compress();
// Commitment to 0 with blinding as 0
let zero_comm = pc_gens.commit(Scalar::from(ZERO_CONST), Scalar::zero()).compress();
let v = verifier.commit(zero_comm.clone());
statics.push(AllocatedScalar {
variable: v,
assignment: None,
});
let v = verifier.commit(pad_comm);
statics.push(AllocatedScalar {
variable: v,
assignment: None,
});
for _ in 2..num_statics {
let v = verifier.commit(zero_comm.clone());
statics.push(AllocatedScalar {
variable: v,
assignment: None,
});
}
statics
}
#[cfg(test)]
mod tests {
use super::*;
// For benchmarking
use std::time::{Duration, Instant};
use std::sync::atomic::Ordering::SeqCst;
fn get_poseidon_params() -> PoseidonParams{
let width = 6;
let (full_b, full_e) = (4, 4);
let partial_rounds = 140;
PoseidonParams::new(width, full_b, full_e, partial_rounds)
}
fn poseidon_perm(sbox_type: &SboxType, transcript_label: &'static [u8]) {
let s_params = get_poseidon_params();
let width = s_params.width;
let total_rounds = s_params.get_total_rounds();
let mut test_rng: StdRng = SeedableRng::from_seed([24u8; 32]);
let input = (0..width).map(|_| Scalar::random(&mut test_rng)).collect::<Vec<_>>();
let expected_output = Poseidon_permutation(&input, &s_params, sbox_type);
/*println!("Input:\n");
println!("{:?}", &input);
println!("Expected output:\n");
println!("{:?}", &expected_output);*/
let pc_gens = PedersenGens::default();
let bp_gens = BulletproofGens::new(2048, 1);
println!("Proving");
let (proof, commitments) = {
let mut prover_transcript = Transcript::new(transcript_label);
let mut prover = Prover::new(&pc_gens, &mut prover_transcript);
let mut comms = vec![];
let mut allocs = vec![];
for i in 0..width {
let (com, var) = prover.commit(input[i].clone(), Scalar::random(&mut test_rng));
comms.push(com);
allocs.push(AllocatedScalar {
variable: var,
assignment: Some(input[i]),
});
}
assert!(Poseidon_permutation_gadget(&mut prover,
allocs,
&s_params,
sbox_type,
&expected_output).is_ok());
println!("For Poseidon permutation rounds {}, no of constraints is {}, no of multipliers is {}", total_rounds, &prover.num_constraints(), &prover.num_multipliers());
let proof = prover.prove(&bp_gens).unwrap();
(proof, comms)
};
println!("Verifying");
let mut verifier_transcript = Transcript::new(transcript_label);
let mut verifier = Verifier::new(&mut verifier_transcript);
let mut allocs = vec![];
for i in 0..width {
let v = verifier.commit(commitments[i]);
allocs.push(AllocatedScalar {
variable: v,
assignment: None,
});
}
assert!(Poseidon_permutation_gadget(&mut verifier,
allocs,
&s_params,
sbox_type,
&expected_output).is_ok());
assert!(verifier.verify(&proof, &pc_gens, &bp_gens).is_ok());
}
fn poseidon_hash_2(sbox_type: &SboxType, transcript_label: &'static [u8]) {
let s_params = get_poseidon_params();
let width = s_params.width;
let total_rounds = s_params.get_total_rounds();
let mut test_rng: StdRng = SeedableRng::from_seed([24u8; 32]);
let xl = Scalar::random(&mut test_rng);
let xr = Scalar::random(&mut test_rng);
let expected_output = Poseidon_hash_2(xl, xr, &s_params, sbox_type);
/*println!("Input:\n");
println!("xl={:?}", &xl);
println!("xr={:?}", &xr);
println!("Expected output:\n");
println!("{:?}", &expected_output);*/
let pc_gens = PedersenGens::default();
let bp_gens = BulletproofGens::new(2048, 1);
println!("Proving");
let (proof, commitments) = {
let mut prover_transcript = Transcript::new(transcript_label);
let mut prover = Prover::new(&pc_gens, &mut prover_transcript);
let mut comms = vec![];
let (com_l, var_l) = prover.commit(xl.clone(), Scalar::random(&mut test_rng));
comms.push(com_l);
let l_alloc = AllocatedScalar {
variable: var_l,
assignment: Some(xl),
};
let (com_r, var_r) = prover.commit(xr.clone(), Scalar::random(&mut test_rng));
comms.push(com_r);
let r_alloc = AllocatedScalar {
variable: var_r,
assignment: Some(xr),
};
let num_statics = 4;
let statics = allocate_statics_for_prover(&mut prover, num_statics);
let start = Instant::now();
assert!(Poseidon_hash_2_gadget(&mut prover,
l_alloc,
r_alloc,
statics,
&s_params,
sbox_type,
&expected_output).is_ok());
println!("For Poseidon hash 2:1 rounds {}, no of constraints is {}, no of multipliers is {}", total_rounds, &prover.num_constraints(), &prover.num_multipliers());
let proof = prover.prove(&bp_gens).unwrap();
let end = start.elapsed();
println!("Proving time is {:?}", end);
(proof, comms)
};
println!("Verifying");
let mut verifier_transcript = Transcript::new(transcript_label);
let mut verifier = Verifier::new(&mut verifier_transcript);
let lv = verifier.commit(commitments[0]);
let rv = verifier.commit(commitments[1]);
let l_alloc = AllocatedScalar {
variable: lv,
assignment: None,
};
let r_alloc = AllocatedScalar {
variable: rv,
assignment: None,
};
let num_statics = 4;
let statics = allocate_statics_for_verifier(&mut verifier, num_statics, &pc_gens);
let start = Instant::now();
assert!(Poseidon_hash_2_gadget(&mut verifier,
l_alloc,
r_alloc,
statics,
&s_params,
sbox_type,
&expected_output).is_ok());
assert!(verifier.verify(&proof, &pc_gens, &bp_gens).is_ok());
let end = start.elapsed();
println!("Verification time is {:?}", end);
}
fn poseidon_hash_4(sbox_type: &SboxType, transcript_label: &'static [u8]) {
let s_params = get_poseidon_params();
let width = s_params.width;
let total_rounds = s_params.get_total_rounds();
let mut test_rng: StdRng = SeedableRng::from_seed([24u8; 32]);
let _input = (0..4).map(|_| Scalar::random(&mut test_rng)).collect::<Vec<_>>();
let mut input = [Scalar::zero(); 4];
input.copy_from_slice(_input.as_slice());
let expected_output = Poseidon_hash_4(input, &s_params, sbox_type);
/*println!("Input:\n");
println!("xl={:?}", &xl);
println!("xr={:?}", &xr);
println!("Expected output:\n");
println!("{:?}", &expected_output);*/
let pc_gens = PedersenGens::default();
let bp_gens = BulletproofGens::new(2048, 1);
println!("Proving");
let (proof, commitments) = {
let mut prover_transcript = Transcript::new(transcript_label);
let mut prover = Prover::new(&pc_gens, &mut prover_transcript);
let mut comms = vec![];
let mut allocs = vec![];
for inp in input.iter() {
let (com, var) = prover.commit(inp.clone(), Scalar::random(&mut test_rng));
comms.push(com);
allocs.push(AllocatedScalar {
variable: var,
assignment: Some(inp.clone()),
});
}
let num_statics = 2;
let statics = allocate_statics_for_prover(&mut prover, num_statics);
let start = Instant::now();
assert!(Poseidon_hash_4_gadget(&mut prover,
allocs,
statics,
&s_params,
sbox_type,
&expected_output).is_ok());
println!("For Poseidon hash 4:1 rounds {}, no of constraints is {}, no of multipliers is {}", total_rounds, &prover.num_constraints(), &prover.num_multipliers());
let proof = prover.prove(&bp_gens).unwrap();
let end = start.elapsed();
println!("Proving time is {:?}", end);
(proof, comms)
};
println!("Verifying");
let mut verifier_transcript = Transcript::new(transcript_label);
let mut verifier = Verifier::new(&mut verifier_transcript);
let mut allocs = vec![];
for com in commitments {
let v = verifier.commit(com);
allocs.push({
AllocatedScalar {
variable: v,
assignment: None,
}
});
}
let num_statics = 2;
let statics = allocate_statics_for_verifier(&mut verifier, num_statics, &pc_gens);
let start = Instant::now();
assert!(Poseidon_hash_4_gadget(&mut verifier,
allocs,
statics,
&s_params,
sbox_type,
&expected_output).is_ok());
assert!(verifier.verify(&proof, &pc_gens, &bp_gens).is_ok());
let end = start.elapsed();
println!("Verification time is {:?}", end);
}
#[test]
fn test_poseidon_perm_cube_sbox() {
poseidon_perm(&SboxType::Cube, b"Poseidon_perm_cube");
}
#[test]
fn test_poseidon_perm_inverse_sbox() {
poseidon_perm(&SboxType::Inverse, b"Poseidon_perm_inverse");
}
#[test]
fn test_poseidon_hash_2_cube_sbox() {
poseidon_hash_2(&SboxType::Cube, b"Poseidon_hash_2_cube");
}
#[test]
fn test_poseidon_hash_2_inverse_sbox() {
poseidon_hash_2(&SboxType::Inverse, b"Poseidon_hash_2_inverse");
}
#[test]
fn test_poseidon_hash_4_cube_sbox() {
poseidon_hash_4(&SboxType::Cube, b"Poseidon_hash_2_cube");
}
#[test]
fn test_poseidon_hash_4_inverse_sbox() {
poseidon_hash_4(&SboxType::Inverse, b"Poseidon_hash_2_inverse");
}
}