Skip to content

Commit 2b48a64

Browse files
authored
Merge pull request #6528 from oasisprotocol/peternose/trivial/migrate-to-crypto-sha3
go: Replace golang.org/x/crypto/sha3 with crypto/sha3
2 parents 904bd72 + 142a900 commit 2b48a64

7 files changed

Lines changed: 43 additions & 49 deletions

File tree

.changelog/6528.trivial.md

Whitespace-only changes.

go/common/crypto/tuplehash/tuplehash.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
package tuplehash
33

44
import (
5+
"crypto/sha3"
56
"encoding/binary"
67
"math"
78
"math/bits"
8-
9-
"golang.org/x/crypto/sha3"
109
)
1110

1211
var constN = []byte("TupleHash")
1312

1413
// Hasher is a TupleHash instance.
1514
type Hasher struct {
16-
cShake sha3.ShakeHash
15+
cShake *sha3.SHAKE
1716
outputSize uint64
1817
}
1918

@@ -33,24 +32,14 @@ func (h *Hasher) Write(b []byte) (int, error) {
3332
}
3433

3534
// Sum appends the current hash to b and returns the resulting slice.
36-
// It does not change the underlying hash state.
35+
// The underlying hash state is changed.
3736
func (h *Hasher) Sum(b []byte) []byte {
38-
cShake := h.cShake.Clone()
39-
40-
_, _ = cShake.Write(rightEncode(h.outputSize * 8)) // in bits
37+
_, _ = h.cShake.Write(rightEncode(h.outputSize * 8)) // in bits
4138
digest := make([]byte, int(h.outputSize))
42-
_, _ = cShake.Read(digest)
39+
_, _ = h.cShake.Read(digest)
4340
return append(b, digest...)
4441
}
4542

46-
// Clone creates a copy of an existing TupleHash instance.
47-
func (h *Hasher) Clone() *Hasher {
48-
return &Hasher{
49-
cShake: h.cShake.Clone(),
50-
outputSize: h.outputSize,
51-
}
52-
}
53-
5443
// New128 creates a new TupleHash128 instance with the specified output size
5544
// (in bytes) and customization string.
5645
func New128(outputSize int, customizationString []byte) *Hasher {
@@ -70,12 +59,12 @@ func doNew(securityStrength, outputSize int, customizationString []byte) *Hasher
7059
panic("common/crypto/tuplehash: invalid output size")
7160
}
7261

73-
var cShake sha3.ShakeHash
62+
var cShake *sha3.SHAKE
7463
switch securityStrength {
7564
case 128:
76-
cShake = sha3.NewCShake128(constN, customizationString)
65+
cShake = sha3.NewCSHAKE128(constN, customizationString)
7766
case 256:
78-
cShake = sha3.NewCShake256(constN, customizationString)
67+
cShake = sha3.NewCSHAKE256(constN, customizationString)
7968
default:
8069
panic("common/crypto/tuplehash: invalid security strength")
8170
}

go/consensus/cometbft/apps/beacon/beacon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
package beacon
44

55
import (
6+
"crypto/sha3"
67
"encoding/binary"
78
"fmt"
89

910
"github.com/cometbft/cometbft/abci/types"
10-
"golang.org/x/crypto/sha3"
1111

1212
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
1313
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"

go/consensus/cometbft/apps/keymanager/secrets/status.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package secrets
22

33
import (
44
"bytes"
5+
"crypto/sha3"
56
"fmt"
67
"time"
78

8-
"golang.org/x/crypto/sha3"
9-
109
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
1110
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1211
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"

go/consensus/cometbft/apps/keymanager/secrets/status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package secrets
22

33
import (
4+
"crypto/sha3"
45
"testing"
56

67
"github.com/stretchr/testify/require"
7-
"golang.org/x/crypto/sha3"
88

99
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
1010
"github.com/oasisprotocol/oasis-core/go/common"

go/consensus/cometbft/apps/scheduler/shuffle.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ func shuffleValidators(
6666
"num_proofs", len(vrf.Pi),
6767
)
6868

69-
baseHasher := newBetaHasher(
70-
[]byte("oasis-core:vrf/validator"),
71-
tmBeacon.MustGetChainContext(ctx),
72-
epoch,
73-
)
69+
baseHasher := func() *tuplehash.Hasher {
70+
return newBetaHasher(
71+
[]byte("oasis-core:vrf/validator"),
72+
tmBeacon.MustGetChainContext(ctx),
73+
epoch,
74+
)
75+
}
7476

7577
// Do the cryptographic sortition.
7678
ret := sortNodesByHashedBeta(
@@ -426,13 +428,15 @@ func electCommitteeMembers( //nolint: gocyclo
426428
idxs = rng.Perm(nrNodes)
427429
case true:
428430
// Use the VRF proofs to do the elections.
429-
baseHasher := newCommitteeBetaHasher(
430-
tmBeacon.MustGetChainContext(ctx),
431-
epoch,
432-
rt.ID,
433-
kind,
434-
role,
435-
)
431+
baseHasher := func() *tuplehash.Hasher {
432+
return newCommitteeBetaHasher(
433+
tmBeacon.MustGetChainContext(ctx),
434+
epoch,
435+
rt.ID,
436+
kind,
437+
role,
438+
)
439+
}
436440

437441
idxs = committeeVRFBetaIndexes(
438442
vrf,
@@ -521,7 +525,7 @@ func electCommitteeMembers( //nolint: gocyclo
521525

522526
func committeeVRFBetaIndexes(
523527
vrf *beacon.PrevVRFState,
524-
baseHasher *tuplehash.Hasher,
528+
baseHasher func() *tuplehash.Hasher,
525529
nodes []*node.Node,
526530
) []int {
527531
indexByNode := make(map[signature.PublicKey]int)
@@ -545,7 +549,7 @@ func committeeVRFBetaIndexes(
545549

546550
func sortNodesByHashedBeta(
547551
vrf *beacon.PrevVRFState,
548-
baseHasher *tuplehash.Hasher,
552+
baseHasher func() *tuplehash.Hasher,
549553
nodes []*node.Node,
550554
) []*node.Node {
551555
// Accumulate the hashed betas.
@@ -584,12 +588,12 @@ func sortNodesByHashedBeta(
584588
type hashedBeta [32]byte
585589

586590
func hashBeta(
587-
h *tuplehash.Hasher,
591+
baseHasher func() *tuplehash.Hasher,
588592
beta []byte,
589593
) hashedBeta {
590-
hh := h.Clone()
591-
_, _ = hh.Write(beta)
592-
digest := hh.Sum(nil)
594+
h := baseHasher()
595+
_, _ = h.Write(beta)
596+
digest := h.Sum(nil)
593597

594598
var ret hashedBeta
595599
copy(ret[:], digest)
@@ -658,13 +662,15 @@ func dedupEntityNodesByHashedBeta(
658662
return nodes
659663
}
660664

661-
baseHasher := newCommitteeDedupBetaHasher(
662-
chainContext,
663-
epoch,
664-
runtimeID,
665-
kind,
666-
role,
667-
)
665+
baseHasher := func() *tuplehash.Hasher {
666+
return newCommitteeDedupBetaHasher(
667+
chainContext,
668+
epoch,
669+
runtimeID,
670+
kind,
671+
role,
672+
)
673+
}
668674

669675
// Do the cryptographic sortition.
670676
shuffled := sortNodesByHashedBeta(

go/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ require (
5757
github.com/tidwall/btree v1.6.0
5858
github.com/tyler-smith/go-bip39 v1.1.0
5959
go.uber.org/zap v1.27.0
60-
golang.org/x/crypto v0.49.0
6160
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476
6261
golang.org/x/net v0.52.0
6362
golang.org/x/sync v0.20.0
@@ -206,6 +205,7 @@ require (
206205
go.uber.org/fx v1.24.0 // indirect
207206
go.uber.org/mock v0.5.2 // indirect
208207
go.uber.org/multierr v1.11.0 // indirect
208+
golang.org/x/crypto v0.49.0 // indirect
209209
golang.org/x/mod v0.33.0 // indirect
210210
golang.org/x/sys v0.42.0 // indirect
211211
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4 // indirect

0 commit comments

Comments
 (0)