[crypto/mlkem] Add main subroutines for ml-kem1024 - #30719
Conversation
4b0ff34 to
af2819f
Compare
andrea-caforio
left a comment
There was a problem hiding this comment.
Thanks @siemen11. ML-KEM is on the map now. :-) I did an initial review, haven't checked some of the logic-heavy parts yet.
|
|
||
| /* Basecase even products c0 = a0*b0 + a1*b1*gamma */ | ||
| bn.mulvm.8S w3, w0, w1 | ||
| bn.mulvm.8S w4, w3, w2 |
There was a problem hiding this comment.
Does this work without a dummy addition in between? Our Montgomery multiplier does not do the conditional subtraction, so 2 subsequent multiplications might be incorrect.
There was a problem hiding this comment.
The difference is that ML-KEM works on 12 bit values, not like ML-DSA with 24 bits, so we can leave the overflow unhandled for a bit
There was a problem hiding this comment.
I see. The following addition is not a problem either I guess? Just to verify for myself because the operands for the addition are together larger than 2q but the addition is only well-defined for < 2q.
There was a problem hiding this comment.
The way I understand it is that the outputs from bn.mulvm are strictly bounded by q, so the sum of any two multiplication outputs is at most 2q
There was a problem hiding this comment.
The output of the Montgomery mutiplier used to be in [0, q[ but that was removed in later versions of the SIMD extension.
There was a problem hiding this comment.
The way that I understand it is that, we have a, b smaller than q and q = 3329, a 12 bit prime.
When we call bn.mulvm.8S, we calculate
r = (ab + mq)/2^32 <= floor((a*b - q)/2^32) + q (because m <= w^32 - 1, the Mont constant)
But because a, b < q, that floor is actually equal to 0, so r stays still smaller than q
Specifically, this is also mentioned in the docu:
"Note that when chaining multiplications, the conditional subtraction can be postponed until after the last multiplication in case the initial inputs are in [0, 2q[ and q < (2^d)/4 holds."
We have d = 32, and we have (2^d) / 4 = (2^32) / 4 = 2^30 and q = 2^12
There was a problem hiding this comment.
I did some digging and I think the explanation of @siemen11 is correct (the w in m <= w^32 - 1 should be a 2). For ML-DSA is does not work because q is too big and the floor() is not 0. Because a*b = (q-1)^2 at most and s = (a*b - q) / 2^32 is > 1. This means that r <= q + s.
80cbc75 to
faa95fb
Compare
18cb044 to
222df8a
Compare
25040d5 to
dd8b9d8
Compare
0c52ce7 to
a8bafd2
Compare
etterli
left a comment
There was a problem hiding this comment.
Thanks for the improvements on the decoding. I now reviewed also the encoding functions.
7b96c79 to
8e6346a
Compare
etterli
left a comment
There was a problem hiding this comment.
I reviewed the polynomial arithmetic operations. Nicely optimized computations.
|
|
||
| /* Basecase even products c0 = a0*b0 + a1*b1*gamma */ | ||
| bn.mulvm.8S w3, w0, w1 | ||
| bn.mulvm.8S w4, w3, w2 |
There was a problem hiding this comment.
I did some digging and I think the explanation of @siemen11 is correct (the w in m <= w^32 - 1 should be a 2). For ML-DSA is does not work because q is too big and the floor() is not 0. Because a*b = (q-1)^2 at most and s = (a*b - q) / 2^32 is > 1. This means that r <= q + s.
| /* Zero w31 to guarantee zero register for mask generation */ | ||
| bn.xor w31, w31, w31 |
There was a problem hiding this comment.
Do we need this or is in ML-KEM defined, similar to ML-DSA, that w31 is always 0?
There was a problem hiding this comment.
Decaps uses w31... I need to optimize this still and see whether I can move it out
There was a problem hiding this comment.
I left the w31 there for now, maybe later we can optimize it
9a3fb9a to
63ef84e
Compare
Add the main arithmetic and subroutines for the ml-kem1024 in the otbn. With thanks to Andrea Caforio <andrea.caforio@lowrisc.org> Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
Add the main arithmetic and subroutines for the ml-kem1024 in the otbn.