Skip to content

Commit 0efa414

Browse files
committed
add S256 back
1 parent 6412178 commit 0efa414

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

common/math/big.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
// Various big integer limit values.
2626
var (
27+
tt255 = BigPow(2, 255)
2728
tt256 = BigPow(2, 256)
2829
tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1))
2930
MaxBig256 = new(big.Int).Set(tt256m1)
@@ -177,3 +178,17 @@ func U256(x *big.Int) *big.Int {
177178
func U256Bytes(n *big.Int) []byte {
178179
return PaddedBigBytes(U256(n), 32)
179180
}
181+
182+
// S256 interprets x as a two's complement number.
183+
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
184+
//
185+
// S256(0) = 0
186+
// S256(1) = 1
187+
// S256(2**255) = -2**255
188+
// S256(2**256-1) = -1
189+
func S256(x *big.Int) *big.Int {
190+
if x.Cmp(tt255) < 0 {
191+
return x
192+
}
193+
return new(big.Int).Sub(x, tt256)
194+
}

0 commit comments

Comments
 (0)