Move ETH2 Types Into Prysm (#10534)

* move eth2 types into Prysm

* bazel

* lint

* use existing math helpers

* rem eth2-types dep

Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Raul Jordan
2022-04-28 13:57:40 +00:00
committed by GitHub
parent 58ad800553
commit 001f719cc3
18 changed files with 1602 additions and 1 deletions

View File

@@ -15,5 +15,6 @@ go_test(
deps = [
":go_default_library",
"//testing/require:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
],
)

View File

@@ -20,7 +20,13 @@ func init() {
}
// ErrOverflow occurs when an operation exceeds max or minimum values.
var ErrOverflow = errors.New("integer overflow")
var (
ErrOverflow = errors.New("integer overflow")
ErrDivByZero = errors.New("integer divide by zero")
ErrMulOverflow = errors.New("multiplication overflows")
ErrAddOverflow = errors.New("addition overflows")
ErrSubUnderflow = errors.New("subtraction underflow")
)
// Common square root values.
var squareRootTable = map[uint64]uint64{
@@ -115,6 +121,15 @@ func Mul64(a, b uint64) (uint64, error) {
return val, nil
}
// Div64 divides two 64-bit unsigned integers and checks for errors.
func Div64(a, b uint64) (uint64, error) {
if b == 0 {
return 0, ErrDivByZero
}
val, _ := bits.Div64(0, a, b)
return val, nil
}
// Add64 adds 2 64-bit unsigned integers and checks if they
// lead to an overflow. If they do not, it returns the result
// without an error.
@@ -135,6 +150,15 @@ func Sub64(a, b uint64) (uint64, error) {
return res, nil
}
// Mod64 finds remainder of division of two 64-bit unsigned integers and checks for errors.
func Mod64(a, b uint64) (uint64, error) {
if b == 0 {
return 0, ErrDivByZero
}
_, val := bits.Div64(0, a, b)
return val, nil
}
// Int returns the integer value of the uint64 argument. If there is an overlow, then an error is
// returned.
func Int(u uint64) (int, error) {

View File

@@ -5,6 +5,7 @@ import (
stdmath "math"
"testing"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/math"
"github.com/prysmaticlabs/prysm/testing/require"
)
@@ -85,6 +86,74 @@ func TestIntegerSquareRoot(t *testing.T) {
}
}
func TestMath_Div64(t *testing.T) {
type args struct {
a uint64
b uint64
}
tests := []struct {
args args
res uint64
err bool
}{
{args: args{0, 1}, res: 0, err: false},
{args: args{0, 1}, res: 0},
{args: args{1, 0}, res: 0, err: true},
{args: args{1 << 32, 1 << 32}, res: 1},
{args: args{429496729600, 1 << 32}, res: 100},
{args: args{9223372036854775808, 1 << 32}, res: 1 << 31},
{args: args{a: 1 << 32, b: 1 << 32}, res: 1},
{args: args{9223372036854775808, 1 << 62}, res: 2},
{args: args{9223372036854775808, 1 << 63}, res: 1},
}
for _, tt := range tests {
got, err := math.Div64(tt.args.a, tt.args.b)
if tt.err && err == nil {
t.Errorf("Div64() Expected Error = %v, want error", tt.err)
continue
}
if tt.res != got {
t.Errorf("Div64() %v, want %v", got, tt.res)
}
}
}
func TestMath_Mod(t *testing.T) {
type args struct {
a uint64
b uint64
}
tests := []struct {
args args
res uint64
err bool
}{
{args: args{1, 0}, res: 0, err: true},
{args: args{0, 1}, res: 0},
{args: args{1 << 32, 1 << 32}, res: 0},
{args: args{429496729600, 1 << 32}, res: 0},
{args: args{9223372036854775808, 1 << 32}, res: 0},
{args: args{1 << 32, 1 << 32}, res: 0},
{args: args{9223372036854775808, 1 << 62}, res: 0},
{args: args{9223372036854775808, 1 << 63}, res: 0},
{args: args{1 << 32, 17}, res: 1},
{args: args{1 << 32, 19}, res: (1 << 32) % 19},
{args: args{stdmath.MaxUint64, stdmath.MaxUint64}, res: 0},
{args: args{1 << 63, 2}, res: 0},
{args: args{1<<63 + 1, 2}, res: 1},
}
for _, tt := range tests {
got, err := types.Mod64(tt.args.a, tt.args.b)
if tt.err && err == nil {
t.Errorf("Mod64() Expected Error = %v, want error", tt.err)
continue
}
if tt.res != got {
t.Errorf("Mod64() %v, want %v", got, tt.res)
}
}
}
func BenchmarkIntegerSquareRootBelow52Bits(b *testing.B) {
val := uint64(1 << 33)
for i := 0; i < b.N; i++ {