Skip to content

Commit

Permalink
add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Sep 26, 2024
1 parent e941cb9 commit 575ee9d
Show file tree
Hide file tree
Showing 16 changed files with 3,311 additions and 1,337 deletions.
472 changes: 472 additions & 0 deletions arithmetic.go

Large diffs are not rendered by default.

326 changes: 326 additions & 0 deletions arithmetic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
package u256

import "testing"

type binOp2Test struct {
x, y, want string
}

func TestAdd(t *testing.T) {
tests := []binOp2Test{
{"0", "1", "1"},
{"1", "0", "1"},
{"1", "1", "2"},
{"1", "3", "4"},
{"10", "10", "20"},
{"18446744073709551615", "18446744073709551615", "36893488147419103230"}, // uint64 overflow
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Add(x, y)

if got.Neq(want) {
t.Errorf("Add(%s, %s) = %v, want %v", tc.x, tc.y, got.ToString(), want.ToString())
}
}
}

func TestSub(t *testing.T) {
tests := []binOp2Test{
{"1", "0", "1"},
{"1", "1", "0"},
{"10", "10", "0"},
{"31337", "1337", "30000"},
{"2", "3", "115792089237316195423570985008687907853269984665640564039457584007913129639935"}, // underflow
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Sub(x, y)

if got.Neq(want) {
t.Errorf("Sub(%s, %s) = %v, want %v", tc.x, tc.y, got.ToString(), want.ToString())
}
}
}

func TestMul(t *testing.T) {
tests := []binOp2Test{
{"1", "0", "0"},
{"1", "1", "1"},
{"10", "10", "100"},
{"18446744073709551615", "2", "36893488147419103230"}, // uint64 overflow
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Mul(x, y)

if got.Neq(want) {
t.Errorf("Mul(%s, %s) = %v, want %v", tc.x, tc.y, got.ToString(), want.ToString())
}
}
}

func TestDiv(t *testing.T) {
tests := []binOp2Test{
{"31337", "3", "10445"},
{"31337", "0", "0"},
{"0", "31337", "0"},
{"1", "1", "1"},
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Div(x, y)

if got.Neq(want) {
t.Errorf("Div(%s, %s) = %v, want %v", tc.x, tc.y, got.ToString(), want.ToString())
}
}
}

func TestMod(t *testing.T) {
tests := []binOp2Test{
{"31337", "3", "2"},
{"31337", "0", "0"},
{"0", "31337", "0"},
{"2", "31337", "2"},
{"1", "1", "0"},
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Mod(x, y)

if got.Neq(want) {
t.Errorf("Mod(%s, %s) = %v, want %v", tc.x, tc.y, got.ToString(), want.ToString())
}
}
}

func TestDivMod(t *testing.T) {
tests := []struct {
x string
y string
wantDiv string
wantMod string
}{
{"1", "1", "1", "0"},
{"10", "10", "1", "0"},
{"100", "10", "10", "0"},
{"31337", "3", "10445", "2"},
{"31337", "0", "0", "0"},
{"0", "31337", "0", "0"},
{"2", "31337", "0", "2"},
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

wantDiv, err := FromDecimal(tc.wantDiv)
if err != nil {
t.Error(err)
continue
}

wantMod, err := FromDecimal(tc.wantMod)
if err != nil {
t.Error(err)
continue
}

gotDiv := new(Uint)
gotMod := new(Uint)
gotDiv.DivMod(x, y, gotMod)

for i := range gotDiv.arr {
if gotDiv.arr[i] != wantDiv.arr[i] {
t.Errorf("DivMod(%s, %s) got Div %v, want Div %v", tc.x, tc.y, gotDiv, wantDiv)
break
}
}
for i := range gotMod.arr {
if gotMod.arr[i] != wantMod.arr[i] {
t.Errorf("DivMod(%s, %s) got Mod %v, want Mod %v", tc.x, tc.y, gotMod, wantMod)
break
}
}
}
}

func TestNeg(t *testing.T) {
tests := []struct {
x string
want string
}{
{"31337", "115792089237316195423570985008687907853269984665640564039457584007913129608599"},
{"115792089237316195423570985008687907853269984665640564039457584007913129608599", "31337"},
{"0", "0"},
{"2", "115792089237316195423570985008687907853269984665640564039457584007913129639934"},
{"1", "115792089237316195423570985008687907853269984665640564039457584007913129639935"},
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Neg(x)

if got.Neq(want) {
t.Errorf("Neg(%s) = %v, want %v", tc.x, got.ToString(), want.ToString())
}
}
}

func TestExp(t *testing.T) {
tests := []binOp2Test{
{"31337", "3", "30773171189753"},
{"31337", "0", "1"},
{"0", "31337", "0"},
{"1", "1", "1"},
{"2", "3", "8"},
{"2", "64", "18446744073709551616"},
{"2", "128", "340282366920938463463374607431768211456"},
{"2", "255", "57896044618658097711785492504343953926634992332820282019728792003956564819968"},
{"2", "256", "0"}, // overflow
}

for _, tc := range tests {
x, err := FromDecimal(tc.x)
if err != nil {
t.Error(err)
continue
}

y, err := FromDecimal(tc.y)
if err != nil {
t.Error(err)
continue
}

want, err := FromDecimal(tc.want)
if err != nil {
t.Error(err)
continue
}

got := &Uint{}
got.Exp(x, y)

if got.Neq(want) {
t.Errorf("Exp(%s, %s) = %v, want %v", tc.x, tc.y, got.ToString(), want.ToString())
}
}
}
12 changes: 5 additions & 7 deletions bits.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -16,7 +14,7 @@
// architecture and the Go release.
package u256

const uintSize = 32 << (^uint(0) >> 63) // 32 or 64
// const uintSize = 32 << (^uint(0) >> 63) // 32 or 64

// UintSize is the size of a uint in bits.
const UintSize = uintSize
Expand Down Expand Up @@ -506,7 +504,7 @@ func Div(hi, lo, y uint) (quo, rem uint) {
// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
func Div32(hi, lo, y uint32) (quo, rem uint32) {
if y != 0 && y <= hi {
panic(overflowError)
panic(errOverflow)
}
z := uint64(hi)<<32 | uint64(lo)
quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y))
Expand All @@ -519,10 +517,10 @@ func Div32(hi, lo, y uint32) (quo, rem uint32) {
// Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
func Div64(hi, lo, y uint64) (quo, rem uint64) {
if y == 0 {
panic(divideError)
panic(errDivide)
}
if y <= hi {
panic(overflowError)
panic(errOverflow)
}

// If high part is zero, we can directly return the results.
Expand Down Expand Up @@ -598,4 +596,4 @@ func Rem64(hi, lo, y uint64) uint64 {
// hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y)
_, rem := Div64(hi%y, lo, y)
return rem
}
}
7 changes: 2 additions & 5 deletions bits_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ import (
"errors"
)

//go:linkname overflowError runtime.overflowError
var overflowError error = errors.New("u256: integer overflow")

//go:linkname divideError runtime.divideError
var divideError error = errors.New("u256: integer divide by zero")
var errOverflow error = errors.New("u256: integer overflow")
var errDivide error = errors.New("u256: integer divide by zero")
2 changes: 1 addition & 1 deletion bits_table.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 575ee9d

Please sign in to comment.