From 944543c9b9d00586c7e739b39f3cbebc7cc3ddc1 Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Thu, 27 May 2021 10:15:57 +0100 Subject: [PATCH] fix bug in kyber previous commit introduced a bug in Barrett reduction --- src/kem/kyber/common/reduce.c | 7 +++---- src/rustapi/pqc-sys/src/bindings.rs | 12 +++++++++++- src/rustapi/pqc-sys/src/build.rs | 2 +- test/katrunner/src/main.rs | 14 ++++++++++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/kem/kyber/common/reduce.c b/src/kem/kyber/common/reduce.c index 1a1c5a43..903d677e 100644 --- a/src/kem/kyber/common/reduce.c +++ b/src/kem/kyber/common/reduce.c @@ -35,9 +35,8 @@ int16_t kyber_montgomery_reduce(int32_t a) { * Returns: integer in {0,q} congruent to a modulo q. **************************************************/ int16_t kyber_barrett_reduce(int16_t a) { + int16_t t; static const int32_t v = 20159; - int32_t t; - t = v*a; - t >>= 26; - return a - ((int16_t)t)*KYBER_Q; + t = ((v * a) + (1 << 25)) >> 26; + return a - (t*KYBER_Q); } diff --git a/src/rustapi/pqc-sys/src/bindings.rs b/src/rustapi/pqc-sys/src/bindings.rs index af4b40bf..81728f5a 100644 --- a/src/rustapi/pqc-sys/src/bindings.rs +++ b/src/rustapi/pqc-sys/src/bindings.rs @@ -257,7 +257,17 @@ pub const PQC_ALG_KEM_HQCRMRS128: ::std::os::raw::c_uint = 16; pub const PQC_ALG_KEM_HQCRMRS192: ::std::os::raw::c_uint = 17; pub const PQC_ALG_KEM_HQCRMRS256: ::std::os::raw::c_uint = 18; pub const PQC_ALG_KEM_SIKE434: ::std::os::raw::c_uint = 19; -pub const PQC_ALG_KEM_MAX: ::std::os::raw::c_uint = 20; +pub const PQC_ALG_KEM_MCELIECE348864: ::std::os::raw::c_uint = 20; +pub const PQC_ALG_KEM_MCELIECE460896: ::std::os::raw::c_uint = 21; +pub const PQC_ALG_KEM_MCELIECE6688128: ::std::os::raw::c_uint = 22; +pub const PQC_ALG_KEM_MCELIECE6960119: ::std::os::raw::c_uint = 23; +pub const PQC_ALG_KEM_MCELIECE8192128: ::std::os::raw::c_uint = 24; +pub const PQC_ALG_KEM_MCELIECE348864F: ::std::os::raw::c_uint = 25; +pub const PQC_ALG_KEM_MCELIECE460896F: ::std::os::raw::c_uint = 26; +pub const PQC_ALG_KEM_MCELIECE6688128F: ::std::os::raw::c_uint = 27; +pub const PQC_ALG_KEM_MCELIECE6960119F: ::std::os::raw::c_uint = 28; +pub const PQC_ALG_KEM_MCELIECE8192128F: ::std::os::raw::c_uint = 29; +pub const PQC_ALG_KEM_MAX: ::std::os::raw::c_uint = 30; pub type _bindgen_ty_2 = ::std::os::raw::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] diff --git a/src/rustapi/pqc-sys/src/build.rs b/src/rustapi/pqc-sys/src/build.rs index 761dd312..35205437 100644 --- a/src/rustapi/pqc-sys/src/build.rs +++ b/src/rustapi/pqc-sys/src/build.rs @@ -4,7 +4,7 @@ extern crate bindgen; fn main() { let dst = Config::new("../../../") - .profile("Release") + .profile("Debug") .very_verbose(true) .build(); diff --git a/test/katrunner/src/main.rs b/test/katrunner/src/main.rs index 3bad546c..2acd184e 100644 --- a/test/katrunner/src/main.rs +++ b/test/katrunner/src/main.rs @@ -226,11 +226,14 @@ const KATS: &'static[Register] = &[ //REG_SIGN!(PQC_ALG_SIG_RAINBOWIIICLASSIC), ]; -fn execute(kat_dir: String, thc: usize) { +fn execute(kat_dir: String, thc: usize, file_filter: &str) { // Can't do multi-threads as DRBG context is global let pool = ThreadPool::new(thc); for k in KATS.iter() { let tmp = kat_dir.clone(); + if !file_filter.is_empty() && !k.kat.kat_file.contains(file_filter) { + continue; + } pool.execute(move || { DRBGV.lock().unwrap() .insert(thread::current().id(), DrbgCtx::new()); @@ -266,8 +269,15 @@ fn main() { None => 4 /* by default 4 threads */, }; + // Run only selected name of the KAT file + let file_filter = match argmap.get(&"--filter".to_string()) { + Some(n) => n, + None => "" + }; + match argmap.get(&"--katdir".to_string()) { - Some(kat_dir) => execute(kat_dir.to_string(), thread_number), + Some(kat_dir) => execute(kat_dir.to_string(), thread_number, file_filter), None => panic!("--katdir required") }; + }