From 447bee57eed2d9c9cfe80532f5ffe98179a360d1 Mon Sep 17 00:00:00 2001 From: supinie <86788874+supinie@users.noreply.github.com> Date: Mon, 27 May 2024 16:57:52 +0100 Subject: [PATCH] add explanation for new consts --- src/params.rs | 8 ++++++-- src/polynomials.rs | 15 +++++++++------ src/vectors.rs | 10 +++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/params.rs b/src/params.rs index 82054aa..5a7f72d 100644 --- a/src/params.rs +++ b/src/params.rs @@ -4,13 +4,17 @@ use num_enum::{IntoPrimitive, TryFromPrimitive}; pub const N: usize = 256; pub const Q: usize = 3329; +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] pub const Q_I16: i16 = Q as i16; +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] pub const Q_U16: u16 = Q as u16; +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] pub const Q_I32: i32 = Q as i32; +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] pub const Q_U32: u32 = Q as u32; -pub const Q_DIV: u64 = 80635; -pub const Q_DIV_VEC: u64 = 1290167; +pub const Q_DIV: u64 = 80_635; // round(2^28 / Q) +pub const Q_DIV_VEC: u64 = 1_290_167; // round(2^32 / Q) pub const SYMBYTES: usize = 32; // size of hashes diff --git a/src/polynomials.rs b/src/polynomials.rs index 9494280..9b37380 100644 --- a/src/polynomials.rs +++ b/src/polynomials.rs @@ -4,7 +4,7 @@ mod sample; use crate::{ errors::{CrystalsError, PackingError}, field_operations::{barrett_reduce, conditional_sub_q, mont_form, montgomery_reduce}, - params::{SecurityLevel, N, POLYBYTES, Q, Q_I16, Q_U16, Q_U32, Q_DIV, SYMBYTES}, + params::{SecurityLevel, N, POLYBYTES, Q, Q_DIV, Q_I16, Q_U16, Q_U32, SYMBYTES}, polynomials::ntt::ZETAS, }; use core::num::TryFromIntError; @@ -319,7 +319,9 @@ impl Poly { let mut temp = *coeff; temp += (temp >> 15) & Q_I16; *t_elem = u8::try_from( - (((((u64::try_from(temp)?) << 4) + u64::from(Q_U16 / 2)) * Q_DIV) >> 28) & 0xf, + (((((u64::try_from(temp)?) << 4) + u64::from(Q_U16 / 2)) * Q_DIV) + >> 28) + & 0xf, )?; } @@ -340,7 +342,10 @@ impl Poly { let mut temp = *coeff; temp += (temp >> 15) & Q_I16; *t_elem = u8::try_from( - (((((u64::try_from(temp)?) << 5) + u64::from(Q_U32 / 2)) * (Q_DIV / 2)) >> 27) & 0x1f, + (((((u64::try_from(temp)?) << 5) + u64::from(Q_U32 / 2)) + * (Q_DIV / 2)) + >> 27) + & 0x1f, )?; } @@ -467,9 +472,7 @@ impl Poly { buf_chunk[4] >> 3, ]; for (coeff, t_elem) in coeffs_chunk.iter_mut().zip(temp.iter()) { - *coeff = i16::try_from( - ((u32::from(*t_elem) & 31) * Q_U32 + 16) >> 5, - )?; + *coeff = i16::try_from(((u32::from(*t_elem) & 31) * Q_U32 + 16) >> 5)?; } } Ok(Self { diff --git a/src/vectors.rs b/src/vectors.rs index 722f270..024f89b 100644 --- a/src/vectors.rs +++ b/src/vectors.rs @@ -1,6 +1,6 @@ use crate::{ errors::{CrystalsError, PackingError}, - params::{Eta, SecurityLevel, K, N, POLYBYTES, Q_I16, Q_U32, Q_DIV_VEC}, + params::{Eta, SecurityLevel, K, N, POLYBYTES, Q_DIV_VEC, Q_I16, Q_U32}, polynomials::{Barrett, Montgomery, Normalised, Poly, Reduced, State, Unnormalised, Unreduced}, }; use tinyvec::{array_vec, ArrayVec}; @@ -184,7 +184,9 @@ impl PolyVec { *t_elem = *coeff as u16; *t_elem = t_elem.wrapping_add((((*t_elem as i16) >> 15) & Q_I16) as u16); - *t_elem = (((((u64::from(*t_elem) << 10) + u64::from(Q_U32 / 2)) * Q_DIV_VEC) >> 32) + *t_elem = (((((u64::from(*t_elem) << 10) + u64::from(Q_U32 / 2)) + * Q_DIV_VEC) + >> 32) & 0x3ff) as u16; } @@ -225,7 +227,9 @@ impl PolyVec { *t_elem = *coeff as u16; *t_elem = t_elem.wrapping_add((((*t_elem as i16) >> 15) & Q_I16) as u16); - *t_elem = (((((u64::from(*t_elem) << 11) + u64::from(Q_U32 / 2)) * (Q_DIV_VEC / 2)) >> 31) + *t_elem = (((((u64::from(*t_elem) << 11) + u64::from(Q_U32 / 2)) + * (Q_DIV_VEC / 2)) + >> 31) & 0x7ff) as u16; }