BigNum provides a Swift wrapper for the BoringSSL BIGNUM library.
It provides most of the standard library functions
Basic arithmetic operators (with and without modulus)
Bitwise operators
Powers (with and without modulus)
Greatest common denominator
Prime generation
Random number generation
Examples
Factorial
Below is a function that creates factorial 1000 and then verifies that for every number from 1 to 1000 the greatest common denominator between the variable factorial and that number is equal to that number.
var factorial = BigNum(1)
for i in 1..<1000 {
factorial = factorial * BigNum(i)
}
for i in 1..<1000 {
assert(BigNum.gcd(i, factorial) == i)
}
Another standard operation that BigNum can be used for is generating Secure Remote Password keys. Assuming we have the following
Safe prime N
Generator value g (very commonly 2)
Random number a
A hashing function H
username and password
A value A is calculated and sent to the server
A = g.power(a, modulus: N)
The server responds with a large value B and a salt value. Then the client generates the password authentication key
// calculate u = H(A,B)
let u = BigNum(data: H(A.data, B.data))
// calculate x = H(salt , H(userId | ":" | password))
let message = Data("\(username):\(password)".utf8)
let x = BigNum(data: H(salt, H(message)))
// calculate k = H(N,g)
let k = BigNum(data: H(N.data, g.data))
// calculate S
let S = (B - k * g.power(x, modulus: N)).power(a + u * x, modulus: N)
A hashed version of S can be sent back to the server and the server can use that to verify the correct password was provided.
Compatibility
BigNum uses a vendored cutdown version of BoringSSL (Google’s version of OpenSSL) so doesn’t require a separate OpenSSL library. This means it can be run on iOS and on macOS and Linux platforms without requiring a separate library to be installed.
BigNum
BigNum provides a Swift wrapper for the BoringSSL BIGNUM library.
It provides most of the standard library functions
Examples
Factorial
Below is a function that creates factorial 1000 and then verifies that for every number from 1 to 1000 the greatest common denominator between the variable
factorial
and that number is equal to that number.fyi factorial 1000 is quite a big number
Secure Remote Password
Another standard operation that BigNum can be used for is generating Secure Remote Password keys. Assuming we have the following
N
g
(very commonly 2)a
H
A value
A
is calculated and sent to the serverThe server responds with a large value
B
and asalt
value. Then the client generates the password authentication keyA hashed version of S can be sent back to the server and the server can use that to verify the correct password was provided.
Compatibility
BigNum uses a vendored cutdown version of BoringSSL (Google’s version of OpenSSL) so doesn’t require a separate OpenSSL library. This means it can be run on iOS and on macOS and Linux platforms without requiring a separate library to be installed.