Skip to content

Commit 6723bf0

Browse files
committed
change hashing algorithm form sha256 to blake3
1 parent 2ebc651 commit 6723bf0

File tree

24 files changed

+99
-93
lines changed

24 files changed

+99
-93
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ require (
116116
github.com/improbable-eng/grpc-web v0.15.0 // indirect
117117
github.com/inconshreveable/mousetrap v1.1.0 // indirect
118118
github.com/jmhodges/levigo v1.0.0 // indirect
119+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
119120
github.com/kr/pretty v0.3.1 // indirect
120121
github.com/kr/text v0.2.0 // indirect
121122
github.com/linxGnu/grocksdb v1.8.14 // indirect
@@ -168,6 +169,7 @@ require (
168169
google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a // indirect
169170
gopkg.in/ini.v1 v1.67.0 // indirect
170171
gotest.tools/v3 v3.5.1 // indirect
172+
lukechampine.com/blake3 v1.4.0 // indirect
171173
lukechampine.com/uint128 v1.3.0 // indirect
172174
nhooyr.io/websocket v1.8.6 // indirect
173175
pgregory.net/rapid v1.1.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ github.com/klauspost/compress v1.11.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYs
517517
github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
518518
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
519519
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
520+
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
521+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
520522
github.com/kolesa-team/go-webp v1.0.4 h1:wQvU4PLG/X7RS0vAeyhiivhLRoxfLVRlDq4I3frdxIQ=
521523
github.com/kolesa-team/go-webp v1.0.4/go.mod h1:oMvdivD6K+Q5qIIkVC2w4k2ZUnI1H+MyP7inwgWq9aA=
522524
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -1130,6 +1132,8 @@ honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWh
11301132
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
11311133
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
11321134
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
1135+
lukechampine.com/blake3 v1.4.0 h1:xDbKOZCVbnZsfzM6mHSYcGRHZ3YrLDzqz8XnV4uaD5w=
1136+
lukechampine.com/blake3 v1.4.0/go.mod h1:MQJNQCTnR+kwOP/JEZSxj3MaQjp80FOFSNMMHXcSeX0=
11331137
lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=
11341138
lukechampine.com/uint128 v1.3.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
11351139
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=

p2p/DEVDOCS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ Data stored in the network is:
132132

133133
- Use `localOnly: true` with `Retrieve()` to only check local storage
134134
- DHT operations use a modified Kademlia with `Alpha=6` for parallelism
135-
- Key format is base58-encoded SHA-256 hash of the data
135+
- Key format is base58-encoded Blake3 hash of the data

p2p/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Client interface {
2222
// - the base58 encoded identifier will be returned
2323
Store(ctx context.Context, data []byte, typ int) (string, error)
2424

25-
// StoreBatch will store a batch of values with their SHA256 hash as the key
25+
// StoreBatch will store a batch of values with their Blake3 hash as the key
2626
StoreBatch(ctx context.Context, values [][]byte, typ int, taskID string) error
2727

2828
// Delete a key, value
@@ -41,10 +41,10 @@ type Client interface {
4141
// - the base58 encoded identifier will be returned
4242
LocalStore(ctx context.Context, key string, data []byte) (string, error)
4343

44-
// DisableKey adds key to disabled keys list - It takes in a B58 encoded SHA-256 hash
44+
// DisableKey adds key to disabled keys list - It takes in a B58 encoded blake3 hash
4545
DisableKey(ctx context.Context, b58EncodedHash string) error
4646

47-
// EnableKey removes key from disabled list - It takes in a B58 encoded SHA-256 hash
47+
// EnableKey removes key from disabled list - It takes in a B58 encoded blake3 hash
4848
EnableKey(ctx context.Context, b58EncodedHash string) error
4949

5050
// GetLocalKeys returns a list of all keys stored locally

p2p/kademlia/dht.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (s *DHT) retryStore(ctx context.Context, key []byte, data []byte, typ int)
207207

208208
// Store the data into the network
209209
func (s *DHT) Store(ctx context.Context, data []byte, typ int) (string, error) {
210-
key, _ := utils.Sha3256hash(data)
210+
key, _ := utils.Blake3Hash(data)
211211

212212
retKey := base58.Encode(key)
213213
// store the key to queries storage
@@ -224,7 +224,7 @@ func (s *DHT) Store(ctx context.Context, data []byte, typ int) (string, error) {
224224
return retKey, nil
225225
}
226226

227-
// StoreBatch will store a batch of values with their SHA256 hash as the key
227+
// StoreBatch will store a batch of values with their Blake3 hash as the key
228228
func (s *DHT) StoreBatch(ctx context.Context, values [][]byte, typ int, taskID string) error {
229229
log.WithContext(ctx).WithField("taskID", taskID).WithField("records", len(values)).Info("store db batch begin")
230230
if err := s.store.StoreBatch(ctx, values, typ, true); err != nil {
@@ -823,7 +823,7 @@ func (s *DHT) iterate(ctx context.Context, iterativeType int, target []byte, dat
823823
closestNode := nl.Nodes[0]
824824
// if it's a find node, reset the refresh timer
825825
if iterativeType == IterateFindNode {
826-
hashedTargetID, _ := utils.Sha3256hash(target)
826+
hashedTargetID, _ := utils.Blake3Hash(target)
827827
bucket := s.ht.bucketIndex(s.ht.self.HashedID, hashedTargetID)
828828
log.P2P().WithContext(ctx).Debugf("bucket for target: %v", sKey)
829829

@@ -1173,7 +1173,7 @@ func (s *DHT) storeToAlphaNodes(ctx context.Context, nl *NodeList, data []byte,
11731173
}
11741174
}(n)
11751175
}
1176-
skey, _ := utils.Sha3256hash(data)
1176+
skey, _ := utils.Blake3Hash(data)
11771177

11781178
// Collect results from parallel requests
11791179
for i := 0; i < Alpha && i < len(nl.Nodes); i++ {
@@ -1259,7 +1259,7 @@ func (s *DHT) IterateBatchStore(ctx context.Context, values [][]byte, typ int, i
12591259

12601260
log.WithContext(ctx).WithField("task-id", id).WithField("keys", len(values)).Info("iterate batch store begin")
12611261
for i := 0; i < len(values); i++ {
1262-
target, _ := utils.Sha3256hash(values[i])
1262+
target, _ := utils.Blake3Hash(values[i])
12631263
hashes[i] = target
12641264
top6 := s.ht.closestContactsWithInlcudingNode(Alpha, target, s.ignorelist.ToNodeList(), nil)
12651265

p2p/kademlia/fetch_and_store.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99

1010
json "github.com/json-iterator/go"
1111

12+
"github.com/LumeraProtocol/supernode/p2p/kademlia/domain"
1213
"github.com/LumeraProtocol/supernode/pkg/log"
1314
"github.com/LumeraProtocol/supernode/pkg/utils"
14-
"github.com/LumeraProtocol/supernode/p2p/kademlia/domain"
1515
"github.com/cenkalti/backoff/v4"
1616
)
1717

@@ -368,8 +368,8 @@ func VerifyAndFilter(decompressedMap map[string][]byte) (map[string][]byte, []st
368368
continue
369369
}
370370

371-
// Compute the SHA256 hash of the value using the helper function
372-
hash, err := utils.Sha3256hash(value)
371+
// Compute the Blake3 hash of the value using the helper function
372+
hash, err := utils.Blake3Hash(value)
373373
if err != nil {
374374
failedKeys = append(failedKeys, key)
375375
log.WithError(err).Error("failed to compute hash")

p2p/kademlia/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (s *Network) handleFindNode(ctx context.Context, message *Message) (res []b
143143
s.dht.addNode(ctx, message.Sender)
144144

145145
// the closest contacts
146-
hashedTargetID, _ := utils.Sha3256hash(request.Target)
146+
hashedTargetID, _ := utils.Blake3Hash(request.Target)
147147
closest, _ := s.dht.ht.closestContacts(K, hashedTargetID, []*Node{message.Sender})
148148

149149
response := &FindNodeResponse{
@@ -232,7 +232,7 @@ func (s *Network) handleStoreData(ctx context.Context, message *Message) (res []
232232
s.dht.addNode(ctx, message.Sender)
233233

234234
// format the key
235-
key, _ := utils.Sha3256hash(request.Data)
235+
key, _ := utils.Blake3Hash(request.Data)
236236

237237
value, err := s.dht.store.Retrieve(ctx, key)
238238
if err != nil || len(value) == 0 {

p2p/kademlia/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Node struct {
2929
// SetHashedID sets hash of ID
3030
func (s *Node) SetHashedID() {
3131
if len(s.HashedID) == 0 {
32-
s.HashedID, _ = utils.Sha3256hash(s.ID)
32+
s.HashedID, _ = utils.Blake3Hash(s.ID)
3333
}
3434
}
3535

p2p/kademlia/store/sqlite/replication.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/LumeraProtocol/supernode/pkg/log"
1110
"github.com/LumeraProtocol/supernode/p2p/kademlia/domain"
11+
"github.com/LumeraProtocol/supernode/pkg/log"
1212
"github.com/cenkalti/backoff/v4"
1313
"github.com/jmoiron/sqlx"
1414
)
@@ -185,7 +185,7 @@ func (s *Store) DeleteRepKey(hkey string) error {
185185
return nil
186186
}
187187

188-
// StoreBatchRepKeys will store a batch of values with their SHA256 hash as the key
188+
// StoreBatchRepKeys will store a batch of values with their Blake3 hash as the key
189189
func (s *Store) StoreBatchRepKeys(values []string, id string, ip string, port uint16) error {
190190
operation := func() error {
191191
tx, err := s.db.Beginx()

p2p/kademlia/store/sqlite/sqlite.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/LumeraProtocol/supernode/p2p/kademlia/store/cloud.go"
1314
"github.com/LumeraProtocol/supernode/pkg/log"
1415
"github.com/LumeraProtocol/supernode/pkg/utils"
15-
"github.com/LumeraProtocol/supernode/p2p/kademlia/store/cloud.go"
1616

1717
"github.com/cenkalti/backoff/v4"
1818
"github.com/jmoiron/sqlx"
@@ -542,7 +542,7 @@ func (s *Store) storeRecord(key []byte, value []byte, typ int, isOriginal bool)
542542
return nil
543543
}
544544

545-
// storeBatchRecord will store a batch of values with their SHA256 hash as the key
545+
// storeBatchRecord will store a batch of values with their Blake3 hash as the key
546546
func (s *Store) storeBatchRecord(values [][]byte, typ int, isOriginal bool) error {
547547
hkeys := make([]UpdateMessage, len(values))
548548

@@ -565,8 +565,8 @@ func (s *Store) storeBatchRecord(values [][]byte, typ int, isOriginal bool) erro
565565
// For each value, calculate its hash and insert into DB
566566
now := time.Now().UTC()
567567
for i := 0; i < len(values); i++ {
568-
// Compute the SHA256 hash
569-
hashed, err := utils.Sha3256hash(values[i])
568+
// Compute the Blake3 hash
569+
hashed, err := utils.Blake3Hash(values[i])
570570
if err != nil {
571571
tx.Rollback()
572572
return fmt.Errorf("cannot compute hash: %w", err)

0 commit comments

Comments
 (0)