Skip to content

Commit 9da4dd9

Browse files
authored
Modify: Doc comments on functions (#96)
* 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
1 parent 056aeab commit 9da4dd9

29 files changed

+874
-1010
lines changed

build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[rustfmt::skip]
12
fn main() {
23
#[cfg(not(feature = "wasm"))]
34
{
@@ -16,11 +17,10 @@ fn main() {
1617
nasm.files(paths);
1718
nasm.include(ROOT);
1819

19-
for o in nasm.compile_objects().expect(
20-
"
21-
Compiling NASM files:
22-
Ensure it is installed and in your path
23-
https://www.nasm.us/",
20+
for o in nasm.compile_objects().expect("
21+
Compiling NASM files:
22+
Ensure it is installed and in your path
23+
https://www.nasm.us/",
2424
) {
2525
linker.object(o);
2626
}

fuzz/src/bin/decap.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ use honggfuzz::fuzz;
22
use pqc_kyber::*;
33

44
fn main() -> Result<(), KyberError> {
5-
let mut ss = [0u8; KYBER_SSBYTES];
6-
const CTBYTES: usize = KYBER_CIPHERTEXTBYTES;
7-
const SKBYTES: usize = KYBER_SECRETKEYBYTES;
8-
loop {
9-
fuzz!(|data: &[u8] | {
10-
if data.len() != CTBYTES + SKBYTES {return};
11-
match crypto_kem_dec(&mut ss, &data[..CTBYTES], &data[CTBYTES..SKBYTES]) {
12-
Ok(_) => (),
13-
Err(_) => ()
14-
}
15-
});
16-
};
17-
}
5+
let mut ss = [0u8; KYBER_SSBYTES];
6+
const CTBYTES: usize = KYBER_CIPHERTEXTBYTES;
7+
const SKBYTES: usize = KYBER_SECRETKEYBYTES;
8+
loop {
9+
fuzz!(|data: &[u8]| {
10+
if data.len() != CTBYTES + SKBYTES {
11+
return;
12+
};
13+
match crypto_kem_dec(&mut ss, &data[..CTBYTES], &data[CTBYTES..SKBYTES]) {
14+
Ok(_) => (),
15+
Err(_) => (),
16+
}
17+
});
18+
}
19+
}

fuzz/src/bin/encap.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
use honggfuzz::fuzz;
22
use pqc_kyber::*;
3-
use rand_xoshiro::rand_core::{SeedableRng, RngCore};
3+
use rand_xoshiro::rand_core::{RngCore, SeedableRng};
44
use rand_xoshiro::Xoshiro256Plus;
55

66
fn main() -> Result<(), KyberError> {
7-
let mut _rng = rand::thread_rng(); //placeholder
8-
let mut rng = Xoshiro256Plus::seed_from_u64(0);
9-
let mut ct = [0u8; KYBER_CIPHERTEXTBYTES];
10-
let mut ss = [0u8; KYBER_SSBYTES];
11-
let mut s1 = [0u8; 32];
12-
loop {
13-
rng.fill_bytes(&mut s1);
14-
fuzz!(|data: &[u8] | {
15-
if data.len() != KYBER_PUBLICKEYBYTES {return};
16-
crypto_kem_enc(&mut ct, &mut ss, data, &mut _rng, Some(&s1))?;
17-
});
18-
};
19-
}
7+
let mut _rng = rand::thread_rng(); //placeholder
8+
let mut rng = Xoshiro256Plus::seed_from_u64(0);
9+
let mut ct = [0u8; KYBER_CIPHERTEXTBYTES];
10+
let mut ss = [0u8; KYBER_SSBYTES];
11+
let mut s1 = [0u8; 32];
12+
loop {
13+
rng.fill_bytes(&mut s1);
14+
fuzz!(|data: &[u8]| {
15+
if data.len() != KYBER_PUBLICKEYBYTES {
16+
return;
17+
};
18+
crypto_kem_enc(&mut ct, &mut ss, data, &mut _rng, Some(&s1))?;
19+
});
20+
}
21+
}

fuzz/src/bin/keypair.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use honggfuzz::fuzz;
22
use pqc_kyber::*;
3-
use rand_xoshiro::rand_core::{SeedableRng, RngCore};
3+
use rand_xoshiro::rand_core::{RngCore, SeedableRng};
44
use rand_xoshiro::Xoshiro256Plus;
5-
5+
66
fn main() -> Result<(), KyberError> {
7-
let mut _rng = rand::thread_rng(); //placeholder
8-
let mut rng = Xoshiro256Plus::seed_from_u64(0);
9-
let mut public = [0u8; KYBER_PUBLICKEYBYTES];
10-
let mut secret = [0u8; KYBER_SECRETKEYBYTES];
11-
let mut s1 = [0u8; 32];
12-
let mut s2 = [0u8; 32];
13-
loop {
14-
rng.fill_bytes(&mut s1);
15-
rng.fill_bytes(&mut s2);
16-
fuzz!(|data: ()| {
17-
crypto_kem_keypair(&mut public, &mut secret, &mut _rng, Some((&s1, &s2)))?;
18-
});
19-
};
20-
}
7+
let mut _rng = rand::thread_rng(); //placeholder
8+
let mut rng = Xoshiro256Plus::seed_from_u64(0);
9+
let mut public = [0u8; KYBER_PUBLICKEYBYTES];
10+
let mut secret = [0u8; KYBER_SECRETKEYBYTES];
11+
let mut s1 = [0u8; 32];
12+
let mut s2 = [0u8; 32];
13+
loop {
14+
rng.fill_bytes(&mut s1);
15+
rng.fill_bytes(&mut s2);
16+
fuzz!(|data: ()| {
17+
crypto_kem_keypair(&mut public, &mut secret, &mut _rng, Some((&s1, &s2)))?;
18+
});
19+
}
20+
}

rustfmt.toml

Whitespace-only changes.

src/avx2/aes256ctr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::arch::x86_64::*;
88

99
#[derive(Clone, Copy)]
1010
#[repr(C)]
11-
pub(crate) struct Aes256CtrCtx {
11+
pub struct Aes256CtrCtx {
1212
pub rkeys: [__m128i; 16],
1313
pub n: __m128i,
1414
}
@@ -76,7 +76,7 @@ unsafe fn cast_128(x: __m128i) -> __m128 {
7676
_mm_castsi128_ps(x)
7777
}
7878

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

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

151151
#[cfg(feature = "90s")]
152-
pub(crate) fn aes256ctr_prf(out: &mut [u8], mut outlen: usize, seed: &[u8], nonce: u8) {
152+
pub fn aes256ctr_prf(out: &mut [u8], mut outlen: usize, seed: &[u8], nonce: u8) {
153153
let mut buf = [0u8; 64];
154154
let mut idx = 0;
155155
let mut pad_nonce = [0u8; 12];

src/avx2/align.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ use crate::poly::NOISE_NBLOCKS;
77
use crate::symmetric::*;
88
use core::arch::x86_64::*;
99

10-
// Buffer unions
11-
// #[derive(Copy, Clone)]
12-
// #[repr(C, align(8))]
13-
// pub(crate) union Align8<const N: usize, const V: usize> {
14-
// pub coeffs: [u8; N],
15-
// pub vec: [__m256i; V]
16-
// }
17-
18-
// impl<const N: usize, const V: usize> Align8 <N, V>{
19-
// pub fn new() -> Self {
20-
// Self {
21-
// coeffs: [0u8; N]
22-
// }
23-
// }
24-
// }
25-
2610
#[derive(Copy, Clone)]
2711
#[repr(C, align(32))]
2812
pub union GenMatrixBuf {

src/avx2/consts.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,3 @@
1111
#define _ZETAS_EXP 160
1212
#define _16XSHIFT 624
1313

14-
/* The C ABI on MacOS exports all symbols with a leading
15-
* underscore. This means that any symbols we refer to from
16-
* C files (functions) can't be found, and all symbols we
17-
* refer to from ASM also can't be found.
18-
*
19-
* This define helps us get around this
20-
*/
21-
// #ifdef __ASSEMBLER__
22-
// #if defined(__WIN32__) || defined(__APPLE__)
23-
// #define decorate(s) _##s
24-
// #define cdecl2(s) decorate(s)
25-
// #define cdecl(s) cdecl2(KYBER_NAMESPACE(##s))
26-
// #else
27-
// #define cdecl(s) KYBER_NAMESPACE(##s)
28-
// #endif
29-
// #endif
30-
31-
// #ifndef __ASSEMBLER__
32-
// #include "align.h"
33-
// typedef ALIGNED_INT16(640) qdata_t;
34-
// #define qdata KYBER_NAMESPACE(qdata)
35-
// extern const qdata_t qdata;
36-
// #endif
37-
38-
// #endif

src/avx2/consts.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
use crate::params::KYBER_Q;
22
use core::arch::x86_64::*;
33

4-
pub(crate) const Q: i16 = KYBER_Q as i16;
5-
// pub(crate) const MONT: i16 = -1044; // 2^16 mod q
6-
pub(crate) const QINV: i16 = -3327; // q^-1 mod 2^16
7-
pub(crate) const V: i16 = 20159; // floor(2^26/q + 0.5)
8-
pub(crate) const FHI: i16 = 1441; // mont^2/128
9-
pub(crate) const FLO: i16 = -10079; // qinv*FHI
10-
pub(crate) const MONTSQHI: i16 = 1353; // mont^2
11-
pub(crate) const MONTSQLO: i16 = 20553; // qinv*MONTSQHI
12-
pub(crate) const MASK: i16 = 4095;
13-
pub(crate) const SHIFT: i16 = 32;
4+
pub const Q: i16 = KYBER_Q as i16;
5+
pub const QINV: i16 = -3327; // q^-1 mod 2^16
6+
pub const V: i16 = 20159; // floor(2^26/q + 0.5)
7+
pub const FHI: i16 = 1441; // mont^2/128
8+
pub const FLO: i16 = -10079; // qinv*FHI
9+
pub const MONTSQHI: i16 = 1353; // mont^2
10+
pub const MONTSQLO: i16 = 20553; // qinv*MONTSQHI
11+
pub const MASK: i16 = 4095;
12+
pub const SHIFT: i16 = 32;
1413

15-
pub(crate) const _16XQ: usize = 0;
16-
pub(crate) const _16XQINV: usize = 16;
17-
pub(crate) const _16XV: usize = 32;
18-
pub(crate) const _16XFLO: usize = 48;
19-
pub(crate) const _16XFHI: usize = 64;
20-
pub(crate) const _16XMONTSQLO: usize = 80;
21-
pub(crate) const _16XMONTSQHI: usize = 96;
22-
pub(crate) const _16XMASK: usize = 112;
23-
pub(crate) const _REVIDXB: usize = 128;
24-
pub(crate) const _REVIDXD: usize = 144;
25-
pub(crate) const _ZETAS_EXP: usize = 160;
26-
pub(crate) const _16XSHIFT: usize = 624;
14+
pub const _16XQ: usize = 0;
15+
pub const _16XQINV: usize = 16;
16+
pub const _16XV: usize = 32;
17+
pub const _16XFLO: usize = 48;
18+
pub const _16XFHI: usize = 64;
19+
pub const _16XMONTSQLO: usize = 80;
20+
pub const _16XMONTSQHI: usize = 96;
21+
pub const _16XMASK: usize = 112;
22+
pub const _REVIDXB: usize = 128;
23+
pub const _REVIDXD: usize = 144;
24+
pub const _ZETAS_EXP: usize = 160;
25+
pub const _16XSHIFT: usize = 624;
2726

2827
#[repr(C, align(32))]
2928
pub union Qdata {

0 commit comments

Comments
 (0)