I'm implementing some fast finite field arithmetics and found big number types in Accelerate framework. Unfortunately multiplication behavior is strange and Acceleration framework is not too well documented.
Here is some code
public typealias U128 = vU128
public func add(_ a: U128) -> U128 {
return U128(v: vU128Add(self.v, a.v))
}
public func mul(_ a: U128) -> U256 {
var result = U256(v: (BigNumber.vZERO, BigNumber.vZERO)) // just empty init
var aCopy = a
var selfCopy = self
withUnsafePointer(to: &selfCopy) { (selfPtr: UnsafePointer<vU128>) -> Void in
withUnsafePointer(to: &aCopy, { (aPtr: UnsafePointer<vU128>) -> Void in
withUnsafeMutablePointer(to: &result, { (resultPtr: UnsafeMutablePointer<vU256>) -> Void in
vU128FullMultiply(selfPtr, aPtr, resultPtr)
})
})
}
return result
}
Addition of two numbers works well ( 1 + 2 == 3), although fullWidth multiplication results in 1 byte left shifted result ( 1 * 2 = 512 ! )