Skip to content

Commit 5773b73

Browse files
committed
fix: uint256 overflow when generate 256bit seed words
1 parent b6cb7bf commit 5773b73

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ Total Wallet Resolved: 50000 w
257257

258258
- [conseweb/coinutil](https://github.com/conseweb/coinutil) - for BIP39 implementation in Go
259259
- [tyler-smith/go-bip39](https://github.com/tyler-smith/go-bip39) - for BIP39 implementation in Go
260-
- [holiman/uint256](https://github.com/holiman/uint256) - for high performance fixed size unsigned 256-bit math library
261260

262261
## Contributing ![eth](https://user-images.githubusercontent.com/37617738/120125730-1d1bd680-c1e4-11eb-83ad-45664245cae9.png)
263262

bip39/bip39.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import (
1111
"crypto/rand"
1212
"crypto/sha256"
1313
"crypto/sha512"
14+
"math/big"
1415
"strings"
1516

16-
"github.com/holiman/uint256"
1717
"github.com/pkg/errors"
1818
"golang.org/x/crypto/pbkdf2"
1919
)
2020

2121
var (
22-
one = uint256.NewInt(1)
23-
two = uint256.NewInt(2)
22+
one = big.NewInt(1)
23+
two = big.NewInt(2)
2424

2525
bitsChunkSize = 11
26-
shift11BitsMask = new(uint256.Int).Lsh(one, uint(bitsChunkSize)) // 2^11 = 2048
27-
last11BitsMask = new(uint256.Int).Sub(shift11BitsMask, one) // 2^11 - 1 = 2047
26+
shift11BitsMask = new(big.Int).Lsh(one, uint(bitsChunkSize)) // 2^11 = 2048
27+
last11BitsMask = new(big.Int).Sub(shift11BitsMask, one) // 2^11 - 1 = 2047
2828
)
2929

3030
// NewEntropy will create random entropy bytes
@@ -61,10 +61,10 @@ func NewMnemonic(entropy []byte) (string, error) {
6161

6262
// Add checksum to entropy.
6363
// Entropy as an int so we can bitmask without worrying about bytes slices.
64-
entropyInt := new(uint256.Int).SetBytes(addChecksum(entropy))
64+
entropyInt := addChecksum(entropy)
6565

66-
// Throw away uint256.Int for AND masking.
67-
word := uint256.NewInt(0)
66+
// Throw away big.Int for AND masking.
67+
word := big.NewInt(0)
6868

6969
// Slice to hold words in.
7070
words := make([]string, sentenceLength)
@@ -92,10 +92,10 @@ func NewSeed(mnemonic, password string) []byte {
9292
}
9393

9494
// Appends to data the first (len(data) / 32)bits of the result of sha256(data)
95-
// abd returns the result as a uint256.Int.
95+
// abd returns the result as a big.Int.
9696
//
9797
// Currently only supports data up to 32 bytes.
98-
func addChecksum(data []byte) []byte {
98+
func addChecksum(data []byte) *big.Int {
9999
// Get first byte of sha256
100100
hash := computeChecksum(data)
101101
firstChecksumByte := hash[0]
@@ -106,7 +106,7 @@ func addChecksum(data []byte) []byte {
106106
// For each bit of check sum we want we shift the data one the left
107107
// and then set the (new) right most bit equal to checksum bit at that index
108108
// staring from the left
109-
dataInt := new(uint256.Int).SetBytes(data)
109+
dataInt := new(big.Int).SetBytes(data)
110110
for i := uint(0); i < checksumBitLength; i++ {
111111
// Bitshift 1 left
112112
dataInt.Mul(dataInt, two)
@@ -117,7 +117,7 @@ func addChecksum(data []byte) []byte {
117117
}
118118
}
119119

120-
return dataInt.Bytes()
120+
return dataInt
121121
}
122122

123123
func computeChecksum(data []byte) []byte {

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/btcsuite/btcd/btcutil v1.1.3
88
github.com/cheggaaa/pb/v3 v3.1.2
99
github.com/ethereum/go-ethereum v1.11.4
10-
github.com/holiman/uint256 v1.2.2
1110
github.com/pkg/errors v0.9.1
1211
github.com/schollz/progressbar/v3 v3.13.0
1312
github.com/tyler-smith/go-bip39 v1.1.0
@@ -36,7 +35,7 @@ require (
3635
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
3736
github.com/rivo/uniseg v0.4.4 // indirect
3837
github.com/stretchr/testify v1.8.2
39-
golang.org/x/crypto v0.7.0 // indirect
38+
golang.org/x/crypto v0.7.0
4039
golang.org/x/sys v0.6.0 // indirect
4140
golang.org/x/term v0.6.0 // indirect
4241
)

go.sum

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
7272
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
7373
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
7474
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
75-
github.com/holiman/uint256 v1.2.2 h1:TXKcSGc2WaxPD2+bmzAsVthL4+pEN0YwXcL5qED83vk=
76-
github.com/holiman/uint256 v1.2.2/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
7775
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
7876
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
7977
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
@@ -134,7 +132,6 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
134132
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
135133
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
136134
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
137-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
138135
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
139136
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
140137
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
@@ -186,6 +183,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
186183
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
187184
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
188185
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
186+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
189187
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
190188
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
191189
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=

0 commit comments

Comments
 (0)