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

Add DivMod() #113

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions uint256.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,20 @@ func (z *Int) Mod(x, y *Int) *Int {
}

var quot Int
rem := udivrem(quot[:], x[:], y)
return z.Set(&rem)
*z = udivrem(quot[:], x[:], y)
return z
}

// DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0.
// If y == 0, both z and m are set to 0 (OBS: differs from the big.Int)
func (z *Int) DivMod(x, y, m *Int) (*Int, *Int) {
if y.IsZero() {
return z.Clear(), m.Clear()
}
var quot Int
*m = udivrem(quot[:], x[:], y)
*z = quot
return z, m
}

// SMod interprets x and y as two's complement signed integers,
Expand Down
17 changes: 17 additions & 0 deletions uint256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ func TestRandomBinOp(t *testing.T) {
t.Run("Mod", func(t *testing.T) { testRandomOp(t, (*Int).Mod, bigMod) })
t.Run("SDiv", func(t *testing.T) { testRandomOp(t, (*Int).SDiv, bigSDiv) })
t.Run("SMod", func(t *testing.T) { testRandomOp(t, (*Int).SMod, bigSMod) })
t.Run("DivMod/Div", func(t *testing.T) { testRandomOp(t, divModDiv, bigDiv) })
t.Run("DivMod/Mod", func(t *testing.T) { testRandomOp(t, divModMod, bigMod) })
t.Run("udivrem/Div", func(t *testing.T) { testRandomOp(t, udivremDiv, bigDiv) })
t.Run("udivrem/Mod", func(t *testing.T) { testRandomOp(t, udivremMod, bigMod) })
}
Expand Down Expand Up @@ -298,6 +300,19 @@ func TestRandomSquare(t *testing.T) {
)
}

// divModDiv wraps DivMod and returns quotient only
func divModDiv(z, x, y *Int) *Int {
var m Int
z.DivMod(x, y, &m)
return z
}

// divModMod wraps DivMod and returns modulus only
func divModMod(z, x, y *Int) *Int {
new(Int).DivMod(x, y, z)
return z
}

// udivremDiv wraps udivrem and returns quotient
func udivremDiv(z, x, y *Int) *Int {
var quot Int
Expand Down Expand Up @@ -1243,6 +1258,8 @@ func TestBinOp(t *testing.T) {
t.Run("Mod", func(t *testing.T) { proc(t, (*Int).Mod, bigMod) })
t.Run("SDiv", func(t *testing.T) { proc(t, (*Int).SDiv, bigSDiv) })
t.Run("SMod", func(t *testing.T) { proc(t, (*Int).SMod, bigSMod) })
t.Run("DivMod/Div", func(t *testing.T) { proc(t, divModDiv, bigDiv) })
t.Run("DivMod/Mod", func(t *testing.T) { proc(t, divModMod, bigMod) })
t.Run("udivrem/Div", func(t *testing.T) { proc(t, udivremDiv, bigDiv) })
t.Run("udivrem/Mod", func(t *testing.T) { proc(t, udivremMod, bigMod) })
t.Run("Exp", func(t *testing.T) { proc(t, (*Int).Exp, bigExp) })
Expand Down