Skip to content

Commit

Permalink
add explanation for new consts
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed May 27, 2024
1 parent 57e3b9a commit 447bee5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions src/polynomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -319,7 +319,9 @@ impl Poly<Normalised> {
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,
)?;
}

Expand All @@ -340,7 +342,10 @@ impl Poly<Normalised> {
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,
)?;
}

Expand Down Expand Up @@ -467,9 +472,7 @@ impl Poly<Normalised> {
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 {
Expand Down
10 changes: 7 additions & 3 deletions src/vectors.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -184,7 +184,9 @@ impl PolyVec<Normalised> {
*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;
}

Expand Down Expand Up @@ -225,7 +227,9 @@ impl PolyVec<Normalised> {
*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;
}

Expand Down

0 comments on commit 447bee5

Please sign in to comment.