Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
ethcrypto
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nebula
ethcrypto
Commits
1f73f3d6
Commit
1f73f3d6
authored
Jul 07, 2025
by
vicotor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update code
parent
53f899d3
Changes
13
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
4 additions
and
1908 deletions
+4
-1908
hexutil.go
common/hexutil/hexutil.go
+0
-241
hexutil_test.go
common/hexutil/hexutil_test.go
+0
-215
json.go
common/hexutil/json.go
+0
-421
json_example_test.go
common/hexutil/json_example_test.go
+0
-45
json_test.go
common/hexutil/json_test.go
+0
-434
big.go
common/math/big.go
+0
-179
big_test.go
common/math/big_test.go
+0
-159
integer.go
common/math/integer.go
+0
-93
integer_test.go
common/math/integer_test.go
+0
-117
crypto.go
crypto/crypto.go
+1
-1
signature_cgo.go
crypto/signature_cgo.go
+1
-1
decode_test.go
rlp/decode_test.go
+1
-1
encode_test.go
rlp/encode_test.go
+1
-1
No files found.
common/hexutil/hexutil.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
/*
Package hexutil implements hex encoding with 0x prefix.
This encoding is used by the Ethereum RPC API to transport binary data in JSON payloads.
# Encoding Rules
All hex data must have prefix "0x".
For byte slices, the hex data must be of even length. An empty byte slice
encodes as "0x".
Integers are encoded using the least amount of digits (no leading zero digits). Their
encoding may be of uneven length. The number zero encodes as "0x0".
*/
package
hexutil
import
(
"encoding/hex"
"fmt"
"math/big"
"strconv"
)
const
uintBits
=
32
<<
(
uint64
(
^
uint
(
0
))
>>
63
)
// Errors
var
(
ErrEmptyString
=
&
decError
{
"empty hex string"
}
ErrSyntax
=
&
decError
{
"invalid hex string"
}
ErrMissingPrefix
=
&
decError
{
"hex string without 0x prefix"
}
ErrOddLength
=
&
decError
{
"hex string of odd length"
}
ErrEmptyNumber
=
&
decError
{
"hex string
\"
0x
\"
"
}
ErrLeadingZero
=
&
decError
{
"hex number with leading zero digits"
}
ErrUint64Range
=
&
decError
{
"hex number > 64 bits"
}
ErrUintRange
=
&
decError
{
fmt
.
Sprintf
(
"hex number > %d bits"
,
uintBits
)}
ErrBig256Range
=
&
decError
{
"hex number > 256 bits"
}
)
type
decError
struct
{
msg
string
}
func
(
err
decError
)
Error
()
string
{
return
err
.
msg
}
// Decode decodes a hex string with 0x prefix.
func
Decode
(
input
string
)
([]
byte
,
error
)
{
if
len
(
input
)
==
0
{
return
nil
,
ErrEmptyString
}
if
!
has0xPrefix
(
input
)
{
return
nil
,
ErrMissingPrefix
}
b
,
err
:=
hex
.
DecodeString
(
input
[
2
:
])
if
err
!=
nil
{
err
=
mapError
(
err
)
}
return
b
,
err
}
// MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
func
MustDecode
(
input
string
)
[]
byte
{
dec
,
err
:=
Decode
(
input
)
if
err
!=
nil
{
panic
(
err
)
}
return
dec
}
// Encode encodes b as a hex string with 0x prefix.
func
Encode
(
b
[]
byte
)
string
{
enc
:=
make
([]
byte
,
len
(
b
)
*
2
+
2
)
copy
(
enc
,
"0x"
)
hex
.
Encode
(
enc
[
2
:
],
b
)
return
string
(
enc
)
}
// DecodeUint64 decodes a hex string with 0x prefix as a quantity.
func
DecodeUint64
(
input
string
)
(
uint64
,
error
)
{
raw
,
err
:=
checkNumber
(
input
)
if
err
!=
nil
{
return
0
,
err
}
dec
,
err
:=
strconv
.
ParseUint
(
raw
,
16
,
64
)
if
err
!=
nil
{
err
=
mapError
(
err
)
}
return
dec
,
err
}
// MustDecodeUint64 decodes a hex string with 0x prefix as a quantity.
// It panics for invalid input.
func
MustDecodeUint64
(
input
string
)
uint64
{
dec
,
err
:=
DecodeUint64
(
input
)
if
err
!=
nil
{
panic
(
err
)
}
return
dec
}
// EncodeUint64 encodes i as a hex string with 0x prefix.
func
EncodeUint64
(
i
uint64
)
string
{
enc
:=
make
([]
byte
,
2
,
10
)
copy
(
enc
,
"0x"
)
return
string
(
strconv
.
AppendUint
(
enc
,
i
,
16
))
}
var
bigWordNibbles
int
func
init
()
{
// This is a weird way to compute the number of nibbles required for big.Word.
// The usual way would be to use constant arithmetic but go vet can't handle that.
b
,
_
:=
new
(
big
.
Int
)
.
SetString
(
"FFFFFFFFFF"
,
16
)
switch
len
(
b
.
Bits
())
{
case
1
:
bigWordNibbles
=
16
case
2
:
bigWordNibbles
=
8
default
:
panic
(
"weird big.Word size"
)
}
}
// DecodeBig decodes a hex string with 0x prefix as a quantity.
// Numbers larger than 256 bits are not accepted.
func
DecodeBig
(
input
string
)
(
*
big
.
Int
,
error
)
{
raw
,
err
:=
checkNumber
(
input
)
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
raw
)
>
64
{
return
nil
,
ErrBig256Range
}
words
:=
make
([]
big
.
Word
,
len
(
raw
)
/
bigWordNibbles
+
1
)
end
:=
len
(
raw
)
for
i
:=
range
words
{
start
:=
end
-
bigWordNibbles
if
start
<
0
{
start
=
0
}
for
ri
:=
start
;
ri
<
end
;
ri
++
{
nib
:=
decodeNibble
(
raw
[
ri
])
if
nib
==
badNibble
{
return
nil
,
ErrSyntax
}
words
[
i
]
*=
16
words
[
i
]
+=
big
.
Word
(
nib
)
}
end
=
start
}
dec
:=
new
(
big
.
Int
)
.
SetBits
(
words
)
return
dec
,
nil
}
// MustDecodeBig decodes a hex string with 0x prefix as a quantity.
// It panics for invalid input.
func
MustDecodeBig
(
input
string
)
*
big
.
Int
{
dec
,
err
:=
DecodeBig
(
input
)
if
err
!=
nil
{
panic
(
err
)
}
return
dec
}
// EncodeBig encodes bigint as a hex string with 0x prefix.
func
EncodeBig
(
bigint
*
big
.
Int
)
string
{
if
sign
:=
bigint
.
Sign
();
sign
==
0
{
return
"0x0"
}
else
if
sign
>
0
{
return
"0x"
+
bigint
.
Text
(
16
)
}
else
{
return
"-0x"
+
bigint
.
Text
(
16
)[
1
:
]
}
}
func
has0xPrefix
(
input
string
)
bool
{
return
len
(
input
)
>=
2
&&
input
[
0
]
==
'0'
&&
(
input
[
1
]
==
'x'
||
input
[
1
]
==
'X'
)
}
func
checkNumber
(
input
string
)
(
raw
string
,
err
error
)
{
if
len
(
input
)
==
0
{
return
""
,
ErrEmptyString
}
if
!
has0xPrefix
(
input
)
{
return
""
,
ErrMissingPrefix
}
input
=
input
[
2
:
]
if
len
(
input
)
==
0
{
return
""
,
ErrEmptyNumber
}
if
len
(
input
)
>
1
&&
input
[
0
]
==
'0'
{
return
""
,
ErrLeadingZero
}
return
input
,
nil
}
const
badNibble
=
^
uint64
(
0
)
func
decodeNibble
(
in
byte
)
uint64
{
switch
{
case
in
>=
'0'
&&
in
<=
'9'
:
return
uint64
(
in
-
'0'
)
case
in
>=
'A'
&&
in
<=
'F'
:
return
uint64
(
in
-
'A'
+
10
)
case
in
>=
'a'
&&
in
<=
'f'
:
return
uint64
(
in
-
'a'
+
10
)
default
:
return
badNibble
}
}
func
mapError
(
err
error
)
error
{
if
err
,
ok
:=
err
.
(
*
strconv
.
NumError
);
ok
{
switch
err
.
Err
{
case
strconv
.
ErrRange
:
return
ErrUint64Range
case
strconv
.
ErrSyntax
:
return
ErrSyntax
}
}
if
_
,
ok
:=
err
.
(
hex
.
InvalidByteError
);
ok
{
return
ErrSyntax
}
if
err
==
hex
.
ErrLength
{
return
ErrOddLength
}
return
err
}
common/hexutil/hexutil_test.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
hexutil
import
(
"bytes"
"math/big"
"testing"
)
type
marshalTest
struct
{
input
interface
{}
want
string
}
type
unmarshalTest
struct
{
input
string
want
interface
{}
wantErr
error
// if set, decoding must fail on any platform
wantErr32bit
error
// if set, decoding must fail on 32bit platforms (used for Uint tests)
}
var
(
encodeBytesTests
=
[]
marshalTest
{
{[]
byte
{},
"0x"
},
{[]
byte
{
0
},
"0x00"
},
{[]
byte
{
0
,
0
,
1
,
2
},
"0x00000102"
},
}
encodeBigTests
=
[]
marshalTest
{
{
referenceBig
(
"0"
),
"0x0"
},
{
referenceBig
(
"1"
),
"0x1"
},
{
referenceBig
(
"ff"
),
"0xff"
},
{
referenceBig
(
"112233445566778899aabbccddeeff"
),
"0x112233445566778899aabbccddeeff"
},
{
referenceBig
(
"80a7f2c1bcc396c00"
),
"0x80a7f2c1bcc396c00"
},
{
referenceBig
(
"-80a7f2c1bcc396c00"
),
"-0x80a7f2c1bcc396c00"
},
}
encodeUint64Tests
=
[]
marshalTest
{
{
uint64
(
0
),
"0x0"
},
{
uint64
(
1
),
"0x1"
},
{
uint64
(
0xff
),
"0xff"
},
{
uint64
(
0x1122334455667788
),
"0x1122334455667788"
},
}
encodeUintTests
=
[]
marshalTest
{
{
uint
(
0
),
"0x0"
},
{
uint
(
1
),
"0x1"
},
{
uint
(
0xff
),
"0xff"
},
{
uint
(
0x11223344
),
"0x11223344"
},
}
decodeBytesTests
=
[]
unmarshalTest
{
// invalid
{
input
:
``
,
wantErr
:
ErrEmptyString
},
{
input
:
`0`
,
wantErr
:
ErrMissingPrefix
},
{
input
:
`0x0`
,
wantErr
:
ErrOddLength
},
{
input
:
`0x023`
,
wantErr
:
ErrOddLength
},
{
input
:
`0xxx`
,
wantErr
:
ErrSyntax
},
{
input
:
`0x01zz01`
,
wantErr
:
ErrSyntax
},
// valid
{
input
:
`0x`
,
want
:
[]
byte
{}},
{
input
:
`0X`
,
want
:
[]
byte
{}},
{
input
:
`0x02`
,
want
:
[]
byte
{
0x02
}},
{
input
:
`0X02`
,
want
:
[]
byte
{
0x02
}},
{
input
:
`0xffffffffff`
,
want
:
[]
byte
{
0xff
,
0xff
,
0xff
,
0xff
,
0xff
}},
{
input
:
`0xffffffffffffffffffffffffffffffffffff`
,
want
:
[]
byte
{
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
},
},
}
decodeBigTests
=
[]
unmarshalTest
{
// invalid
{
input
:
`0`
,
wantErr
:
ErrMissingPrefix
},
{
input
:
`0x`
,
wantErr
:
ErrEmptyNumber
},
{
input
:
`0x01`
,
wantErr
:
ErrLeadingZero
},
{
input
:
`0xx`
,
wantErr
:
ErrSyntax
},
{
input
:
`0x1zz01`
,
wantErr
:
ErrSyntax
},
{
input
:
`0x10000000000000000000000000000000000000000000000000000000000000000`
,
wantErr
:
ErrBig256Range
,
},
// valid
{
input
:
`0x0`
,
want
:
big
.
NewInt
(
0
)},
{
input
:
`0x2`
,
want
:
big
.
NewInt
(
0x2
)},
{
input
:
`0x2F2`
,
want
:
big
.
NewInt
(
0x2f2
)},
{
input
:
`0X2F2`
,
want
:
big
.
NewInt
(
0x2f2
)},
{
input
:
`0x1122aaff`
,
want
:
big
.
NewInt
(
0x1122aaff
)},
{
input
:
`0xbBb`
,
want
:
big
.
NewInt
(
0xbbb
)},
{
input
:
`0xfffffffff`
,
want
:
big
.
NewInt
(
0xfffffffff
)},
{
input
:
`0x112233445566778899aabbccddeeff`
,
want
:
referenceBig
(
"112233445566778899aabbccddeeff"
),
},
{
input
:
`0xffffffffffffffffffffffffffffffffffff`
,
want
:
referenceBig
(
"ffffffffffffffffffffffffffffffffffff"
),
},
{
input
:
`0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`
,
want
:
referenceBig
(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
),
},
}
decodeUint64Tests
=
[]
unmarshalTest
{
// invalid
{
input
:
`0`
,
wantErr
:
ErrMissingPrefix
},
{
input
:
`0x`
,
wantErr
:
ErrEmptyNumber
},
{
input
:
`0x01`
,
wantErr
:
ErrLeadingZero
},
{
input
:
`0xfffffffffffffffff`
,
wantErr
:
ErrUint64Range
},
{
input
:
`0xx`
,
wantErr
:
ErrSyntax
},
{
input
:
`0x1zz01`
,
wantErr
:
ErrSyntax
},
// valid
{
input
:
`0x0`
,
want
:
uint64
(
0
)},
{
input
:
`0x2`
,
want
:
uint64
(
0x2
)},
{
input
:
`0x2F2`
,
want
:
uint64
(
0x2f2
)},
{
input
:
`0X2F2`
,
want
:
uint64
(
0x2f2
)},
{
input
:
`0x1122aaff`
,
want
:
uint64
(
0x1122aaff
)},
{
input
:
`0xbbb`
,
want
:
uint64
(
0xbbb
)},
{
input
:
`0xffffffffffffffff`
,
want
:
uint64
(
0xffffffffffffffff
)},
}
)
func
TestEncode
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
encodeBytesTests
{
enc
:=
Encode
(
test
.
input
.
([]
byte
))
if
enc
!=
test
.
want
{
t
.
Errorf
(
"input %x: wrong encoding %s"
,
test
.
input
,
enc
)
}
}
}
func
TestDecode
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
decodeBytesTests
{
dec
,
err
:=
Decode
(
test
.
input
)
if
!
checkError
(
t
,
test
.
input
,
err
,
test
.
wantErr
)
{
continue
}
if
!
bytes
.
Equal
(
test
.
want
.
([]
byte
),
dec
)
{
t
.
Errorf
(
"input %s: value mismatch: got %x, want %x"
,
test
.
input
,
dec
,
test
.
want
)
continue
}
}
}
func
TestEncodeBig
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
encodeBigTests
{
enc
:=
EncodeBig
(
test
.
input
.
(
*
big
.
Int
))
if
enc
!=
test
.
want
{
t
.
Errorf
(
"input %x: wrong encoding %s"
,
test
.
input
,
enc
)
}
}
}
func
TestDecodeBig
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
decodeBigTests
{
dec
,
err
:=
DecodeBig
(
test
.
input
)
if
!
checkError
(
t
,
test
.
input
,
err
,
test
.
wantErr
)
{
continue
}
if
dec
.
Cmp
(
test
.
want
.
(
*
big
.
Int
))
!=
0
{
t
.
Errorf
(
"input %s: value mismatch: got %x, want %x"
,
test
.
input
,
dec
,
test
.
want
)
continue
}
}
}
func
TestEncodeUint64
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
encodeUint64Tests
{
enc
:=
EncodeUint64
(
test
.
input
.
(
uint64
))
if
enc
!=
test
.
want
{
t
.
Errorf
(
"input %x: wrong encoding %s"
,
test
.
input
,
enc
)
}
}
}
func
TestDecodeUint64
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
decodeUint64Tests
{
dec
,
err
:=
DecodeUint64
(
test
.
input
)
if
!
checkError
(
t
,
test
.
input
,
err
,
test
.
wantErr
)
{
continue
}
if
dec
!=
test
.
want
.
(
uint64
)
{
t
.
Errorf
(
"input %s: value mismatch: got %x, want %x"
,
test
.
input
,
dec
,
test
.
want
)
continue
}
}
}
func
BenchmarkEncodeBig
(
b
*
testing
.
B
)
{
for
_
,
bench
:=
range
encodeBigTests
{
b
.
Run
(
bench
.
want
,
func
(
b
*
testing
.
B
)
{
b
.
ReportAllocs
()
bigint
:=
bench
.
input
.
(
*
big
.
Int
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
EncodeBig
(
bigint
)
}
})
}
}
common/hexutil/json.go
deleted
100644 → 0
View file @
53f899d3
This diff is collapsed.
Click to expand it.
common/hexutil/json_example_test.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
hexutil_test
import
(
"encoding/json"
"fmt"
"code.wuban.net.cn/cmpchain/ethcrypto/common/hexutil"
)
type
MyType
[
5
]
byte
func
(
v
*
MyType
)
UnmarshalText
(
input
[]
byte
)
error
{
return
hexutil
.
UnmarshalFixedText
(
"MyType"
,
input
,
v
[
:
])
}
func
(
v
MyType
)
String
()
string
{
return
hexutil
.
Bytes
(
v
[
:
])
.
String
()
}
func
ExampleUnmarshalFixedText
()
{
var
v1
,
v2
MyType
fmt
.
Println
(
"v1 error:"
,
json
.
Unmarshal
([]
byte
(
`"0x01"`
),
&
v1
))
fmt
.
Println
(
"v2 error:"
,
json
.
Unmarshal
([]
byte
(
`"0x0101010101"`
),
&
v2
))
fmt
.
Println
(
"v2:"
,
v2
)
// Output:
// v1 error: hex string has length 2, want 10 for MyType
// v2 error: <nil>
// v2: 0x0101010101
}
common/hexutil/json_test.go
deleted
100644 → 0
View file @
53f899d3
This diff is collapsed.
Click to expand it.
common/math/big.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package math provides integer math utilities.
package
math
import
(
"fmt"
"math/big"
)
// Various big integer limit values.
var
(
tt256
=
BigPow
(
2
,
256
)
tt256m1
=
new
(
big
.
Int
)
.
Sub
(
tt256
,
big
.
NewInt
(
1
))
MaxBig256
=
new
(
big
.
Int
)
.
Set
(
tt256m1
)
)
const
(
// number of bits in a big.Word
wordBits
=
32
<<
(
uint64
(
^
big
.
Word
(
0
))
>>
63
)
// number of bytes in a big.Word
wordBytes
=
wordBits
/
8
)
// HexOrDecimal256 marshals big.Int as hex or decimal.
type
HexOrDecimal256
big
.
Int
// NewHexOrDecimal256 creates a new HexOrDecimal256
func
NewHexOrDecimal256
(
x
int64
)
*
HexOrDecimal256
{
b
:=
big
.
NewInt
(
x
)
h
:=
HexOrDecimal256
(
*
b
)
return
&
h
}
// UnmarshalJSON implements json.Unmarshaler.
//
// It is similar to UnmarshalText, but allows parsing real decimals too, not just
// quoted decimal strings.
func
(
i
*
HexOrDecimal256
)
UnmarshalJSON
(
input
[]
byte
)
error
{
if
len
(
input
)
>
1
&&
input
[
0
]
==
'"'
{
input
=
input
[
1
:
len
(
input
)
-
1
]
}
return
i
.
UnmarshalText
(
input
)
}
// UnmarshalText implements encoding.TextUnmarshaler.
func
(
i
*
HexOrDecimal256
)
UnmarshalText
(
input
[]
byte
)
error
{
bigint
,
ok
:=
ParseBig256
(
string
(
input
))
if
!
ok
{
return
fmt
.
Errorf
(
"invalid hex or decimal integer %q"
,
input
)
}
*
i
=
HexOrDecimal256
(
*
bigint
)
return
nil
}
// MarshalText implements encoding.TextMarshaler.
func
(
i
*
HexOrDecimal256
)
MarshalText
()
([]
byte
,
error
)
{
if
i
==
nil
{
return
[]
byte
(
"0x0"
),
nil
}
return
fmt
.
Appendf
(
nil
,
"%#x"
,
(
*
big
.
Int
)(
i
)),
nil
}
// Decimal256 unmarshals big.Int as a decimal string. When unmarshalling,
// it however accepts either "0x"-prefixed (hex encoded) or non-prefixed (decimal)
type
Decimal256
big
.
Int
// NewDecimal256 creates a new Decimal256
func
NewDecimal256
(
x
int64
)
*
Decimal256
{
b
:=
big
.
NewInt
(
x
)
d
:=
Decimal256
(
*
b
)
return
&
d
}
// UnmarshalText implements encoding.TextUnmarshaler.
func
(
i
*
Decimal256
)
UnmarshalText
(
input
[]
byte
)
error
{
bigint
,
ok
:=
ParseBig256
(
string
(
input
))
if
!
ok
{
return
fmt
.
Errorf
(
"invalid hex or decimal integer %q"
,
input
)
}
*
i
=
Decimal256
(
*
bigint
)
return
nil
}
// MarshalText implements encoding.TextMarshaler.
func
(
i
*
Decimal256
)
MarshalText
()
([]
byte
,
error
)
{
return
[]
byte
(
i
.
String
()),
nil
}
// String implements Stringer.
func
(
i
*
Decimal256
)
String
()
string
{
if
i
==
nil
{
return
"0"
}
return
fmt
.
Sprintf
(
"%#d"
,
(
*
big
.
Int
)(
i
))
}
// ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax.
// Leading zeros are accepted. The empty string parses as zero.
func
ParseBig256
(
s
string
)
(
*
big
.
Int
,
bool
)
{
if
s
==
""
{
return
new
(
big
.
Int
),
true
}
var
bigint
*
big
.
Int
var
ok
bool
if
len
(
s
)
>=
2
&&
(
s
[
:
2
]
==
"0x"
||
s
[
:
2
]
==
"0X"
)
{
bigint
,
ok
=
new
(
big
.
Int
)
.
SetString
(
s
[
2
:
],
16
)
}
else
{
bigint
,
ok
=
new
(
big
.
Int
)
.
SetString
(
s
,
10
)
}
if
ok
&&
bigint
.
BitLen
()
>
256
{
bigint
,
ok
=
nil
,
false
}
return
bigint
,
ok
}
// MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.
func
MustParseBig256
(
s
string
)
*
big
.
Int
{
v
,
ok
:=
ParseBig256
(
s
)
if
!
ok
{
panic
(
"invalid 256 bit integer: "
+
s
)
}
return
v
}
// BigPow returns a ** b as a big integer.
func
BigPow
(
a
,
b
int64
)
*
big
.
Int
{
r
:=
big
.
NewInt
(
a
)
return
r
.
Exp
(
r
,
big
.
NewInt
(
b
),
nil
)
}
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
// of the slice is at least n bytes.
func
PaddedBigBytes
(
bigint
*
big
.
Int
,
n
int
)
[]
byte
{
if
bigint
.
BitLen
()
/
8
>=
n
{
return
bigint
.
Bytes
()
}
ret
:=
make
([]
byte
,
n
)
ReadBits
(
bigint
,
ret
)
return
ret
}
// ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure
// that buf has enough space. If buf is too short the result will be incomplete.
func
ReadBits
(
bigint
*
big
.
Int
,
buf
[]
byte
)
{
i
:=
len
(
buf
)
for
_
,
d
:=
range
bigint
.
Bits
()
{
for
j
:=
0
;
j
<
wordBytes
&&
i
>
0
;
j
++
{
i
--
buf
[
i
]
=
byte
(
d
)
d
>>=
8
}
}
}
// U256 encodes x as a 256 bit two's complement number. This operation is destructive.
func
U256
(
x
*
big
.
Int
)
*
big
.
Int
{
return
x
.
And
(
x
,
tt256m1
)
}
// U256Bytes converts a big Int into a 256bit EVM number.
// This operation is destructive.
func
U256Bytes
(
n
*
big
.
Int
)
[]
byte
{
return
PaddedBigBytes
(
U256
(
n
),
32
)
}
common/math/big_test.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
math
import
(
"bytes"
"encoding/hex"
"math/big"
"testing"
)
func
TestHexOrDecimal256
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
num
*
big
.
Int
ok
bool
}{
{
""
,
big
.
NewInt
(
0
),
true
},
{
"0"
,
big
.
NewInt
(
0
),
true
},
{
"0x0"
,
big
.
NewInt
(
0
),
true
},
{
"12345678"
,
big
.
NewInt
(
12345678
),
true
},
{
"0x12345678"
,
big
.
NewInt
(
0x12345678
),
true
},
{
"0X12345678"
,
big
.
NewInt
(
0x12345678
),
true
},
// Tests for leading zero behaviour:
{
"0123456789"
,
big
.
NewInt
(
123456789
),
true
},
// note: not octal
{
"00"
,
big
.
NewInt
(
0
),
true
},
{
"0x00"
,
big
.
NewInt
(
0
),
true
},
{
"0x012345678abc"
,
big
.
NewInt
(
0x12345678abc
),
true
},
// Invalid syntax:
{
"abcdef"
,
nil
,
false
},
{
"0xgg"
,
nil
,
false
},
// Larger than 256 bits:
{
"115792089237316195423570985008687907853269984665640564039457584007913129639936"
,
nil
,
false
},
}
for
_
,
test
:=
range
tests
{
var
num
HexOrDecimal256
err
:=
num
.
UnmarshalText
([]
byte
(
test
.
input
))
if
(
err
==
nil
)
!=
test
.
ok
{
t
.
Errorf
(
"ParseBig(%q) -> (err == nil) == %t, want %t"
,
test
.
input
,
err
==
nil
,
test
.
ok
)
continue
}
if
test
.
num
!=
nil
&&
(
*
big
.
Int
)(
&
num
)
.
Cmp
(
test
.
num
)
!=
0
{
t
.
Errorf
(
"ParseBig(%q) -> %d, want %d"
,
test
.
input
,
(
*
big
.
Int
)(
&
num
),
test
.
num
)
}
}
}
func
TestMustParseBig256
(
t
*
testing
.
T
)
{
defer
func
()
{
if
recover
()
==
nil
{
t
.
Error
(
"MustParseBig should've panicked"
)
}
}()
MustParseBig256
(
"ggg"
)
}
func
TestPaddedBigBytes
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
num
*
big
.
Int
n
int
result
[]
byte
}{
{
num
:
big
.
NewInt
(
0
),
n
:
4
,
result
:
[]
byte
{
0
,
0
,
0
,
0
}},
{
num
:
big
.
NewInt
(
1
),
n
:
4
,
result
:
[]
byte
{
0
,
0
,
0
,
1
}},
{
num
:
big
.
NewInt
(
512
),
n
:
4
,
result
:
[]
byte
{
0
,
0
,
2
,
0
}},
{
num
:
BigPow
(
2
,
32
),
n
:
4
,
result
:
[]
byte
{
1
,
0
,
0
,
0
,
0
}},
}
for
_
,
test
:=
range
tests
{
if
result
:=
PaddedBigBytes
(
test
.
num
,
test
.
n
);
!
bytes
.
Equal
(
result
,
test
.
result
)
{
t
.
Errorf
(
"PaddedBigBytes(%d, %d) = %v, want %v"
,
test
.
num
,
test
.
n
,
result
,
test
.
result
)
}
}
}
func
BenchmarkPaddedBigBytesLargePadding
(
b
*
testing
.
B
)
{
bigint
:=
MustParseBig256
(
"123456789123456789123456789123456789"
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
PaddedBigBytes
(
bigint
,
200
)
}
}
func
BenchmarkPaddedBigBytesSmallPadding
(
b
*
testing
.
B
)
{
bigint
:=
MustParseBig256
(
"0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC"
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
PaddedBigBytes
(
bigint
,
5
)
}
}
func
BenchmarkPaddedBigBytesSmallOnePadding
(
b
*
testing
.
B
)
{
bigint
:=
MustParseBig256
(
"0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC"
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
PaddedBigBytes
(
bigint
,
32
)
}
}
func
BenchmarkByteAtOld
(
b
*
testing
.
B
)
{
bigint
:=
MustParseBig256
(
"0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC"
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
PaddedBigBytes
(
bigint
,
32
)
}
}
func
TestReadBits
(
t
*
testing
.
T
)
{
check
:=
func
(
input
string
)
{
want
,
_
:=
hex
.
DecodeString
(
input
)
n
,
_
:=
new
(
big
.
Int
)
.
SetString
(
input
,
16
)
buf
:=
make
([]
byte
,
len
(
want
))
ReadBits
(
n
,
buf
)
if
!
bytes
.
Equal
(
buf
,
want
)
{
t
.
Errorf
(
"have: %x
\n
want: %x"
,
buf
,
want
)
}
}
check
(
"000000000000000000000000000000000000000000000000000000FEFCF3F8F0"
)
check
(
"0000000000012345000000000000000000000000000000000000FEFCF3F8F0"
)
check
(
"18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0"
)
}
func
TestU256
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
x
,
y
*
big
.
Int
}{
{
x
:
big
.
NewInt
(
0
),
y
:
big
.
NewInt
(
0
)},
{
x
:
big
.
NewInt
(
1
),
y
:
big
.
NewInt
(
1
)},
{
x
:
BigPow
(
2
,
255
),
y
:
BigPow
(
2
,
255
)},
{
x
:
BigPow
(
2
,
256
),
y
:
big
.
NewInt
(
0
)},
{
x
:
new
(
big
.
Int
)
.
Add
(
BigPow
(
2
,
256
),
big
.
NewInt
(
1
)),
y
:
big
.
NewInt
(
1
)},
// negative values
{
x
:
big
.
NewInt
(
-
1
),
y
:
new
(
big
.
Int
)
.
Sub
(
BigPow
(
2
,
256
),
big
.
NewInt
(
1
))},
{
x
:
big
.
NewInt
(
-
2
),
y
:
new
(
big
.
Int
)
.
Sub
(
BigPow
(
2
,
256
),
big
.
NewInt
(
2
))},
{
x
:
BigPow
(
2
,
-
255
),
y
:
big
.
NewInt
(
1
)},
}
for
_
,
test
:=
range
tests
{
if
y
:=
U256
(
new
(
big
.
Int
)
.
Set
(
test
.
x
));
y
.
Cmp
(
test
.
y
)
!=
0
{
t
.
Errorf
(
"U256(%x) = %x, want %x"
,
test
.
x
,
y
,
test
.
y
)
}
}
}
func
TestU256Bytes
(
t
*
testing
.
T
)
{
ubytes
:=
make
([]
byte
,
32
)
ubytes
[
31
]
=
1
unsigned
:=
U256Bytes
(
big
.
NewInt
(
1
))
if
!
bytes
.
Equal
(
unsigned
,
ubytes
)
{
t
.
Errorf
(
"expected %x got %x"
,
ubytes
,
unsigned
)
}
}
common/math/integer.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
math
import
(
"fmt"
"math/bits"
"strconv"
)
// HexOrDecimal64 marshals uint64 as hex or decimal.
type
HexOrDecimal64
uint64
// UnmarshalJSON implements json.Unmarshaler.
//
// It is similar to UnmarshalText, but allows parsing real decimals too, not just
// quoted decimal strings.
func
(
i
*
HexOrDecimal64
)
UnmarshalJSON
(
input
[]
byte
)
error
{
if
len
(
input
)
>
1
&&
input
[
0
]
==
'"'
{
input
=
input
[
1
:
len
(
input
)
-
1
]
}
return
i
.
UnmarshalText
(
input
)
}
// UnmarshalText implements encoding.TextUnmarshaler.
func
(
i
*
HexOrDecimal64
)
UnmarshalText
(
input
[]
byte
)
error
{
n
,
ok
:=
ParseUint64
(
string
(
input
))
if
!
ok
{
return
fmt
.
Errorf
(
"invalid hex or decimal integer %q"
,
input
)
}
*
i
=
HexOrDecimal64
(
n
)
return
nil
}
// MarshalText implements encoding.TextMarshaler.
func
(
i
HexOrDecimal64
)
MarshalText
()
([]
byte
,
error
)
{
return
fmt
.
Appendf
(
nil
,
"%#x"
,
uint64
(
i
)),
nil
}
// ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
// Leading zeros are accepted. The empty string parses as zero.
func
ParseUint64
(
s
string
)
(
uint64
,
bool
)
{
if
s
==
""
{
return
0
,
true
}
if
len
(
s
)
>=
2
&&
(
s
[
:
2
]
==
"0x"
||
s
[
:
2
]
==
"0X"
)
{
v
,
err
:=
strconv
.
ParseUint
(
s
[
2
:
],
16
,
64
)
return
v
,
err
==
nil
}
v
,
err
:=
strconv
.
ParseUint
(
s
,
10
,
64
)
return
v
,
err
==
nil
}
// MustParseUint64 parses s as an integer and panics if the string is invalid.
func
MustParseUint64
(
s
string
)
uint64
{
v
,
ok
:=
ParseUint64
(
s
)
if
!
ok
{
panic
(
"invalid unsigned 64 bit integer: "
+
s
)
}
return
v
}
// SafeSub returns x-y and checks for overflow.
func
SafeSub
(
x
,
y
uint64
)
(
uint64
,
bool
)
{
diff
,
borrowOut
:=
bits
.
Sub64
(
x
,
y
,
0
)
return
diff
,
borrowOut
!=
0
}
// SafeAdd returns x+y and checks for overflow.
func
SafeAdd
(
x
,
y
uint64
)
(
uint64
,
bool
)
{
sum
,
carryOut
:=
bits
.
Add64
(
x
,
y
,
0
)
return
sum
,
carryOut
!=
0
}
// SafeMul returns x*y and checks for overflow.
func
SafeMul
(
x
,
y
uint64
)
(
uint64
,
bool
)
{
hi
,
lo
:=
bits
.
Mul64
(
x
,
y
)
return
lo
,
hi
!=
0
}
common/math/integer_test.go
deleted
100644 → 0
View file @
53f899d3
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
math
import
(
"math"
"testing"
)
type
operation
byte
const
(
sub
operation
=
iota
add
mul
)
func
TestOverflow
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
[]
struct
{
x
uint64
y
uint64
overflow
bool
op
operation
}{
// add operations
{
math
.
MaxUint64
,
1
,
true
,
add
},
{
math
.
MaxUint64
-
1
,
1
,
false
,
add
},
// sub operations
{
0
,
1
,
true
,
sub
},
{
0
,
0
,
false
,
sub
},
// mul operations
{
0
,
0
,
false
,
mul
},
{
10
,
10
,
false
,
mul
},
{
math
.
MaxUint64
,
2
,
true
,
mul
},
{
math
.
MaxUint64
,
1
,
false
,
mul
},
}
{
var
overflows
bool
switch
test
.
op
{
case
sub
:
_
,
overflows
=
SafeSub
(
test
.
x
,
test
.
y
)
case
add
:
_
,
overflows
=
SafeAdd
(
test
.
x
,
test
.
y
)
case
mul
:
_
,
overflows
=
SafeMul
(
test
.
x
,
test
.
y
)
}
if
test
.
overflow
!=
overflows
{
t
.
Errorf
(
"%d failed. Expected test to be %v, got %v"
,
i
,
test
.
overflow
,
overflows
)
}
}
}
func
TestHexOrDecimal64
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
num
uint64
ok
bool
}{
{
""
,
0
,
true
},
{
"0"
,
0
,
true
},
{
"0x0"
,
0
,
true
},
{
"12345678"
,
12345678
,
true
},
{
"0x12345678"
,
0x12345678
,
true
},
{
"0X12345678"
,
0x12345678
,
true
},
// Tests for leading zero behaviour:
{
"0123456789"
,
123456789
,
true
},
// note: not octal
{
"0x00"
,
0
,
true
},
{
"0x012345678abc"
,
0x12345678abc
,
true
},
// Invalid syntax:
{
"abcdef"
,
0
,
false
},
{
"0xgg"
,
0
,
false
},
// Doesn't fit into 64 bits:
{
"18446744073709551617"
,
0
,
false
},
}
for
_
,
test
:=
range
tests
{
var
num
HexOrDecimal64
err
:=
num
.
UnmarshalText
([]
byte
(
test
.
input
))
if
(
err
==
nil
)
!=
test
.
ok
{
t
.
Errorf
(
"ParseUint64(%q) -> (err == nil) = %t, want %t"
,
test
.
input
,
err
==
nil
,
test
.
ok
)
continue
}
if
err
==
nil
&&
uint64
(
num
)
!=
test
.
num
{
t
.
Errorf
(
"ParseUint64(%q) -> %d, want %d"
,
test
.
input
,
num
,
test
.
num
)
}
}
}
func
TestMustParseUint64
(
t
*
testing
.
T
)
{
if
v
:=
MustParseUint64
(
"12345"
);
v
!=
12345
{
t
.
Errorf
(
`MustParseUint64("12345") = %d, want 12345`
,
v
)
}
}
func
TestMustParseUint64Panic
(
t
*
testing
.
T
)
{
defer
func
()
{
if
recover
()
==
nil
{
t
.
Error
(
"MustParseBig should've panicked"
)
}
}()
MustParseUint64
(
"ggg"
)
}
crypto/crypto.go
View file @
1f73f3d6
...
...
@@ -24,7 +24,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"
code.wuban.net.cn/cmpchain/ethcrypto
/common/math"
"
github.com/CaduceusMetaverseProtocol/MetaTypes
/common/math"
"hash"
"io"
"math/big"
...
...
crypto/signature_cgo.go
View file @
1f73f3d6
...
...
@@ -23,7 +23,7 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"
code.wuban.net.cn/cmpchain/ethcrypto
/common/math"
"
github.com/CaduceusMetaverseProtocol/MetaTypes
/common/math"
"code.wuban.net.cn/cmpchain/ethcrypto/crypto/secp256k1"
)
...
...
rlp/decode_test.go
View file @
1f73f3d6
...
...
@@ -28,7 +28,7 @@ import (
"strings"
"testing"
"
code.wuban.net.cn/cmpchain/ethcrypto
/common/math"
"
github.com/CaduceusMetaverseProtocol/MetaTypes
/common/math"
"github.com/holiman/uint256"
)
...
...
rlp/encode_test.go
View file @
1f73f3d6
...
...
@@ -26,7 +26,7 @@ import (
"sync"
"testing"
"
code.wuban.net.cn/cmpchain/ethcrypto
/common/math"
"
github.com/CaduceusMetaverseProtocol/MetaTypes
/common/math"
"github.com/holiman/uint256"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment