Skip to content

Commit a8d4de7

Browse files
committed
crypto/sha3 in vendor
1 parent 84c3690 commit a8d4de7

121 files changed

Lines changed: 25085 additions & 1132 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

accounts/accounts.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
"fmt"
2222
"math/big"
2323

24-
"golang.org/x/crypto/sha3"
25-
2624
cortex "github.com/CortexFoundation/CortexTheseus"
2725
"github.com/CortexFoundation/CortexTheseus/common"
2826
"github.com/CortexFoundation/CortexTheseus/core/types"
27+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
2928
"github.com/CortexFoundation/CortexTheseus/event"
3029
)
3130

@@ -195,7 +194,7 @@ func TextHash(data []byte) []byte {
195194
// This gives context to the signed message and prevents signing of transactions.
196195
func TextAndHash(data []byte) ([]byte, string) {
197196
msg := fmt.Sprintf("\x19Cortex Signed Message:\n%d%s", len(data), data)
198-
hasher := sha3.NewLegacyKeccak256()
197+
hasher := keccak.NewLegacyKeccak256()
199198
hasher.Write([]byte(msg))
200199
return hasher.Sum(nil), msg
201200
}

common/types.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import (
2828
"strconv"
2929
"strings"
3030

31-
"golang.org/x/crypto/sha3"
32-
3331
"github.com/CortexFoundation/CortexTheseus/common/hexutil"
32+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
3433
)
3534

3635
// Lengths of hashes and addresses in bytes.
@@ -268,7 +267,7 @@ func (a *Address) checksumHex() []byte {
268267
buf := a.hex()
269268

270269
// compute checksum
271-
sha := sha3.NewLegacyKeccak256()
270+
sha := keccak.NewLegacyKeccak256()
272271
sha.Write(buf[2:])
273272
hash := sha.Sum(nil)
274273
for i := 2; i < len(buf); i++ {

consensus/clique/clique.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"time"
2929

3030
lru "github.com/hashicorp/golang-lru"
31-
"golang.org/x/crypto/sha3"
3231

3332
"github.com/CortexFoundation/CortexTheseus/accounts"
3433
"github.com/CortexFoundation/CortexTheseus/common"
@@ -38,6 +37,7 @@ import (
3837
"github.com/CortexFoundation/CortexTheseus/core/state"
3938
"github.com/CortexFoundation/CortexTheseus/core/types"
4039
"github.com/CortexFoundation/CortexTheseus/crypto"
40+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
4141
"github.com/CortexFoundation/CortexTheseus/ctxcdb"
4242
"github.com/CortexFoundation/CortexTheseus/log"
4343
"github.com/CortexFoundation/CortexTheseus/params"
@@ -155,7 +155,7 @@ type SignerFn func(signer accounts.Account, mimeType string, message []byte) ([]
155155
// Note, the method requires the extra data to be at least 65 bytes, otherwise it
156156
// panics. This is done to avoid accidentally using both forms (signature present
157157
// or not), which could be abused to produce different hashes for the same header.
158-
func SealHash(header *types.Header) (hash common.Hash) {
158+
/*func SealHash(header *types.Header) (hash common.Hash) {
159159
hasher := sha3.NewLegacyKeccak256()
160160
161161
rlp.Encode(hasher, []any{
@@ -178,7 +178,7 @@ func SealHash(header *types.Header) (hash common.Hash) {
178178
//hasher.Sum(hash[:0])
179179
hasher.(crypto.KeccakState).Read(hash[:])
180180
return hash
181-
}
181+
}*/
182182

183183
// ecrecover extracts the Cortex account address from a signed header.
184184
func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, error) {
@@ -730,6 +730,21 @@ func calcDifficulty(snap *Snapshot, signer common.Address) *big.Int {
730730
return new(big.Int).Set(diffNoTurn)
731731
}
732732

733+
// SealHash returns the hash of a block prior to it being sealed.
734+
func SealHash(header *types.Header) (hash common.Hash) {
735+
hasher := keccak.NewLegacyKeccak256()
736+
encodeSigHeader(hasher, header)
737+
hasher.(crypto.KeccakState).Read(hash[:])
738+
return hash
739+
}
740+
741+
// CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority
742+
// sealing. The RLP to sign consists of the entire header apart from the 65 byte signature
743+
// contained at the end of the extra data.
744+
//
745+
// Note, the method requires the extra data to be at least 65 bytes, otherwise it
746+
// panics. This is done to avoid accidentally using both forms (signature present
747+
// or not), which could be abused to produce different hashes for the same header.
733748
func CliqueRLP(header *types.Header) []byte {
734749
b := new(bytes.Buffer)
735750
encodeSigHeader(b, header)

consensus/cuckoo/consensus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"time"
2929

3030
mapset "github.com/deckarep/golang-set/v2"
31-
"golang.org/x/crypto/sha3"
3231

3332
// "strconv"
3433
// "strings"
@@ -41,6 +40,7 @@ import (
4140
"github.com/CortexFoundation/CortexTheseus/core/state"
4241
"github.com/CortexFoundation/CortexTheseus/core/types"
4342
"github.com/CortexFoundation/CortexTheseus/crypto"
43+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
4444
"github.com/CortexFoundation/CortexTheseus/log"
4545
"github.com/CortexFoundation/CortexTheseus/params"
4646
"github.com/CortexFoundation/CortexTheseus/rlp"
@@ -762,7 +762,7 @@ func (cuckoo *Cuckoo) FinalizeWithoutParent(chain consensus.ChainHeaderReader, h
762762

763763
// SealHash returns the hash of a block prior to it being sealed.
764764
func (cuckoo *Cuckoo) SealHash(header *types.Header) (hash common.Hash) {
765-
hasher := sha3.NewLegacyKeccak256()
765+
hasher := keccak.NewLegacyKeccak256()
766766

767767
rlp.Encode(hasher, []any{
768768
header.ParentHash,

core/rawdb/accessors_chain_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import (
2626

2727
"github.com/CortexFoundation/CortexTheseus/common"
2828
"github.com/CortexFoundation/CortexTheseus/core/types"
29+
"github.com/CortexFoundation/CortexTheseus/crypto"
30+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
2931
"github.com/CortexFoundation/CortexTheseus/params"
3032
"github.com/CortexFoundation/CortexTheseus/rlp"
31-
"golang.org/x/crypto/sha3"
3233
)
3334

3435
// Tests block header storage and retrieval operations.
@@ -50,10 +51,7 @@ func TestHeaderStorage(t *testing.T) {
5051
if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
5152
t.Fatalf("Stored header RLP not found")
5253
} else {
53-
hasher := sha3.NewLegacyKeccak256()
54-
hasher.Write(entry)
55-
56-
if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
54+
if hash := crypto.Keccak256Hash(entry); hash != header.Hash() {
5755
t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
5856
}
5957
}
@@ -70,8 +68,7 @@ func TestBodyStorage(t *testing.T) {
7068

7169
// Create a test body to move around the database and make sure it's really new
7270
body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
73-
74-
hasher := sha3.NewLegacyKeccak256()
71+
hasher := keccak.NewLegacyKeccak256()
7572
rlp.Encode(hasher, body)
7673
hash := common.BytesToHash(hasher.Sum(nil))
7774

@@ -88,10 +85,7 @@ func TestBodyStorage(t *testing.T) {
8885
if entry := ReadBodyRLP(db, hash, 0); entry == nil {
8986
t.Fatalf("Stored body RLP not found")
9087
} else {
91-
hasher := sha3.NewLegacyKeccak256()
92-
hasher.Write(entry)
93-
94-
if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
88+
if calc := crypto.Keccak256Hash(entry); calc != hash {
9589
t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
9690
}
9791
}

core/rawdb/accessors_indexes_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323

2424
"github.com/CortexFoundation/CortexTheseus/common"
2525
"github.com/CortexFoundation/CortexTheseus/core/types"
26+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
2627
"github.com/CortexFoundation/CortexTheseus/ctxcdb"
2728
"github.com/CortexFoundation/CortexTheseus/rlp"
28-
29-
"golang.org/x/crypto/sha3"
3029
)
3130

3231
// testHasher is the helper tool for transaction/receipt list hashing.
@@ -37,7 +36,7 @@ type testHasher struct {
3736
}
3837

3938
func newHasher() *testHasher {
40-
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
39+
return &testHasher{hasher: keccak.NewLegacyKeccak256()}
4140
}
4241

4342
func (h *testHasher) Reset() {

core/types/block_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
"github.com/CortexFoundation/CortexTheseus/common"
2929
"github.com/CortexFoundation/CortexTheseus/common/math"
3030
"github.com/CortexFoundation/CortexTheseus/crypto"
31+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
3132
"github.com/CortexFoundation/CortexTheseus/params"
3233
"github.com/CortexFoundation/CortexTheseus/rlp"
33-
"golang.org/x/crypto/sha3"
3434
)
3535

3636
func TestBlockEncoding(t *testing.T) {
@@ -152,7 +152,7 @@ type testHasher struct {
152152
}
153153

154154
func newHasher() *testHasher {
155-
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
155+
return &testHasher{hasher: keccak.NewLegacyKeccak256()}
156156
}
157157

158158
func (h *testHasher) Reset() {

crypto/keccak.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ import (
2222
"sync"
2323

2424
"github.com/CortexFoundation/CortexTheseus/common"
25-
"golang.org/x/crypto/sha3"
25+
"github.com/CortexFoundation/CortexTheseus/crypto/keccak"
2626
)
2727

2828
// NewKeccakState creates a new KeccakState
2929
func NewKeccakState() KeccakState {
30-
return sha3.NewLegacyKeccak256().(KeccakState)
30+
return keccak.NewLegacyKeccak256().(KeccakState)
3131
}
3232

3333
var hasherPool = sync.Pool{
3434
New: func() any {
35-
return sha3.NewLegacyKeccak256().(KeccakState)
35+
return keccak.NewLegacyKeccak256().(KeccakState)
3636
},
3737
}
3838

crypto/keccak/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2009 The Go Authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

crypto/keccak/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This is a vendored and modified copy of golang.org/x/crypto/sha3, with an assembly
2+
implementation of keccak256. We wish to retain the assembly implementation,
3+
which was removed in v0.44.0.
4+
5+
Ethereum uses a 'legacy' variant of Keccak, which was defined before it became SHA3. As
6+
such, we cannot use the standard library crypto/sha3 package.

0 commit comments

Comments
 (0)