Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use montgomery multiplication everywhere. #49

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/kem/kyber768/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ pub(crate) fn barrett_reduce(value: i16) -> KyberFieldElement {
value - (quotient * FIELD_MODULUS)
}

pub(crate) fn fe_mul(lhs: KyberFieldElement, rhs: KyberFieldElement) -> KyberFieldElement {
// TODO: This will shortly be replaced by an implementation of
// montgomery reduction.
let product: i32 = i32::from(lhs) * i32::from(rhs);

let reduced = (product % i32::from(FIELD_MODULUS)) as i16;

if reduced > FIELD_MODULUS / 2 {
reduced - FIELD_MODULUS
} else if reduced < -FIELD_MODULUS / 2 {
reduced + FIELD_MODULUS
} else {
reduced
}
const MONTGOMERY_SHIFT: i64 = 16;
const MONTGOMERY_R: i64 = 1i64 << MONTGOMERY_SHIFT;
const INVERSE_OF_MODULUS_MOD_R: i64 = -3327; // FIELD_MODULUS^{-1} mod MONTGOMERY_R

pub(crate) fn montgomery_reduce(value: i32) -> KyberFieldElement {
let t: i64 = i64::from(value) * INVERSE_OF_MODULUS_MOD_R;
let t: i32 = (t & (MONTGOMERY_R - 1)) as i32;

let t = value - (t * i32::from(FIELD_MODULUS));

(t >> MONTGOMERY_SHIFT) as i16
}

// Given a |value|, return |value|*R mod q. Notice that montgomery_reduce
// returns a value aR^{-1} mod q, and so montgomery_reduce(|value| * R^2)
// returns |value| * R^2 & R^{-1} mod q = |value| * R mod q.
pub(crate) fn to_montgomery_domain(value: i32) -> KyberFieldElement {
// R^2 mod q = (2^16)^2 mod 3329 = 1353
montgomery_reduce(1353 * value)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
6 changes: 4 additions & 2 deletions src/kem/kyber768/ind_cpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ pub(crate) fn generate_keypair(
))
}

fn encode_and_compress_u(input: [KyberPolynomialRingElement; RANK]) -> [u8; VECTOR_U_ENCODED_SIZE] {
fn compress_then_encode_u(
input: [KyberPolynomialRingElement; RANK],
) -> [u8; VECTOR_U_ENCODED_SIZE] {
let mut out = [0u8; VECTOR_U_ENCODED_SIZE];
for (i, re) in input.into_iter().enumerate() {
out[i * BYTES_PER_ENCODED_ELEMENT_OF_U..(i + 1) * BYTES_PER_ENCODED_ELEMENT_OF_U]
Expand Down Expand Up @@ -258,7 +260,7 @@ pub(crate) fn encrypt(
+ decompress(message_as_ring_element, 1);

// c_1 := Encode_{du}(Compress_q(u,d_u))
let c1 = encode_and_compress_u(u);
let c1 = compress_then_encode_u(u);

// c_2 := Encode_{dv}(Compress_q(v,d_v))
let c2 = serialize_little_endian_4(compress(v, VECTOR_V_COMPRESSION_FACTOR));
Expand Down
120 changes: 68 additions & 52 deletions src/kem/kyber768/ntt.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
use crate::kem::kyber768::{
arithmetic::{barrett_reduce, KyberPolynomialRingElement},
arithmetic::{barrett_reduce, to_montgomery_domain, KyberPolynomialRingElement},
parameters::RANK,
};

use self::kyber_polynomial_ring_element_mod::ntt_multiply;

pub(crate) mod kyber_polynomial_ring_element_mod {
use crate::kem::kyber768::{
arithmetic::{barrett_reduce, fe_mul, KyberFieldElement, KyberPolynomialRingElement},
arithmetic::{
barrett_reduce, montgomery_reduce, KyberFieldElement, KyberPolynomialRingElement,
},
parameters::COEFFICIENTS_IN_RING_ELEMENT,
};

const ZETAS: [i16; 128] = [
1, -1600, -749, -40, -687, 630, -1432, 848, 1062, -1410, 193, 797, -543, -69, 569, -1583,
296, -882, 1339, 1476, -283, 56, -1089, 1333, 1426, -1235, 535, -447, -936, -450, -1355,
821, 289, 331, -76, -1573, 1197, -1025, -1052, -1274, 650, -1352, -816, 632, -464, 33,
1320, -1414, -1010, 1435, 807, 452, 1438, -461, 1534, -927, -682, -712, 1481, 648, -855,
-219, 1227, 910, 17, -568, 583, -680, 1637, 723, -1041, 1100, 1409, -667, -48, 233, 756,
-1173, -314, -279, -1626, 1651, -540, -1540, -1482, 952, 1461, -642, 939, -1021, -892,
-941, 733, -992, 268, 641, 1584, -1031, -1292, -109, 375, -780, -1239, 1645, 1063, 319,
-556, 757, -1230, 561, -863, -735, -525, 1092, 403, 1026, 1143, -1179, -554, 886, -1607,
1212, -1455, 1029, -1219, -394, 885, -1175,
];

const MOD_ROOTS: [i16; 128] = [
17, -17, -568, 568, 583, -583, -680, 680, 1637, -1637, 723, -723, -1041, 1041, 1100, -1100,
1409, -1409, -667, 667, -48, 48, 233, -233, 756, -756, -1173, 1173, -314, 314, -279, 279,
-1626, 1626, 1651, -1651, -540, 540, -1540, 1540, -1482, 1482, 952, -952, 1461, -1461,
-642, 642, 939, -939, -1021, 1021, -892, 892, -941, 941, 733, -733, -992, 992, 268, -268,
641, -641, 1584, -1584, -1031, 1031, -1292, 1292, -109, 109, 375, -375, -780, 780, -1239,
1239, 1645, -1645, 1063, -1063, 319, -319, -556, 556, 757, -757, -1230, 1230, 561, -561,
-863, 863, -735, 735, -525, 525, 1092, -1092, 403, -403, 1026, -1026, 1143, -1143, -1179,
1179, -554, 554, 886, -886, -1607, 1607, 1212, -1212, -1455, 1455, 1029, -1029, -1219,
1219, -394, 394, 885, -885, -1175, 1175,
const ZETAS_MONTGOMERY_DOMAIN: [i32; 128] = [
-1044, -758, -359, -1517, 1493, 1422, 287, 202, -171, 622, 1577, 182, 962, -1202, -1474,
1468, 573, -1325, 264, 383, -829, 1458, -1602, -130, -681, 1017, 732, 608, -1542, 411,
-205, -1571, 1223, 652, -552, 1015, -1293, 1491, -282, -1544, 516, -8, -320, -666, -1618,
-1162, 126, 1469, -853, -90, -271, 830, 107, -1421, -247, -951, -398, 961, -1508, -725,
448, -1065, 677, -1275, -1103, 430, 555, 843, -1251, 871, 1550, 105, 422, 587, 177, -235,
-291, -460, 1574, 1653, -246, 778, 1159, -147, -777, 1483, -602, 1119, -1590, 644, -872,
349, 418, 329, -156, -75, 817, 1097, 603, 610, 1322, -1285, -1465, 384, -1215, -136, 1218,
-1335, -874, 220, -1187, -1659, -1185, -1530, -1278, 794, -1510, -854, -870, 478, -108,
-308, 996, 991, 958, -1460, 1522, 1628,
];

const NTT_LAYERS: [usize; 7] = [2, 4, 8, 16, 32, 64, 128];
Expand All @@ -44,7 +34,9 @@ pub(crate) mod kyber_polynomial_ring_element_mod {
zeta_i += 1;

for j in offset..offset + layer {
let t = fe_mul(re[j + layer], ZETAS[zeta_i]);
let t = montgomery_reduce(
i32::from(re[j + layer]) * ZETAS_MONTGOMERY_DOMAIN[zeta_i],
);
re[j + layer] = re[j] - t;
re[j] += t;
}
Expand All @@ -56,34 +48,48 @@ pub(crate) mod kyber_polynomial_ring_element_mod {
}

pub fn invert_ntt(mut re: KyberPolynomialRingElement) -> KyberPolynomialRingElement {
let inverse_of_2: i16 = -1664;

let mut zeta_i = COEFFICIENTS_IN_RING_ELEMENT / 2;

for layer in NTT_LAYERS {
for offset in (0..(COEFFICIENTS_IN_RING_ELEMENT - layer)).step_by(2 * layer) {
zeta_i -= 1;

for j in offset..offset + layer {
let a_minus_b = re[j + layer] - re[j];
re[j] = fe_mul(re[j] + re[j + layer], inverse_of_2);
re[j + layer] = fe_mul(fe_mul(a_minus_b, ZETAS[zeta_i]), inverse_of_2);
let a_minus_b = (re[j + layer] - re[j]) as i32;

// Instead of dividing by 2 here, we just divide by
// 2^7 at once in the end.
re[j] = barrett_reduce(re[j] + re[j + layer]);
re[j + layer] = montgomery_reduce(a_minus_b * ZETAS_MONTGOMERY_DOMAIN[zeta_i]);
}
}
}
re.coefficients = re.coefficients.map(barrett_reduce);

// We first convert (2^7)^-1 = (128)^-1 into the montgomery domain by
// computing (2^7)^-1 * 2^16 mod 3329. This is equal to 512 = 2^9, and
// so we can just left-shift by 9 bits.
re.coefficients = re
.coefficients
.map(|coefficient| montgomery_reduce((coefficient as i32) << 9));

re
}

fn ntt_multiply_binomials(
(a0, a1): (KyberFieldElement, KyberFieldElement),
(b0, b1): (KyberFieldElement, KyberFieldElement),
zeta: i16,
zeta: i32,
) -> (KyberFieldElement, KyberFieldElement) {
let a0 = a0 as i32;
let a1 = a1 as i32;

let b0 = b0 as i32;
let b1 = b1 as i32;

(
fe_mul(a0, b0) + fe_mul(fe_mul(a1, b1), zeta),
fe_mul(a0, b1) + fe_mul(a1, b0),
montgomery_reduce(a0 * b0)
+ montgomery_reduce((montgomery_reduce(a1 * b1) as i32) * zeta),
montgomery_reduce(a0 * b1) + montgomery_reduce(a1 * b0),
)
}

Expand All @@ -97,25 +103,44 @@ pub(crate) mod kyber_polynomial_ring_element_mod {
let product = ntt_multiply_binomials(
(left[i], left[i + 1]),
(right[i], right[i + 1]),
MOD_ROOTS[i / 2],
ZETAS_MONTGOMERY_DOMAIN[64 + (i / 4)],
);
out[i] = product.0;
out[i + 1] = product.1;

let product = ntt_multiply_binomials(
(left[i + 2], left[i + 3]),
(right[i + 2], right[i + 3]),
MOD_ROOTS[(i + 2) / 2],
-ZETAS_MONTGOMERY_DOMAIN[64 + (i / 4)],
);
out[i + 2] = product.0;
out[i + 3] = product.1;
}
out.coefficients = out.coefficients.map(barrett_reduce);

out
}
}

pub(crate) fn multiply_row_by_column(
row_vector: &[KyberPolynomialRingElement; RANK],
column_vector: &[KyberPolynomialRingElement; RANK],
) -> KyberPolynomialRingElement {
let mut result = KyberPolynomialRingElement::ZERO;

for (row_element, column_element) in row_vector.iter().zip(column_vector.iter()) {
result = result + ntt_multiply(row_element, column_element);
}

// The coefficients of the form aR^{-1} mod q, which means
// calling to_montgomery_domain() on them should return a mod q.
result.coefficients = result
.coefficients
.map(|coefficient| to_montgomery_domain(coefficient as i32))
.map(barrett_reduce);

result
}

pub(crate) fn multiply_matrix_by_column(
matrix: &[[KyberPolynomialRingElement; RANK]; RANK],
vector: &[KyberPolynomialRingElement; RANK],
Expand All @@ -127,23 +152,14 @@ pub(crate) fn multiply_matrix_by_column(
let product = ntt_multiply(matrix_element, &vector[j]);
result[i] = result[i] + product;
}
result[i].coefficients = result[i].coefficients.map(barrett_reduce);
}

result
}

pub(crate) fn multiply_row_by_column(
row_vector: &[KyberPolynomialRingElement; RANK],
column_vector: &[KyberPolynomialRingElement; RANK],
) -> KyberPolynomialRingElement {
let mut result = KyberPolynomialRingElement::ZERO;

for (row_element, column_element) in row_vector.iter().zip(column_vector.iter()) {
result = result + ntt_multiply(row_element, column_element);
// The coefficients of the form aR^{-1} mod q, which means
// calling to_montgomery_domain() on them should return a mod q.
result[i].coefficients = result[i]
.coefficients
.map(|coefficient| to_montgomery_domain(coefficient as i32))
.map(barrett_reduce);
}

result.coefficients = result.coefficients.map(barrett_reduce);

result
}
6 changes: 6 additions & 0 deletions src/kem/kyber768/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ use crate::kem::kyber768::{
///
/// Otherwise `deserialize_little_endian` is not injective and therefore has
/// no left inverse.
///
/// N.B.: All the `serialize_little_endian_{n}` functions work on the canonical
/// unsigned representative of each coefficient in the polynomial ring.
/// Only `serialize_little_endian_12` actually performs this conversion in the
/// function itself; the rest don't since they are called only after `compress_q`
/// is called, and `compress_q` also performs this conversion.

pub fn serialize_little_endian_1(
re: KyberPolynomialRingElement,
Expand Down