From 311c6f8a3fed5ac03ee4b442fd0f420072bc41b4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 15 Oct 2014 17:12:26 +0200 Subject: Fixed remote Arithmetic tests --- ethutil/big.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'ethutil/big.go') diff --git a/ethutil/big.go b/ethutil/big.go index e23d8f659..bdcf86421 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -1,8 +1,8 @@ package ethutil -import ( - "math/big" -) +import "math/big" + +var MaxInt256 *big.Int = BigD(Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // Big pow // @@ -37,18 +37,29 @@ func BigD(data []byte) *big.Int { // To256 // // "cast" the big int to a 256 big int (i.e., limit to) -var tt256 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)) +var tt256 = new(big.Int).Lsh(big.NewInt(1), 256) +var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)) +var tt255 = new(big.Int).Lsh(big.NewInt(1), 255) -func To256(x *big.Int) *big.Int { - x.And(x, tt256) +func U256(x *big.Int) *big.Int { + //if x.Cmp(Big0) < 0 { + // return new(big.Int).Add(tt256, x) + // } - if x.Cmp(new(big.Int)) < 0 { - x.SetInt64(0) - } + x.And(x, tt256m1) return x } +func S256(x *big.Int) *big.Int { + if x.Cmp(tt255) < 0 { + return x + } else { + // We don't want to modify x, ever + return new(big.Int).Sub(x, tt256) + } +} + // Big to bytes // // Returns the bytes of a big integer with the size specified by **base** -- cgit v1.2.3