Skip to content

Add debug assertions for reduce.c and polyvec.c #216 #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

willieyz
Copy link
Contributor

@willieyz willieyz commented Aug 7, 2025

  • Resolves Add debug assertions for reduce.c and polyvec.c #216

  • All implementations of functions in poly.c, polyvec.c have bounds assertions
    for inputs and outputs added that match the CBMC specifications.

  • As for mlkem-native, if CBMC is enabled, the bounds assertions
    are interpreted as CBMC proof obligations.

@willieyz willieyz force-pushed the cbmc-bound-assertions branch 7 times, most recently from 6ed6054 to 407a414 Compare August 8, 2025 02:39
- This commit is ported from mlkem PR #214
- All implementations of functions in poly.c have bounds assertions for inputs and outputs added that match the CBMC specifications.

Signed-off-by: willieyz <[email protected]>
@willieyz willieyz force-pushed the cbmc-bound-assertions branch from 407a414 to a4cfae5 Compare August 8, 2025 04:02
@willieyz willieyz changed the title Debug: Add bounds assertions macros (port mlkem-native #214) Debug: Add bounds assertions macros (port PR#214) Aug 8, 2025
@willieyz willieyz changed the title Debug: Add bounds assertions macros (port PR#214) Add debug assertions for reduce.c and polyvec.c #216 Aug 8, 2025
@willieyz willieyz force-pushed the cbmc-bound-assertions branch 5 times, most recently from b99b71f to 1a95672 Compare August 11, 2025 08:53
@willieyz willieyz marked this pull request as ready for review August 11, 2025 09:19
@willieyz willieyz requested a review from a team as a code owner August 11, 2025 09:19
Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @willieyz. This is a nice addition!
There are still quite a few things that could be improved.

mldsa/polyvec.c Outdated
@@ -384,11 +421,15 @@ void mld_polyveck_sub(mld_polyveck *u, const mld_polyveck *v)
{
mld_poly_sub(&u->vec[i], &v->vec[i]);
}

mld_assert_bound_2d(u->vec, MLDSA_K, MLDSA_N, (int64_t)INT32_MIN,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast should not be needed.

mldsa/polyvec.c Outdated
}

void mld_polyvecl_reduce(mld_polyvecl *v)
{
unsigned int i;
mld_assert_bound_2d(v->vec, MLDSA_L, MLDSA_N, (int64_t)INT32_MIN,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast should not be needed.

@@ -74,6 +74,8 @@ void mld_poly_add(mld_poly *r, const mld_poly *b)
void mld_poly_sub(mld_poly *r, const mld_poly *b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you miss mld_poly_add or was that intentionally skipped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkannwischer , I leave this intentionally, because there is no any array_bound or array_abs_bound post-condition in the contract of mld_poly_add.
It do have some post-condition, but none of those are array_bound and array_abs_bound.
or should I add assertion for it according to those?

mldsa/poly.c Outdated
@@ -379,6 +393,8 @@ void mld_poly_uniform(mld_poly *a, const uint8_t seed[MLDSA_SEEDBYTES + 2])

/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
mld_zeroize(buf, sizeof(buf));

mld_assert_bound(a->coeffs, MLDSA_N, 0, MLDSA_Q);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have all assertions before the zeroize call please? Just to be consistent with mlkem-native that zeroize is the last step in the function.

Copy link
Contributor Author

@willieyz willieyz Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, Thank you for your reviewing!

mldsa/poly.c Outdated
@@ -84,6 +86,8 @@ void mld_poly_sub(mld_poly *r, const mld_poly *b)
{
r->coeffs[i] = r->coeffs[i] - b->coeffs[i];
}

mld_assert_bound(r->coeffs, MLDSA_N, (int64_t)INT32_MIN, REDUCE32_DOMAIN_MAX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast should not be needed

}

void mld_polyvecl_pointwise_acc_montgomery(mld_poly *w, const mld_polyvecl *u,
const mld_polyvecl *v)
{
unsigned int i, j;
mld_assert_bound_2d(u->vec, MLDSA_L, MLDSA_N, 0, MLDSA_Q);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this missing a pre-condition on v?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry about the miss..
I had fixed it now, thank you for your reviewing!

@@ -372,6 +407,8 @@ void mld_polyveck_add(mld_polyveck *u, const mld_polyveck *v)
void mld_polyveck_sub(mld_polyveck *u, const mld_polyveck *v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mld_polyveck_add can have a post-condition in the contract - just like mld_polyvecl_add. That should be added and also be asserted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thank you for your reviewing!

mldsa/polyvec.c Outdated
@@ -431,12 +477,15 @@ void mld_polyveck_invntt_tomont(mld_polyveck *v)
{
mld_poly_invntt_tomont(&v->vec[i]);
}

mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLD_NTT_BOUND);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be INTT_BOUND.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the assertion for mld_poly_reduce now be added?

@@ -141,6 +146,9 @@ void mld_poly_pointwise_montgomery(mld_poly *c, const mld_poly *a,
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mld_poly_invntt_tomont should have an MLD_INTT_BOUND output bound.

@willieyz willieyz marked this pull request as draft August 13, 2025 08:57
@willieyz willieyz force-pushed the cbmc-bound-assertions branch 2 times, most recently from 8b12dbf to 7e05da5 Compare August 15, 2025 01:41
- This commit is ported from mlkem PR #214
- All implementations of functions in polyvec.c have bounds assertions for inputs and outputs added that match the CBMC specifications.

Signed-off-by: willieyz <[email protected]>
@willieyz willieyz force-pushed the cbmc-bound-assertions branch from 7e05da5 to d36b82b Compare August 15, 2025 02:03
@willieyz willieyz force-pushed the cbmc-bound-assertions branch from d36b82b to 2ed2e64 Compare August 21, 2025 05:25
Signed-off-by: willieyz <[email protected]>
@willieyz willieyz force-pushed the cbmc-bound-assertions branch from 494c599 to f3858ea Compare August 22, 2025 07:10
Signed-off-by: willieyz <[email protected]>
Signed-off-by: willieyz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add debug assertions for reduce.c and polyvec.c
2 participants