File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import (
24
24
25
25
// Various big integer limit values.
26
26
var (
27
+ tt255 = BigPow (2 , 255 )
27
28
tt256 = BigPow (2 , 256 )
28
29
tt256m1 = new (big.Int ).Sub (tt256 , big .NewInt (1 ))
29
30
MaxBig256 = new (big.Int ).Set (tt256m1 )
@@ -177,3 +178,17 @@ func U256(x *big.Int) *big.Int {
177
178
func U256Bytes (n * big.Int ) []byte {
178
179
return PaddedBigBytes (U256 (n ), 32 )
179
180
}
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
+ }
You can’t perform that action at this time.
0 commit comments