I developed a Golang package BESON for doing operation of big number.
The Multiply operation code:
func Multiply(a []byte, b []byte) {
ans := make([]byte, len(a) + len(b))
bits := nbits(b)
var i uint
for i = bits - 1; int(i) >= 0; i-- {
byteNum := i >> 3
bitNum := i & 7
LeftShift(ans, 1, 0)
if (b[byteNum] & (1 << bitNum)) > 0 {
Add(ans, a)
}
}
copy(a, ans)
}
My way is to add every a
multiply bits of b
. Is there more efficient way to implement Multiply
?
Edit
The BESON package represent a big number in byte array. For example, it represent a 128-bit unsigned interger in an byte array of size 16. Therefore, when doing two 128-bit unsigned interger multiplying, it's actually multiplying two byte array.
Example:
a
, b
a = []byte{ 204, 19, 46, 255, 0, 0, 0, 0 }
b = []byte{ 117, 10, 68, 47, 0, 0, 0, 0 }
Multiply(a, b)
fmt.Println(a)
a
(The result will write back to a
)[60 4 5 35 76 72 29 47]