-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add derandomized kyber768 ref from libjade-sys (#23)
- Loading branch information
Showing
13 changed files
with
19,130 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Allow dead code for now. | ||
// The libjade code here isn't verified yet and thus isn't used. | ||
#![allow(dead_code)] | ||
|
||
use libjade_sys::{ | ||
jade_kem_kyber_kyber768_amd64_ref_dec, jade_kem_kyber_kyber768_amd64_ref_enc_derand, | ||
jade_kem_kyber_kyber768_amd64_ref_keypair_derand, | ||
}; | ||
|
||
type Kyber768KeypairSeed = [u8; 64]; | ||
|
||
type Kyber768PublicKey = [u8; 1184]; | ||
type Kyber768SecretKey = [u8; 2400]; | ||
|
||
type Kyber768EncapsulateSeed = [u8; 32]; | ||
|
||
type Kyber768Ciphertext = [u8; 1088]; | ||
type Kyber768SharedSecret = [u8; 32]; | ||
|
||
fn kyber768_keypair_derand_ref( | ||
seed: Kyber768KeypairSeed, | ||
) -> Result<(Kyber768PublicKey, Kyber768SecretKey), &'static str> { | ||
let mut public_key: Kyber768PublicKey = [0; 1184]; | ||
let mut secret_key: Kyber768SecretKey = [0; 2400]; | ||
|
||
log::trace!("Jasmin kyber768 keypair_derand ref"); | ||
let r = unsafe { | ||
jade_kem_kyber_kyber768_amd64_ref_keypair_derand( | ||
public_key.as_mut_ptr(), | ||
secret_key.as_mut_ptr(), | ||
seed.as_ptr() as _, | ||
) | ||
}; | ||
|
||
if r != 0 { | ||
Err("Error while generating kyber768 keypair.") | ||
} else { | ||
Ok((public_key, secret_key)) | ||
} | ||
} | ||
|
||
fn kyber768_enc_derand_ref( | ||
public_key: Kyber768PublicKey, | ||
seed: Kyber768EncapsulateSeed, | ||
) -> Result<(Kyber768Ciphertext, Kyber768SharedSecret), &'static str> { | ||
let mut ciphertext: Kyber768Ciphertext = [0; 1088]; | ||
let mut shared_secret = Kyber768SharedSecret::default(); | ||
|
||
log::trace!("Jasmin kyber768 enc_derand ref"); | ||
let r = unsafe { | ||
jade_kem_kyber_kyber768_amd64_ref_enc_derand( | ||
ciphertext.as_mut_ptr(), | ||
shared_secret.as_mut_ptr(), | ||
public_key.as_ptr() as _, | ||
seed.as_ptr() as _, | ||
) | ||
}; | ||
|
||
if r != 0 { | ||
Err("Error while running kyber768 derandomized encapsulated.") | ||
} else { | ||
Ok((ciphertext, shared_secret)) | ||
} | ||
} | ||
|
||
fn kyber768_dec_ref( | ||
ciphertext: Kyber768Ciphertext, | ||
secret_key: Kyber768SecretKey, | ||
) -> Result<Kyber768SharedSecret, &'static str> { | ||
let mut shared_secret = Kyber768SharedSecret::default(); | ||
|
||
log::trace!("Jasmin kyber768 dec ref"); | ||
let r = unsafe { | ||
jade_kem_kyber_kyber768_amd64_ref_dec( | ||
shared_secret.as_mut_ptr(), | ||
ciphertext.as_ptr() as _, | ||
secret_key.as_ptr() as _, | ||
) | ||
}; | ||
|
||
if r != 0 { | ||
Err("Error while running kyber768 decapsulate.") | ||
} else { | ||
Ok(shared_secret) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn consistency_test() { | ||
let _ = pretty_env_logger::try_init(); | ||
|
||
let keypair_seed = [0u8; 64]; | ||
let enc_seed = [0u8; 32]; | ||
|
||
let (public_key, secret_key) = kyber768_keypair_derand_ref(keypair_seed).unwrap(); | ||
|
||
let (ciphertext, shared_secret) = kyber768_enc_derand_ref(public_key, enc_seed).unwrap(); | ||
|
||
let shared_secret_decapsulated = kyber768_dec_ref(ciphertext, secret_key).unwrap(); | ||
|
||
assert_eq!(shared_secret_decapsulated, shared_secret); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef JADE_KEM_KYBER_KYBER768_AMD64_REF_API_H | ||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_API_H | ||
|
||
#include <stdint.h> | ||
|
||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_SECRETKEYBYTES 2400 | ||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_PUBLICKEYBYTES 1184 | ||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_CIPHERTEXTBYTES 1088 | ||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_KEYPAIRCOINBYTES 64 | ||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_ENCCOINBYTES 32 | ||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_BYTES 32 | ||
|
||
#define JADE_KEM_KYBER_KYBER768_AMD64_REF_ALGNAME "Kyber768" | ||
|
||
int jade_kem_kyber_kyber768_amd64_ref_keypair_derand( | ||
uint8_t *public_key, | ||
uint8_t *secret_key, | ||
const uint8_t *coins | ||
); | ||
|
||
int jade_kem_kyber_kyber768_amd64_ref_enc_derand( | ||
uint8_t *ciphertext, | ||
uint8_t *shared_secret, | ||
const uint8_t *public_key, | ||
const uint8_t *coins | ||
); | ||
|
||
int jade_kem_kyber_kyber768_amd64_ref_dec( | ||
uint8_t *shared_secret, | ||
const uint8_t *ciphertext, | ||
const uint8_t *secret_key | ||
); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.