Skip to content

Commit

Permalink
adds acceptable rng trait
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed May 21, 2024
1 parent 0728df7 commit 285746c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/indcpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl PrivateKey {

pub fn decrypt(
&self,
ciphertext: &ArrayVec<[u8; 2048]>,
ciphertext: &[u8],
) -> Result<[u8; SYMBYTES], EncryptionDecryptionError> {
let sec_level = self.sec_level();
if ciphertext.len() == sec_level.indcpa_bytes() {
Expand Down
42 changes: 23 additions & 19 deletions src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
use rand_chacha::ChaCha20Rng;
use rand_core::{CryptoRng, RngCore, SeedableRng};
use sha3::{Digest, Sha3_256};
use tinyvec::ArrayVec;

pub struct PrivateKey {
sk: IndcpaPrivateKey,
Expand Down Expand Up @@ -46,6 +45,8 @@ fn new_key_from_seed(
Ok((PublicKey { pk, h_pk }, PrivateKey { sk, pk, h_pk, z }))
}

pub trait AcceptableRng: RngCore + CryptoRng {}

/// Generates a new keypair for a given security level.
/// Takes either a given RNG, or will generate one using `ChaCha20`
/// # Errors
Expand All @@ -57,10 +58,11 @@ fn new_key_from_seed(
/// use enc_rust::kem::generate_key_pair;
///
/// let (pk, sk) = generate_key_pair(None, 3)?;
///
/// # Ok::<(), enc_rust::errors::KeyGenerationError>(())
/// ```
pub fn generate_key_pair<R: RngCore + CryptoRng>(
rng: Option<&mut R>,
pub fn generate_key_pair(
rng: Option<&mut dyn AcceptableRng>,
k: usize,
) -> Result<(PublicKey, PrivateKey), KeyGenerationError> {
let k_result = K::try_from(k);
Expand Down Expand Up @@ -88,7 +90,7 @@ impl PrivateKey {
self.sk.sec_level()
}

pub fn get_public_key(&self) -> PublicKey {
pub fn get_public_key(&self) -> PublicKey {
PublicKey {
pk: self.pk,
h_pk: self.h_pk,
Expand All @@ -105,18 +107,20 @@ impl PrivateKey {
// let m = self.sk.decrypt(&

}
// impl PublicKey {
// pub fn encapsulate<R: RngCore + CryptoRng>(
// &self,
// seed: Option<&[u8]>,
// shared_secret: Option<[u8; SHAREDSECRETBYTES]>,
// rng: Option<&mut R>,
// ) -> Result<(CIPHERTEXT, SHAREDSECRET) EncryptionDecryptionError> {
// if let Some(seed) = seed {
// if seed.len() != SYMBYTES {
// Err(CrystalsError::InvalidSeedLength(seed.len(), SYMBYTES).into())
// }

// Ok(())
// }
// }

#[cfg(target_os = "none")]
impl PublicKey {
pub fn encapsulate<R: RngCore + CryptoRng>(
&self,
seed: Option<&[u8]>,
shared_secret: Option<[u8; SHAREDSECRETBYTES]>,
rng: Option<&mut R>,
) -> Result<(CIPHERTEXT, SHAREDSECRET), EncryptionDecryptionError> {
if let Some(seed) = seed {
if seed.len() != SYMBYTES {
Err(CrystalsError::InvalidSeedLength(seed.len(), SYMBYTES).into())
}
Ok(())
}
}
}
2 changes: 1 addition & 1 deletion src/polynomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl Poly<Normalised> {
})
})
.collect::<Result<ArrayVec<[u8; SYMBYTES]>, TryFromIntError>>()
.map(tinyvec::ArrayVec::into_inner);
.map(ArrayVec::into_inner);

buf
}
Expand Down

0 comments on commit 285746c

Please sign in to comment.