Fixed remote Arithmetic tests

This commit is contained in:
obscuren
2014-10-15 17:12:26 +02:00
parent 266d212094
commit 311c6f8a3f
11 changed files with 187 additions and 100 deletions

View File

@ -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**