-
Notifications
You must be signed in to change notification settings - Fork 26
/
picnic_impl.c
1504 lines (1320 loc) · 52.2 KB
/
picnic_impl.c
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "bitstream.h"
#include "compat.h"
#include "io.h"
#include "kdf_shake.h"
#include "lowmc.h"
#include "mpc_lowmc.h"
#include "picnic_impl.h"
#include "randomness.h"
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* max number of ZKB++ rounds */
#if defined(WITH_LOWMC_255_255_4) || defined(WITH_LOWMC_256_256_38)
#define MAX_NUM_ROUNDS 438
#elif defined(WITH_LOWMC_192_192_4) || defined(WITH_LOWMC_192_192_30)
#define MAX_NUM_ROUNDS 329
#elif defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_128_128_20)
#define MAX_NUM_ROUNDS 219
#endif
/* max view size per round */
#if defined(WITH_LOWMC_256_256_38)
#define MAX_VIEW_SIZE 143
#elif defined(WITH_LOWMC_255_255_4)
#define MAX_VIEW_SIZE 128
#elif defined(WITH_LOWMC_192_192_30)
#define MAX_VIEW_SIZE 113
#elif defined(WITH_LOWMC_192_192_4)
#define MAX_VIEW_SIZE 96
#elif defined(WITH_LOWMC_128_128_20)
#define MAX_VIEW_SIZE 75
#elif defined(WITH_LOWMC_129_129_4)
#define MAX_VIEW_SIZE 65
#endif
typedef struct {
uint8_t* seeds[SC_PROOF];
uint8_t* commitments[SC_PROOF];
uint8_t* input_shares[SC_PROOF];
uint8_t* communicated_bits[SC_PROOF];
uint8_t* output_shares[SC_PROOF];
#if defined(WITH_UNRUH)
uint8_t* gs[SC_PROOF];
#endif
} proof_round_t;
typedef struct {
uint8_t salt[SALT_SIZE];
uint8_t* challenge;
proof_round_t round[];
} sig_proof_t;
typedef uint16_t sorting_helper_t;
#if !defined(NO_UINT64_FALLBACK)
static void mzd_share_uint64_128(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mzd_xor_uint64_128(r, v1, v2);
mzd_xor_uint64_128(r, r, v3);
}
static void mzd_share_uint64_192(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mzd_xor_uint64_192(r, v1, v2);
mzd_xor_uint64_192(r, r, v3);
}
static void mzd_share_uint64_256(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mzd_xor_uint64_256(r, v1, v2);
mzd_xor_uint64_256(r, r, v3);
}
#endif
#if defined(WITH_OPT)
#if defined(WITH_SSE2) || defined(WITH_NEON)
ATTR_TARGET_S128
static void mzd_share_s128_128(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mm128_store(r->w64,
mm128_xor(mm128_xor(mm128_load(v1->w64), mm128_load(v2->w64)), mm128_load(v3->w64)));
}
ATTR_TARGET_S128
static void mzd_share_s128_256(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mm128_store(&r->w64[0], mm128_xor(mm128_xor(mm128_load(&v1->w64[0]), mm128_load(&v2->w64[0])),
mm128_load(&v3->w64[0])));
mm128_store(&r->w64[2], mm128_xor(mm128_xor(mm128_load(&v1->w64[2]), mm128_load(&v2->w64[2])),
mm128_load(&v3->w64[2])));
}
#endif
#if defined(WITH_AVX2)
ATTR_TARGET_AVX2
static void mzd_share_s256_128(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mm128_store(r->w64,
mm128_xor(mm128_xor(mm128_load(v1->w64), mm128_load(v2->w64)), mm128_load(v3->w64)));
}
ATTR_TARGET_AVX2
static void mzd_share_s256_256(mzd_local_t* r, const mzd_local_t* v1, const mzd_local_t* v2,
const mzd_local_t* v3) {
mm256_store(r->w64,
mm256_xor(mm256_xor(mm256_load(v1->w64), mm256_load(v2->w64)), mm256_load(v3->w64)));
}
#endif
#endif
static void mzd_share(const lowmc_parameters_t* lowmc, mzd_local_t* r, const mzd_local_t* v1,
const mzd_local_t* v2, const mzd_local_t* v3) {
#if defined(WITH_OPT)
#if defined(WITH_AVX2)
if (CPU_SUPPORTS_AVX2) {
if (lowmc->n > 128) {
mzd_share_s256_256(r, v1, v2, v3);
} else {
mzd_share_s256_128(r, v1, v2, v3);
}
return;
}
#endif
#if defined(WITH_SSE2) || defined(WITH_NEON)
if (CPU_SUPPORTS_SSE2 || CPU_SUPPORTS_NEON) {
if (lowmc->n > 128) {
mzd_share_s128_256(r, v1, v2, v3);
} else {
mzd_share_s128_128(r, v1, v2, v3);
}
return;
}
#endif
#endif
#if !defined(NO_UINT64_FALLBACK)
if (lowmc->n <= 128) {
mzd_share_uint64_128(r, v1, v2, v3);
} else if (lowmc->n <= 192) {
mzd_share_uint64_192(r, v1, v2, v3);
} else {
mzd_share_uint64_256(r, v1, v2, v3);
}
#endif
}
ATTR_CONST static inline unsigned int collapsed_challenge_size(const unsigned int num_rounds) {
return (2 * num_rounds + 7) / 8;
}
static inline void clear_padding_bits(uint8_t* v, const unsigned int diff) {
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_255_255_4)
*v &= UINT8_C(0xff) << diff;
#else
(void)v;
(void)diff;
#endif
}
/**
* Collapse challenge from one char per challenge to bit array.
*
* Returns the number of written bytes.
*/
static unsigned int collapse_challenge(uint8_t* collapsed, unsigned int num_rounds,
const uint8_t* challenge) {
bitstream_t bs;
bs.buffer.w = collapsed;
bs.position = 0;
for (unsigned int i = 0; i < num_rounds; ++i) {
// flip challenge bits according to spec
bitstream_put_bits_8(&bs, (challenge[i] >> 1) | ((challenge[i] & 1) << 1), 2);
}
return (bs.position + 7) / 8;
}
/**
* Expand challenge from bit array to one char per challenge.
*
* Returns the number of consumed bytes if successful.
*/
static unsigned int expand_challenge(uint8_t* challenge, unsigned int num_rounds,
const uint8_t* collapsed) {
bitstream_t bs;
bs.buffer.r = collapsed;
bs.position = 0;
for (unsigned int i = 0; i < num_rounds; ++i) {
const uint8_t ch = bitstream_get_bits_8(&bs, 2);
if (ch == 3) {
return 0;
}
// flip challenge bits according to spec
challenge[i] = (ch & 1) << 1 | (ch >> 1);
}
const unsigned int remaining_bits = 8 - (bs.position % 8);
if (remaining_bits && bitstream_get_bits(&bs, remaining_bits)) {
return 0;
}
return bs.position / 8;
}
static sig_proof_t* proof_new(const picnic_instance_t* pp, const picnic_context_t* ctx) {
const unsigned int digest_size = pp->digest_size;
const unsigned int seed_size = pp->seed_size;
const unsigned int num_rounds = pp->num_rounds;
const unsigned int input_output_size = pp->input_output_size;
const unsigned int view_size = ALIGNU64T(pp->view_size);
#if defined(WITH_UNRUH)
const unsigned int unruh_without_input_bytes_size = input_output_size + pp->view_size;
#else
(void)ctx;
#endif
sig_proof_t* prf = calloc(1, sizeof(sig_proof_t) + num_rounds * sizeof(proof_round_t));
if (!prf) {
return NULL;
}
unsigned int per_round_mem =
SC_PROOF * (seed_size + digest_size + input_output_size + input_output_size + view_size);
#if defined(WITH_UNRUH)
if (ctx->unruh) {
per_round_mem += SC_PROOF * unruh_without_input_bytes_size + input_output_size;
}
#endif
// in memory:
// - challenge (aligned to uint64_t)
// - seeds
// - salt
// - commitments
// - input shares
// - communicated bits (aligned to uint64_t)
// - output shares
// - Gs
//
// Since seeds size, commitment size, input share size and output share size are all divisible by
// the alignment of uint64_t, this means, that up to the memory of the Gs, everything is
// uint64_t-aligned.
uint8_t* slab = calloc(1, num_rounds * per_round_mem + ALIGNU64T(num_rounds) + SALT_SIZE);
prf->challenge = slab;
slab += ALIGNU64T(num_rounds);
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_PROOF; ++i) {
prf->round[r].seeds[i] = slab;
slab += seed_size;
}
}
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_PROOF; ++i) {
prf->round[r].commitments[i] = slab;
slab += digest_size;
}
}
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_PROOF; ++i) {
prf->round[r].input_shares[i] = slab;
slab += input_output_size;
}
}
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_PROOF; ++i) {
prf->round[r].communicated_bits[i] = slab;
slab += view_size;
}
}
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_PROOF; ++i) {
prf->round[r].output_shares[i] = slab;
slab += input_output_size;
}
}
#if defined(WITH_UNRUH)
if (ctx->unruh) {
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_PROOF; ++i) {
prf->round[r].gs[i] = slab;
slab += unruh_without_input_bytes_size;
}
slab += input_output_size;
}
}
#endif
return prf;
}
static sig_proof_t* proof_new_verify(const picnic_instance_t* pp, const picnic_context_t* ctx,
uint8_t** rslab) {
const unsigned int digest_size = pp->digest_size;
const unsigned int num_rounds = pp->num_rounds;
const unsigned int input_output_size = pp->input_output_size;
const unsigned int view_size = ALIGNU64T(pp->view_size);
sig_proof_t* proof = calloc(1, sizeof(sig_proof_t) + num_rounds * sizeof(proof_round_t));
if (!proof) {
return NULL;
}
unsigned int per_round_mem = SC_VERIFY * digest_size;
#if defined(WITH_UNRUH)
const unsigned int unruh_with_input_bytes_size = pp->view_size + 2 * input_output_size;
if (ctx->unruh) {
// we don't know what we actually need, so allocate more than needed
per_round_mem += SC_VERIFY * unruh_with_input_bytes_size;
}
#else
(void)ctx;
#endif
per_round_mem += SC_VERIFY * input_output_size + SC_PROOF * input_output_size + view_size;
uint8_t* slab = calloc(1, num_rounds * per_round_mem + ALIGNU64T(num_rounds));
proof->challenge = slab;
slab += ALIGNU64T(num_rounds);
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_VERIFY; ++i) {
proof->round[r].commitments[i] = slab;
slab += digest_size;
}
}
for (unsigned int r = 0; r < num_rounds; ++r) {
proof->round[r].communicated_bits[0] = slab;
slab += view_size;
}
for (unsigned int r = 0; r < num_rounds; ++r) {
proof->round[r].output_shares[0] = slab;
slab += input_output_size;
proof->round[r].output_shares[1] = slab;
slab += input_output_size;
proof->round[r].output_shares[2] = slab;
slab += input_output_size;
}
#if defined(WITH_UNRUH)
if (ctx->unruh) {
for (unsigned int r = 0; r < num_rounds; ++r) {
for (unsigned int i = 0; i < SC_VERIFY; ++i) {
proof->round[r].gs[i] = slab;
slab += unruh_with_input_bytes_size;
}
}
}
#endif
*rslab = slab;
return proof;
}
static void proof_free(sig_proof_t* prf) {
free(prf->challenge);
free(prf);
}
static void kdf_init_from_seed(kdf_shake_t* kdf, const uint8_t* seed, const uint8_t* salt,
uint16_t round_number, uint16_t player_number,
bool include_input_size, const picnic_instance_t* pp) {
const unsigned int digest_size = pp->digest_size;
// Hash the seed with H_2.
kdf_shake_init_prefix(kdf, digest_size, HASH_PREFIX_2);
kdf_shake_update_key(kdf, seed, pp->seed_size);
kdf_shake_finalize_key(kdf);
uint8_t tmp[MAX_DIGEST_SIZE];
kdf_shake_get_randomness(kdf, tmp, digest_size);
kdf_shake_clear(kdf);
// Initialize KDF with H_2(seed) || salt || round_number || player_number || output_size.
kdf_shake_init(kdf, digest_size);
kdf_shake_update_key(kdf, tmp, digest_size);
kdf_shake_update_key(kdf, salt, SALT_SIZE);
kdf_shake_update_key_uint16_le(kdf, round_number);
kdf_shake_update_key_uint16_le(kdf, player_number);
kdf_shake_update_key_uint16_le(kdf,
pp->view_size + (include_input_size ? pp->input_output_size : 0));
kdf_shake_finalize_key(kdf);
}
static void kdf_init_x4_from_seed(kdf_shake_x4_t* kdf, const uint8_t** seed, const uint8_t* salt,
const uint16_t round_number[4], const uint16_t player_number,
bool include_input_size, const picnic_instance_t* pp) {
const unsigned int digest_size = pp->digest_size;
// Hash the seed with H_2.
kdf_shake_x4_init_prefix(kdf, digest_size, HASH_PREFIX_2);
kdf_shake_x4_update_key(kdf, seed, pp->seed_size);
kdf_shake_x4_finalize_key(kdf);
uint8_t tmp[4][MAX_DIGEST_SIZE];
kdf_shake_x4_get_randomness_4(kdf, tmp[0], tmp[1], tmp[2], tmp[3], digest_size);
kdf_shake_x4_clear(kdf);
// Initialize KDF with H_2(seed) || salt || round_number || player_number || output_size.
kdf_shake_x4_init(kdf, digest_size);
kdf_shake_x4_update_key_4(kdf, tmp[0], tmp[1], tmp[2], tmp[3], digest_size);
kdf_shake_x4_update_key_1(kdf, salt, SALT_SIZE);
kdf_shake_x4_update_key_uint16s_le(kdf, round_number);
kdf_shake_x4_update_key_uint16_le(kdf, player_number);
kdf_shake_x4_update_key_uint16_le(kdf, pp->view_size +
(include_input_size ? pp->input_output_size : 0));
kdf_shake_x4_finalize_key(kdf);
}
// clang-format off
#if defined(WITH_LOWMC_128_128_20) || defined(WITH_LOWMC_192_192_30) || defined(WITH_LOWMC_256_256_38)
// clang-format on
static void uint64_to_bitstream_10(bitstream_t* bs, const uint64_t v) {
bitstream_put_bits(bs, v >> (64 - 30), 30);
}
static uint64_t uint64_from_bitstream_10(bitstream_t* bs) {
return bitstream_get_bits(bs, 30) << (64 - 30);
}
#endif
static void compress_view(uint8_t* dst, const picnic_instance_t* pp, const view_t* views,
const unsigned int idx) {
const unsigned int num_views = pp->lowmc.r;
bitstream_t bs;
bs.buffer.w = dst;
bs.position = 0;
const view_t* v = &views[0];
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_192_192_4) || defined(WITH_LOWMC_255_255_4)
if (pp->lowmc.m != 10) {
const unsigned int view_round_size = pp->lowmc.m * 3;
const unsigned int width = (pp->lowmc.n + 63) / 64;
for (unsigned int i = 0; i < num_views; ++i, ++v) {
mzd_to_bitstream(&bs, &v->s[idx], width, view_round_size);
}
return;
}
#endif
// clang-format off
#if defined(WITH_LOWMC_128_128_20) || defined(WITH_LOWMC_192_192_30) || defined(WITH_LOWMC_256_256_38)
// clang-format on
if (pp->lowmc.m == 10) {
for (unsigned int i = 0; i < num_views; ++i, ++v) {
uint64_to_bitstream_10(&bs, v->t[idx]);
}
return;
}
#endif
UNREACHABLE;
}
static void decompress_view(view_t* views, const picnic_instance_t* pp, const uint8_t* src,
const unsigned int idx) {
const unsigned int num_views = pp->lowmc.r;
bitstream_t bs;
bs.buffer.r = src;
bs.position = 0;
view_t* v = &views[0];
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_192_192_4) || defined(WITH_LOWMC_255_255_4)
if (pp->lowmc.m != 10) {
const unsigned int view_round_size = pp->lowmc.m * 3;
const unsigned int width = (pp->lowmc.n + 63) / 64;
for (unsigned int i = 0; i < num_views; ++i, ++v) {
mzd_from_bitstream(&bs, &v->s[idx], width, view_round_size);
}
return;
}
#endif
// clang-format off
#if defined(WITH_LOWMC_128_128_20) || defined(WITH_LOWMC_192_192_30) || defined(WITH_LOWMC_256_256_38)
// clang-format on
if (pp->lowmc.m == 10) {
for (unsigned int i = 0; i < num_views; ++i, ++v) {
v->t[idx] = uint64_from_bitstream_10(&bs);
}
return;
}
#endif
UNREACHABLE;
}
static void decompress_random_tape(rvec_t* rvec, const picnic_instance_t* pp, const uint8_t* src,
const unsigned int idx) {
decompress_view(rvec, pp, src, idx);
}
/**
* Compute commitment to a view.
*/
static void hash_commitment(const picnic_instance_t* pp, proof_round_t* prf_round,
const unsigned int vidx) {
const unsigned int hashlen = pp->digest_size;
hash_context ctx;
// hash the seed
hash_init_prefix(&ctx, hashlen, HASH_PREFIX_4);
hash_update(&ctx, prf_round->seeds[vidx], pp->seed_size);
hash_final(&ctx);
uint8_t tmp[MAX_DIGEST_SIZE];
hash_squeeze(&ctx, tmp, hashlen);
hash_clear(&ctx);
// compute H_0(H_4(seed), view)
hash_init_prefix(&ctx, hashlen, HASH_PREFIX_0);
hash_update(&ctx, tmp, hashlen);
// hash input share
hash_update(&ctx, prf_round->input_shares[vidx], pp->input_output_size);
// hash communicated bits
hash_update(&ctx, prf_round->communicated_bits[vidx], pp->view_size);
// hash output share
hash_update(&ctx, prf_round->output_shares[vidx], pp->input_output_size);
hash_final(&ctx);
hash_squeeze(&ctx, prf_round->commitments[vidx], hashlen);
hash_clear(&ctx);
}
/**
* Compute commitment to 4 views.
*/
static void hash_commitment_x4(const picnic_instance_t* pp, proof_round_t* prf_round,
const unsigned int vidx) {
const unsigned int hashlen = pp->digest_size;
hash_context_x4 ctx;
// hash the seed
hash_init_prefix_x4(&ctx, hashlen, HASH_PREFIX_4);
hash_update_x4_4(&ctx, prf_round[0].seeds[vidx], prf_round[1].seeds[vidx],
prf_round[2].seeds[vidx], prf_round[3].seeds[vidx], pp->seed_size);
hash_final_x4(&ctx);
uint8_t tmp[4][MAX_DIGEST_SIZE];
hash_squeeze_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], hashlen);
hash_clear_x4(&ctx);
// compute H_0(H_4(seed), view)
hash_init_prefix_x4(&ctx, hashlen, HASH_PREFIX_0);
hash_update_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], hashlen);
// hash input share
hash_update_x4_4(&ctx, prf_round[0].input_shares[vidx], prf_round[1].input_shares[vidx],
prf_round[2].input_shares[vidx], prf_round[3].input_shares[vidx],
pp->input_output_size);
// hash communicated bits
hash_update_x4_4(&ctx, prf_round[0].communicated_bits[vidx], prf_round[1].communicated_bits[vidx],
prf_round[2].communicated_bits[vidx], prf_round[3].communicated_bits[vidx],
pp->view_size);
// hash output share
hash_update_x4_4(&ctx, prf_round[0].output_shares[vidx], prf_round[1].output_shares[vidx],
prf_round[2].output_shares[vidx], prf_round[3].output_shares[vidx],
pp->input_output_size);
hash_final_x4(&ctx);
hash_squeeze_x4_4(&ctx, prf_round[0].commitments[vidx], prf_round[1].commitments[vidx],
prf_round[2].commitments[vidx], prf_round[3].commitments[vidx], hashlen);
hash_clear_x4(&ctx);
}
/**
* Compute commitment to 4 views, for verification
*/
static void hash_commitment_x4_verify(const picnic_instance_t* pp, const proof_round_t* prf,
const sorting_helper_t* helper, const unsigned int vidx) {
const unsigned int hashlen = pp->digest_size;
const proof_round_t* prf0 = &prf[helper[0]];
const proof_round_t* prf1 = &prf[helper[1]];
const proof_round_t* prf2 = &prf[helper[2]];
const proof_round_t* prf3 = &prf[helper[3]];
hash_context_x4 ctx;
// hash the seed
hash_init_prefix_x4(&ctx, hashlen, HASH_PREFIX_4);
hash_update_x4_4(&ctx, prf0->seeds[vidx], prf1->seeds[vidx], prf2->seeds[vidx], prf3->seeds[vidx],
pp->seed_size);
hash_final_x4(&ctx);
uint8_t tmp[4][MAX_DIGEST_SIZE];
hash_squeeze_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], hashlen);
hash_clear_x4(&ctx);
// compute H_0(H_4(seed), view)
hash_init_prefix_x4(&ctx, hashlen, HASH_PREFIX_0);
hash_update_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], hashlen);
// hash input share
hash_update_x4_4(&ctx, prf0->input_shares[vidx], prf1->input_shares[vidx],
prf2->input_shares[vidx], prf3->input_shares[vidx], pp->input_output_size);
// hash communicated bits
hash_update_x4_4(&ctx, prf0->communicated_bits[vidx], prf1->communicated_bits[vidx],
prf2->communicated_bits[vidx], prf3->communicated_bits[vidx], pp->view_size);
// hash output share
hash_update_x4_4(&ctx, prf0->output_shares[vidx], prf1->output_shares[vidx],
prf2->output_shares[vidx], prf3->output_shares[vidx], pp->input_output_size);
hash_final_x4(&ctx);
hash_squeeze_x4_4(&ctx, prf0->commitments[vidx], prf1->commitments[vidx], prf2->commitments[vidx],
prf3->commitments[vidx], hashlen);
hash_clear_x4(&ctx);
}
/**
* Compute challenge from transform dependent hash - outputs {1,2 or 3}^t
*/
static void H3_compute(const picnic_instance_t* pp, uint8_t* hash, uint8_t* ch) {
const unsigned int digest_size = pp->digest_size;
const unsigned int digest_size_bits = digest_size << 3;
// Pick bits from hash
uint8_t* eof = ch + pp->num_rounds;
unsigned int bit_idx = 0;
while (ch < eof) {
if (bit_idx >= digest_size_bits) {
hash_context ctx;
hash_init_prefix(&ctx, digest_size, HASH_PREFIX_1);
hash_update(&ctx, hash, digest_size);
hash_final(&ctx);
hash_squeeze(&ctx, hash, digest_size);
hash_clear(&ctx);
bit_idx = 0;
}
const uint8_t twobits = (hash[bit_idx >> 3] >> ((6 - (bit_idx & 0x7)))) & 0x3;
if (twobits != 0x3) {
*ch++ = twobits;
}
bit_idx += 2;
}
}
/**
* Hash public key, salt and message
*/
static void H3_public_key_message(hash_context* ctx, const picnic_instance_t* pp,
const uint8_t* salt, const picnic_context_t* context) {
// hash circuit out and input (public key)
hash_update(ctx, context->public_key, pp->input_output_size);
hash_update(ctx, context->plaintext, pp->input_output_size);
// hash salt
hash_update(ctx, salt, SALT_SIZE);
// hash message
hash_update(ctx, context->msg, context->msglen);
}
/**
* Re-compute challenge for verification
*/
static void H3_verify(const picnic_instance_t* pp, sig_proof_t* prf,
const picnic_context_t* context, uint8_t* ch) {
const unsigned int digest_size = pp->digest_size;
const unsigned int num_rounds = pp->num_rounds;
const unsigned int input_output_size = pp->input_output_size;
hash_context ctx;
hash_init_prefix(&ctx, digest_size, HASH_PREFIX_1);
// hash output shares
proof_round_t* round = prf->round;
for (unsigned int i = 0; i < num_rounds; ++i, ++round) {
switch (prf->challenge[i]) {
case 0: {
hash_update(&ctx, round->output_shares[0], input_output_size);
hash_update(&ctx, round->output_shares[1], input_output_size);
hash_update(&ctx, round->output_shares[2], input_output_size);
break;
}
case 1: {
hash_update(&ctx, round->output_shares[2], input_output_size);
hash_update(&ctx, round->output_shares[0], input_output_size);
hash_update(&ctx, round->output_shares[1], input_output_size);
break;
}
default: {
hash_update(&ctx, round->output_shares[1], input_output_size);
hash_update(&ctx, round->output_shares[2], input_output_size);
hash_update(&ctx, round->output_shares[0], input_output_size);
break;
}
}
}
// hash commitments
round = prf->round;
for (unsigned int i = 0; i < num_rounds; ++i, ++round) {
switch (prf->challenge[i]) {
case 0: {
hash_update(&ctx, round->commitments[0], digest_size);
hash_update(&ctx, round->commitments[1], digest_size);
hash_update(&ctx, round->commitments[2], digest_size);
break;
}
case 1: {
hash_update(&ctx, round->commitments[2], digest_size);
hash_update(&ctx, round->commitments[0], digest_size);
hash_update(&ctx, round->commitments[1], digest_size);
break;
}
default: {
hash_update(&ctx, round->commitments[1], digest_size);
hash_update(&ctx, round->commitments[2], digest_size);
hash_update(&ctx, round->commitments[0], digest_size);
break;
}
}
}
#if defined(WITH_UNRUH)
if (context->unruh) {
const unsigned int without_input_bytes_size = pp->view_size + input_output_size;
const unsigned int with_input_bytes_size = without_input_bytes_size + input_output_size;
// hash commitments
round = prf->round;
for (unsigned int i = 0; i < num_rounds; ++i, ++round) {
switch (prf->challenge[i]) {
case 0: {
hash_update(&ctx, round->gs[0], without_input_bytes_size);
hash_update(&ctx, round->gs[1], without_input_bytes_size);
hash_update(&ctx, round->gs[2], with_input_bytes_size);
break;
}
case 1: {
hash_update(&ctx, round->gs[2], without_input_bytes_size);
hash_update(&ctx, round->gs[0], without_input_bytes_size);
hash_update(&ctx, round->gs[1], with_input_bytes_size);
break;
}
default: {
hash_update(&ctx, round->gs[1], without_input_bytes_size);
hash_update(&ctx, round->gs[2], without_input_bytes_size);
hash_update(&ctx, round->gs[0], with_input_bytes_size);
break;
}
}
}
}
#endif
// hash public key, salt, and message
H3_public_key_message(&ctx, pp, prf->salt, context);
hash_final(&ctx);
uint8_t hash[MAX_DIGEST_SIZE] = {0};
hash_squeeze(&ctx, hash, digest_size);
hash_clear(&ctx);
H3_compute(pp, hash, ch);
}
/**
* Compute challenge
*/
static void H3(const picnic_instance_t* pp, sig_proof_t* prf, const picnic_context_t* context) {
const unsigned int num_rounds = pp->num_rounds;
hash_context ctx;
hash_init_prefix(&ctx, pp->digest_size, HASH_PREFIX_1);
// hash output shares
hash_update(&ctx, prf->round[0].output_shares[0], pp->input_output_size * num_rounds * SC_PROOF);
// hash all commitments C
hash_update(&ctx, prf->round[0].commitments[0], pp->digest_size * num_rounds * SC_PROOF);
#if defined(WITH_UNRUH)
if (context->unruh) {
// hash all commitments G
hash_update(&ctx, prf->round[0].gs[0],
num_rounds * (SC_PROOF * pp->view_size + (SC_PROOF + 1) * pp->input_output_size));
}
#endif
// hash public key, salt, and message
H3_public_key_message(&ctx, pp, prf->salt, context);
hash_final(&ctx);
uint8_t hash[MAX_DIGEST_SIZE] = {0};
hash_squeeze(&ctx, hash, pp->digest_size);
hash_clear(&ctx);
/* parts of this hash will be published as challenge so is public anyway */
picnic_declassify(hash, MAX_DIGEST_SIZE);
H3_compute(pp, hash, prf->challenge);
}
#if defined(WITH_UNRUH)
/*
* G permutation for Unruh transform
*/
static void unruh_G(const picnic_instance_t* pp, proof_round_t* prf_round, unsigned int vidx,
bool include_is) {
const unsigned int digest_size = pp->digest_size;
// Hash the seed with H_5, store digest in output
hash_context ctx;
hash_init_prefix(&ctx, digest_size, HASH_PREFIX_5);
hash_update(&ctx, prf_round->seeds[vidx], pp->seed_size);
hash_final(&ctx);
uint8_t tmp[MAX_DIGEST_SIZE];
hash_squeeze(&ctx, tmp, digest_size);
hash_clear(&ctx);
// Hash H_5(seed), the view, and the length
hash_init(&ctx, digest_size);
hash_update(&ctx, tmp, digest_size);
if (include_is) {
hash_update(&ctx, prf_round->input_shares[vidx], pp->input_output_size);
}
hash_update(&ctx, prf_round->communicated_bits[vidx], pp->view_size);
const unsigned int outputlen =
pp->view_size + pp->input_output_size + (include_is ? pp->input_output_size : 0);
hash_update_uint16_le(&ctx, outputlen);
hash_final(&ctx);
hash_squeeze(&ctx, prf_round->gs[vidx], outputlen);
hash_clear(&ctx);
}
/*
* 4x G permutation for Unruh transform
*/
static void unruh_G_x4(const picnic_instance_t* pp, proof_round_t* prf_round, unsigned int vidx,
bool include_is) {
const unsigned int digest_size = pp->digest_size;
// Hash the seed with H_5, store digest in output
hash_context_x4 ctx;
hash_init_prefix_x4(&ctx, digest_size, HASH_PREFIX_5);
hash_update_x4_4(&ctx, prf_round[0].seeds[vidx], prf_round[1].seeds[vidx],
prf_round[2].seeds[vidx], prf_round[3].seeds[vidx], pp->seed_size);
hash_final_x4(&ctx);
uint8_t tmp[4][MAX_DIGEST_SIZE];
hash_squeeze_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], digest_size);
hash_clear_x4(&ctx);
// Hash H_5(seed), the view, and the length
hash_init_x4(&ctx, digest_size);
hash_update_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], digest_size);
if (include_is) {
hash_update_x4_4(&ctx, prf_round[0].input_shares[vidx], prf_round[1].input_shares[vidx],
prf_round[2].input_shares[vidx], prf_round[3].input_shares[vidx],
pp->input_output_size);
}
hash_update_x4_4(&ctx, prf_round[0].communicated_bits[vidx], prf_round[1].communicated_bits[vidx],
prf_round[2].communicated_bits[vidx], prf_round[3].communicated_bits[vidx],
pp->view_size);
const unsigned int outputlen =
pp->view_size + pp->input_output_size + (include_is ? pp->input_output_size : 0);
hash_update_x4_uint16_le(&ctx, outputlen);
hash_final_x4(&ctx);
hash_squeeze_x4_4(&ctx, prf_round[0].gs[vidx], prf_round[1].gs[vidx], prf_round[2].gs[vidx],
prf_round[3].gs[vidx], outputlen);
hash_clear_x4(&ctx);
}
/*
* 4x G permutation for Unruh transform, for verification
*/
static void unruh_G_x4_verify(const picnic_instance_t* pp, const proof_round_t* prf,
const sorting_helper_t* helper, unsigned int vidx, bool include_is) {
const unsigned int digest_size = pp->digest_size;
const proof_round_t* prf0 = &prf[helper[0]];
const proof_round_t* prf1 = &prf[helper[1]];
const proof_round_t* prf2 = &prf[helper[2]];
const proof_round_t* prf3 = &prf[helper[3]];
// Hash the seed with H_5, store digest in output
hash_context_x4 ctx;
hash_init_prefix_x4(&ctx, digest_size, HASH_PREFIX_5);
hash_update_x4_4(&ctx, prf0->seeds[vidx], prf1->seeds[vidx], prf2->seeds[vidx], prf3->seeds[vidx],
pp->seed_size);
hash_final_x4(&ctx);
uint8_t tmp[4][MAX_DIGEST_SIZE];
hash_squeeze_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], digest_size);
hash_clear_x4(&ctx);
// Hash H_5(seed), the view, and the length
hash_init_x4(&ctx, digest_size);
hash_update_x4_4(&ctx, tmp[0], tmp[1], tmp[2], tmp[3], digest_size);
if (include_is) {
hash_update_x4_4(&ctx, prf0->input_shares[vidx], prf1->input_shares[vidx],
prf2->input_shares[vidx], prf3->input_shares[vidx], pp->input_output_size);
}
hash_update_x4_4(&ctx, prf0->communicated_bits[vidx], prf1->communicated_bits[vidx],
prf2->communicated_bits[vidx], prf3->communicated_bits[vidx], pp->view_size);
const unsigned int outputlen =
pp->view_size + pp->input_output_size + (include_is ? pp->input_output_size : 0);
hash_update_x4_uint16_le(&ctx, outputlen);
hash_final_x4(&ctx);
hash_squeeze_x4_4(&ctx, prf0->gs[vidx], prf1->gs[vidx], prf2->gs[vidx], prf3->gs[vidx],
outputlen);
hash_clear_x4(&ctx);
}
#endif
// serilization helper functions
static int sig_proof_to_char_array(const picnic_instance_t* pp, const sig_proof_t* prf,
uint8_t* result, size_t* siglen) {
const unsigned int num_rounds = pp->num_rounds;
const unsigned int seed_size = pp->seed_size;
const unsigned int digest_size = pp->digest_size;
const unsigned int view_size = pp->view_size;
const unsigned int input_output_size = pp->input_output_size;
#if defined(WITH_UNRUH)
const unsigned int unruh_without_input_bytes_size = view_size + input_output_size;
#endif
uint8_t* tmp = result;
// write challenge
tmp += collapse_challenge(tmp, num_rounds, prf->challenge);
// write salt
memcpy(tmp, prf->salt, SALT_SIZE);
tmp += SALT_SIZE;
const proof_round_t* round = prf->round;
for (unsigned int i = 0; i < num_rounds; ++i, ++round) {
const unsigned int a = prf->challenge[i];
const unsigned int b = (a + 1) % 3;
const unsigned int c = (a + 2) % 3;
// write commitment
memcpy(tmp, round->commitments[c], digest_size);
tmp += digest_size;
#if defined(WITH_UNRUH)
// write unruh G
if (round->gs[c]) {
const uint32_t unruh_g_size = unruh_without_input_bytes_size + (a ? 0 : input_output_size);
memcpy(tmp, round->gs[c], unruh_g_size);
tmp += unruh_g_size;
}
#endif
// write views
memcpy(tmp, round->communicated_bits[b], view_size);
tmp += view_size;
// write seeds
memcpy(tmp, round->seeds[a], seed_size);
tmp += seed_size;
memcpy(tmp, round->seeds[b], seed_size);
tmp += seed_size;
if (a) {
// write input share
memcpy(tmp, round->input_shares[SC_PROOF - 1], input_output_size);
tmp += input_output_size;
}
}
*siglen = tmp - result;
return 0;
}
static sig_proof_t* sig_proof_from_char_array(const picnic_instance_t* pp,
const picnic_context_t* ctx, const uint8_t* data,
size_t len) {
uint8_t* slab = NULL;
sig_proof_t* proof = proof_new_verify(pp, ctx, &slab);
if (!proof) {
return NULL;
}
const unsigned int digest_size = pp->digest_size;
const unsigned int seed_size = pp->seed_size;
const unsigned int num_rounds = pp->num_rounds;
const unsigned int input_output_size = pp->input_output_size;
const unsigned int view_size = pp->view_size;
const unsigned int view_diff = pp->view_size * 8 - 3 * pp->lowmc.m * pp->lowmc.r;
const unsigned int input_share_diff = pp->input_output_size * 8 - pp->lowmc.n;
#if defined(WITH_UNRUH)
const unsigned int without_input_bytes_size = view_size + input_output_size;
#endif
size_t remaining_len = len;