Skip to content

CBMC: Add contracts and proofs for various polyvec functions #151

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

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

Conversation

mkannwischer
Copy link
Contributor

@mkannwischer mkannwischer commented Apr 12, 2025

polyvecl_add, polyveck_add, polyveck_sub,polyveck_use_hint, polyveck_reduce, polyvecl_reduce were too slow when using unrolling - I instead did a direct proof with the workaround from #92. I added them to #102 to remove the whole-struct assignments once the corresponding CBMC issue is resolved.

@mkannwischer mkannwischer force-pushed the cbmc-polyvec-proofs branch 4 times, most recently from 29b7232 to 5c4cfad Compare April 13, 2025 14:46
@mkannwischer mkannwischer marked this pull request as ready for review April 13, 2025 14:47
@mkannwischer mkannwischer requested a review from a team as a code owner April 13, 2025 14:47
@mkannwischer mkannwischer force-pushed the cbmc-polyvec-proofs branch 2 times, most recently from d60ef12 to 0049ad3 Compare April 16, 2025 08:44
@mkannwischer
Copy link
Contributor Author

I removed the proofs from here that don't have acceptable run time yet and moved them to #169.
This way we can merge this one first.

@mkannwischer mkannwischer force-pushed the cbmc-polyvec-proofs branch 2 times, most recently from cdec6c2 to 6050f70 Compare April 16, 2025 09:39
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Arm Cortex-A72 (Raspberry Pi 4) benchmarks (opt)

Benchmark suite Current: 6050f70 Previous: db0c059 Ratio
ML-DSA-44 keypair 305686 cycles 302258 cycles 1.01
ML-DSA-44 sign 1054556 cycles 1038859 cycles 1.02
ML-DSA-44 verify 328806 cycles 325882 cycles 1.01
ML-DSA-65 keypair 560506 cycles 557736 cycles 1.00
ML-DSA-65 sign 1549828 cycles 1517159 cycles 1.02
ML-DSA-65 verify 537579 cycles 532945 cycles 1.01
ML-DSA-87 keypair 863362 cycles 862513 cycles 1.00
ML-DSA-87 sign 1986410 cycles 1962426 cycles 1.01
ML-DSA-87 verify 894623 cycles 890895 cycles 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Arm Cortex-A72 (Raspberry Pi 4) benchmarks (no-opt)

Benchmark suite Current: 6050f70 Previous: db0c059 Ratio
ML-DSA-44 keypair 305610 cycles 302563 cycles 1.01
ML-DSA-44 sign 949111 cycles 1038239 cycles 0.91
ML-DSA-44 verify 329175 cycles 325625 cycles 1.01
ML-DSA-65 keypair 560411 cycles 557708 cycles 1.00
ML-DSA-65 sign 1543294 cycles 1521737 cycles 1.01
ML-DSA-65 verify 536667 cycles 533310 cycles 1.01
ML-DSA-87 keypair 865489 cycles 861766 cycles 1.00
ML-DSA-87 sign 1994548 cycles 1959841 cycles 1.02
ML-DSA-87 verify 897060 cycles 889017 cycles 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Resolves #116

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Resolves #117

Unrolling resulted in too poor performance. The direct proof works, but it
requires to workaround the CBMC limitation described in:
#102
diffblue/cbmc#8617

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Resolves #124.

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Resolves #126

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Resolves #127

Unrolling resulted in too poor performance. The direct proof works, but it
requires to workaround the CBMC limitation described in:
#102
diffblue/cbmc#8617

This also moves the correctness post-condition in poly_sub into a assert at
the end of the function. This vastly improves performance.

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Resolves #136

Signed-off-by: Matthias J. Kannwischer <[email protected]>
Resolves #135

Unrolling resulted in too poor performance. The direct proof works, but it
requires to workaround the CBMC limitation described in:
#102
diffblue/cbmc#8617

Signed-off-by: Matthias J. Kannwischer <[email protected]>
@mkannwischer
Copy link
Contributor Author

There is a measurable overhead of the whole-struct assignment CBMC workaround of 1-2% I'd say. That shouldn't stop us from proceeding, I think.

@mkannwischer
Copy link
Contributor Author

This is ready for review now. Much pain.

void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v);
void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v)
__contract__(
requires(memory_no_alias(w, sizeof(polyvecl)))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not going to work, and similar for poly_add, since the function is being called with aliasing first and second argument -- like in mlkem-native.

Copy link
Contributor

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

poly_add, polyvecl_add, and polyveck_add are always called with first and second argument matching, which violates the current preconditions assuming them to be disjoint. The signature should be changed to the destructive version, as in mlkem-native.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants