diff --git a/include/bit_packing.hpp b/include/bit_packing.hpp index 9f61a0c..4d62abd 100644 --- a/include/bit_packing.hpp +++ b/include/bit_packing.hpp @@ -15,7 +15,7 @@ namespace bit_packing { // // See section 5.2 ( which describes bit packing ) of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr void encode(std::span poly, std::span arr) requires(dilithium_params::check_sbw(sbw)) @@ -184,7 +184,7 @@ encode(std::span poly, std::span +template static inline constexpr void decode(std::span arr, std::span poly) requires(dilithium_params::check_sbw(sbw)) @@ -331,7 +331,7 @@ decode(std::span arr, std::span +template static inline constexpr void encode_hint_bits(std::span h, std::span arr) { @@ -361,7 +361,7 @@ encode_hint_bits(std::span h, std::span +template static inline constexpr bool decode_hint_bits(std::span arr, std::span h) { diff --git a/include/dilithium.hpp b/include/dilithium.hpp index 82d903e..d65bd00 100644 --- a/include/dilithium.hpp +++ b/include/dilithium.hpp @@ -21,7 +21,7 @@ namespace dilithium { // Note, ebw = ceil(log2(2 * η + 1)) // // See section 5.4 of specification for public key and secret key byte length. -template +template static inline void keygen(std::span seed, std::span()> pubkey, @@ -133,16 +133,16 @@ keygen(std::span seed, // // See section 5.4 of specification for understanding how signature is byte // serialized. -template +template static inline void sign(std::span()> seckey, std::span msg, @@ -323,14 +323,7 @@ sign(std::span()> secke // // Verification algorithm is described in figure 4 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline bool verify(std::span()> pubkey, std::span msg, diff --git a/include/ntt.hpp b/include/ntt.hpp index d021cab..e53d0f2 100644 --- a/include/ntt.hpp +++ b/include/ntt.hpp @@ -25,7 +25,7 @@ constexpr auto INV_N = field::zq_t(N).inv(); // See // https://github.com/itzmeanjan/kyber/blob/3cd41a5/include/ntt.hpp#L74-L93 // for source of inspiration. -template +template static inline constexpr size_t bit_rev(const size_t v) requires(mbw == LOG2N) diff --git a/include/poly.hpp b/include/poly.hpp index 42dcbf5..2eba84f 100644 --- a/include/poly.hpp +++ b/include/poly.hpp @@ -11,7 +11,7 @@ namespace poly { // Given a degree-255 polynomial over Z_q | q = 2^23 - 2^13 + 1, this routine // attempts to extract out high and low order bits from each of 256 coefficients -template +template static inline constexpr void power2round(std::span poly, std::span poly_hi, @@ -40,7 +40,7 @@ mul(std::span polya, // Given a degree-255 polynomial, which has all of its coefficients in [-x, x], // this routine subtracts each coefficient from x, so that they stay in [0, 2x]. -template +template static inline constexpr void sub_from_x(std::span poly) { @@ -53,7 +53,7 @@ sub_from_x(std::span poly) // Given a degree-255 polynomial, this routine extracts out high order bits ( // using decompose routine ), while not mutating source polynomial -template +template static inline constexpr void highbits(std::span src, std::span dst) { @@ -64,7 +64,7 @@ highbits(std::span src, std::span +template static inline constexpr void lowbits(std::span src, std::span dst) { @@ -95,7 +95,7 @@ infinity_norm(std::span poly) // Given two degree-255 polynomials, this routine computes hint bit for each // coefficient, using `make_hint` routine. -template +template static inline constexpr void make_hint(std::span polya, std::span polyb, @@ -110,7 +110,7 @@ make_hint(std::span polya, // polynomial r with arbitrary coefficients ∈ Z_q, this routine recovers high // order bits of r + z s.t. hint bit was computed using `make_hint` routine and // z is another degree-255 polynomial with small coefficients. -template +template static inline constexpr void use_hint(std::span polyh, std::span polyr, @@ -138,7 +138,7 @@ count_1s(std::span poly) // Given a degree-255 polynomial, this routine shifts each coefficient // leftwards, by d bits -template +template static inline constexpr void shl(std::span poly) { diff --git a/include/polyvec.hpp b/include/polyvec.hpp index df76d4a..4739fcd 100644 --- a/include/polyvec.hpp +++ b/include/polyvec.hpp @@ -14,7 +14,7 @@ using const_poly_t = std::span; using poly_t = std::span; // Applies NTT on a vector ( of dimension k x 1 ) of degree-255 polynomials -template +template static inline constexpr void ntt(std::span vec) { @@ -25,7 +25,7 @@ ntt(std::span vec) } // Applies iNTT on a vector ( of dimension k x 1 ) of degree-255 polynomials -template +template static inline constexpr void intt(std::span vec) { @@ -37,7 +37,7 @@ intt(std::span vec) // Compresses vector ( of dimension k x 1 ) of degree-255 polynomials by // extracting out high and low order bits -template +template static inline constexpr void power2round(std::span poly, std::span poly_hi, @@ -55,7 +55,7 @@ power2round(std::span poly, // Given two matrices ( in NTT domain ) of compatible dimension, where each // matrix element is a degree-255 polynomial over Z_q | q = 2^23 -2^13 + 1, this // routine attempts to multiply and compute resulting matrix -template +template static inline constexpr void matrix_multiply(std::span a, std::span b, @@ -86,7 +86,7 @@ matrix_multiply(std::span a, // Given a vector ( of dimension k x 1 ) of degree-255 polynomials, this // routine adds it to another polynomial vector of same dimension s.t. // destination vector is mutated. -template +template static inline constexpr void add_to(std::span src, std::span dst) { @@ -101,7 +101,7 @@ add_to(std::span src, std::span +template static inline constexpr void neg(std::span vec) { @@ -117,7 +117,7 @@ neg(std::span vec) // Given a vector ( of dimension k x 1 ) of degree-255 polynomials s.t. each // coefficient ∈ [-x, x], this routine subtracts each coefficient from x so that // coefficients now stay in [0, 2x]. -template +template static inline constexpr void sub_from_x(std::span vec) { @@ -130,7 +130,7 @@ sub_from_x(std::span vec) // Given a vector ( of dimension k x 1 ) of degree-255 polynomials, this routine // encodes each of those polynomials into 32 x sbw -bytes, writing to a // (k x 32 x sbw) -bytes destination array. -template +template static inline constexpr void encode(std::span src, std::span dst) { @@ -149,7 +149,7 @@ encode(std::span src, std::span +template static inline constexpr void decode(std::span src, std::span dst) { @@ -167,7 +167,7 @@ decode(std::span src, std::span +template static inline constexpr void highbits(std::span src, std::span dst) { @@ -179,7 +179,7 @@ highbits(std::span src, std::span +template static inline constexpr void lowbits(std::span src, std::span dst) { @@ -193,7 +193,7 @@ lowbits(std::span src, std::span +template static inline constexpr void mul_by_poly(std::span poly, std::span src_vec, @@ -210,7 +210,7 @@ mul_by_poly(std::span poly, // // See point `Sizes of elements` in section 2.1 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr field::zq_t infinity_norm(std::span vec) { @@ -226,7 +226,7 @@ infinity_norm(std::span vec) // Given two vector ( of dimension k x 1 ) of degree-255 polynomials, this // routine computes hint bit for each coefficient, using `make_hint` routine. -template +template static inline constexpr void make_hint(std::span polya, std::span polyb, @@ -243,7 +243,7 @@ make_hint(std::span polya, // Recovers high order bits of a vector of degree-255 polynomials ( i.e. r + z // ) s.t. hint bits ( say h ) and another polynomial vector ( say r ) are // provided. -template +template static inline constexpr void use_hint(std::span polyh, std::span polyr, @@ -259,7 +259,7 @@ use_hint(std::span polyh, // Given a vector ( of dimension k x 1 ) of degree-255 polynomials, this routine // counts number of coefficients having value 1. -template +template static inline constexpr size_t count_1s(std::span vec) { @@ -275,7 +275,7 @@ count_1s(std::span vec) // Given a vector ( of dimension k x 1 ) of degree-255 polynomials, this routine // shifts each coefficient leftwards by d bits -template +template static inline constexpr void shl(std::span vec) { diff --git a/include/reduction.hpp b/include/reduction.hpp index fb1014c..790920c 100644 --- a/include/reduction.hpp +++ b/include/reduction.hpp @@ -19,7 +19,7 @@ namespace reduction { // // This implementation collects some ideas from // https://github.com/pq-crystals/dilithium/blob/3e9b9f1/ref/rounding.c#L5-L23 -template +template static inline constexpr std::pair power2round(const field::zq_t r) requires(dilithium_params::check_d(d)) @@ -45,7 +45,7 @@ power2round(const field::zq_t r) // // See definition of this routine in figure 3 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr std::pair decompose(const field::zq_t r) requires(dilithium_params::check_γ2(alpha / 2)) @@ -74,7 +74,7 @@ decompose(const field::zq_t r) // // See definition of this routine in figure 3 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr field::zq_t highbits(const field::zq_t r) { @@ -87,7 +87,7 @@ highbits(const field::zq_t r) // // See definition of this routine in figure 3 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr field::zq_t lowbits(const field::zq_t r) { @@ -103,7 +103,7 @@ lowbits(const field::zq_t r) // // See definition of this routine in figure 3 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr field::zq_t make_hint(const field::zq_t z, const field::zq_t r) { @@ -118,7 +118,7 @@ make_hint(const field::zq_t z, const field::zq_t r) // // See definition of this routine in figure 3 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr field::zq_t use_hint(const field::zq_t h, const field::zq_t r) { diff --git a/include/sampling.hpp b/include/sampling.hpp index c76706b..a50e8e6 100644 --- a/include/sampling.hpp +++ b/include/sampling.hpp @@ -15,15 +15,14 @@ namespace sampling { using poly_t = std::span; -// Given a 32 -bytes uniform seed ρ, a k x l matrix is deterministically -// sampled, where each coefficient is a degree-255 polynomial ∈ R_q -// | q = 2^23 - 2^13 + 1 +// Given a 32 -bytes uniform seed ρ, a k x l matrix is deterministically sampled ( using the method of rejection +// sampling ), where each coefficient is a degree-255 polynomial ∈ R_q | q = 2^23 - 2^13 + 1 // // Shake128 Xof is used for expanding 32 -bytes seed to matrix over R_q^(k x l). // // See `Expanding the Matrix A` point in section 5.3 of Dilithium specification, // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr void expand_a(std::span rho, std::span mat) { @@ -67,7 +66,7 @@ expand_a(std::span rho, std::span rho, std::span +// specification https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf +template static inline constexpr void expand_s(std::span rho_prime, std::span vec) requires(dilithium_params::check_η(η) && dilithium_params::check_nonce(nonce)) @@ -152,7 +150,7 @@ expand_s(std::span rho_prime, std::span +template static inline constexpr void expand_mask(std::span seed, const uint16_t nonce, std::span vec) requires(dilithium_params::check_γ1(γ1)) @@ -190,7 +188,7 @@ expand_mask(std::span seed, const uint16_t nonce, std::span +template static inline constexpr void sample_in_ball(std::span seed, std::span poly) requires(dilithium_params::check_τ(τ)) diff --git a/include/utils.hpp b/include/utils.hpp index 01edd58..ae6e3be 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -15,7 +15,7 @@ namespace dilithium_utils { // // See table 2 and section 5.4 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr size_t pub_key_len() requires(dilithium_params::check_d(d)) @@ -30,7 +30,7 @@ pub_key_len() // // See table 2 and section 5.4 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr size_t sec_key_len() requires(dilithium_params::check_d(d)) @@ -45,7 +45,7 @@ sec_key_len() // // See table 2 and section 5.4 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static inline constexpr size_t sig_len() { diff --git a/tests/test_bit_packing.cpp b/tests/test_bit_packing.cpp index 55d9a23..e71e60c 100644 --- a/tests/test_bit_packing.cpp +++ b/tests/test_bit_packing.cpp @@ -8,7 +8,7 @@ // // - polynomial to byte array encoding // - decoding of polynomial from byte array -template +template void test_encode_decode() requires(dilithium_params::check_sbw(sbw)) @@ -56,7 +56,7 @@ TEST(Dilithium, PolynomialEncodingDecoding) // Generates random hint bit polynomial vector of dimension k x 1, with <= ω // coefficients set to 1. -template +template void generate_random_hint_bits(std::span poly) { @@ -77,7 +77,7 @@ generate_random_hint_bits(std::span poly) // Test functional correctness of encoding and decoding of hint bit polynomial // vector. -template +template void test_encode_decode_hint_bits() { diff --git a/tests/test_ntt.cpp b/tests/test_ntt.cpp index 852926d..a730dbf 100644 --- a/tests/test_ntt.cpp +++ b/tests/test_ntt.cpp @@ -29,11 +29,5 @@ TEST(Dilithium, NumberTheoreticTransform) ntt::ntt(_poly_b); ntt::intt(_poly_b); - bool flg = true; - - for (size_t i = 0; i < ntt::N; i++) { - flg &= (_poly_a[i] == _poly_b[i]); - } - - EXPECT_TRUE(flg); + EXPECT_EQ(poly_a, poly_b); } diff --git a/tests/test_reduction.cpp b/tests/test_reduction.cpp index 7ff9125..d3a41c1 100644 --- a/tests/test_reduction.cpp +++ b/tests/test_reduction.cpp @@ -30,7 +30,7 @@ TEST(Dilithium, Power2Round) // // Read section 2.4 of Dilithium specification // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -template +template static void test_decompose() { diff --git a/tests/test_sampling.cpp b/tests/test_sampling.cpp index 3442eab..73e820f 100644 --- a/tests/test_sampling.cpp +++ b/tests/test_sampling.cpp @@ -4,7 +4,7 @@ #include // Check whether hashing to a ball routine works as expected or not. -template +template static void test_sample_in_ball() {