Skip to content

Add support to ESP32C3 #50

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

Merged
merged 30 commits into from
Aug 16, 2024
Merged

Conversation

potsrevennil
Copy link
Contributor

@potsrevennil potsrevennil commented Aug 7, 2024

depends on #48

This PR added esp32c3 support to the build system
running

make PLATFORM=esp32-c3

should be able to compile the binary for all tests (test/speed/stack/nistkat).

Then flash to the esp32c3 device with

esptool.py -p <PORT> write_flash 0x0 build/esp32-c3/bin/<bin_name>.bin

where PORT start with /dev/tty and /dev/cu on Linux and macOS respectively.

This is not integrated to the tests script yet, there will be a separate PR for this.

You can receive serial output from the same device (after resetting the board) - the baud rate is 115200:

pyserial-miniterm <PORT> 115200 

Building the artifacts for esp32c3 is included in CI as well.

UPDATE:

Cycle Count

  • CLOCK_BENCHMARK = 80MHZ (which is the max clock frequency for flash)
keypair cycles
4015868
encaps cycles
3736502
decaps cycles
4492722
  • CLOCK_FAST = 160 MHZ, will get
keypair cycles
5426636
encaps cycles
4846023
decaps cycles
6056913

Stack Usage

keypair stack usage:
3600
encaps stack usage:
3648
decaps stack usage:
3616
  • Does this PR change the input/output behaviour of a cryptographic algorithm (i.e., does it change known answer test values)? (If so, a version bump will be required from x.y.z to x.(y+1).0.)

@potsrevennil potsrevennil marked this pull request as ready for review August 7, 2024 02:39
@potsrevennil potsrevennil requested a review from a team as a code owner August 7, 2024 02:39
@potsrevennil potsrevennil changed the title Add support to riscv Add support to ESP32C3 Aug 7, 2024
@potsrevennil potsrevennil marked this pull request as draft August 7, 2024 03:06
Signed-off-by: Thing-han, Lim <[email protected]>
Signed-off-by: Thing-han, Lim <[email protected]>
Signed-off-by: Thing-han, Lim <[email protected]>
Signed-off-by: Thing-han, Lim <[email protected]>
Signed-off-by: Thing-han, Lim <[email protected]>
Signed-off-by: Thing-han, Lim <[email protected]>
@potsrevennil potsrevennil marked this pull request as ready for review August 7, 2024 03:20
Signed-off-by: Thing-han, Lim <[email protected]>
@mkannwischer
Copy link
Contributor

It's worth mentioning that the baud rate is 115200

Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

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

I tested that everything works. What is a little weird to me is that the flashing and the printing of the output are done over the same USB device? How do you run this in parallel?

What's printed as cycle counts seems way too low for me (this is for Kyber-512):

+
keypair cycles
94613
encaps cycles
80242
decaps cycles
106573
$

We should except something over 1M cycles for encapsulation according to https://eprint.iacr.org/2020/049.pdf.

@mkannwischer
Copy link
Contributor

Do you think adding emulation with qemu is going to be easy?
Maybe @rpls has that lying around and can add it?

@potsrevennil
Copy link
Contributor Author

What is a little weird to me is that the flashing and the printing of the output are done over the same USB device? How do you run this in parallel?

I flashed it, and run the pyserial then, reset the board.
So this might cause some issues to integrate this into the python script

@potsrevennil
Copy link
Contributor Author

I will also look into the clock configuration as well

@potsrevennil
Copy link
Contributor Author

Adding emulation with qemu might need to write another linker script, but if you already had some working examples, then it shouldn't be too hard or complicated I expect.

@potsrevennil
Copy link
Contributor Author

For CLOCK_BENCHMARK = 80MHZ (which is the max clock frequency for flash), will get

keypair cycles
4015868
encaps cycles
3736502
decaps cycles
4492722

and for CLOCK_FAST = 160 MHZ, will get

keypair cycles
5426636
encaps cycles
4846023
decaps cycles
6056913

should be some reasonable cycle counts for me

@potsrevennil
Copy link
Contributor Author

stack api are updated, and the resulting stack size is as follow

keypair stack usage:
3600
encaps stack usage:
3648
decaps stack usage:
3616

Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

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

It's confusing that if I run tests --list-platforms it lists esp32-c3, but if I run tests func esp32-c3 it prints unsupported platform {platform}.
Let's not offer the option in the scripts until it's support.

@mkannwischer
Copy link
Contributor

Thanks @potsrevennil! This is looking great!
I've tested that I can correctly build and flash the binaries.
Outputs for tests, speed, and stack are looking good.

There is also some gibberish in the beginning of the output (that is sent at 115200 baud; while the rest is at 38400) . You'll either have to switch to 115200 everywhere or need to change the baud rate at an earlier boot stage. Or somehow turn off that that is printed. Here is the output

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x1 (POWERON),boot:0xc (SPI_FAST_FLASH_BOOT)

Integration with the scripts is still needed (and looks rather annoying since flashing and output are going through the same serial device). Please open an issue for that.

Signed-off-by: Thing-han, Lim <[email protected]>
Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

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

Thanks! This all looks good now! We can merge this once CI passes!

Thanks for all this work, @potsrevennil!

We could consider putting this into https://github.com/mupq/pqriscv as well in case @rpls thinks it is useful.

@mkannwischer mkannwischer merged commit 2968023 into pq-code-package:main Aug 16, 2024
2 checks passed
@mkannwischer mkannwischer mentioned this pull request Aug 16, 2024
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