-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsign.c
More file actions
1406 lines (1255 loc) · 47 KB
/
sign.c
File metadata and controls
1406 lines (1255 loc) · 47 KB
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
/*
* Copyright (c) The mldsa-native project authors
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
*/
/* References
* ==========
*
* - [FIPS140_3_IG]
* Implementation Guidance for FIPS 140-3 and the Cryptographic Module
* Validation Program
* National Institute of Standards and Technology
* https://csrc.nist.gov/projects/cryptographic-module-validation-program/fips-140-3-ig-announcements
*
* - [FIPS204]
* FIPS 204 Module-Lattice-Based Digital Signature Standard
* National Institute of Standards and Technology
* https://csrc.nist.gov/pubs/fips/204/final
*
* - [Round3_Spec]
* CRYSTALS-Dilithium Algorithm Specifications and Supporting Documentation
* (Version 3.1)
* Bai, Ducas, Kiltz, Lepoint, Lyubashevsky, Schwabe, Seiler, Stehlé
* https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf
*/
#include <stdint.h>
#include <string.h>
#include "cbmc.h"
#include "ct.h"
#include "debug.h"
#include "packing.h"
#include "poly.h"
#include "poly_kl.h"
#include "polyvec.h"
#include "randombytes.h"
#include "sign.h"
#include "symmetric.h"
/* Parameter set namespacing
* This is to facilitate building multiple instances
* of mldsa-native (e.g. with varying parameter sets)
* within a single compilation unit. */
#define mld_check_pct MLD_ADD_PARAM_SET(mld_check_pct)
#define mld_sample_s1_s2 MLD_ADD_PARAM_SET(mld_sample_s1_s2)
#define mld_validate_hash_length MLD_ADD_PARAM_SET(mld_validate_hash_length)
#define mld_get_hash_oid MLD_ADD_PARAM_SET(mld_get_hash_oid)
#define mld_H MLD_ADD_PARAM_SET(mld_H)
#define mld_attempt_signature_generation \
MLD_ADD_PARAM_SET(mld_attempt_signature_generation)
#define mld_compute_t0_t1_tr_from_sk_components \
MLD_ADD_PARAM_SET(mld_compute_t0_t1_tr_from_sk_components)
/* End of parameter set namespacing */
static int mld_check_pct(uint8_t const pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
uint8_t const sk[MLDSA_CRYPTO_SECRETKEYBYTES])
__contract__(
requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
requires(memory_no_alias(sk, MLDSA_CRYPTO_SECRETKEYBYTES))
ensures(return_value == 0
|| return_value == MLD_ERR_FAIL
|| return_value == MLD_ERR_OUT_OF_MEMORY)
);
#if defined(MLD_CONFIG_KEYGEN_PCT)
/*************************************************
* @[FIPS140_3_IG]
* (https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf)
*
* TE10.35.02: Pair-wise Consistency Test (PCT) for DSA keypairs
*
* Purpose: Validates that a generated public/private key pair can correctly
* sign and verify data. Test performs signature generation using the private
* key (sk), followed by signature verification using the public key (pk).
* Returns 0 if the signature was successfully verified, non-zero if it cannot.
*
* Note: @[FIPS204] requires that public/private key pairs are to be used only
* for the calculation and/of verification of digital signatures.
**************************************************/
static int mld_check_pct(uint8_t const pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
uint8_t const sk[MLDSA_CRYPTO_SECRETKEYBYTES])
{
MLD_ALIGN uint8_t message[1] = {0};
size_t siglen;
int ret;
MLD_ALLOC(signature, uint8_t, MLDSA_CRYPTO_BYTES);
MLD_ALLOC(pk_test, uint8_t, MLDSA_CRYPTO_PUBLICKEYBYTES);
if (signature == NULL || pk_test == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
/* Copy public key for testing */
mld_memcpy(pk_test, pk, MLDSA_CRYPTO_PUBLICKEYBYTES);
/* Sign a test message using the original secret key */
ret = crypto_sign_signature(signature, &siglen, message, sizeof(message),
NULL, 0, sk);
if (ret != 0)
{
goto cleanup;
}
#if defined(MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST)
/* Deliberately break public key for testing purposes */
if (mld_break_pct())
{
pk_test[0] = ~pk_test[0];
}
#endif /* MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST */
/* Verify the signature using the (potentially corrupted) public key */
ret = crypto_sign_verify(signature, siglen, message, sizeof(message), NULL, 0,
pk_test);
cleanup:
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
MLD_FREE(signature, uint8_t, MLDSA_CRYPTO_BYTES);
MLD_FREE(pk_test, uint8_t, MLDSA_CRYPTO_PUBLICKEYBYTES);
return ret;
}
#else /* MLD_CONFIG_KEYGEN_PCT */
static int mld_check_pct(uint8_t const pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
uint8_t const sk[MLDSA_CRYPTO_SECRETKEYBYTES])
{
/* Skip PCT */
((void)pk);
((void)sk);
return 0;
}
#endif /* !MLD_CONFIG_KEYGEN_PCT */
static void mld_sample_s1_s2(mld_polyvecl *s1, mld_polyveck *s2,
const uint8_t seed[MLDSA_CRHBYTES])
__contract__(
requires(memory_no_alias(s1, sizeof(mld_polyvecl)))
requires(memory_no_alias(s2, sizeof(mld_polyveck)))
requires(memory_no_alias(seed, MLDSA_CRHBYTES))
assigns(object_whole(s1), object_whole(s2))
ensures(forall(l0, 0, MLDSA_L, array_abs_bound(s1->vec[l0].coeffs, 0, MLDSA_N, MLDSA_ETA + 1)))
ensures(forall(k0, 0, MLDSA_K, array_abs_bound(s2->vec[k0].coeffs, 0, MLDSA_N, MLDSA_ETA + 1)))
)
{
/* Sample short vectors s1 and s2 */
#if defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
int i;
uint16_t nonce = 0;
/* Safety: The nonces are at most 14 (MLDSA_L + MLDSA_K - 1), and, hence, the
* casts are safe. */
for (i = 0; i < MLDSA_L; i++)
{
mld_poly_uniform_eta(&s1->vec[i], seed, (uint8_t)(nonce + i));
}
for (i = 0; i < MLDSA_K; i++)
{
mld_poly_uniform_eta(&s2->vec[i], seed, (uint8_t)(nonce + MLDSA_L + i));
}
#else /* MLD_CONFIG_SERIAL_FIPS202_ONLY */
#if MLD_CONFIG_PARAMETER_SET == 44
mld_poly_uniform_eta_4x(&s1->vec[0], &s1->vec[1], &s1->vec[2], &s1->vec[3],
seed, 0, 1, 2, 3);
mld_poly_uniform_eta_4x(&s2->vec[0], &s2->vec[1], &s2->vec[2], &s2->vec[3],
seed, 4, 5, 6, 7);
#elif MLD_CONFIG_PARAMETER_SET == 65
mld_poly_uniform_eta_4x(&s1->vec[0], &s1->vec[1], &s1->vec[2], &s1->vec[3],
seed, 0, 1, 2, 3);
mld_poly_uniform_eta_4x(&s1->vec[4], &s2->vec[0], &s2->vec[1],
&s2->vec[2] /* irrelevant */, seed, 4, 5, 6,
0xFF /* irrelevant */);
mld_poly_uniform_eta_4x(&s2->vec[2], &s2->vec[3], &s2->vec[4], &s2->vec[5],
seed, 7, 8, 9, 10);
#elif MLD_CONFIG_PARAMETER_SET == 87
mld_poly_uniform_eta_4x(&s1->vec[0], &s1->vec[1], &s1->vec[2], &s1->vec[3],
seed, 0, 1, 2, 3);
mld_poly_uniform_eta_4x(&s1->vec[4], &s1->vec[5], &s1->vec[6],
&s2->vec[0] /* irrelevant */, seed, 4, 5, 6,
0xFF /* irrelevant */);
mld_poly_uniform_eta_4x(&s2->vec[0], &s2->vec[1], &s2->vec[2], &s2->vec[3],
seed, 7, 8, 9, 10);
mld_poly_uniform_eta_4x(&s2->vec[4], &s2->vec[5], &s2->vec[6], &s2->vec[7],
seed, 11, 12, 13, 14);
#endif /* MLD_CONFIG_PARAMETER_SET == 87 */
#endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY */
}
/*************************************************
* Name: mld_compute_t0_t1_tr_from_sk_components
*
* Description: Computes t0, t1, tr, and pk from secret key components
* rho, s1, s2. This is the shared computation used by
* both keygen and generating the public key from the
* secret key.
*
* Arguments: - mld_polyveck *t0: output t0
* - mld_polyveck *t1: output t1
* - uint8_t tr[MLDSA_TRBYTES]: output tr
* - uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]: output public key
* - const uint8_t rho[MLDSA_SEEDBYTES]: input rho
* - const mld_polyvecl *s1: input s1
* - const mld_polyveck *s2: input s2
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static int mld_compute_t0_t1_tr_from_sk_components(
mld_polyveck *t0, mld_polyveck *t1, uint8_t tr[MLDSA_TRBYTES],
uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], const uint8_t rho[MLDSA_SEEDBYTES],
const mld_polyvecl *s1, const mld_polyveck *s2)
__contract__(
requires(memory_no_alias(t0, sizeof(mld_polyveck)))
requires(memory_no_alias(t1, sizeof(mld_polyveck)))
requires(memory_no_alias(tr, MLDSA_TRBYTES))
requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
requires(memory_no_alias(rho, MLDSA_SEEDBYTES))
requires(memory_no_alias(s1, sizeof(mld_polyvecl)))
requires(memory_no_alias(s2, sizeof(mld_polyveck)))
requires(forall(l0, 0, MLDSA_L, array_bound(s1->vec[l0].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1)))
requires(forall(k0, 0, MLDSA_K, array_bound(s2->vec[k0].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1)))
assigns(memory_slice(t0, sizeof(mld_polyveck)))
assigns(memory_slice(t1, sizeof(mld_polyveck)))
assigns(memory_slice(tr, MLDSA_TRBYTES))
assigns(memory_slice(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
ensures(forall(k1, 0, MLDSA_K, array_bound(t0->vec[k1].coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1)))
ensures(forall(k2, 0, MLDSA_K, array_bound(t1->vec[k2].coeffs, 0, MLDSA_N, 0, 1 << 10)))
ensures(return_value == 0 || return_value == MLD_ERR_OUT_OF_MEMORY))
{
int ret;
MLD_ALLOC(mat, mld_polymat, 1);
MLD_ALLOC(s1hat, mld_polyvecl, 1);
MLD_ALLOC(t, mld_polyveck, 1);
if (mat == NULL || s1hat == NULL || t == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
/* Expand matrix */
mld_polyvec_matrix_expand(mat, rho);
/* Matrix-vector multiplication */
*s1hat = *s1;
mld_polyvecl_ntt(s1hat);
mld_polyvec_matrix_pointwise_montgomery(t, mat, s1hat);
mld_polyveck_reduce(t);
mld_polyveck_invntt_tomont(t);
/* Add error vector s2 */
mld_polyveck_add(t, s2);
/* Reference: The following reduction is not present in the reference
* implementation. Omitting this reduction requires the output of
* the invntt to be small enough such that the addition of s2 does
* not result in absolute values >= MLDSA_Q. While our C, x86_64,
* and AArch64 invntt implementations produce small enough
* values for this to work out, it complicates the bounds
* reasoning. We instead add an additional reduction, and can
* consequently, relax the bounds requirements for the invntt.
*/
mld_polyveck_reduce(t);
/* Decompose to get t1, t0 */
mld_polyveck_caddq(t);
mld_polyveck_power2round(t1, t0, t);
/* Pack public key and compute tr */
mld_pack_pk(pk, rho, t1);
mld_shake256(tr, MLDSA_TRBYTES, pk, MLDSA_CRYPTO_PUBLICKEYBYTES);
ret = 0;
cleanup:
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
MLD_FREE(mat, mld_polymat, 1);
MLD_FREE(s1hat, mld_polyvecl, 1);
MLD_FREE(t, mld_polyveck, 1);
return ret;
}
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign_keypair_internal(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES],
const uint8_t seed[MLDSA_SEEDBYTES])
{
int ret;
const uint8_t *rho, *rhoprime, *key;
MLD_ALLOC(seedbuf, uint8_t, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES);
MLD_ALLOC(inbuf, uint8_t, MLDSA_SEEDBYTES + 2);
MLD_ALLOC(tr, uint8_t, MLDSA_TRBYTES);
MLD_ALLOC(s1, mld_polyvecl, 1);
MLD_ALLOC(s2, mld_polyveck, 1);
MLD_ALLOC(t1, mld_polyveck, 1);
MLD_ALLOC(t0, mld_polyveck, 1);
if (seedbuf == NULL || inbuf == NULL || tr == NULL || s1 == NULL ||
s2 == NULL || t1 == NULL || t0 == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
/* Get randomness for rho, rhoprime and key */
mld_memcpy(inbuf, seed, MLDSA_SEEDBYTES);
inbuf[MLDSA_SEEDBYTES + 0] = MLDSA_K;
inbuf[MLDSA_SEEDBYTES + 1] = MLDSA_L;
mld_shake256(seedbuf, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, inbuf,
MLDSA_SEEDBYTES + 2);
rho = seedbuf;
rhoprime = rho + MLDSA_SEEDBYTES;
key = rhoprime + MLDSA_CRHBYTES;
/* Constant time: rho is part of the public key and, hence, public. */
MLD_CT_TESTING_DECLASSIFY(rho, MLDSA_SEEDBYTES);
/* Sample s1 and s2 */
mld_sample_s1_s2(s1, s2, rhoprime);
/* Compute t0, t1, tr, and pk from rho, s1, s2 */
ret = mld_compute_t0_t1_tr_from_sk_components(t0, t1, tr, pk, rho, s1, s2);
if (ret != 0)
{
goto cleanup;
}
/* Pack secret key */
mld_pack_sk(sk, rho, tr, key, t0, s1, s2);
/* Constant time: pk is the public key, inherently public data */
MLD_CT_TESTING_DECLASSIFY(pk, MLDSA_CRYPTO_PUBLICKEYBYTES);
/* Pairwise Consistency Test (PCT) @[FIPS140_3_IG, p.87] */
ret = mld_check_pct(pk, sk);
cleanup:
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
MLD_FREE(seedbuf, uint8_t, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES);
MLD_FREE(inbuf, uint8_t, MLDSA_SEEDBYTES + 2);
MLD_FREE(tr, uint8_t, MLDSA_TRBYTES);
MLD_FREE(s1, mld_polyvecl, 1);
MLD_FREE(s2, mld_polyveck, 1);
MLD_FREE(t1, mld_polyveck, 1);
MLD_FREE(t0, mld_polyveck, 1);
return ret;
}
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API)
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign_keypair(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES])
{
MLD_ALIGN uint8_t seed[MLDSA_SEEDBYTES];
int ret;
mld_randombytes(seed, MLDSA_SEEDBYTES);
MLD_CT_TESTING_SECRET(seed, sizeof(seed));
ret = crypto_sign_keypair_internal(pk, sk, seed);
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
mld_zeroize(seed, sizeof(seed));
return ret;
}
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */
/*************************************************
* Name: mld_H
*
* Description: Abstracts application of SHAKE256 to
* one, two or three blocks of data,
* yielding a user-requested size of
* output.
*
* Arguments: - uint8_t *out: pointer to output
* - size_t outlen: requested output length in bytes
* - const uint8_t *in1: pointer to input block 1
* Must NOT be NULL
* - size_t in1len: length of input in1 bytes
* - const uint8_t *in2: pointer to input block 2
* May be NULL if in2len=0, in which case
* this block is ignored
* - size_t in2len: length of input in2 bytes
* - const uint8_t *in3: pointer to input block 3
* May be NULL if in3len=0, in which case
* this block is ignored
* - size_t in3len: length of input in3 bytes
**************************************************/
static void mld_H(uint8_t *out, size_t outlen, const uint8_t *in1,
size_t in1len, const uint8_t *in2, size_t in2len,
const uint8_t *in3, size_t in3len)
__contract__(
requires(in1len <= MLD_MAX_BUFFER_SIZE)
requires(in2len <= MLD_MAX_BUFFER_SIZE)
requires(in3len <= MLD_MAX_BUFFER_SIZE)
requires(outlen <= 8 * SHAKE256_RATE /* somewhat arbitrary bound */)
requires(memory_no_alias(in1, in1len))
requires(in2len == 0 || memory_no_alias(in2, in2len))
requires(in3len == 0 || memory_no_alias(in3, in3len))
requires(memory_no_alias(out, outlen))
assigns(memory_slice(out, outlen))
)
{
mld_shake256ctx state;
mld_shake256_init(&state);
mld_shake256_absorb(&state, in1, in1len);
if (in2len != 0)
{
mld_shake256_absorb(&state, in2, in2len);
}
if (in3len != 0)
{
mld_shake256_absorb(&state, in3, in3len);
}
mld_shake256_finalize(&state);
mld_shake256_squeeze(out, outlen, &state);
mld_shake256_release(&state);
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
mld_zeroize(&state, sizeof(state));
}
/* Reference: The reference implementation does not explicitly */
/* check the maximum nonce value, but instead loops indefinitely */
/* (even when the nonce would overflow). Internally, */
/* sampling of y uses (nonceL), (nonceL+1), ... (nonce*L+L-1). */
/* Hence, there are no overflows if nonce < (UINT16_MAX - L)/L. */
/* Explicitly checking for this explicitly allows us to prove */
/* type-safety. Note that FIPS204 explicitly allows an upper- */
/* bound this loop of 814 (< (UINT16_MAX - L)/L) - see */
/* @[FIPS204, Appendix C]. */
#define MLD_NONCE_UB ((UINT16_MAX - MLDSA_L) / MLDSA_L)
#ifdef CBMC
static void mld_cbmc_workaround_8813(mld_polyvecl **p1, mld_polyveck **p2)
__contract__(
requires(memory_no_alias(p1, sizeof(mld_polyvecl *)))
requires(memory_no_alias(p2, sizeof(mld_polyveck *)))
requires(memory_no_alias(*p2, sizeof(mld_polyveck)))
requires(*p1 == *p2)
assigns(*p1)
assigns(*p2)
ensures(memory_no_alias(*p2, sizeof(mld_polyveck)))
ensures(*p1 == *p2)
)
{
(void)p1;
(void)p2;
}
#endif /* CBMC */
/*************************************************
* Name: attempt_signature_generation
*
* Description: Attempts to generate a single signature.
*
* Arguments: - uint8_t *sig: pointer to output signature
* - const uint8_t *mu: pointer to message or hash
* of exactly MLDSA_CRHBYTES bytes
* - const uint8_t *rhoprime: pointer to randomness seed
* - uint16_t nonce: current nonce value
* - const mld_polymat *mat: expanded matrix
* - const polyvecl *s1: secret vector s1
* - const polyveck *s2: secret vector s2
* - const polyveck *t0: vector t0
*
* Returns: - 0: Signature generation succeeded
* - MLD_ERR_FAIL: Signature rejected (norm check failed)
* - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
* used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
*
* Reference: This code differs from the reference implementation
* in that it factors out the core signature generation
* step into a distinct function here in order to improve
* efficiency of CBMC proof.
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static int mld_attempt_signature_generation(
uint8_t sig[MLDSA_CRYPTO_BYTES], const uint8_t *mu,
const uint8_t rhoprime[MLDSA_CRHBYTES], uint16_t nonce,
const mld_polymat *mat, const mld_polyvecl *s1, const mld_polyveck *s2,
const mld_polyveck *t0)
__contract__(
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES))
requires(memory_no_alias(mu, MLDSA_CRHBYTES))
requires(memory_no_alias(rhoprime, MLDSA_CRHBYTES))
requires(memory_no_alias(mat, sizeof(mld_polymat)))
requires(memory_no_alias(s1, sizeof(mld_polyvecl)))
requires(memory_no_alias(s2, sizeof(mld_polyveck)))
requires(memory_no_alias(t0, sizeof(mld_polyveck)))
requires(nonce <= MLD_NONCE_UB)
requires(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
requires(forall(k2, 0, MLDSA_K, array_abs_bound(t0->vec[k2].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
requires(forall(k3, 0, MLDSA_L, array_abs_bound(s1->vec[k3].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
requires(forall(k4, 0, MLDSA_K, array_abs_bound(s2->vec[k4].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
ensures(return_value == 0 || return_value == MLD_ERR_FAIL ||
return_value == MLD_ERR_OUT_OF_MEMORY)
)
{
unsigned int n;
uint32_t z_invalid, w0_invalid, h_invalid;
int ret;
MLD_ALLOC(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES);
typedef union
{
mld_polyvecl y;
mld_polyveck h;
} u_yh;
MLD_ALLOC(yh, u_yh, 1);
MLD_ALLOC(z, mld_polyvecl, 1);
MLD_ALLOC(w1, mld_polyveck, 1);
MLD_ALLOC(w0, mld_polyveck, 1);
MLD_ALLOC(cp, mld_poly, 1);
mld_polyvecl *y = &yh->y;
mld_polyveck *h = &yh->h;
#ifdef CBMC
/* TODO: Remove once
* https://github.com/diffblue/cbmc/issues/8813 is resolved */
mld_cbmc_workaround_8813(&y, &h);
#endif
if (challenge_bytes == NULL || yh == NULL || z == NULL || w1 == NULL ||
w0 == NULL || cp == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
/* Sample intermediate vector y */
mld_polyvecl_uniform_gamma1(y, rhoprime, nonce);
/* Matrix-vector multiplication */
*z = *y;
mld_polyvecl_ntt(z);
mld_polyvec_matrix_pointwise_montgomery(w0, mat, z);
mld_polyveck_reduce(w0);
mld_polyveck_invntt_tomont(w0);
/* Decompose w and call the random oracle */
mld_polyveck_caddq(w0);
mld_polyveck_decompose(w1, w0);
mld_polyveck_pack_w1(sig, w1);
mld_H(challenge_bytes, MLDSA_CTILDEBYTES, mu, MLDSA_CRHBYTES, sig,
MLDSA_K * MLDSA_POLYW1_PACKEDBYTES, NULL, 0);
/* Constant time: Leaking challenge_bytes does not reveal any information
* about the secret key as H() is modelled as random oracle.
* This also applies to challenges for rejected signatures.
* See Section 5.5 of @[Round3_Spec]. */
MLD_CT_TESTING_DECLASSIFY(challenge_bytes, MLDSA_CTILDEBYTES);
mld_poly_challenge(cp, challenge_bytes);
mld_poly_ntt(cp);
/* Compute z, reject if it reveals secret */
mld_polyvecl_pointwise_poly_montgomery(z, cp, s1);
mld_polyvecl_invntt_tomont(z);
mld_polyvecl_add(z, y);
mld_polyvecl_reduce(z);
z_invalid = mld_polyvecl_chknorm(z, MLDSA_GAMMA1 - MLDSA_BETA);
/* Constant time: It is fine (and prohibitively expensive to avoid)
* leaking the result of the norm check. In case of rejection it
* would even be okay to leak which coefficient led to rejection
* as the candidate signature will be discarded anyway.
* See Section 5.5 of @[Round3_Spec]. */
MLD_CT_TESTING_DECLASSIFY(&z_invalid, sizeof(uint32_t));
if (z_invalid)
{
ret = MLD_ERR_FAIL; /* reject */
goto cleanup;
}
/* If z is valid, then its coefficients are bounded by */
/* MLDSA_GAMMA1 - MLDSA_BETA. This will be needed below */
/* to prove the pre-condition of pack_sig() */
mld_assert_abs_bound_2d(z->vec, MLDSA_L, MLDSA_N,
(MLDSA_GAMMA1 - MLDSA_BETA));
/* Check that subtracting cs2 does not change high bits of w and low bits
* do not reveal secret information */
mld_polyveck_pointwise_poly_montgomery(h, cp, s2);
mld_polyveck_invntt_tomont(h);
mld_polyveck_sub(w0, h);
mld_polyveck_reduce(w0);
w0_invalid = mld_polyveck_chknorm(w0, MLDSA_GAMMA2 - MLDSA_BETA);
/* Constant time: w0_invalid may be leaked - see comment for z_invalid. */
MLD_CT_TESTING_DECLASSIFY(&w0_invalid, sizeof(uint32_t));
if (w0_invalid)
{
ret = MLD_ERR_FAIL; /* reject */
goto cleanup;
}
/* Compute hints for w1 */
mld_polyveck_pointwise_poly_montgomery(h, cp, t0);
mld_polyveck_invntt_tomont(h);
mld_polyveck_reduce(h);
h_invalid = mld_polyveck_chknorm(h, MLDSA_GAMMA2);
/* Constant time: h_invalid may be leaked - see comment for z_invalid. */
MLD_CT_TESTING_DECLASSIFY(&h_invalid, sizeof(uint32_t));
if (h_invalid)
{
ret = MLD_ERR_FAIL; /* reject */
goto cleanup;
}
mld_polyveck_add(w0, h);
/* Constant time: At this point all norm checks have passed and we, hence,
* know that the signature does not leak any secret information.
* Consequently, any value that can be computed from the signature and public
* key is considered public.
* w0 and w1 are public as they can be computed from Az - ct = \alpha w1 + w0.
* h=c*t0 is public as both c and t0 are public.
* For a more detailed discussion, refer to https://eprint.iacr.org/2022/1406.
*/
MLD_CT_TESTING_DECLASSIFY(w0, sizeof(*w0));
MLD_CT_TESTING_DECLASSIFY(w1, sizeof(*w1));
n = mld_polyveck_make_hint(h, w0, w1);
if (n > MLDSA_OMEGA)
{
ret = MLD_ERR_FAIL; /* reject */
goto cleanup;
}
/* All is well - write signature */
/* Constant time: At this point it is clear that the signature is valid - it
* can, hence, be considered public. */
MLD_CT_TESTING_DECLASSIFY(h, sizeof(*h));
MLD_CT_TESTING_DECLASSIFY(z, sizeof(*z));
mld_pack_sig(sig, challenge_bytes, z, h, n);
ret = 0; /* success */
cleanup:
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
MLD_FREE(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES);
MLD_FREE(yh, u_yh, 1);
MLD_FREE(z, mld_polyvecl, 1);
MLD_FREE(w1, mld_polyveck, 1);
MLD_FREE(w0, mld_polyveck, 1);
MLD_FREE(cp, mld_poly, 1);
return ret;
}
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign_signature_internal(
uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen, const uint8_t *m,
size_t mlen, const uint8_t *pre, size_t prelen,
const uint8_t rnd[MLDSA_RNDBYTES],
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], int externalmu)
{
int ret;
uint8_t *rho, *tr, *key, *mu, *rhoprime;
uint16_t nonce = 0;
MLD_ALLOC(seedbuf, uint8_t,
2 * MLDSA_SEEDBYTES + MLDSA_TRBYTES + 2 * MLDSA_CRHBYTES);
MLD_ALLOC(mat, mld_polymat, 1);
MLD_ALLOC(s1, mld_polyvecl, 1);
MLD_ALLOC(t0, mld_polyveck, 1);
MLD_ALLOC(s2, mld_polyveck, 1);
if (seedbuf == NULL || mat == NULL || s1 == NULL || t0 == NULL || s2 == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
rho = seedbuf;
tr = rho + MLDSA_SEEDBYTES;
key = tr + MLDSA_TRBYTES;
mu = key + MLDSA_SEEDBYTES;
rhoprime = mu + MLDSA_CRHBYTES;
mld_unpack_sk(rho, tr, key, t0, s1, s2, sk);
if (!externalmu)
{
/* Compute mu = CRH(tr, pre, msg) */
mld_H(mu, MLDSA_CRHBYTES, tr, MLDSA_TRBYTES, pre, prelen, m, mlen);
}
else
{
/* mu has been provided directly */
mld_memcpy(mu, m, MLDSA_CRHBYTES);
}
/* Compute rhoprime = CRH(key, rnd, mu) */
mld_H(rhoprime, MLDSA_CRHBYTES, key, MLDSA_SEEDBYTES, rnd, MLDSA_RNDBYTES, mu,
MLDSA_CRHBYTES);
/* Constant time: rho is part of the public key and, hence, public. */
MLD_CT_TESTING_DECLASSIFY(rho, MLDSA_SEEDBYTES);
/* Expand matrix and transform vectors */
mld_polyvec_matrix_expand(mat, rho);
mld_polyvecl_ntt(s1);
mld_polyveck_ntt(s2);
mld_polyveck_ntt(t0);
/* By default, return failure. Flip to success and write output
* once signature generation succeeds. */
ret = MLD_ERR_FAIL;
/* Reference: This code is re-structured using a while(1), */
/* with explicit "continue" statements (rather than "goto") */
/* to implement rejection of invalid signatures. */
while (1)
__loop__(
assigns(nonce, ret, object_whole(siglen), memory_slice(sig, MLDSA_CRYPTO_BYTES))
invariant(nonce <= MLD_NONCE_UB)
/* t0, s1, s2, and mat are initialized above and are NOT changed by this */
/* loop. We can therefore re-assert their bounds here as part of the */
/* loop invariant. This makes proof noticeably faster with CBMC */
invariant(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
invariant(forall(k2, 0, MLDSA_K, array_abs_bound(t0->vec[k2].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
invariant(forall(k3, 0, MLDSA_L, array_abs_bound(s1->vec[k3].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
invariant(forall(k4, 0, MLDSA_K, array_abs_bound(s2->vec[k4].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
invariant(ret == MLD_ERR_FAIL)
)
{
/* Reference: this code explicitly checks for exhaustion of nonce */
/* values to provide predictable termination and results in that case */
/* Checking here also means that incrementing nonce below can also */
/* be proven to be type-safe. */
if (nonce == MLD_NONCE_UB)
{
/* Note that ret == MLD_ERR_FAIL by default, so we
* don't need to set it here. */
break;
}
ret = mld_attempt_signature_generation(sig, mu, rhoprime, nonce, mat, s1,
s2, t0);
nonce++;
if (ret == 0)
{
*siglen = MLDSA_CRYPTO_BYTES;
break;
}
else if (ret != MLD_ERR_FAIL)
{
/* For failures such as out-of-memory, propagate and exit immediately. */
break;
}
/* Otherwise, try again. */
}
cleanup:
if (ret != 0)
{
/* To be on the safe-side, we zeroize the signature buffer. */
*siglen = 0;
mld_memset(sig, 0, MLDSA_CRYPTO_BYTES);
}
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
MLD_FREE(seedbuf, uint8_t,
2 * MLDSA_SEEDBYTES + MLDSA_TRBYTES + 2 * MLDSA_CRHBYTES);
MLD_FREE(mat, mld_polymat, 1);
MLD_FREE(s1, mld_polyvecl, 1);
MLD_FREE(s2, mld_polyveck, 1);
MLD_FREE(t0, mld_polyveck, 1);
return ret;
}
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API)
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign_signature(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen,
const uint8_t *m, size_t mlen, const uint8_t *ctx,
size_t ctxlen,
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES])
{
size_t pre_len;
int ret;
MLD_ALLOC(pre, uint8_t, MLD_DOMAIN_SEPARATION_MAX_BYTES);
MLD_ALLOC(rnd, uint8_t, MLDSA_RNDBYTES);
if (pre == NULL || rnd == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
/* Prepare domain separation prefix for pure ML-DSA */
pre_len = mld_prepare_domain_separation_prefix(pre, NULL, 0, ctx, ctxlen,
MLD_PREHASH_NONE);
if (pre_len == 0)
{
ret = MLD_ERR_FAIL;
goto cleanup;
}
/* Randomized variant of ML-DSA. If you need the deterministic variant,
* call crypto_sign_signature_internal directly with all-zero rnd. */
mld_randombytes(rnd, MLDSA_RNDBYTES);
MLD_CT_TESTING_SECRET(rnd, sizeof(rnd));
ret = crypto_sign_signature_internal(sig, siglen, m, mlen, pre, pre_len, rnd,
sk, 0);
cleanup:
if (ret != 0)
{
/* To be on the safe-side, make sure *siglen and sig have a well-defined
* value, even in the case of error.
*
* If we come from crypto_sign_signature_internal, both are redundant,
* but the error case should not be the norm, and the added cost of the
* memset insignificant. */
*siglen = 0;
mld_memset(sig, 0, MLDSA_CRYPTO_BYTES);
}
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
MLD_FREE(pre, uint8_t, MLD_DOMAIN_SEPARATION_MAX_BYTES);
MLD_FREE(rnd, uint8_t, MLDSA_RNDBYTES);
return ret;
}
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API)
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign_signature_extmu(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen,
const uint8_t mu[MLDSA_CRHBYTES],
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES])
{
MLD_ALIGN uint8_t rnd[MLDSA_RNDBYTES];
int ret;
/* Randomized variant of ML-DSA. If you need the deterministic variant,
* call crypto_sign_signature_internal directly with all-zero rnd. */
mld_randombytes(rnd, MLDSA_RNDBYTES);
MLD_CT_TESTING_SECRET(rnd, sizeof(rnd));
ret = crypto_sign_signature_internal(sig, siglen, mu, MLDSA_CRHBYTES, NULL, 0,
rnd, sk, 1);
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
mld_zeroize(rnd, sizeof(rnd));
return ret;
}
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API)
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES])
{
int ret;
size_t i;
for (i = 0; i < mlen; ++i)
__loop__(
assigns(i, object_whole(sm))
invariant(i <= mlen)
)
{
sm[MLDSA_CRYPTO_BYTES + mlen - 1 - i] = m[mlen - 1 - i];
}
ret = crypto_sign_signature(sm, smlen, sm + MLDSA_CRYPTO_BYTES, mlen, ctx,
ctxlen, sk);
*smlen += mlen;
return ret;
}
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */
MLD_MUST_CHECK_RETURN_VALUE
MLD_EXTERNAL_API
int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
const uint8_t *m, size_t mlen,
const uint8_t *pre, size_t prelen,
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
int externalmu)
{
unsigned int i;
int ret;
MLD_ALLOC(buf, uint8_t, (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES));
MLD_ALLOC(rho, uint8_t, MLDSA_SEEDBYTES);
MLD_ALLOC(mu, uint8_t, MLDSA_CRHBYTES);
MLD_ALLOC(c, uint8_t, MLDSA_CTILDEBYTES);
MLD_ALLOC(c2, uint8_t, MLDSA_CTILDEBYTES);
MLD_ALLOC(cp, mld_poly, 1);
MLD_ALLOC(mat, mld_polymat, 1);
MLD_ALLOC(z, mld_polyvecl, 1);
MLD_ALLOC(t1, mld_polyveck, 1);
MLD_ALLOC(w1, mld_polyveck, 1);
MLD_ALLOC(tmp, mld_polyveck, 1);
MLD_ALLOC(h, mld_polyveck, 1);
if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL ||
cp == NULL || mat == NULL || z == NULL || t1 == NULL || w1 == NULL ||
tmp == NULL || h == NULL)
{
ret = MLD_ERR_OUT_OF_MEMORY;
goto cleanup;
}
if (siglen != MLDSA_CRYPTO_BYTES)
{
ret = MLD_ERR_FAIL;
goto cleanup;
}
mld_unpack_pk(rho, t1, pk);
/* mld_unpack_sig and mld_polyvecl_chknorm signal failure through a
* single non-zero error code that's not yet aligned with MLD_ERR_XXX.
* Map it to MLD_ERR_FAIL explicitly. */
if (mld_unpack_sig(c, z, h, sig))
{
ret = MLD_ERR_FAIL;
goto cleanup;
}
if (mld_polyvecl_chknorm(z, MLDSA_GAMMA1 - MLDSA_BETA))
{
ret = MLD_ERR_FAIL;
goto cleanup;
}
if (!externalmu)
{
/* Compute CRH(H(rho, t1), pre, msg) */
MLD_ALIGN uint8_t hpk[MLDSA_CRHBYTES];
mld_H(hpk, MLDSA_TRBYTES, pk, MLDSA_CRYPTO_PUBLICKEYBYTES, NULL, 0, NULL,
0);
mld_H(mu, MLDSA_CRHBYTES, hpk, MLDSA_TRBYTES, pre, prelen, m, mlen);
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
mld_zeroize(hpk, sizeof(hpk));
}
else
{
/* mu has been provided directly */
mld_memcpy(mu, m, MLDSA_CRHBYTES);
}
/* Matrix-vector multiplication; compute Az - c2^dt1 */
mld_poly_challenge(cp, c);
mld_polyvec_matrix_expand(mat, rho);
mld_polyvecl_ntt(z);
mld_polyvec_matrix_pointwise_montgomery(w1, mat, z);
mld_poly_ntt(cp);
mld_polyveck_shiftl(t1);
mld_polyveck_ntt(t1);
mld_polyveck_pointwise_poly_montgomery(tmp, cp, t1);
mld_polyveck_sub(w1, tmp);
mld_polyveck_reduce(w1);
mld_polyveck_invntt_tomont(w1);
/* Reconstruct w1 */
mld_polyveck_caddq(w1);
mld_polyveck_use_hint(tmp, w1, h);
mld_polyveck_pack_w1(buf, tmp);
/* Call random oracle and verify challenge */
mld_H(c2, MLDSA_CTILDEBYTES, mu, MLDSA_CRHBYTES, buf,
MLDSA_K * MLDSA_POLYW1_PACKEDBYTES, NULL, 0);
/* Constant time: All data in verification is usually considered public.
* However, in our constant-time tests we do not declassify the message and
* context string.
* The following conditional is the only place in verification whose run-time
* depends on the message. As all that can be leakaged here is the output of
* a hash call (that should behave like a random oracle), it is safe to
* declassify here even with a secret message.
*/
MLD_CT_TESTING_DECLASSIFY(c2, MLDSA_CTILDEBYTES);
ret = MLD_ERR_FAIL;
for (i = 0; i < MLDSA_CTILDEBYTES; ++i)
__loop__(
invariant(i <= MLDSA_CTILDEBYTES)
)
{
if (c[i] != c2[i])
{
goto cleanup;
}
}
ret = 0;