Skip to content

Commit

Permalink
Cleanup: function visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mberry committed Aug 22, 2023
1 parent 7197257 commit 2e44965
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 60 deletions.
8 changes: 4 additions & 4 deletions src/avx2/aes256ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::arch::x86_64::*;

#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct Aes256CtrCtx {
pub struct Aes256CtrCtx {
pub rkeys: [__m128i; 16],
pub n: __m128i,
}
Expand Down Expand Up @@ -76,7 +76,7 @@ unsafe fn cast_128(x: __m128i) -> __m128 {
_mm_castsi128_ps(x)
}

pub(crate) fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 12]) {
pub fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 12]) {
unsafe {
let mut idx = 0;
let key0 = _mm_loadu_si128(key.as_ptr() as *const __m128i);
Expand Down Expand Up @@ -138,7 +138,7 @@ pub(crate) fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 1
}
}

pub(crate) fn aes256ctr_squeezeblocks(out: &mut [u8], nblocks: usize, state: &mut Aes256CtrCtx) {
pub fn aes256ctr_squeezeblocks(out: &mut [u8], nblocks: usize, state: &mut Aes256CtrCtx) {
let mut idx = 0;
for _ in 0..nblocks {
unsafe {
Expand All @@ -149,7 +149,7 @@ pub(crate) fn aes256ctr_squeezeblocks(out: &mut [u8], nblocks: usize, state: &mu
}

#[cfg(feature = "90s")]
pub(crate) fn aes256ctr_prf(out: &mut [u8], mut outlen: usize, seed: &[u8], nonce: u8) {
pub fn aes256ctr_prf(out: &mut [u8], mut outlen: usize, seed: &[u8], nonce: u8) {
let mut buf = [0u8; 64];
let mut idx = 0;
let mut pad_nonce = [0u8; 12];
Expand Down
42 changes: 21 additions & 21 deletions src/avx2/consts.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use crate::params::KYBER_Q;
use core::arch::x86_64::*;

pub(crate) const Q: i16 = KYBER_Q as i16;
pub(crate) const QINV: i16 = -3327; // q^-1 mod 2^16
pub(crate) const V: i16 = 20159; // floor(2^26/q + 0.5)
pub(crate) const FHI: i16 = 1441; // mont^2/128
pub(crate) const FLO: i16 = -10079; // qinv*FHI
pub(crate) const MONTSQHI: i16 = 1353; // mont^2
pub(crate) const MONTSQLO: i16 = 20553; // qinv*MONTSQHI
pub(crate) const MASK: i16 = 4095;
pub(crate) const SHIFT: i16 = 32;
pub const Q: i16 = KYBER_Q as i16;
pub const QINV: i16 = -3327; // q^-1 mod 2^16
pub const V: i16 = 20159; // floor(2^26/q + 0.5)
pub const FHI: i16 = 1441; // mont^2/128
pub const FLO: i16 = -10079; // qinv*FHI
pub const MONTSQHI: i16 = 1353; // mont^2
pub const MONTSQLO: i16 = 20553; // qinv*MONTSQHI
pub const MASK: i16 = 4095;
pub const SHIFT: i16 = 32;

pub(crate) const _16XQ: usize = 0;
pub(crate) const _16XQINV: usize = 16;
pub(crate) const _16XV: usize = 32;
pub(crate) const _16XFLO: usize = 48;
pub(crate) const _16XFHI: usize = 64;
pub(crate) const _16XMONTSQLO: usize = 80;
pub(crate) const _16XMONTSQHI: usize = 96;
pub(crate) const _16XMASK: usize = 112;
pub(crate) const _REVIDXB: usize = 128;
pub(crate) const _REVIDXD: usize = 144;
pub(crate) const _ZETAS_EXP: usize = 160;
pub(crate) const _16XSHIFT: usize = 624;
pub const _16XQ: usize = 0;
pub const _16XQINV: usize = 16;
pub const _16XV: usize = 32;
pub const _16XFLO: usize = 48;
pub const _16XFHI: usize = 64;
pub const _16XMONTSQLO: usize = 80;
pub const _16XMONTSQHI: usize = 96;
pub const _16XMASK: usize = 112;
pub const _REVIDXB: usize = 128;
pub const _REVIDXD: usize = 144;
pub const _ZETAS_EXP: usize = 160;
pub const _16XSHIFT: usize = 624;

#[repr(C, align(32))]
pub union Qdata {
Expand Down
2 changes: 1 addition & 1 deletion src/avx2/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{align::*, cbd::*, consts::*, fips202::*, fips202x4::*, params::*, symmetric::*};
use core::arch::x86_64::*;

pub(crate) const NOISE_NBLOCKS: usize =
pub const NOISE_NBLOCKS: usize =
(KYBER_ETA1 * KYBER_N / 4 + SHAKE256_RATE - 1) / SHAKE256_RATE;

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion src/avx2/rejsample.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{consts::*, params::*, symmetric::*};
use core::arch::x86_64::*;

pub(crate) const REJ_UNIFORM_AVX_NBLOCKS: usize =
pub const REJ_UNIFORM_AVX_NBLOCKS: usize =
(12 * KYBER_N / 8 * (1 << 12) / KYBER_Q + XOF_BLOCKBYTES) / XOF_BLOCKBYTES;
const REJ_UNIFORM_AVX_BUFLEN: usize = REJ_UNIFORM_AVX_NBLOCKS * XOF_BLOCKBYTES;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mod symmetric;
pub use api::*;
pub use error::KyberError;
pub use kex::*;
pub use params::*;
pub use params::{KYBER_PUBLICKEYBYTES, KYBER_SECRETKEYBYTES, KYBER_SSBYTES, KYBER_CIPHERTEXTBYTES, KYBER_90S};
pub use rand_core::{CryptoRng, RngCore};

// Feature hack to expose private functions for the Known Answer Tests
Expand Down
26 changes: 13 additions & 13 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@ pub const KYBER_K: usize = if cfg!(feature = "kyber512") {
/// Defaults to false, set`features = ["90s"]` in Cargo.toml to enable.
pub const KYBER_90S: bool = cfg!(feature = "90s");

pub(crate) const KYBER_N: usize = 256;
pub(crate) const KYBER_Q: usize = 3329;
pub const KYBER_N: usize = 256;
pub const KYBER_Q: usize = 3329;

pub(crate) const KYBER_ETA1: usize = if cfg!(feature = "kyber512") { 3 } else { 2 };
pub(crate) const KYBER_ETA2: usize = 2;
pub const KYBER_ETA1: usize = if cfg!(feature = "kyber512") { 3 } else { 2 };
pub const KYBER_ETA2: usize = 2;

// Size of the hashes and seeds
pub const KYBER_SYMBYTES: usize = 32;

/// Size of the shared key
pub const KYBER_SSBYTES: usize = 32;

pub(crate) const KYBER_POLYBYTES: usize = 384;
pub(crate) const KYBER_POLYVECBYTES: usize = KYBER_K * KYBER_POLYBYTES;
pub const KYBER_POLYBYTES: usize = 384;
pub const KYBER_POLYVECBYTES: usize = KYBER_K * KYBER_POLYBYTES;

#[cfg(not(feature = "kyber1024"))]
pub(crate) const KYBER_POLYCOMPRESSEDBYTES: usize = 128;
pub const KYBER_POLYCOMPRESSEDBYTES: usize = 128;
#[cfg(not(feature = "kyber1024"))]
pub(crate) const KYBER_POLYVECCOMPRESSEDBYTES: usize = KYBER_K * 320;
pub const KYBER_POLYVECCOMPRESSEDBYTES: usize = KYBER_K * 320;

#[cfg(feature = "kyber1024")]
pub(crate) const KYBER_POLYCOMPRESSEDBYTES: usize = 160;
pub const KYBER_POLYCOMPRESSEDBYTES: usize = 160;
#[cfg(feature = "kyber1024")]
pub(crate) const KYBER_POLYVECCOMPRESSEDBYTES: usize = KYBER_K * 352;
pub const KYBER_POLYVECCOMPRESSEDBYTES: usize = KYBER_K * 352;

pub(crate) const KYBER_INDCPA_PUBLICKEYBYTES: usize = KYBER_POLYVECBYTES + KYBER_SYMBYTES;
pub(crate) const KYBER_INDCPA_SECRETKEYBYTES: usize = KYBER_POLYVECBYTES;
pub(crate) const KYBER_INDCPA_BYTES: usize =
pub const KYBER_INDCPA_PUBLICKEYBYTES: usize = KYBER_POLYVECBYTES + KYBER_SYMBYTES;
pub const KYBER_INDCPA_SECRETKEYBYTES: usize = KYBER_POLYVECBYTES;
pub const KYBER_INDCPA_BYTES: usize =
KYBER_POLYVECCOMPRESSEDBYTES + KYBER_POLYCOMPRESSEDBYTES;

/// Size in bytes of the Kyber public key
Expand Down
2 changes: 1 addition & 1 deletion src/reference/fips202.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ fn keccak_finalize(s: &mut [u64], pos: usize, r: usize, p: u8) {
/// - u64 mlen: length of input in bytes
/// - [u8] p: domain-separation byte for different Keccak-derived functions
pub fn keccak_absorb_once(s: &mut [u64], r: usize, input: &[u8], mut inlen: usize, p: u8) {
/// Zero State
// Zero State
s.fill(0);

let mut idx = 0usize;
Expand Down
2 changes: 1 addition & 1 deletion src/reference/indcpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub fn indcpa_enc(c: &mut [u8], m: &[u8], pk: &[u8], coins: &[u8]) {

polyvec_ntt(&mut sp);

/// matrix-vector multiplication
// matrix-vector multiplication
for i in 0..KYBER_K {
polyvec_basemul_acc_montgomery(&mut b.vec[i], &at[i], &sp);
}
Expand Down
34 changes: 17 additions & 17 deletions src/symmetric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ use aes::cipher::{generic_array::GenericArray, KeyIvInit, StreamCipher};
type Aes256Ctr = ctr::Ctr32BE<aes::Aes256>;

#[cfg(feature = "90s")]
pub(crate) const AES256CTR_BLOCKBYTES: usize = 64;
pub const AES256CTR_BLOCKBYTES: usize = 64;

#[cfg(feature = "90s")]
pub(crate) const XOF_BLOCKBYTES: usize = AES256CTR_BLOCKBYTES;
pub const XOF_BLOCKBYTES: usize = AES256CTR_BLOCKBYTES;
#[cfg(not(feature = "90s"))]
pub(crate) const XOF_BLOCKBYTES: usize = SHAKE128_RATE;
pub const XOF_BLOCKBYTES: usize = SHAKE128_RATE;

#[cfg(not(feature = "90s"))]
pub(crate) type XofState = KeccakState;
pub type XofState = KeccakState;

#[cfg(feature = "90s")]
pub(crate) type XofState = Aes256CtrCtx;
pub type XofState = Aes256CtrCtx;

#[derive(Copy, Clone)]
pub(crate) struct KeccakState {
pub struct KeccakState {
pub s: [u64; 25],
pub pos: usize,
}
Expand All @@ -48,57 +48,57 @@ impl KeccakState {

/// SHA3-256
#[cfg(not(feature = "90s"))]
pub(crate) fn hash_h(out: &mut [u8], input: &[u8], inlen: usize) {
pub fn hash_h(out: &mut [u8], input: &[u8], inlen: usize) {
sha3_256(out, input, inlen);
}

/// 90s mode SHA2-256
#[cfg(feature = "90s")]
pub(crate) fn hash_h(out: &mut [u8], input: &[u8], inlen: usize) {
pub fn hash_h(out: &mut [u8], input: &[u8], inlen: usize) {
let mut hasher = Sha256::new();
hasher.update(&input[..inlen]);
let digest = hasher.finalize();
out[..digest.len()].copy_from_slice(&digest);
}

#[cfg(not(feature = "90s"))]
pub(crate) fn hash_g(out: &mut [u8], input: &[u8], inlen: usize) {
pub fn hash_g(out: &mut [u8], input: &[u8], inlen: usize) {
sha3_512(out, input, inlen);
}

#[cfg(feature = "90s")]
pub(crate) fn hash_g(out: &mut [u8], input: &[u8], inlen: usize) {
pub fn hash_g(out: &mut [u8], input: &[u8], inlen: usize) {
let mut hasher = Sha512::new();
hasher.update(&input[..inlen]);
let digest = hasher.finalize();
out[..digest.len()].copy_from_slice(&digest);
}

#[cfg(not(feature = "90s"))]
pub(crate) fn xof_absorb(state: &mut XofState, input: &[u8], x: u8, y: u8) {
pub fn xof_absorb(state: &mut XofState, input: &[u8], x: u8, y: u8) {
kyber_shake128_absorb(state, &input, x, y);
}

#[cfg(feature = "90s")]
pub(crate) fn xof_absorb(state: &mut XofState, input: &[u8], x: u8, y: u8) {
pub fn xof_absorb(state: &mut XofState, input: &[u8], x: u8, y: u8) {
let mut nonce = [0u8; 12];
nonce[0] = x;
nonce[1] = y;
aes256ctr_init(state, &input, nonce);
}

#[cfg(not(feature = "90s"))]
pub(crate) fn xof_squeezeblocks(out: &mut [u8], outblocks: usize, state: &mut XofState) {
pub fn xof_squeezeblocks(out: &mut [u8], outblocks: usize, state: &mut XofState) {
kyber_shake128_squeezeblocks(out, outblocks, state);
}

#[cfg(feature = "90s")]
pub(crate) fn xof_squeezeblocks(out: &mut [u8], outblocks: usize, state: &mut XofState) {
pub fn xof_squeezeblocks(out: &mut [u8], outblocks: usize, state: &mut XofState) {
aes256ctr_squeezeblocks(out, outblocks, state);
}

#[cfg(not(feature = "90s"))]
pub(crate) fn prf(out: &mut [u8], outbytes: usize, key: &[u8], nonce: u8) {
pub fn prf(out: &mut [u8], outbytes: usize, key: &[u8], nonce: u8) {
shake256_prf(out, outbytes, &key, nonce);
}

Expand All @@ -121,12 +121,12 @@ pub fn prf(out: &mut [u8], _outbytes: usize, key: &[u8], nonce: u8) {
}

#[cfg(not(feature = "90s"))]
pub(crate) fn kdf(out: &mut [u8], input: &[u8], inlen: usize) {
pub fn kdf(out: &mut [u8], input: &[u8], inlen: usize) {
shake256(out, KYBER_SSBYTES, input, inlen);
}

#[cfg(feature = "90s")]
pub(crate) fn kdf(out: &mut [u8], input: &[u8], inlen: usize) {
pub fn kdf(out: &mut [u8], input: &[u8], inlen: usize) {
let mut hasher = Sha256::new();
hasher.update(&input[..inlen]);
let digest = hasher.finalize();
Expand Down

0 comments on commit 2e44965

Please sign in to comment.