-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow negative observations, error if bounds exceeded
- Loading branch information
Showing
4 changed files
with
329 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// code from: https://github.com/smartcontractkit/chainlink-terra/blob/develop/pkg/terra/marshal_signed_int.go | ||
// will eventually be removed and replaced with a generalized version from libocr | ||
|
||
package solana | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/big" | ||
) | ||
|
||
var i = big.NewInt | ||
|
||
func bounds(numBytes uint) (*big.Int, *big.Int) { | ||
max := i(0).Sub(i(0).Lsh(i(1), numBytes*8-1), i(1)) // 2**(numBytes*8-1)- 1 | ||
min := i(0).Sub(i(0).Neg(max), i(1)) // -2**(numBytes*8-1) | ||
return min, max | ||
} | ||
|
||
// ToBigInt interprets bytes s as a big-endian signed integer | ||
// of size numBytes. | ||
func ToBigInt(s []byte, numBytes uint) (*big.Int, error) { | ||
if uint(len(s)) != numBytes { | ||
return nil, fmt.Errorf("invalid int length: expected %d got %d", numBytes, len(s)) | ||
} | ||
val := (&big.Int{}).SetBytes(s) | ||
numBits := numBytes * 8 | ||
_, max := bounds(numBytes) | ||
negative := val.Cmp(max) > 0 | ||
if negative { | ||
// Get the complement wrt to 2^numBits | ||
maxUint := big.NewInt(1) | ||
maxUint.Lsh(maxUint, numBits) | ||
val.Sub(maxUint, val) | ||
val.Neg(val) | ||
} | ||
return val, nil | ||
} | ||
|
||
// ToBytes converts *big.Int o into bytes as a big-endian signed | ||
// integer of size numBytes | ||
func ToBytes(o *big.Int, numBytes uint) ([]byte, error) { | ||
min, max := bounds(numBytes) | ||
if o.Cmp(max) > 0 || o.Cmp(min) < 0 { | ||
return nil, fmt.Errorf("value won't fit in int%v: 0x%x", numBytes*8, o) | ||
} | ||
negative := o.Sign() < 0 | ||
val := (&big.Int{}) | ||
numBits := numBytes * 8 | ||
if negative { | ||
// compute two's complement as 2**numBits - abs(o) = 2**numBits + o | ||
val.SetInt64(1) | ||
val.Lsh(val, numBits) | ||
val.Add(val, o) | ||
} else { | ||
val.Set(o) | ||
} | ||
b := val.Bytes() // big-endian representation of abs(val) | ||
if uint(len(b)) > numBytes { | ||
return nil, fmt.Errorf("b must fit in %v bytes", numBytes) | ||
} | ||
b = bytes.Join([][]byte{bytes.Repeat([]byte{0}, int(numBytes)-len(b)), b}, []byte{}) | ||
if uint(len(b)) != numBytes { | ||
return nil, fmt.Errorf("wrong length; there must be an error in the padding of b: %v", b) | ||
} | ||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package solana | ||
|
||
import ( | ||
"encoding/hex" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMarshalSignedInt(t *testing.T) { | ||
var tt = []struct { | ||
bytesVal string | ||
size uint | ||
expected *big.Int | ||
expectErr bool | ||
}{ | ||
{ | ||
"ffffffffffffffff", | ||
8, | ||
big.NewInt(-1), | ||
false, | ||
}, | ||
{ | ||
"fffffffffffffffe", | ||
8, | ||
big.NewInt(-2), | ||
false, | ||
}, | ||
{ | ||
"0000000000000000", | ||
8, | ||
big.NewInt(0), | ||
false, | ||
}, | ||
{ | ||
"0000000000000001", | ||
8, | ||
big.NewInt(1), | ||
false, | ||
}, | ||
{ | ||
"0000000000000002", | ||
8, | ||
big.NewInt(2), | ||
false, | ||
}, | ||
{ | ||
"7fffffffffffffff", | ||
8, | ||
big.NewInt(9223372036854775807), // 2^63 - 1 | ||
false, | ||
}, | ||
{ | ||
"00000000000000000000000000000000", | ||
16, | ||
big.NewInt(0), | ||
false, | ||
}, | ||
{ | ||
"00000000000000000000000000000001", | ||
16, | ||
big.NewInt(1), | ||
false, | ||
}, | ||
{ | ||
"00000000000000000000000000000002", | ||
16, | ||
big.NewInt(2), | ||
false, | ||
}, | ||
{ | ||
"7fffffffffffffffffffffffffffffff", // 2^127 - 1 | ||
16, | ||
big.NewInt(0).Sub(big.NewInt(0).Lsh(big.NewInt(1), 127), big.NewInt(1)), | ||
false, | ||
}, | ||
{ | ||
"ffffffffffffffffffffffffffffffff", | ||
16, | ||
big.NewInt(-1), | ||
false, | ||
}, | ||
{ | ||
"fffffffffffffffffffffffffffffffe", | ||
16, | ||
big.NewInt(-2), | ||
false, | ||
}, | ||
{ | ||
"000000000000000000000000000000000000000000000000", | ||
24, | ||
big.NewInt(0), | ||
false, | ||
}, | ||
{ | ||
"000000000000000000000000000000000000000000000001", | ||
24, | ||
big.NewInt(1), | ||
false, | ||
}, | ||
{ | ||
"000000000000000000000000000000000000000000000002", | ||
24, | ||
big.NewInt(2), | ||
false, | ||
}, | ||
{ | ||
"ffffffffffffffffffffffffffffffffffffffffffffffff", | ||
24, | ||
big.NewInt(-1), | ||
false, | ||
}, | ||
{ | ||
"fffffffffffffffffffffffffffffffffffffffffffffffe", | ||
24, | ||
big.NewInt(-2), | ||
false, | ||
}, | ||
} | ||
for _, tc := range tt { | ||
tc := tc | ||
b, err := hex.DecodeString(tc.bytesVal) | ||
require.NoError(t, err) | ||
i, err := ToBigInt(b, tc.size) | ||
require.NoError(t, err) | ||
assert.Equal(t, i.String(), tc.expected.String()) | ||
|
||
// Marshalling back should give us the same bytes | ||
bAfter, err := ToBytes(i, tc.size) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.bytesVal, hex.EncodeToString(bAfter)) | ||
} | ||
|
||
var tt2 = []struct { | ||
o *big.Int | ||
numBytes uint | ||
expectErr bool | ||
}{ | ||
{ | ||
big.NewInt(128), | ||
1, | ||
true, | ||
}, | ||
{ | ||
big.NewInt(-129), | ||
1, | ||
true, | ||
}, | ||
{ | ||
big.NewInt(-128), | ||
1, | ||
false, | ||
}, | ||
{ | ||
big.NewInt(2147483648), | ||
4, | ||
true, | ||
}, | ||
{ | ||
big.NewInt(2147483647), | ||
4, | ||
false, | ||
}, | ||
{ | ||
big.NewInt(-2147483649), | ||
4, | ||
true, | ||
}, | ||
{ | ||
big.NewInt(-2147483648), | ||
4, | ||
false, | ||
}, | ||
} | ||
for _, tc := range tt2 { | ||
tc := tc | ||
_, err := ToBytes(tc.o, tc.numBytes) | ||
if tc.expectErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters