-
Notifications
You must be signed in to change notification settings - Fork 54
lowram: Keep w1 in packed form during signature generation #1345
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright (c) The mldsa-native project authors | ||
| # SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT | ||
| # | ||
| # Track the reduced signing work buffer of mldsa-native. The crypto | ||
| # library wrapper pins the work buffer sizes and cross-checks them | ||
| # against MLD_TOTAL_ALLOC_* with a static assertion, so the signing | ||
| # sizes have to follow when mldsa-native allocates less. | ||
|
|
||
| diff --git a/sw/device/lib/crypto/include/mldsa.h b/sw/device/lib/crypto/include/mldsa.h | ||
| --- a/sw/device/lib/crypto/include/mldsa.h | ||
| +++ b/sw/device/lib/crypto/include/mldsa.h | ||
| @@ -42,15 +42,15 @@ | ||
|
|
||
| // Work buffer sizes in 32-bit words | ||
| kOtcryptoMldsa44WorkBufferKeypairWords = 11584 / sizeof(uint32_t), | ||
| - kOtcryptoMldsa44WorkBufferSignWords = 13120 / sizeof(uint32_t), | ||
| + kOtcryptoMldsa44WorkBufferSignWords = 9792 / sizeof(uint32_t), | ||
| kOtcryptoMldsa44WorkBufferVerifyWords = 9120 / sizeof(uint32_t), | ||
|
|
||
| kOtcryptoMldsa65WorkBufferKeypairWords = 14656 / sizeof(uint32_t), | ||
| - kOtcryptoMldsa65WorkBufferSignWords = 17248 / sizeof(uint32_t), | ||
| + kOtcryptoMldsa65WorkBufferSignWords = 11872 / sizeof(uint32_t), | ||
| kOtcryptoMldsa65WorkBufferVerifyWords = 10208 / sizeof(uint32_t), | ||
|
|
||
| kOtcryptoMldsa87WorkBufferKeypairWords = 18752 / sizeof(uint32_t), | ||
| - kOtcryptoMldsa87WorkBufferSignWords = 21344 / sizeof(uint32_t), | ||
| + kOtcryptoMldsa87WorkBufferSignWords = 14176 / sizeof(uint32_t), | ||
| kOtcryptoMldsa87WorkBufferVerifyWords = 12512 / sizeof(uint32_t), | ||
| }; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1057,6 +1057,57 @@ void mld_polyw1_pack_32(uint8_t r[MLDSA_POLYW1_PACKEDBYTES_32], | |
| || MLD_CONFIG_PARAMETER_SET == 87 */ | ||
| #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */ | ||
|
|
||
| #if !defined(MLD_CONFIG_NO_SIGN_API) | ||
| #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44 | ||
| MLD_INTERNAL_API | ||
| void mld_polyw1_unpack_88(mld_poly *r, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realize that those have no use prior to the final optimization, but can you still hoist the introduction of the |
||
| const uint8_t a[MLDSA_POLYW1_PACKEDBYTES_88]) | ||
| { | ||
| unsigned int i; | ||
|
|
||
| for (i = 0; i < MLDSA_N / 4; ++i) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent use of |
||
| __loop__( | ||
| invariant(i <= MLDSA_N/4) | ||
| invariant(array_bound(r->coeffs, 0, i*4, 0, 1 << 6)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MLDSA_POLYW1_PACKED_BITS_88 |
||
| decreases(MLDSA_N / 4 - i)) | ||
| { | ||
| r->coeffs[4 * i + 0] = a[3 * i + 0] & 0x3F; | ||
| r->coeffs[4 * i + 1] = | ||
| ((a[3 * i + 0] >> 6) | ((int32_t)a[3 * i + 1] << 2)) & 0x3F; | ||
| r->coeffs[4 * i + 2] = | ||
| ((a[3 * i + 1] >> 4) | ((int32_t)a[3 * i + 2] << 4)) & 0x3F; | ||
| r->coeffs[4 * i + 3] = a[3 * i + 2] >> 2; | ||
| } | ||
|
|
||
| mld_assert_bound(r->coeffs, MLDSA_N, 0, 1 << 6); | ||
| } | ||
| #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \ | ||
| */ | ||
|
|
||
| #if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \ | ||
| (MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87) | ||
| MLD_INTERNAL_API | ||
| void mld_polyw1_unpack_32(mld_poly *r, | ||
| const uint8_t a[MLDSA_POLYW1_PACKEDBYTES_32]) | ||
| { | ||
| unsigned int i; | ||
|
|
||
| for (i = 0; i < MLDSA_N / 2; ++i) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent use of |
||
| __loop__( | ||
| invariant(i <= MLDSA_N/2) | ||
| invariant(array_bound(r->coeffs, 0, i*2, 0, 1 << 4)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MLDSA_POLYW1_PACKED_BITS_32 |
||
| decreases(MLDSA_N / 2 - i)) | ||
| { | ||
| r->coeffs[2 * i + 0] = a[i] & 0x0F; | ||
| r->coeffs[2 * i + 1] = a[i] >> 4; | ||
| } | ||
|
|
||
| mld_assert_bound(r->coeffs, MLDSA_N, 0, 1 << 4); | ||
| } | ||
| #endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \ | ||
| || MLD_CONFIG_PARAMETER_SET == 87 */ | ||
| #endif /* !MLD_CONFIG_NO_SIGN_API */ | ||
|
|
||
| #else /* !MLD_CONFIG_MULTILEVEL_NO_SHARED */ | ||
| MLD_EMPTY_CU(mld_poly) | ||
| #endif /* MLD_CONFIG_MULTILEVEL_NO_SHARED */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introducing those is slightly inconsistent with existing packing routines, which hardcode their bit widths. E.g.
mld_polyt1_[un]pack.