Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uint256: minor improvement for Mul #167

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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