/* Written in 2023 by Sebastiano Vigna (vigna@acm.org) To the extent possible under law, the author has dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* This is a minimal library to perform modular arithmetic on multiple-precision natural numbers represented by an array of MP_SIZE uint64_t integers using only 64-bit arithmetic. The only purpose of this code is to make the jump code of (G)MWC generators self-contained. Modular multiplications use Montgomery's method. This code was inspired by the CUDA implementation of these generators in NVIDIA's CCCL, for which Rory Mitchell suggested the method. This code is intentionally not documented to discourage general usage. */ #include #include // Multiplications work on 32-bit limbs, so that 64-bit products suffice #define MP_LIMB(a, i) ((uint32_t)((a)[(i) / 2] >> ((i) % 2 * 32))) static int mp_cmp(const uint64_t * const a, const uint64_t * const b) { int i; for(i = MP_SIZE; i-- != 0; ) { if (a[i] < b[i]) return -1; if (a[i] > b[i]) return 1; } return 0; } // Assumes a >= b static void _mp_sub(uint64_t * const a, const uint64_t * const b) { uint64_t borrow = 0; for(int i = 0; i < MP_SIZE; i++) { // Branchless: compilers turn this into subtractions with borrow const uint64_t d = a[i] - b[i]; const uint64_t borrow1 = a[i] < b[i]; a[i] = d - borrow; borrow = borrow1 | (d < borrow); } } static void mp_rem(uint64_t * const a, const uint64_t * const m) { for(;;) { if (mp_cmp(a, m) < 0) return; _mp_sub(a, m); } } static void mp_add(uint64_t * const a, const uint64_t * const b, const uint64_t * const m) { uint64_t carry = 0; for(int i = 0; i < MP_SIZE; i++) { // Branchless: compilers turn this into additions with carry const uint64_t s = a[i] + b[i]; const uint64_t carry1 = s < a[i]; a[i] = s + carry; carry = carry1 | (a[i] < carry); } if (m) mp_rem(a, m); } static void mp_sub(uint64_t * const a, const uint64_t * const b, const uint64_t * const m) { if (mp_cmp(a, b) >= 0) _mp_sub(a, b); else { uint64_t t[MP_SIZE]; memcpy(t, m, sizeof t); _mp_sub(t, b); mp_add(a, t, m); } } // a = a * b mod 2^(64 * MP_SIZE) static void mp_mul(uint64_t * const a, const uint64_t * const b) { uint32_t t[2 * MP_SIZE]; uint64_t lo = 0, hi = 0; for(int k = 0; k < 2 * MP_SIZE; k++) { // Limb k is the sum of the products a_i * b_(k - i), plus the carry for(int i = 0; i <= k; i++) { const uint64_t p = (uint64_t)MP_LIMB(a, i) * MP_LIMB(b, k - i); lo += (uint32_t)p; hi += p >> 32; } t[k] = (uint32_t)lo; lo = (lo >> 32) + hi; hi = 0; } for(int i = 0; i < MP_SIZE; i++) a[i] = t[2 * i] | (uint64_t)t[2 * i + 1] << 32; } // The following functions implement Montgomery's modular arithmetic for an // odd modulus m such that R / 2 < m < R, where R = 2^(64 * (MP_SIZE - 1)). // A number is in Montgomery form when it is multiplied by R modulo m. #define MP_LIMBS (2 * (MP_SIZE - 1)) // a = a * b / R mod m, for a < R and b < m static void mp_montmul(uint64_t * const a, const uint64_t * const b, const uint64_t * const m) { // -m^-1 mod 2^32 by Newton's method: m[0] is its own inverse modulo 8, // and each iteration doubles the number of correct bits uint32_t minv = (uint32_t)m[0]; for(int i = 0; i < 4; i++) minv *= 2 - (uint32_t)m[0] * minv; minv = 0 - minv; uint32_t t[MP_LIMBS + 1] = { 0 }; for(int i = 0; i < MP_LIMBS; i++) { // t = (t + a_i * b + u * m) / 2^32, where u makes the division exact const uint64_t ai = MP_LIMB(a, i); const uint64_t u = (uint32_t)((t[0] + ai * MP_LIMB(b, 0)) * minv); uint64_t cb = 0, cm = 0; for(int j = 0; j < MP_LIMBS; j++) { cb += t[j] + ai * MP_LIMB(b, j); cm += (uint32_t)cb + u * MP_LIMB(m, j); if (j != 0) t[j - 1] = (uint32_t)cm; cb >>= 32; cm >>= 32; } cb += t[MP_LIMBS] + cm; t[MP_LIMBS - 1] = (uint32_t)cb; t[MP_LIMBS] = (uint32_t)(cb >> 32); } // Now t < 2m for(int i = 0; i < MP_SIZE - 1; i++) a[i] = t[2 * i] | (uint64_t)t[2 * i + 1] << 32; a[MP_SIZE - 1] = t[MP_LIMBS]; mp_rem(a, m); } // r = base^e, where base and r are in Montgomery form and // e = exp[0] + exp[1] * 2^64 + ... is the little-endian integer held in the // len words of exp (right-to-left square-and-multiply). Used to jump a // (G)MWC generator by an arbitrary number of steps. static void mp_montpow(uint64_t * const r, const uint64_t * const base, const uint64_t * const exp, const int len, const uint64_t * const m) { uint64_t b[MP_SIZE]; memcpy(b, base, sizeof b); // One in Montgomery form is R mod m = R - m (no carry, as m is odd) for(int i = 0; i < MP_SIZE - 1; i++) r[i] = ~m[i]; r[0]++; r[MP_SIZE - 1] = 0; int bits = len * 64; while (bits != 0 && ((exp[(bits - 1) >> 6] >> ((bits - 1) & 63)) & 1) == 0) bits--; for (int i = 0; i < bits; i++) { if ((exp[i >> 6] >> (i & 63)) & 1) mp_montmul(r, b, m); if (i + 1 < bits) mp_montmul(b, b, m); } }