Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed Aug 18, 2023
1 parent 0057e36 commit c93928e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/field_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use more_asserts::assert_ge;
use crate::params::*;

// given -2^15 q <= x < 2^15 q, returns -q < y < q with y = x 2^-16 mod q
// Example:
// let x = montgomery_reduce(y);
pub fn montgomery_reduce(x: i32) -> i16 {
const QPRIME: i32 = 62209;
let m = x.wrapping_mul(QPRIME) as i16;
Expand All @@ -11,6 +13,8 @@ pub fn montgomery_reduce(x: i32) -> i16 {
}

// given x, return x 2^16 mod q
// Example:
// let x = to_mont(y);
pub fn to_mont(x: i16) -> i16 {
const R_SQUARED_MOD_Q: i32 = 1353;
return montgomery_reduce((x as i32) * R_SQUARED_MOD_Q);
Expand All @@ -19,6 +23,8 @@ pub fn to_mont(x: i16) -> i16 {
// given x, find 0 <= y <= q with y = x mod q
//
// iff x = -nq for some natural number n, barrett_reduce(x) = q != 0
// Example:
// let x = barrett_reduce(y);
pub fn barrett_reduce(x: i16) -> i16 {
const APPROXIMATION: usize = 20159;
// From Cloudflare's circl Kyber implementation:
Expand All @@ -38,6 +44,9 @@ pub fn barrett_reduce(x: i16) -> i16 {
return x.wrapping_sub(inside_floor.wrapping_mul(Q as i16));
}

// given x, if x < Q return x, otherwise return x - Q
// Example:
// let x = cond_sub_q(y);
pub fn cond_sub_q(x: i16) -> i16 {
assert_ge!(
x,
Expand Down

0 comments on commit c93928e

Please sign in to comment.