@@ -1082,51 +1082,53 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10821082 * https://github.com/diffblue/cbmc/issues/8813 */
10831083 typedef MLD_UNION_OR_STRUCT
10841084 {
1085+ mld_polyvecl z ;
1086+ mld_poly cp ;
1087+ }
1088+ zcp_u ;
1089+ mld_polyvecl * z ;
1090+ mld_poly * cp ;
1091+
1092+ /* TODO: Remove the following workaround for
1093+ * https://github.com/diffblue/cbmc/issues/8813 */
1094+ typedef MLD_UNION_OR_STRUCT
1095+ {
1096+ mld_polymat mat ;
10851097 mld_polyveck t1 ;
1086- mld_polyveck w1 ;
1098+ mld_polyveck tmp ;
1099+ mld_polyveck h ;
10871100 }
1088- t1w1_u ;
1089- mld_polyveck * t1 ;
1090- mld_polyveck * w1 ;
1101+ reuse_u ;
10911102
10921103 MLD_ALLOC (buf , uint8_t , (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES ), context );
10931104 MLD_ALLOC (rho , uint8_t , MLDSA_SEEDBYTES , context );
10941105 MLD_ALLOC (mu , uint8_t , MLDSA_CRHBYTES , context );
10951106 MLD_ALLOC (c , uint8_t , MLDSA_CTILDEBYTES , context );
10961107 MLD_ALLOC (c2 , uint8_t , MLDSA_CTILDEBYTES , context );
1097- MLD_ALLOC (cp , mld_poly , 1 , context );
1098- MLD_ALLOC (mat , mld_polymat , 1 , context );
1099- MLD_ALLOC (z , mld_polyvecl , 1 , context );
1100- MLD_ALLOC (t1w1 , t1w1_u , 1 , context );
1101- MLD_ALLOC (tmp , mld_polyveck , 1 , context );
1102- MLD_ALLOC (h , mld_polyveck , 1 , context );
1108+ MLD_ALLOC (zcp , zcp_u , 1 , context );
1109+ MLD_ALLOC (w1 , mld_polyveck , 1 , context );
1110+ MLD_ALLOC (reuse , reuse_u , 1 , context );
11031111
11041112 if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL ||
1105- cp == NULL || mat == NULL || z == NULL || t1w1 == NULL || tmp == NULL ||
1106- h == NULL )
1113+ zcp == NULL || w1 == NULL || reuse == NULL )
11071114 {
11081115 ret = MLD_ERR_OUT_OF_MEMORY ;
11091116 goto cleanup ;
11101117 }
1111- t1 = & t1w1 -> t1 ;
1112- w1 = & t1w1 -> w1 ;
1118+ z = & zcp -> z ;
1119+ cp = & zcp -> cp ;
11131120
11141121 if (siglen != MLDSA_CRYPTO_BYTES )
11151122 {
11161123 ret = MLD_ERR_FAIL ;
11171124 goto cleanup ;
11181125 }
11191126
1120- mld_unpack_pk (rho , t1 , pk );
1127+ mld_memcpy (rho , pk , MLDSA_SEEDBYTES );
1128+
1129+ mld_memcpy (c , sig , MLDSA_CTILDEBYTES );
1130+ mld_polyvecl_unpack_z (z , sig + MLDSA_CTILDEBYTES );
11211131
1122- /* mld_unpack_sig and mld_polyvecl_chknorm signal failure through a
1123- * single non-zero error code that's not yet aligned with MLD_ERR_XXX.
1124- * Map it to MLD_ERR_FAIL explicitly. */
1125- if (mld_unpack_sig (c , z , h , sig ))
1126- {
1127- ret = MLD_ERR_FAIL ;
1128- goto cleanup ;
1129- }
11301132 if (mld_polyvecl_chknorm (z , MLDSA_GAMMA1 - MLDSA_BETA ))
11311133 {
11321134 ret = MLD_ERR_FAIL ;
@@ -1151,23 +1153,31 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
11511153 }
11521154
11531155 /* Matrix-vector multiplication; compute Az - c2^dt1 */
1156+ mld_polyvecl_ntt (z );
1157+ mld_polyvec_matrix_expand (& reuse -> mat , rho );
1158+ mld_polyvec_matrix_pointwise_montgomery (w1 , & reuse -> mat , z );
1159+
11541160 mld_poly_challenge (cp , c );
11551161 mld_poly_ntt (cp );
1156- mld_polyveck_shiftl (t1 );
1157- mld_polyveck_ntt (t1 );
1158- mld_polyveck_pointwise_poly_montgomery (tmp , cp , t1 );
1159-
1160- mld_polyvec_matrix_expand (mat , rho );
1161- mld_polyvecl_ntt (z );
1162- mld_polyvec_matrix_pointwise_montgomery (w1 , mat , z );
1163- mld_polyveck_sub (w1 , tmp );
1162+ mld_unpack_pk (rho , & reuse -> t1 , pk );
1163+ mld_polyveck_shiftl (& reuse -> t1 );
1164+ mld_polyveck_ntt (& reuse -> t1 );
1165+ mld_polyveck_pointwise_poly_montgomery (& reuse -> tmp , cp , & reuse -> t1 );
1166+ mld_polyveck_sub (w1 , & reuse -> tmp );
11641167 mld_polyveck_reduce (w1 );
11651168 mld_polyveck_invntt_tomont (w1 );
11661169
11671170 /* Reconstruct w1 */
11681171 mld_polyveck_caddq (w1 );
1169- mld_polyveck_use_hint (tmp , w1 , h );
1170- mld_polyveck_pack_w1 (buf , tmp );
1172+ if (mld_unpack_hints (& reuse -> h , sig + MLDSA_CTILDEBYTES +
1173+ MLDSA_L * MLDSA_POLYZ_PACKEDBYTES ))
1174+ {
1175+ ret = MLD_ERR_FAIL ;
1176+ goto cleanup ;
1177+ }
1178+ mld_polyveck_use_hint (& reuse -> tmp , w1 , & reuse -> h );
1179+ mld_polyveck_pack_w1 (buf , & reuse -> tmp );
1180+
11711181 /* Call random oracle and verify challenge */
11721182 mld_H (c2 , MLDSA_CTILDEBYTES , mu , MLDSA_CRHBYTES , buf ,
11731183 MLDSA_K * MLDSA_POLYW1_PACKEDBYTES , NULL , 0 );
@@ -1181,12 +1191,9 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
11811191
11821192cleanup :
11831193 /* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
1184- MLD_FREE (h , mld_polyveck , 1 , context );
1185- MLD_FREE (tmp , mld_polyveck , 1 , context );
1186- MLD_FREE (t1w1 , t1w1_u , 1 , context );
1187- MLD_FREE (z , mld_polyvecl , 1 , context );
1188- MLD_FREE (mat , mld_polymat , 1 , context );
1189- MLD_FREE (cp , mld_poly , 1 , context );
1194+ MLD_FREE (reuse , reuse_u , 1 , context );
1195+ MLD_FREE (w1 , mld_polyveck , 1 , context );
1196+ MLD_FREE (zcp , zcp_u , 1 , context );
11901197 MLD_FREE (c2 , uint8_t , MLDSA_CTILDEBYTES , context );
11911198 MLD_FREE (c , uint8_t , MLDSA_CTILDEBYTES , context );
11921199 MLD_FREE (mu , uint8_t , MLDSA_CRHBYTES , context );
0 commit comments