Skip to content

Conversation

@h-filali
Copy link
Contributor

@h-filali h-filali commented Jan 5, 2026

This PR adds a new KMAC interface to OTBNsim.

This PR is rebased on #29165 so please review the first commit there.
The second commit unifies the classes for the CSRs and WSRs.
The third commit adds an implementation for the model.
The fourth commit connects the KMAC model to OTBN.
The fifth commit contains a working assembly example/test.

@nasahlpa nasahlpa requested a review from vogelpi January 5, 2026 13:51
@h-filali h-filali force-pushed the otbnsim-kmac branch 8 times, most recently from 93a47e0 to 860684f Compare January 5, 2026 16:42
Copy link
Contributor

@etterli etterli left a comment

Choose a reason for hiding this comment

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

Thanks for this first implementation! It looks pretty good.

I reviewed the CSR/WSR and integration part but not yet completely the kmac.py as well as the software test. My feedback is mostly about error handling, otherwise the concept looks good to me.

@h-filali h-filali force-pushed the otbnsim-kmac branch 2 times, most recently from ad88033 to 2e98ad2 Compare January 6, 2026 14:22
@h-filali
Copy link
Contributor Author

h-filali commented Jan 6, 2026

Thanks @etterli for your comments. Nice finds! I added some TODOs to make sure your comments aren't forgotten. I also adapted the documentation.

@h-filali h-filali added the CI:Rerun Rerun failed CI jobs label Jan 6, 2026
@github-actions github-actions bot removed the CI:Rerun Rerun failed CI jobs label Jan 6, 2026
@etterli
Copy link
Contributor

etterli commented Jan 13, 2026

We should also check what must be changed for the iflow (information flow) declarations from the CSR / WSR instructions. See here or here. There are also some other fields like lsu in the instruction definition files which maybe must be adpated.

This can probably be postponed.

@h-filali h-filali force-pushed the otbnsim-kmac branch 4 times, most recently from ec230c6 to 1c22c51 Compare January 20, 2026 10:51
from typing import List, Optional, Tuple
from .ext_regs import OTBNExtRegs
from .ispr import ISPR, DumbISPR, ISPRChange
from .kmac_ispr import KmacDataWSR
Copy link
Contributor

Choose a reason for hiding this comment

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

This kmac_ispr import is not supposed to be in this commit. It should come in the next one.

Copy link
Contributor

@rswarbrick rswarbrick left a comment

Choose a reason for hiding this comment

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

Lots and lots of comments, sorry. I guess this is the danger of an enormous PR :-/

Copy link
Contributor

@etterli etterli left a comment

Choose a reason for hiding this comment

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

Thanks @h-filali for the update. I have some minor feedback but LGTM otherwise.

Assumes that idx is a valid index (call check_idx to ensure this).

'''
# KMAC_DATA_S0/1 should only be through the wrapper class.
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: Incomplete sentence?

Comment on lines +300 to +304
if idx == 0x8:
return self.KMAC_DATA.read_unsigned(share_idx=0)

elif idx == 0x9:
return self.KMAC_DATA.read_unsigned(share_idx=1)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we here extract the read for the KMAC data WSRs (which makes sense) then I think we should not have self.KMAC_DATA.shares[0] and self.KMAC_DATA.shares[1] in _by_idx. Otherwise the comment above saying that these should only be accessed via the wrapper is a little bit contradictory.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, now I see why it is in _by_idx. It is there to simplify the on_start() and also we need these WSRs indexes to be present for check_idx() and has_value_at_idx(). These two are used in the BNWSRR and BNWSRW instructions to check whether the access is legal.

Copy link
Contributor

Choose a reason for hiding this comment

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

As this read_at_idx() is the only place where _by_idx is accessed to read the WSRs I agree that these should stay in _by_idx (same reasoning for write_at_idx()).

Comment on lines 173 to 236
# Set the message write error if an illegal write to KMAC_DATA_S0/1 happened.
if self._wsrs.KMAC_DATA.shares_dirty() and \
not self._csrs.KMAC_IF_STATUS.get_msg_write_rdy():
self._csrs.KMAC_IF_STATUS.set_msg_write_error()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Set the message write error if an illegal write to KMAC_DATA_S0/1 happened.
if self._wsrs.KMAC_DATA.shares_dirty() and \
not self._csrs.KMAC_IF_STATUS.get_msg_write_rdy():
self._csrs.KMAC_IF_STATUS.set_msg_write_error()
# Set the message write error if an illegal write to KMAC_DATA_S0/1 happened.
if (self._wsrs.KMAC_DATA.shares_dirty() and
not self._csrs.KMAC_IF_STATUS.get_msg_write_rdy()):
self._csrs.KMAC_IF_STATUS.set_msg_write_error()

Comment on lines 262 to 264
entry = None
if mode is not None and strength is not None:
entry = MODE_STRENGTH_TABLE.get((mode, strength))
Copy link
Contributor

Choose a reason for hiding this comment

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

The .get() will return None by default if there is no match. You could specify another default if desired with .get(<key>, <YourDefault>).

Suggested change
entry = None
if mode is not None and strength is not None:
entry = MODE_STRENGTH_TABLE.get((mode, strength))
entry = MODE_STRENGTH_TABLE.get((mode, strength))

@h-filali h-filali force-pushed the otbnsim-kmac branch 4 times, most recently from c0c2c71 to cefe00b Compare February 5, 2026 14:36
@h-filali
Copy link
Contributor Author

h-filali commented Feb 5, 2026

Thanks @rswarbrick @etterli please LMK if this LGTY now!

@h-filali
Copy link
Contributor Author

h-filali commented Feb 6, 2026

Thanks @rswarbrick for having another look. Your feedback should be addressed now. PTAL

This commit changes the WSR and DumbWSR classes to be more
generic. This allows code sharing between the WSR classes
and potential new classes with different register size but
the same functionality.

Signed-off-by: Hakim Filali <[email protected]>
This commit adds a new KMAC model that can be used to model
a KMAC-OBTN interface. This model can be used to interface
KMAC from OTBN. This model should behave like the KMAC HW IP.

Signed-off-by: Hakim Filali <[email protected]>
@h-filali h-filali force-pushed the otbnsim-kmac branch 2 times, most recently from 33cdedd to ea3d56c Compare February 6, 2026 15:32
@h-filali h-filali added the CI:Rerun Rerun failed CI jobs label Feb 9, 2026
@github-actions github-actions bot removed the CI:Rerun Rerun failed CI jobs label Feb 9, 2026
Copy link
Contributor

@rswarbrick rswarbrick left a comment

Choose a reason for hiding this comment

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

One minor nit, but I'm very happy for this to land otherwise.

This has been a big PR: well done for getting it to this state!

For masked operations, provide the first share here and the second share in KMAC_DATA_S1.

If masking is not required:
- Set one this share to the plaintext data.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think this should probably be "Set this share" (and similarly for kmac_data_s1)

Copy link
Contributor Author

@h-filali h-filali Feb 9, 2026

Choose a reason for hiding this comment

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

Thanks for catching this one!

Use the new KMAC OTBNsim model to extend OTBNsim.
This commit adds new WSRs and CSRs to OTBNsim and connects
them to the new KMAC model.

Signed-off-by: Hakim Filali <[email protected]>
This commit adds an assembly test that tests all allowed
combinations of KMAC modes and kstrengths.

This commit shall also serve as a first example of how to use
the interface.

Signed-off-by: Hakim Filali <[email protected]>
@h-filali
Copy link
Contributor Author

h-filali commented Feb 9, 2026

Thanks @rswarbrick for your thorough review. I'll merge this piece of work once CI passes!

@h-filali h-filali enabled auto-merge February 9, 2026 14:56
@h-filali h-filali added this pull request to the merge queue Feb 9, 2026
Merged via the queue into lowRISC:master with commit dac9866 Feb 9, 2026
33 of 46 checks passed
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.

5 participants