-
Notifications
You must be signed in to change notification settings - Fork 2
[ML-KEM] Match mainline APIs #112
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
Conversation
|
Blocked on #117 getting merged, which updates the C extraction scripts. |
keks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks like it should work, just the copying is unfortunate. Do you think there is something we can do about with little effort or is that a bigger thing?
| macro_rules! impl_kem_trait { | ||
| ($variant:ty, $pk:ty, $sk:ty, $ct:ty) => { | ||
| impl | ||
| libcrux_traits::kem::arrayref::Kem< | ||
| CPA_PKE_PUBLIC_KEY_SIZE, | ||
| SECRET_KEY_SIZE, | ||
| CPA_PKE_CIPHERTEXT_SIZE, | ||
| SHARED_SECRET_SIZE, | ||
| KEY_GENERATION_SEED_SIZE, | ||
| SHARED_SECRET_SIZE, | ||
| > for $variant | ||
| { | ||
| fn keygen( | ||
| ek: &mut [u8; CPA_PKE_PUBLIC_KEY_SIZE], | ||
| dk: &mut [libcrux_secrets::U8; SECRET_KEY_SIZE], | ||
| rand: &[libcrux_secrets::U8; KEY_GENERATION_SEED_SIZE], | ||
| ) -> Result<(), libcrux_traits::kem::owned::KeyGenError> { | ||
| let key_pair = generate_key_pair(*rand); | ||
| ek.copy_from_slice(key_pair.pk()); | ||
| dk.copy_from_slice(key_pair.sk()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn encaps( | ||
| ct: &mut [u8; CPA_PKE_CIPHERTEXT_SIZE], | ||
| ss: &mut [libcrux_secrets::U8; SHARED_SECRET_SIZE], | ||
| ek: &[u8; CPA_PKE_PUBLIC_KEY_SIZE], | ||
| rand: &[libcrux_secrets::U8; SHARED_SECRET_SIZE], | ||
| ) -> Result<(), libcrux_traits::kem::owned::EncapsError> { | ||
| let public_key: $pk = ek.into(); | ||
|
|
||
| let (ct_, ss_) = encapsulate(&public_key, *rand); | ||
| ct.copy_from_slice(ct_.as_slice()); | ||
| ss.copy_from_slice(ss_.as_slice()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn decaps( | ||
| ss: &mut [libcrux_secrets::U8; SHARED_SECRET_SIZE], | ||
| ct: &[u8; CPA_PKE_CIPHERTEXT_SIZE], | ||
| dk: &[libcrux_secrets::U8; SECRET_KEY_SIZE], | ||
| ) -> Result<(), libcrux_traits::kem::owned::DecapsError> { | ||
| let secret_key: $sk = dk.into(); | ||
| let ciphertext: $ct = ct.into(); | ||
|
|
||
| let ss_ = decapsulate(&secret_key, &ciphertext); | ||
|
|
||
| ss.copy_from_slice(ss_.as_slice()); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit unfortunate that we have to copy everywhere here. I suppose we don't have &mut in this implementation yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it is unfortunate. At the moment, we do not have any top-level APIs here that operate on pre-allocated inputs. This would be good to have and I'll file an issue for it, but since this PR is about matching mainline APIs I would rather not add new top-level APIs here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! Let's get this in as-is
keks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's get this in!
This PR ports over some of the APIs that are provided for ML-KEM in mainline libcrux, namely:
tls_codecThe PR does not port the incremental API.
Fixes https://github.com/cryspen/home/issues/431