BigNumber
A Swift library for representing huge (256-bit) numbers!
This library is not suitable for use in cryptography; all the operations and functions are pass-by-value, rather than reference. I am currently working on making this library also have "secure" functions that only work with pointers.
How it works
This library includes the class BigNumber, which is essentially just a 256-bit unsigned integer.
Because typing 'BigNumber' might get to be a bit too much, it is typealiased as 'BN' and 'UInt256' for your convenience :)
A BigNumber (which will now be refered to as just BN or UInt256) is an array of UInt64s of size 4.
There are public constants in the BigNumber.swift file that detail this information:
/// The size of a big number
public let BN_SIZE = 4
/// Size of BN in Bytes
public let BN_SIZE_IN_BYTES = MemoryLayout<UInt64>.size * BN_SIZE
/// Size of a BN String
public let BN_STR_SIZE = (2 * BN_SIZE_IN_BYTES + 1)
A BN uses the Little Endian format for representing large numbers. To access the specific elements of the array, use the array property:
let bn = BN(hexString: "2a")
let firstArrayItem = bn.array[0] // 42
You can also subscript a BN directly
let firstArrayItem = bn[0]