|
| 1 | +// |
| 2 | +// MRPrimality.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Sahn Cha on 2016. 10. 18.. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +enum MillerRabinError: Error { |
| 12 | + case primeLowAccuracy |
| 13 | + case primeLowerBorder |
| 14 | + case uIntOverflow |
| 15 | +} |
| 16 | + |
| 17 | +/* |
| 18 | + The Miller–Rabin test relies on an equality or set of equalities that |
| 19 | + hold true for prime values, then checks whether or not they hold for |
| 20 | + a number that we want to test for primality. |
| 21 | + |
| 22 | + - Parameter n: an odd integer to be tested for primality; |
| 23 | + - Parameter k: a parameter that determines the accuracy of the test |
| 24 | + - throws: Can throw an error of type `MillerRabinError`. |
| 25 | + - Returns: composite if n is composite, otherwise probably prime |
| 26 | + */ |
| 27 | +public func checkWithMillerRabin(_ n: UInt, accuracy k: UInt = 1) throws -> Bool { |
| 28 | + guard k > 0 else { throw MillerRabinError.primeLowAccuracy } |
| 29 | + guard n > 0 else { throw MillerRabinError.primeLowerBorder } |
| 30 | + guard n > 3 else { return true } |
| 31 | + |
| 32 | + // return false for all even numbers bigger than 2 |
| 33 | + if n % 2 == 0 { |
| 34 | + return false |
| 35 | + } |
| 36 | + |
| 37 | + let s: UInt = UInt((n - 1).trailingZeroBitCount) |
| 38 | + let d: UInt = (n - 1) >> s |
| 39 | + |
| 40 | + guard UInt(pow(2.0, Double(s))) * d == n - 1 else { throw MillerRabinError.primeLowerBorder } |
| 41 | + |
| 42 | + /// Inspect whether a given witness will reveal the true identity of n. |
| 43 | + func tryComposite(_ a: UInt, d: UInt, n: UInt) throws -> Bool? { |
| 44 | + var x = try calculateModularExponentiation(base: a, exponent: d, modulus: n) |
| 45 | + if x == 1 || x == (n - 1) { |
| 46 | + return nil |
| 47 | + } |
| 48 | + for _ in 1..<s { |
| 49 | + x = try calculateModularExponentiation(base: x, exponent: 2, modulus: n) |
| 50 | + if x == 1 { |
| 51 | + return false |
| 52 | + } else if x == (n - 1) { |
| 53 | + return nil |
| 54 | + } |
| 55 | + } |
| 56 | + return false |
| 57 | + } |
| 58 | + |
| 59 | + for _ in 0..<k { |
| 60 | + let a = UInt.random(in: 2..<n-2) |
| 61 | + if let composite = try tryComposite(a, d: d, n: n) { |
| 62 | + return composite |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return true |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | + Calculates the modular exponentiation based on `Applied Cryptography by Bruce Schneier.` |
| 71 | + in `Schneier, Bruce (1996). Applied Cryptography: Protocols, Algorithms, |
| 72 | + and Source Code in C, Second Edition (2nd ed.). Wiley. ISBN 978-0-471-11709-4.` |
| 73 | + |
| 74 | + - Parameter base: The natural base b. |
| 75 | + - Parameter base: The natural exponent e. |
| 76 | + - Parameter base: The natural modulus m. |
| 77 | + - Throws: Can throw a `uIntOverflow` if the modulus' square exceeds the memory |
| 78 | + limitations of UInt on the current system. |
| 79 | + - Returns: The modular exponentiation c. |
| 80 | + */ |
| 81 | +private func calculateModularExponentiation(base: UInt, exponent: UInt, modulus: UInt) throws -> UInt { |
| 82 | + guard modulus > 1 else { return 0 } |
| 83 | + guard !(modulus-1).multipliedReportingOverflow(by: (modulus-1)).overflow else { |
| 84 | + throw MillerRabinError.uIntOverflow |
| 85 | + } |
| 86 | + |
| 87 | + var result: UInt = 1 |
| 88 | + var exponentCopy = exponent |
| 89 | + var baseCopy = base % modulus |
| 90 | + |
| 91 | + while exponentCopy > 0 { |
| 92 | + if exponentCopy % 2 == 1 { |
| 93 | + result = (result * baseCopy) % modulus |
| 94 | + } |
| 95 | + exponentCopy = exponentCopy >> 1 |
| 96 | + baseCopy = (baseCopy * baseCopy) % modulus |
| 97 | + } |
| 98 | + |
| 99 | + return result |
| 100 | +} |
0 commit comments