Skip to content

[OTBN] Integrate compression option for imem and dmem#30163

Open
siemen11 wants to merge 5 commits into
lowRISC:earlgrey_1.0.0from
siemen11:otbn_code_saving
Open

[OTBN] Integrate compression option for imem and dmem#30163
siemen11 wants to merge 5 commits into
lowRISC:earlgrey_1.0.0from
siemen11:otbn_code_saving

Conversation

@siemen11
Copy link
Copy Markdown
Contributor

@siemen11 siemen11 commented May 20, 2026

Use LZ4 to add a compression option in otbn_binary (activated by setting
compress = True). If this is set, the imem and dmem of the otbn
application are compressed before they are stored.
By default, binaries are not compressed.

Change the otbn.c and otbn.h in the cryptolib to use LZ4 from lib/base
in order to decompress the loaded imem and dmem from an application.

The compression option is therefore mandatory in the cryptolib
(lib/crypto), however, the applications from silicon_creator (using
silicon_creator/lib/drivers/otbn.c/h) still use the uncompressed version
(and will not work with the compressed version).

Concerning the CRC: The CRC for the imem/dmem is calculated before
compression. For loading to OTBN, the OTBN is given the CRC of the
uncompressed payload before and checks itself the CRC of what it
receives. I.e., the CRC still provides end-2-end protection.

@siemen11 siemen11 requested a review from johannheyszl May 20, 2026 20:36
@johannheyszl
Copy link
Copy Markdown
Contributor

interesting idea.

kindly, what is the code size overhead of the addition. I'd assume significantly smaller than the saving.

@siemen11
Copy link
Copy Markdown
Contributor Author

interesting idea.

kindly, what is the code size overhead of the addition. I'd assume significantly smaller than the saving.

We require an additional ~400 bytes for the decompression algorithm but save ~7000 bytes

@siemen11 siemen11 force-pushed the otbn_code_saving branch from cd68be9 to 506e010 Compare May 21, 2026 09:37
@siemen11 siemen11 changed the title [test] OTBN flash size saving test [OTBN] Integrate compression option for imem and dmem May 21, 2026
@siemen11 siemen11 force-pushed the otbn_code_saving branch from 506e010 to 0f5b321 Compare May 21, 2026 09:54
@siemen11
Copy link
Copy Markdown
Contributor Author

siemen11 commented May 21, 2026

Some quick overview for the choice of compression algorithms provided by Gemini:

Algorithm RAM (KB) Flash (KB) Speed Ratio %
LZ4 0.00 0.5 100 50%
Heatshrink 0.05 0.8 30 55%
FastLZ 0.20 1.0 90 52%
DEFLATE 4.00 3.0 40 65%
Zstandard 24.00 15.0 80 70%

Currently, LZ4 was chosen since it saves already quite a bit and it would be the least invasive in speed or RAM usage

@siemen11 siemen11 force-pushed the otbn_code_saving branch 5 times, most recently from 7c46b9d to 6c530c1 Compare May 21, 2026 11:36
Update python versions using gen-python-requirements.sh

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
@siemen11 siemen11 force-pushed the otbn_code_saving branch 2 times, most recently from c2200af to 673988c Compare May 21, 2026 13:10
@siemen11
Copy link
Copy Markdown
Contributor Author

Collateral is that otbn_sca and otbn_fi have to use only drivers/otbn (compressed binaries) and not otbn_dif (uncompressed binaries)

siemen11 added 4 commits May 21, 2026 16:14
Remove dif_otbn from the dependencies in pentest, instead use
crypto/drivers/otbn.

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
Add lz4 to the python dependencies.

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
Add the lz4 decompression algorithm. This algorithm costs around 400
bytes.

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
Use LZ4 to add a compression option in otbn_binary (activated by setting
compress = True). If this is set, the imem and dmem of the otbn
application are compressed before they are stored.
By default, binaries are not compressed.

Change the otbn.c and otbn.h in the cryptolib to use LZ4 from lib/base
in order to decompress the loaded imem and dmem from an application.

The compression option is therefore mandatory in the cryptolib
(lib/crypto), however, the applications from silicon_creator (using
silicon_creator/lib/drivers/otbn.c/h) still use the uncompressed version
(and will not work with the compressed version).

This option saves the cryptolib ~7000bytes while increasing ~400bytes to
load LZ4.c while providing backwards compatability for the boot.

Concerning the CRC: The CRC for the imem/dmem is calculated before
compression. For loading to OTBN, the OTBN is given the CRC of the
uncompressed payload before and checks itself the CRC of what it
receives. I.e., the CRC still provides end-2-end protection.

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
@siemen11 siemen11 force-pushed the otbn_code_saving branch from 673988c to 7a55643 Compare May 21, 2026 14:20
@johannheyszl
Copy link
Copy Markdown
Contributor

Thanks @siemen11 - have to say that I don't see any immediate issues with this approach, and it clearly helps save code size.

Any quick thoughts on this @cfrantz @moidx ?

@siemen11 siemen11 marked this pull request as ready for review May 21, 2026 15:36
@siemen11 siemen11 requested review from a team and cfrantz as code owners May 21, 2026 15:36
@siemen11 siemen11 requested review from andrea-caforio, engdoreis and nasahlpa and removed request for a team and engdoreis May 21, 2026 15:36
@siemen11 siemen11 removed the request for review from cfrantz May 21, 2026 15:37
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