@@ -1024,51 +1024,53 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10241024 * https://github.com/diffblue/cbmc/issues/8813 */
10251025 typedef MLD_UNION_OR_STRUCT
10261026 {
1027+ mld_polyvecl z ;
1028+ mld_poly cp ;
1029+ }
1030+ zcp_u ;
1031+ mld_polyvecl * z ;
1032+ mld_poly * cp ;
1033+
1034+ /* TODO: Remove the following workaround for
1035+ * https://github.com/diffblue/cbmc/issues/8813 */
1036+ typedef MLD_UNION_OR_STRUCT
1037+ {
1038+ mld_polymat mat ;
10271039 mld_polyveck t1 ;
1028- mld_polyveck w1 ;
1040+ mld_polyveck tmp ;
1041+ mld_polyveck h ;
10291042 }
1030- t1w1_u ;
1031- mld_polyveck * t1 ;
1032- mld_polyveck * w1 ;
1043+ reuse_u ;
10331044
10341045 MLD_ALLOC (buf , uint8_t , (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES ), context );
10351046 MLD_ALLOC (rho , uint8_t , MLDSA_SEEDBYTES , context );
10361047 MLD_ALLOC (mu , uint8_t , MLDSA_CRHBYTES , context );
10371048 MLD_ALLOC (c , uint8_t , MLDSA_CTILDEBYTES , context );
10381049 MLD_ALLOC (c2 , uint8_t , MLDSA_CTILDEBYTES , context );
1039- MLD_ALLOC (cp , mld_poly , 1 , context );
1040- MLD_ALLOC (mat , mld_polymat , 1 , context );
1041- MLD_ALLOC (z , mld_polyvecl , 1 , context );
1042- MLD_ALLOC (t1w1 , t1w1_u , 1 , context );
1043- MLD_ALLOC (tmp , mld_polyveck , 1 , context );
1044- MLD_ALLOC (h , mld_polyveck , 1 , context );
1050+ MLD_ALLOC (zcp , zcp_u , 1 , context );
1051+ MLD_ALLOC (w1 , mld_polyveck , 1 , context );
1052+ MLD_ALLOC (reuse , reuse_u , 1 , context );
10451053
10461054 if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL ||
1047- cp == NULL || mat == NULL || z == NULL || t1w1 == NULL || tmp == NULL ||
1048- h == NULL )
1055+ zcp == NULL || w1 == NULL || reuse == NULL )
10491056 {
10501057 ret = MLD_ERR_OUT_OF_MEMORY ;
10511058 goto cleanup ;
10521059 }
1053- t1 = & t1w1 -> t1 ;
1054- w1 = & t1w1 -> w1 ;
1060+ z = & zcp -> z ;
1061+ cp = & zcp -> cp ;
10551062
10561063 if (siglen != MLDSA_CRYPTO_BYTES )
10571064 {
10581065 ret = MLD_ERR_FAIL ;
10591066 goto cleanup ;
10601067 }
10611068
1062- mld_unpack_pk (rho , t1 , pk );
1069+ mld_memcpy (rho , pk , MLDSA_SEEDBYTES );
1070+
1071+ mld_memcpy (c , sig , MLDSA_CTILDEBYTES );
1072+ mld_polyvecl_unpack_z (z , sig + MLDSA_CTILDEBYTES );
10631073
1064- /* mld_unpack_sig and mld_polyvecl_chknorm signal failure through a
1065- * single non-zero error code that's not yet aligned with MLD_ERR_XXX.
1066- * Map it to MLD_ERR_FAIL explicitly. */
1067- if (mld_unpack_sig (c , z , h , sig ))
1068- {
1069- ret = MLD_ERR_FAIL ;
1070- goto cleanup ;
1071- }
10721074 if (mld_polyvecl_chknorm (z , MLDSA_GAMMA1 - MLDSA_BETA ))
10731075 {
10741076 ret = MLD_ERR_FAIL ;
@@ -1093,23 +1095,31 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10931095 }
10941096
10951097 /* Matrix-vector multiplication; compute Az - c2^dt1 */
1098+ mld_polyvecl_ntt (z );
1099+ mld_polyvec_matrix_expand (& reuse -> mat , rho );
1100+ mld_polyvec_matrix_pointwise_montgomery (w1 , & reuse -> mat , z );
1101+
10961102 mld_poly_challenge (cp , c );
10971103 mld_poly_ntt (cp );
1098- mld_polyveck_shiftl (t1 );
1099- mld_polyveck_ntt (t1 );
1100- mld_polyveck_pointwise_poly_montgomery (tmp , cp , t1 );
1101-
1102- mld_polyvec_matrix_expand (mat , rho );
1103- mld_polyvecl_ntt (z );
1104- mld_polyvec_matrix_pointwise_montgomery (w1 , mat , z );
1105- mld_polyveck_sub (w1 , tmp );
1104+ mld_unpack_pk (rho , & reuse -> t1 , pk );
1105+ mld_polyveck_shiftl (& reuse -> t1 );
1106+ mld_polyveck_ntt (& reuse -> t1 );
1107+ mld_polyveck_pointwise_poly_montgomery (& reuse -> tmp , cp , & reuse -> t1 );
1108+ mld_polyveck_sub (w1 , & reuse -> tmp );
11061109 mld_polyveck_reduce (w1 );
11071110 mld_polyveck_invntt_tomont (w1 );
11081111
11091112 /* Reconstruct w1 */
11101113 mld_polyveck_caddq (w1 );
1111- mld_polyveck_use_hint (tmp , w1 , h );
1112- mld_polyveck_pack_w1 (buf , tmp );
1114+ if (mld_unpack_hints (& reuse -> h , sig + MLDSA_CTILDEBYTES +
1115+ MLDSA_L * MLDSA_POLYZ_PACKEDBYTES ))
1116+ {
1117+ ret = MLD_ERR_FAIL ;
1118+ goto cleanup ;
1119+ }
1120+ mld_polyveck_use_hint (& reuse -> tmp , w1 , & reuse -> h );
1121+ mld_polyveck_pack_w1 (buf , & reuse -> tmp );
1122+
11131123 /* Call random oracle and verify challenge */
11141124 mld_H (c2 , MLDSA_CTILDEBYTES , mu , MLDSA_CRHBYTES , buf ,
11151125 MLDSA_K * MLDSA_POLYW1_PACKEDBYTES , NULL , 0 );
@@ -1123,12 +1133,9 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
11231133
11241134cleanup :
11251135 /* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
1126- MLD_FREE (h , mld_polyveck , 1 , context );
1127- MLD_FREE (tmp , mld_polyveck , 1 , context );
1128- MLD_FREE (t1w1 , t1w1_u , 1 , context );
1129- MLD_FREE (z , mld_polyvecl , 1 , context );
1130- MLD_FREE (mat , mld_polymat , 1 , context );
1131- MLD_FREE (cp , mld_poly , 1 , context );
1136+ MLD_FREE (reuse , reuse_u , 1 , context );
1137+ MLD_FREE (w1 , mld_polyveck , 1 , context );
1138+ MLD_FREE (zcp , zcp_u , 1 , context );
11321139 MLD_FREE (c2 , uint8_t , MLDSA_CTILDEBYTES , context );
11331140 MLD_FREE (c , uint8_t , MLDSA_CTILDEBYTES , context );
11341141 MLD_FREE (mu , uint8_t , MLDSA_CRHBYTES , context );
0 commit comments