Skip to content

[crypto/mlkem] Add main subroutines for ml-kem1024#30719

Draft
siemen11 wants to merge 1 commit into
lowRISC:masterfrom
siemen11:mlkemphase1
Draft

[crypto/mlkem] Add main subroutines for ml-kem1024#30719
siemen11 wants to merge 1 commit into
lowRISC:masterfrom
siemen11:mlkemphase1

Conversation

@siemen11

Copy link
Copy Markdown
Contributor

Add the main arithmetic and subroutines for the ml-kem1024 in the otbn.

@siemen11
siemen11 force-pushed the mlkemphase1 branch 17 times, most recently from 4b0ff34 to af2819f Compare July 13, 2026 07:30
@siemen11 siemen11 added the CI:Rerun Rerun failed CI jobs label Jul 13, 2026
@github-actions github-actions Bot removed the CI:Rerun Rerun failed CI jobs label Jul 13, 2026

@andrea-caforio andrea-caforio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The output of the Montgomery mutiplier used to be in [0, q[ but that was removed in later versions of the SIMD extension.

https://opentitan.org/book/hw/ip/otbn/doc/isa.html#bnmulvmhttps://opentitan.org/book/hw/ip/otbn/doc/isa.html#bnmulvm

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_decoding.s Outdated
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_decoding.s
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_decoding.s Outdated
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_decoding.s Outdated
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_sample.s Outdated
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_sample.s Outdated
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_sample.s
Comment thread sw/otbn/crypto/mlkem1024/mlkem1024_ntt.s
.data
.balign 32

_expand_buf:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this really needed? Why not expand directly in the output buffer?

@siemen11
siemen11 marked this pull request as draft July 13, 2026 22:34
@siemen11
siemen11 force-pushed the mlkemphase1 branch 5 times, most recently from 80cbc75 to faa95fb Compare July 15, 2026 09:58
@siemen11
siemen11 force-pushed the mlkemphase1 branch 9 times, most recently from c1e3212 to 2f58500 Compare July 15, 2026 16:44

@andrea-caforio andrea-caforio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice @siemen11. This looks clean.

bn.rshi w3, w2, w3 >> 11
bn.rshi w2, w1, w2 >> 11
bn.rshi w1, w31, w1 >> 11
nop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
nop


jal x1, decode_5

lw x7, -20(x31)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be cleaner to pass the address as parameter to the routine.

li x4, 1
bn.lid x4, 0(x2)

/* Prepare constant vector w2 = [1665, 1665, ..., 1665]. */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This constant can also be stored in DMEM.


/* Division by 3329 via shift-subtract */
addi x9, x0, 0
.irp bit, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For later, if IMEM becomes an issue convert this to a loop. This section alone expands to 384B of instructions.

@siemen11
siemen11 force-pushed the mlkemphase1 branch 4 times, most recently from 8edcd36 to 18cb044 Compare July 16, 2026 12:37
@siemen11
siemen11 marked this pull request as ready for review July 16, 2026 12:53
@siemen11
siemen11 marked this pull request as draft July 16, 2026 21:43
@siemen11
siemen11 force-pushed the mlkemphase1 branch 3 times, most recently from 18cb044 to 222df8a Compare July 17, 2026 09:07
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>
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.

2 participants