Skip to content

Commit

Permalink
add fix and UT (#7)
Browse files Browse the repository at this point in the history
* add fix and UT

* changed func signature
  • Loading branch information
rajkumarGosavi committed Oct 16, 2023
1 parent 4987c3a commit 8537a3d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
33 changes: 33 additions & 0 deletions int256.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (z *Int) SetString(s string) (*Int, error) {
// }

func (z *Int) Add(x, y *Int) *Int {
z.initiateAbs()

neg := x.neg

if x.neg == y.neg {
Expand All @@ -134,6 +136,8 @@ func (z *Int) Add(x, y *Int) *Int {

// Sub sets z to the difference x-y and returns z.
func (z *Int) Sub(x, y *Int) *Int {
z.initiateAbs()

neg := x.neg
if x.neg != y.neg {
// x - (-y) == x + y
Expand All @@ -155,6 +159,8 @@ func (z *Int) Sub(x, y *Int) *Int {

// Mul sets z to the product x*y and returns z.
func (z *Int) Mul(x, y *Int) *Int {
z.initiateAbs()

z.abs = z.abs.Mul(x.abs, y.abs)
z.neg = x.neg != y.neg // 0 has no sign
return z
Expand All @@ -163,6 +169,8 @@ func (z *Int) Mul(x, y *Int) *Int {
// Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z.
// It panics if x is negative.
func (z *Int) Sqrt(x *Int) *Int {
z.initiateAbs()

if x.neg {
panic("square root of negative number")
}
Expand All @@ -173,6 +181,8 @@ func (z *Int) Sqrt(x *Int) *Int {

// Rsh sets z = x >> n and returns z.
func (z *Int) Rsh(x *Int, n uint) *Int {
z.initiateAbs()

if !x.neg {
z.abs.Rsh(x.abs, n)
z.neg = x.neg
Expand All @@ -187,6 +197,8 @@ func (z *Int) Rsh(x *Int, n uint) *Int {
// If y == 0, a division-by-zero run-time panic occurs.
// Quo implements truncated division (like Go); see QuoRem for more details.
func (z *Int) Quo(x, y *Int) *Int {
z.initiateAbs()

z.abs = z.abs.Div(x.abs, y.abs)
z.neg = len(z.abs) > 0 && x.neg != y.neg // 0 has no sign
return z
Expand All @@ -196,6 +208,8 @@ func (z *Int) Quo(x, y *Int) *Int {
// If y == 0, a division-by-zero run-time panic occurs.
// Rem implements truncated modulus (like Go); see QuoRem for more details.
func (z *Int) Rem(x, y *Int) *Int {
z.initiateAbs()

z.abs.Mod(x.abs, y.abs)
z.neg = len(z.abs) > 0 && x.neg // 0 has no sign
return z
Expand All @@ -207,6 +221,8 @@ func (z *Int) Rem(x, y *Int) *Int {
// 0 if x == y
// +1 if x > y
func (z *Int) Cmp(x *Int) (r int) {
z.initiateAbs()

// x cmp y == x cmp y
// x cmp (-y) == x
// (-x) cmp y == y
Expand Down Expand Up @@ -234,6 +250,8 @@ func (z *Int) Cmp(x *Int) (r int) {
// Modular exponentiation of inputs of a particular size is not a
// cryptographically constant-time operation.
func (z *Int) Exp(x, y, m *Int) *Int {
z.initiateAbs()

if x == nil {
panic("x is nil")
}
Expand All @@ -253,6 +271,8 @@ func (z *Int) Exp(x, y, m *Int) *Int {
}

func (z *Int) Div(x, y *Int) *Int {
z.initiateAbs()

z.abs.Div(x.abs, y.abs)
if x.neg == y.neg {
z.neg = false
Expand All @@ -264,6 +284,7 @@ func (z *Int) Div(x, y *Int) *Int {

// Lsh sets z = x << n and returns z.
func (z *Int) Lsh(x *Int, n uint) *Int {
z.initiateAbs()
b := new(big.Int).Lsh(x.abs.ToBig(), n)
z.abs = uint256.MustFromBig(b)
z.neg = x.neg
Expand All @@ -272,6 +293,8 @@ func (z *Int) Lsh(x *Int, n uint) *Int {

// Or sets z = x | y and returns z.
func (z *Int) Or(x, y *Int) *Int {
z.initiateAbs()

if x.neg == y.neg {
if x.neg {
// (-x) | (-y) == ^(x-1) | ^(y-1) == ^((x-1) & (y-1)) == -(((x-1) & (y-1)) + 1)
Expand Down Expand Up @@ -306,6 +329,8 @@ func (z *Int) Or(x, y *Int) *Int {

// And sets z = x & y and returns z.
func (z *Int) And(x, y *Int) *Int {
z.initiateAbs()

if x.neg == y.neg {
if x.neg {
// (-x) & (-y) == ^(x-1) & ^(y-1) == ^((x-1) | (y-1)) == -(((x-1) | (y-1)) + 1)
Expand Down Expand Up @@ -338,3 +363,11 @@ func (z *Int) And(x, y *Int) *Int {
z = MustFromBig(big)
return z
}

// initiateAbs sets default value for `z.abs` value if is nil
func (z *Int) initiateAbs() {
if z.abs == nil {
z.abs = new(uint256.Int)
}

}
15 changes: 15 additions & 0 deletions int256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,21 @@ func TestInt_Mul(t *testing.T) {
}
}

func TestInt_MulPanic(t *testing.T) {
i256 := new(Int)

defer func() {
if r := recover(); r != nil {
t.Error("should not have paniced", r)
}
}()

res := i256.Mul(NewInt(1), NewInt(2))
if res.Cmp(NewInt(2)) != 0 {
t.Errorf("want: 2, got: %v", res)
}
}

func TestInt_Sqrt(t *testing.T) {
type fields struct {
abs *uint256.Int
Expand Down
2 changes: 2 additions & 0 deletions int_64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ func (z *Int) Int64() int64 {
}

func (z *Int) String() string {
z.initiateAbs()

s := z.abs.ToBig().String()
if !z.neg {
return s
Expand Down

0 comments on commit 8537a3d

Please sign in to comment.