1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-22 23:48:58 +00:00

fix bug in kyber

previous commit introduced a bug in Barrett reduction
This commit is contained in:
Henry Case 2021-05-27 10:15:57 +01:00
parent c98780b4d5
commit 944543c9b9
4 changed files with 27 additions and 8 deletions

View File

@ -35,9 +35,8 @@ int16_t kyber_montgomery_reduce(int32_t a) {
* Returns: integer in {0,q} congruent to a modulo q. * Returns: integer in {0,q} congruent to a modulo q.
**************************************************/ **************************************************/
int16_t kyber_barrett_reduce(int16_t a) { int16_t kyber_barrett_reduce(int16_t a) {
int16_t t;
static const int32_t v = 20159; static const int32_t v = 20159;
int32_t t; t = ((v * a) + (1 << 25)) >> 26;
t = v*a; return a - (t*KYBER_Q);
t >>= 26;
return a - ((int16_t)t)*KYBER_Q;
} }

View File

@ -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_HQCRMRS192: ::std::os::raw::c_uint = 17;
pub const PQC_ALG_KEM_HQCRMRS256: ::std::os::raw::c_uint = 18; 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_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; pub type _bindgen_ty_2 = ::std::os::raw::c_uint;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]

View File

@ -4,7 +4,7 @@ extern crate bindgen;
fn main() { fn main() {
let dst = Config::new("../../../") let dst = Config::new("../../../")
.profile("Release") .profile("Debug")
.very_verbose(true) .very_verbose(true)
.build(); .build();

View File

@ -226,11 +226,14 @@ const KATS: &'static[Register] = &[
//REG_SIGN!(PQC_ALG_SIG_RAINBOWIIICLASSIC), //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 // Can't do multi-threads as DRBG context is global
let pool = ThreadPool::new(thc); let pool = ThreadPool::new(thc);
for k in KATS.iter() { for k in KATS.iter() {
let tmp = kat_dir.clone(); let tmp = kat_dir.clone();
if !file_filter.is_empty() && !k.kat.kat_file.contains(file_filter) {
continue;
}
pool.execute(move || { pool.execute(move || {
DRBGV.lock().unwrap() DRBGV.lock().unwrap()
.insert(thread::current().id(), DrbgCtx::new()); .insert(thread::current().id(), DrbgCtx::new());
@ -266,8 +269,15 @@ fn main() {
None => 4 /* by default 4 threads */, 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()) { 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") None => panic!("--katdir required")
}; };
} }