Skip to content

Commit

Permalink
Modify: Doc comments on functions (#96)
Browse files Browse the repository at this point in the history
* Modify: Doc comments on functions
* Remove: Redundant comments
* Modify: Run cargo fmt on fuzz/
* Cleanup: function visibility
* Skip cargo fmt on build.rs, default adds whitespace to error msg
* Add rustfmt.toml file for posterity
Doc comments plays nicer with rust-analyzer, better DX
  • Loading branch information
mberry authored Aug 22, 2023
1 parent 056aeab commit 9da4dd9
Show file tree
Hide file tree
Showing 29 changed files with 874 additions and 1,010 deletions.
10 changes: 5 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[rustfmt::skip]
fn main() {
#[cfg(not(feature = "wasm"))]
{
Expand All @@ -16,11 +17,10 @@ fn main() {
nasm.files(paths);
nasm.include(ROOT);

for o in nasm.compile_objects().expect(
"
Compiling NASM files:
Ensure it is installed and in your path
https://www.nasm.us/",
for o in nasm.compile_objects().expect("
Compiling NASM files:
Ensure it is installed and in your path
https://www.nasm.us/",
) {
linker.object(o);
}
Expand Down
28 changes: 15 additions & 13 deletions fuzz/src/bin/decap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ use honggfuzz::fuzz;
use pqc_kyber::*;

fn main() -> Result<(), KyberError> {
let mut ss = [0u8; KYBER_SSBYTES];
const CTBYTES: usize = KYBER_CIPHERTEXTBYTES;
const SKBYTES: usize = KYBER_SECRETKEYBYTES;
loop {
fuzz!(|data: &[u8] | {
if data.len() != CTBYTES + SKBYTES {return};
match crypto_kem_dec(&mut ss, &data[..CTBYTES], &data[CTBYTES..SKBYTES]) {
Ok(_) => (),
Err(_) => ()
}
});
};
}
let mut ss = [0u8; KYBER_SSBYTES];
const CTBYTES: usize = KYBER_CIPHERTEXTBYTES;
const SKBYTES: usize = KYBER_SECRETKEYBYTES;
loop {
fuzz!(|data: &[u8]| {
if data.len() != CTBYTES + SKBYTES {
return;
};
match crypto_kem_dec(&mut ss, &data[..CTBYTES], &data[CTBYTES..SKBYTES]) {
Ok(_) => (),
Err(_) => (),
}
});
}
}
30 changes: 16 additions & 14 deletions fuzz/src/bin/encap.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use honggfuzz::fuzz;
use pqc_kyber::*;
use rand_xoshiro::rand_core::{SeedableRng, RngCore};
use rand_xoshiro::rand_core::{RngCore, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;

fn main() -> Result<(), KyberError> {
let mut _rng = rand::thread_rng(); //placeholder
let mut rng = Xoshiro256Plus::seed_from_u64(0);
let mut ct = [0u8; KYBER_CIPHERTEXTBYTES];
let mut ss = [0u8; KYBER_SSBYTES];
let mut s1 = [0u8; 32];
loop {
rng.fill_bytes(&mut s1);
fuzz!(|data: &[u8] | {
if data.len() != KYBER_PUBLICKEYBYTES {return};
crypto_kem_enc(&mut ct, &mut ss, data, &mut _rng, Some(&s1))?;
});
};
}
let mut _rng = rand::thread_rng(); //placeholder
let mut rng = Xoshiro256Plus::seed_from_u64(0);
let mut ct = [0u8; KYBER_CIPHERTEXTBYTES];
let mut ss = [0u8; KYBER_SSBYTES];
let mut s1 = [0u8; 32];
loop {
rng.fill_bytes(&mut s1);
fuzz!(|data: &[u8]| {
if data.len() != KYBER_PUBLICKEYBYTES {
return;
};
crypto_kem_enc(&mut ct, &mut ss, data, &mut _rng, Some(&s1))?;
});
}
}
32 changes: 16 additions & 16 deletions fuzz/src/bin/keypair.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use honggfuzz::fuzz;
use pqc_kyber::*;
use rand_xoshiro::rand_core::{SeedableRng, RngCore};
use rand_xoshiro::rand_core::{RngCore, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;

fn main() -> Result<(), KyberError> {
let mut _rng = rand::thread_rng(); //placeholder
let mut rng = Xoshiro256Plus::seed_from_u64(0);
let mut public = [0u8; KYBER_PUBLICKEYBYTES];
let mut secret = [0u8; KYBER_SECRETKEYBYTES];
let mut s1 = [0u8; 32];
let mut s2 = [0u8; 32];
loop {
rng.fill_bytes(&mut s1);
rng.fill_bytes(&mut s2);
fuzz!(|data: ()| {
crypto_kem_keypair(&mut public, &mut secret, &mut _rng, Some((&s1, &s2)))?;
});
};
}
let mut _rng = rand::thread_rng(); //placeholder
let mut rng = Xoshiro256Plus::seed_from_u64(0);
let mut public = [0u8; KYBER_PUBLICKEYBYTES];
let mut secret = [0u8; KYBER_SECRETKEYBYTES];
let mut s1 = [0u8; 32];
let mut s2 = [0u8; 32];
loop {
rng.fill_bytes(&mut s1);
rng.fill_bytes(&mut s2);
fuzz!(|data: ()| {
crypto_kem_keypair(&mut public, &mut secret, &mut _rng, Some((&s1, &s2)))?;
});
}
}
Empty file added rustfmt.toml
Empty file.
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
16 changes: 0 additions & 16 deletions src/avx2/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ use crate::poly::NOISE_NBLOCKS;
use crate::symmetric::*;
use core::arch::x86_64::*;

// Buffer unions
// #[derive(Copy, Clone)]
// #[repr(C, align(8))]
// pub(crate) union Align8<const N: usize, const V: usize> {
// pub coeffs: [u8; N],
// pub vec: [__m256i; V]
// }

// impl<const N: usize, const V: usize> Align8 <N, V>{
// pub fn new() -> Self {
// Self {
// coeffs: [0u8; N]
// }
// }
// }

#[derive(Copy, Clone)]
#[repr(C, align(32))]
pub union GenMatrixBuf {
Expand Down
25 changes: 0 additions & 25 deletions src/avx2/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,3 @@
#define _ZETAS_EXP 160
#define _16XSHIFT 624

/* The C ABI on MacOS exports all symbols with a leading
* underscore. This means that any symbols we refer to from
* C files (functions) can't be found, and all symbols we
* refer to from ASM also can't be found.
*
* This define helps us get around this
*/
// #ifdef __ASSEMBLER__
// #if defined(__WIN32__) || defined(__APPLE__)
// #define decorate(s) _##s
// #define cdecl2(s) decorate(s)
// #define cdecl(s) cdecl2(KYBER_NAMESPACE(##s))
// #else
// #define cdecl(s) KYBER_NAMESPACE(##s)
// #endif
// #endif

// #ifndef __ASSEMBLER__
// #include "align.h"
// typedef ALIGNED_INT16(640) qdata_t;
// #define qdata KYBER_NAMESPACE(qdata)
// extern const qdata_t qdata;
// #endif

// #endif
43 changes: 21 additions & 22 deletions src/avx2/consts.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
use crate::params::KYBER_Q;
use core::arch::x86_64::*;

pub(crate) const Q: i16 = KYBER_Q as i16;
// pub(crate) const MONT: i16 = -1044; // 2^16 mod q
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
Loading

0 comments on commit 9da4dd9

Please sign in to comment.