@@ -100,75 +100,51 @@ void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
100100}
101101
102102MLD_INTERNAL_API
103- void mld_pack_sig_c_h (uint8_t sig [MLDSA_CRYPTO_BYTES ],
104- const uint8_t c [MLDSA_CTILDEBYTES ], const mld_polyveck * h ,
105- const unsigned int number_of_hints )
103+ void mld_pack_sig_c (uint8_t sig [MLDSA_CRYPTO_BYTES ],
104+ const uint8_t c [MLDSA_CTILDEBYTES ])
106105{
107- unsigned int i , j , k ;
108-
109106 mld_memcpy (sig , c , MLDSA_CTILDEBYTES );
110- sig += MLDSA_CTILDEBYTES ;
111-
112- /* skip z component - packed via mld_pack_sig_z */
113- sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES ;
107+ }
114108
115- /* Encode hints h */
109+ MLD_INTERNAL_API
110+ void mld_pack_sig_h_poly (uint8_t sig [MLDSA_CRYPTO_BYTES ], const mld_poly * h ,
111+ unsigned int k , unsigned int n )
112+ {
113+ unsigned int j ;
116114
117- /* The final section of sig[] is MLDSA_POLYVECH_PACKEDBYTES long, where
118- * MLDSA_POLYVECH_PACKEDBYTES = MLDSA_OMEGA + MLDSA_K
115+ /* The hint section of sig[] is MLDSA_POLYVECH_PACKEDBYTES long, where
116+ * MLDSA_POLYVECH_PACKEDBYTES = MLDSA_OMEGA + MLDSA_K.
119117 *
120118 * The first OMEGA bytes record the index numbers of the coefficients
121- * that are not equal to 0
119+ * that are not equal to 0.
122120 *
123121 * The final K bytes record a running tally of the number of hints
124- * coming from each of the K polynomials in h.
125- *
126- * The pre-condition tells us that number_of_hints <= OMEGA, so some
127- * bytes may not be written, so we initialize all of them to zero
128- * to start.
129- */
130- mld_memset (sig , 0 , MLDSA_POLYVECH_PACKEDBYTES );
131-
132- k = 0 ;
133- /* For each polynomial in h... */
134- for (i = 0 ; i < MLDSA_K ; ++ i )
122+ * coming from each of the K polynomials in h. */
123+ uint8_t * sig_h = sig + MLDSA_CTILDEBYTES + MLDSA_L * MLDSA_POLYZ_PACKEDBYTES ;
124+
125+ /* For each coefficient in this polynomial, record it as a hint */
126+ /* if its value is not zero. */
127+ for (j = 0 ; j < MLDSA_N ; j ++ )
135128 __loop__ (
136- assigns (i , j , k , memory_slice (sig , MLDSA_POLYVECH_PACKEDBYTES ))
137- invariant (i <= MLDSA_K )
138- invariant (k <= number_of_hints )
139- invariant (number_of_hints <= MLDSA_OMEGA )
140- decreases (MLDSA_K - i )
129+ assigns (j , n , memory_slice (sig_h , MLDSA_POLYVECH_PACKEDBYTES ))
130+ invariant (j <= MLDSA_N )
131+ invariant (n <= MLDSA_OMEGA )
132+ decreases (MLDSA_N - j )
141133 )
142134 {
143- /* For each coefficient in that polynomial, record it as as hint */
144- /* if its value is not zero */
145- for (j = 0 ; j < MLDSA_N ; ++ j )
146- __loop__ (
147- assigns (j , k , memory_slice (sig , MLDSA_POLYVECH_PACKEDBYTES ))
148- invariant (i <= MLDSA_K )
149- invariant (j <= MLDSA_N )
150- invariant (k <= number_of_hints )
151- invariant (number_of_hints <= MLDSA_OMEGA )
152- decreases (MLDSA_N - j )
153- )
135+ /* The reference implementation implicitly relies on the total */
136+ /* number of hints being less than OMEGA, assuming h is valid. */
137+ /* In mldsa-native, we check this explicitly to ease proof of */
138+ /* type safety. */
139+ if (h -> coeffs [j ] != 0 && n < MLDSA_OMEGA )
154140 {
155- /* The reference implementation implicitly relies on the total */
156- /* number of hints being less than OMEGA, assuming h is valid. */
157- /* In mldsa-native, we check this explicitly to ease proof of */
158- /* type safety. */
159- if (h -> vec [i ].coeffs [j ] != 0 && k < number_of_hints )
160- {
161- /* The enclosing if condition AND the loop invariant infer */
162- /* that k < MLDSA_OMEGA, so writing to sig[k] is safe and k */
163- /* can be incremented. */
164- sig [k ++ ] = (uint8_t )j ;
165- }
141+ sig_h [n ] = (uint8_t )j ;
142+ n ++ ;
166143 }
167- /* Having recorded all the hints for this polynomial, also */
168- /* record the running tally into the correct "slot" for that */
169- /* coefficient in the final K bytes */
170- sig [MLDSA_OMEGA + i ] = (uint8_t )k ;
171144 }
145+ /* Record the running tally into the correct slot for this */
146+ /* polynomial in the final K bytes. */
147+ sig_h [MLDSA_OMEGA + k ] = (uint8_t )n ;
172148}
173149
174150MLD_INTERNAL_API
0 commit comments