@@ -327,9 +327,26 @@ public struct ReduceModulus<T: CoreScalarType>: Equatable, Sendable {
327327 /// Performs modular reduction of a product with modulus `p`.
328328 /// - Parameter x: Must be in `[0, p^2)`.
329329 /// - Returns: `x mod p` for `p`.
330+ ///
331+ /// Details:
332+ /// - "Dhem, J. F. "Modified version of the Barrett algorithm." Technical report, Technical report (1994).
333+ /// - See also Algorithm 2 from
334+ /// "Knezevic, Miroslav, Frederik Vercauteren, and Ingrid Verbauwhede.
335+ /// "Speeding up Barrett and Montgomery modular multiplications."
336+ /// IEEE Transactions on Compute 2 (2009).
337+ /// Available at
338+ /// https://web.archive.org/web/20170830020846/http://homes.esat.kuleuven.be/~fvercaut/papers/bar_mont.pdf
339+ ///
340+ /// / / | x | \ / | 2^(n+alpha) | \ \
341+ /// | floor | |--------| | * floor | |-------------| | |
342+ /// | \ |2^n+beta| / \ | p | / |
343+ /// x mod p = floor |------------------------------------------------------|
344+ /// \ 2^{alpha - beta} /
345+ /// where `x = (x_{n + gamma - 1, ...., x_0)_2``, and modulus `p = (p_{n - 1}, ..., p_0)_2`, `p_{n-1} != 0`, and
346+ /// `gamma <= n`.
347+ /// For `beta = -2` and `\alpha >= \gamma + 1`, we need just a single conditional subtraction.
330348 @inlinable
331349 public func reduceProduct( _ x: T . DoubleWidth ) -> T {
332- // Algorithm 2 from https://homes.esat.kuleuven.be/~fvercaut/papers/bar_mont.pdf
333350 assert ( x < T . DoubleWidth ( modulus. multipliedFullWidth ( by: modulus) ) )
334351 let n = modulus. significantBitCount
335352 let reduceModulusBeta = - 2
0 commit comments