@@ -987,51 +987,53 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
987987 * https://github.com/diffblue/cbmc/issues/8813 */
988988 typedef MLD_UNION_OR_STRUCT
989989 {
990+ mld_polyvecl z ;
991+ mld_poly cp ;
992+ }
993+ zcp_u ;
994+ mld_polyvecl * z ;
995+ mld_poly * cp ;
996+
997+ /* TODO: Remove the following workaround for
998+ * https://github.com/diffblue/cbmc/issues/8813 */
999+ typedef MLD_UNION_OR_STRUCT
1000+ {
1001+ mld_polymat mat ;
9901002 mld_polyveck t1 ;
991- mld_polyveck w1 ;
1003+ mld_polyveck tmp ;
1004+ mld_polyveck h ;
9921005 }
993- t1w1_u ;
994- mld_polyveck * t1 ;
995- mld_polyveck * w1 ;
1006+ reuse_u ;
9961007
9971008 MLD_ALLOC (buf , uint8_t , (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES ), context );
9981009 MLD_ALLOC (rho , uint8_t , MLDSA_SEEDBYTES , context );
9991010 MLD_ALLOC (mu , uint8_t , MLDSA_CRHBYTES , context );
10001011 MLD_ALLOC (c , uint8_t , MLDSA_CTILDEBYTES , context );
10011012 MLD_ALLOC (c2 , uint8_t , MLDSA_CTILDEBYTES , context );
1002- MLD_ALLOC (cp , mld_poly , 1 , context );
1003- MLD_ALLOC (mat , mld_polymat , 1 , context );
1004- MLD_ALLOC (z , mld_polyvecl , 1 , context );
1005- MLD_ALLOC (t1w1 , t1w1_u , 1 , context );
1006- MLD_ALLOC (tmp , mld_polyveck , 1 , context );
1007- MLD_ALLOC (h , mld_polyveck , 1 , context );
1013+ MLD_ALLOC (zcp , zcp_u , 1 , context );
1014+ MLD_ALLOC (w1 , mld_polyveck , 1 , context );
1015+ MLD_ALLOC (reuse , reuse_u , 1 , context );
10081016
10091017 if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL ||
1010- cp == NULL || mat == NULL || z == NULL || t1w1 == NULL || tmp == NULL ||
1011- h == NULL )
1018+ zcp == NULL || w1 == NULL || reuse == NULL )
10121019 {
10131020 ret = MLD_ERR_OUT_OF_MEMORY ;
10141021 goto cleanup ;
10151022 }
1016- t1 = & t1w1 -> t1 ;
1017- w1 = & t1w1 -> w1 ;
1023+ z = & zcp -> z ;
1024+ cp = & zcp -> cp ;
10181025
10191026 if (siglen != MLDSA_CRYPTO_BYTES )
10201027 {
10211028 ret = MLD_ERR_FAIL ;
10221029 goto cleanup ;
10231030 }
10241031
1025- mld_unpack_pk (rho , t1 , pk );
1032+ mld_memcpy (rho , pk , MLDSA_SEEDBYTES );
1033+
1034+ mld_memcpy (c , sig , MLDSA_CTILDEBYTES );
1035+ mld_polyvecl_unpack_z (z , sig + MLDSA_CTILDEBYTES );
10261036
1027- /* mld_unpack_sig and mld_polyvecl_chknorm signal failure through a
1028- * single non-zero error code that's not yet aligned with MLD_ERR_XXX.
1029- * Map it to MLD_ERR_FAIL explicitly. */
1030- if (mld_unpack_sig (c , z , h , sig ))
1031- {
1032- ret = MLD_ERR_FAIL ;
1033- goto cleanup ;
1034- }
10351037 if (mld_polyvecl_chknorm (z , MLDSA_GAMMA1 - MLDSA_BETA ))
10361038 {
10371039 ret = MLD_ERR_FAIL ;
@@ -1056,23 +1058,31 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10561058 }
10571059
10581060 /* Matrix-vector multiplication; compute Az - c2^dt1 */
1061+ mld_polyvecl_ntt (z );
1062+ mld_polyvec_matrix_expand (& reuse -> mat , rho );
1063+ mld_polyvec_matrix_pointwise_montgomery (w1 , & reuse -> mat , z );
1064+
10591065 mld_poly_challenge (cp , c );
10601066 mld_poly_ntt (cp );
1061- mld_polyveck_shiftl (t1 );
1062- mld_polyveck_ntt (t1 );
1063- mld_polyveck_pointwise_poly_montgomery (tmp , cp , t1 );
1064-
1065- mld_polyvec_matrix_expand (mat , rho );
1066- mld_polyvecl_ntt (z );
1067- mld_polyvec_matrix_pointwise_montgomery (w1 , mat , z );
1068- mld_polyveck_sub (w1 , tmp );
1067+ mld_unpack_pk (rho , & reuse -> t1 , pk );
1068+ mld_polyveck_shiftl (& reuse -> t1 );
1069+ mld_polyveck_ntt (& reuse -> t1 );
1070+ mld_polyveck_pointwise_poly_montgomery (& reuse -> tmp , cp , & reuse -> t1 );
1071+ mld_polyveck_sub (w1 , & reuse -> tmp );
10691072 mld_polyveck_reduce (w1 );
10701073 mld_polyveck_invntt_tomont (w1 );
10711074
10721075 /* Reconstruct w1 */
10731076 mld_polyveck_caddq (w1 );
1074- mld_polyveck_use_hint (tmp , w1 , h );
1075- mld_polyveck_pack_w1 (buf , tmp );
1077+ if (mld_unpack_hints (& reuse -> h , sig + MLDSA_CTILDEBYTES +
1078+ MLDSA_L * MLDSA_POLYZ_PACKEDBYTES ))
1079+ {
1080+ ret = MLD_ERR_FAIL ;
1081+ goto cleanup ;
1082+ }
1083+ mld_polyveck_use_hint (& reuse -> tmp , w1 , & reuse -> h );
1084+ mld_polyveck_pack_w1 (buf , & reuse -> tmp );
1085+
10761086 /* Call random oracle and verify challenge */
10771087 mld_H (c2 , MLDSA_CTILDEBYTES , mu , MLDSA_CRHBYTES , buf ,
10781088 MLDSA_K * MLDSA_POLYW1_PACKEDBYTES , NULL , 0 );
@@ -1086,12 +1096,9 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10861096
10871097cleanup :
10881098 /* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
1089- MLD_FREE (h , mld_polyveck , 1 , context );
1090- MLD_FREE (tmp , mld_polyveck , 1 , context );
1091- MLD_FREE (t1w1 , t1w1_u , 1 , context );
1092- MLD_FREE (z , mld_polyvecl , 1 , context );
1093- MLD_FREE (mat , mld_polymat , 1 , context );
1094- MLD_FREE (cp , mld_poly , 1 , context );
1099+ MLD_FREE (reuse , reuse_u , 1 , context );
1100+ MLD_FREE (w1 , mld_polyveck , 1 , context );
1101+ MLD_FREE (zcp , zcp_u , 1 , context );
10951102 MLD_FREE (c2 , uint8_t , MLDSA_CTILDEBYTES , context );
10961103 MLD_FREE (c , uint8_t , MLDSA_CTILDEBYTES , context );
10971104 MLD_FREE (mu , uint8_t , MLDSA_CRHBYTES , context );
0 commit comments