Skip to content
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

Introduce common coeff_signed_to_unsigned function in poly.c #53

Closed

Conversation

rod-chapman
Copy link
Contributor

@rod-chapman rod-chapman commented Jun 10, 2024

This PR introduces a common "coeff_signed_to_unsigned()" function in poly.c, its contracts and verification thereof using CBMC. This function reduces repetition of the same idiom in poly.c and simplifies the calling code in poly_compress() and other functions.

Additionally, this PR adds contract-based proof of scalar_compress_q_16 and scalar_compress_q_32

@rod-chapman rod-chapman requested a review from a team June 10, 2024 18:45
mlkem/poly.c Outdated
Comment on lines 111 to 118
int32_t factor = (r < 0); // 1 if r < 0; 0 if r >= 0
r = r + (factor * KYBER_Q);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's not unlikely that a compiler would introduce a branch here. One way to thwart this is by calculating r and r + KYBER_q and having a safe_cond_select() stub which, based on either (a) a mask that's 0 or -1, or (b) an int that's 0 or 1, picks one of the two arguments. This function would itself be a C stub that's ideally replaced by ASM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - we need a translation unit of safe constant-time functions like cond_select() and others. Disagree about the third parameter - it's a Boolean not a bleedin' Integer!

Copy link
Contributor

Choose a reason for hiding this comment

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

The multiplication factor*KYBER_Q you want will disappear when compiler optimize it.
https://godbolt.org/z/xaY39sdWr
So the compiler see our intentions and convert to:
r += (c >> 15) & KYBER_Q or r += (c >> 31) & KYBER_Q.
On other platform like AVR it's a branch condition.
I think the new style and the old style are equivalent in the eye of compiler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The semantics of the >> operator on a signed, negative integer is implementation-defined (see WG14 N2310 6.5.7(5) for example), so I prefer not to use it. Strictly speaking, the use of >> here defies verification with CBMC.

@hanno-becker
Copy link
Contributor

hanno-becker commented Jun 10, 2024

@rod-chapman You need DCO's for the CI to be happy. Running the other checks now.

@rod-chapman
Copy link
Contributor Author

What's a DCO?

@cothan
Copy link
Contributor

cothan commented Jun 10, 2024

Hi @rod-chapman , Thanks for the PR. the DCO is agreement to contribute, so it needs your signature.
You need to sign off your commit git commit -s -m "my commit".
When you click to the Details it shows what you need to do: https://github.com/pq-code-package/mlkem-c-aarch64/pull/53/checks?check_run_id=26040102532

@rod-chapman rod-chapman force-pushed the poly_compress_contracts branch from c22d9c1 to 4c53ce5 Compare June 11, 2024 08:19
@rod-chapman
Copy link
Contributor Author

OK... I have rebase my commits with --sign-off and pushed. Does that look better?

@hanno-becker
Copy link
Contributor

@rod-chapman Yes, all good.

@hanno-becker
Copy link
Contributor

@rod-chapman Can you rebase your PR on main?

@rod-chapman rod-chapman force-pushed the poly_compress_contracts branch from 4c53ce5 to 328f920 Compare June 11, 2024 08:32
@hanno-becker
Copy link
Contributor

@rod-chapman Linting fails (check the formatting of poly.[ch]), as well as CBMC.

@hanno-becker
Copy link
Contributor

@rod-chapman CBMC seems to be failing, can you have a look? Also, can you rebase on main and cleanup the history?

@rod-chapman rod-chapman force-pushed the poly_compress_contracts branch 2 times, most recently from 3e23577 to 06ce2a8 Compare June 17, 2024 09:25
@cothan
Copy link
Contributor

cothan commented Jun 20, 2024

@rod-chapman , can you do the honor, test the "update with rebase" button after #71 landed ?

@rod-chapman
Copy link
Contributor Author

Seemed to work OK!

Signed-off-by: Hanno Becker <[email protected]>
@hanno-becker
Copy link
Contributor

It looks like this PR needs manual rebase after https://github.com/pq-code-package/mlkem-c-aarch64/pull/67 was merged separately.

@hanno-becker hanno-becker force-pushed the poly_compress_contracts branch from 4aa85e0 to e939eaf Compare July 30, 2024 05:02
@rod-chapman
Copy link
Contributor Author

I think this branch and PR are dead and subsumed by #67. Work on other functions can begin afresh.

- scalar_compress_q_16
- scalar_compress_q_32
- poly_compress

Signed-off-by: Rod Chapman <[email protected]>
Signed-off-by: Hanno Becker <[email protected]>
@hanno-becker hanno-becker force-pushed the poly_compress_contracts branch from e939eaf to fdecdb2 Compare July 30, 2024 09:14
@hanno-becker
Copy link
Contributor

hanno-becker commented Jul 30, 2024

This PR did also introduce a loop-invariant based proof of poly_compress, which is not superseded by #53 and which I'd like to resurrect. However, after cleaning up the PR and rebasing onto main, CBMC fails on poly_compress, and I cannot seem to find legible descriptions of what is actually going wrong:

[15/17] poly_compress: checking safety properties                                                                                                                                                                     FAILED: ~/repos/mlkem-c-aarch64/cbmc/proofs/poly_compress/logs/result.xml ~/repos/mlkem-c-aarch64/cbmc/proofs/output/litani/runs/9c45a16a-0a7b-4254-b0a2-33390d2d7cc3/status/e87ddcc8-996c-4c98-9240-d6af84d2cd85.json

@rod-chapman Where would I find human-readable descriptions of the failure here? Can you reproduce the issue locally?

…rmal parameter r. Uses array indexing instead.

Signed-off-by: Rod Chapman <[email protected]>
2. Remove commented-out line in body of poly_compress()

Signed-off-by: Rod Chapman <[email protected]>
1. Introduce common num_blocks constant to avoid repetition of "KYBER_N / 8"
2. Add explanatory comment on switch from pointer arithmetic to array indexing
   in assignment to r[]
3. Introduce loop invariants for the KYBER_K=4 branch of the code.

Signed-off-by: Rod Chapman <[email protected]>
@rod-chapman rod-chapman closed this Aug 8, 2024
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.

3 participants