Skip to content

Commit

Permalink
uint256: minor improvement for Mul (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronChen0 committed May 15, 2024
1 parent 5ecf78c commit 66a4528
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions uint256.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,25 +367,22 @@ func umul(x, y *Int, res *[8]uint64) {
// Mul sets z to the product x*y
func (z *Int) Mul(x, y *Int) *Int {
var (
carry uint64
res0, res1, res2, res3 uint64
carry0, carry1, carry2 uint64
res1, res2 uint64
x0, x1, x2, x3 = x[0], x[1], x[2], x[3]
y0, y1, y2, y3 = y[0], y[1], y[2], y[3]
)

carry, res0 = bits.Mul64(x[0], y[0])
carry, res1 = umulHop(carry, x[1], y[0])
carry, res2 = umulHop(carry, x[2], y[0])
res3 = x[3]*y[0] + carry
carry0, z[0] = bits.Mul64(x0, y0)
carry0, res1 = umulHop(carry0, x1, y0)
carry0, res2 = umulHop(carry0, x2, y0)

carry, res1 = umulHop(res1, x[0], y[1])
carry, res2 = umulStep(res2, x[1], y[1], carry)
res3 = res3 + x[2]*y[1] + carry

carry, res2 = umulHop(res2, x[0], y[2])
res3 = res3 + x[1]*y[2] + carry
carry1, z[1] = umulHop(res1, x0, y1)
carry1, res2 = umulStep(res2, x1, y1, carry1)

res3 = res3 + x[0]*y[3]
carry2, z[2] = umulHop(res2, x0, y2)

z[0], z[1], z[2], z[3] = res0, res1, res2, res3
z[3] = x3*y0 + x2*y1 + x0*y3 + x1*y2 + carry0 + carry1 + carry2
return z
}

Expand Down

0 comments on commit 66a4528

Please sign in to comment.