Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
google.golang.org/protobuf v1.36.6
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
lukechampine.com/blake3 v1.4.0
)

require (
Expand Down Expand Up @@ -76,7 +77,6 @@ require (
github.com/cosmos/ledger-cosmos-go v0.14.0 // indirect
github.com/danieljoos/wincred v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/badger/v4 v4.2.0 // indirect
Expand All @@ -92,7 +92,6 @@ require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gofrs/flock v0.12.1 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.4 // indirect
Expand Down
94 changes: 12 additions & 82 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion p2p/DEVDOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ Data stored in the network is:

- Use `localOnly: true` with `Retrieve()` to only check local storage
- DHT operations use a modified Kademlia with `Alpha=6` for parallelism
- Key format is base58-encoded SHA-256 hash of the data
- Key format is base58-encoded Blake3 hash of the data
6 changes: 3 additions & 3 deletions p2p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Client interface {
// - the base58 encoded identifier will be returned
Store(ctx context.Context, data []byte, typ int) (string, error)

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

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

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

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

// GetLocalKeys returns a list of all keys stored locally
Expand Down
10 changes: 5 additions & 5 deletions p2p/kademlia/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (s *DHT) retryStore(ctx context.Context, key []byte, data []byte, typ int)

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

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

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

Expand Down Expand Up @@ -1172,7 +1172,7 @@ func (s *DHT) storeToAlphaNodes(ctx context.Context, nl *NodeList, data []byte,
}
}(n)
}
skey, _ := utils.Sha3256hash(data)
skey, _ := utils.Blake3Hash(data)

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

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

Expand Down
6 changes: 3 additions & 3 deletions p2p/kademlia/fetch_and_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

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

"github.com/LumeraProtocol/supernode/p2p/kademlia/domain"
"github.com/LumeraProtocol/supernode/pkg/log"
"github.com/LumeraProtocol/supernode/pkg/utils"
"github.com/LumeraProtocol/supernode/p2p/kademlia/domain"
"github.com/cenkalti/backoff/v4"
)

Expand Down Expand Up @@ -368,8 +368,8 @@ func VerifyAndFilter(decompressedMap map[string][]byte) (map[string][]byte, []st
continue
}

// Compute the SHA256 hash of the value using the helper function
hash, err := utils.Sha3256hash(value)
// Compute the Blake3 hash of the value using the helper function
hash, err := utils.Blake3Hash(value)
if err != nil {
failedKeys = append(failedKeys, key)
log.WithError(err).Error("failed to compute hash")
Expand Down
4 changes: 2 additions & 2 deletions p2p/kademlia/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (s *Network) handleFindNode(ctx context.Context, message *Message) (res []b
s.dht.addNode(ctx, message.Sender)

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

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

// format the key
key, _ := utils.Sha3256hash(request.Data)
key, _ := utils.Blake3Hash(request.Data)

value, err := s.dht.store.Retrieve(ctx, key)
if err != nil || len(value) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/kademlia/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Node struct {
// SetHashedID sets hash of ID
func (s *Node) SetHashedID() {
if len(s.HashedID) == 0 {
s.HashedID, _ = utils.Sha3256hash(s.ID)
s.HashedID, _ = utils.Blake3Hash(s.ID)
}
}

Expand Down
4 changes: 2 additions & 2 deletions p2p/kademlia/store/sqlite/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"time"

"github.com/LumeraProtocol/supernode/pkg/log"
"github.com/LumeraProtocol/supernode/p2p/kademlia/domain"
"github.com/LumeraProtocol/supernode/pkg/log"
"github.com/cenkalti/backoff/v4"
"github.com/jmoiron/sqlx"
)
Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *Store) DeleteRepKey(hkey string) error {
return nil
}

// StoreBatchRepKeys will store a batch of values with their SHA256 hash as the key
// StoreBatchRepKeys will store a batch of values with their Blake3 hash as the key
func (s *Store) StoreBatchRepKeys(values []string, id string, ip string, port uint16) error {
operation := func() error {
tx, err := s.db.Beginx()
Expand Down
8 changes: 4 additions & 4 deletions p2p/kademlia/store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"strings"
"time"

"github.com/LumeraProtocol/supernode/p2p/kademlia/store/cloud.go"
"github.com/LumeraProtocol/supernode/pkg/log"
"github.com/LumeraProtocol/supernode/pkg/utils"
"github.com/LumeraProtocol/supernode/p2p/kademlia/store/cloud.go"

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

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

Expand All @@ -565,8 +565,8 @@ func (s *Store) storeBatchRecord(values [][]byte, typ int, isOriginal bool) erro
// For each value, calculate its hash and insert into DB
now := time.Now().UTC()
for i := 0; i < len(values); i++ {
// Compute the SHA256 hash
hashed, err := utils.Sha3256hash(values[i])
// Compute the Blake3 hash
hashed, err := utils.Blake3Hash(values[i])
if err != nil {
tx.Rollback()
return fmt.Errorf("cannot compute hash: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions p2p/kademlia/store/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"testing"
"time"

"github.com/LumeraProtocol/supernode/pkg/utils"
"github.com/LumeraProtocol/supernode/p2p/kademlia/store/cloud.go"
"github.com/LumeraProtocol/supernode/pkg/utils"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -88,21 +88,21 @@ func TestStore(t *testing.T) {
r2 := []byte("test-record-2")
r3 := []byte("test-record-3")

hashed, err := utils.Sha3256hash(r1)
hashed, err := utils.Blake3Hash(r1)
if err != nil {
t.Fatalf("failed to hash record: %v", err)
}

r1Key := hex.EncodeToString(hashed)

hashed, err = utils.Sha3256hash(r2)
hashed, err = utils.Blake3Hash(r2)
if err != nil {
t.Fatalf("failed to hash record: %v", err)
}

r2Key := hex.EncodeToString(hashed)

hashed, err = utils.Sha3256hash(r3)
hashed, err = utils.Blake3Hash(r3)
if err != nil {
t.Fatalf("failed to hash record: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

const (
logPrefix = "p2p"
// B is the number of bits in a SHA256 hash
// B is the number of bits in a Blake3 hash
B = 256
)

Expand Down Expand Up @@ -122,7 +122,7 @@ func (s *p2p) Store(ctx context.Context, data []byte, typ int) (string, error) {
return s.dht.Store(ctx, data, typ)
}

// StoreBatch will store a batch of values with their SHA256 hash as the key
// StoreBatch will store a batch of values with their Blake3 hash as the key
func (s *p2p) StoreBatch(ctx context.Context, data [][]byte, typ int, taskID string) error {
ctx = log.ContextWithPrefix(ctx, logPrefix)

Expand Down Expand Up @@ -293,7 +293,7 @@ func (s *p2p) LocalStore(ctx context.Context, key string, data []byte) (string,
return s.dht.LocalStore(ctx, key, data)
}

// DisableKey adds key to disabled keys list - It takes in a B58 encoded SHA-256 hash
// DisableKey adds key to disabled keys list - It takes in a B58 encoded Blake3 hash
func (s *p2p) DisableKey(ctx context.Context, b58EncodedHash string) error {
decoded := base58.Decode(b58EncodedHash)
if len(decoded) != B/8 {
Expand All @@ -303,7 +303,7 @@ func (s *p2p) DisableKey(ctx context.Context, b58EncodedHash string) error {
return s.metaStore.Store(ctx, decoded)
}

// EnableKey removes key from disabled list - It takes in a B58 encoded SHA-256 hash
// EnableKey removes key from disabled list - It takes in a B58 encoded Blake3 hash
func (s *p2p) EnableKey(ctx context.Context, b58EncodedHash string) error {
decoded := base58.Decode(b58EncodedHash)
if len(decoded) != B/8 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/crypto/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package crypto

48 changes: 24 additions & 24 deletions pkg/net/credentials/alts/common/utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package common

import (
"crypto/sha256"
"encoding/binary"
"fmt"
"io"
"net"
"crypto/sha256"

"golang.org/x/crypto/hkdf"
"github.com/cosmos/gogoproto/proto"
"golang.org/x/crypto/hkdf"

lumeraidtypes "github.com/LumeraProtocol/lumera/x/lumeraid/types"
)
Expand All @@ -24,22 +24,22 @@ func defaultSendHandshakeMessage(conn net.Conn, handshakeBytes, signature []byte
// Calculate total message size and allocate a single buffer
totalSize := MsgLenFieldSize + len(handshakeBytes) + MsgLenFieldSize + len(signature)
buf := make([]byte, totalSize)

// Write all data into the buffer
offset := 0

// Write handshake length
binary.BigEndian.PutUint32(buf[offset:], uint32(len(handshakeBytes)))
offset += MsgLenFieldSize

// Write handshake bytes
copy(buf[offset:], handshakeBytes)
offset += len(handshakeBytes)

// Write signature length
binary.BigEndian.PutUint32(buf[offset:], uint32(len(signature)))
offset += MsgLenFieldSize

// Write signature
copy(buf[offset:], signature)

Expand Down Expand Up @@ -124,36 +124,36 @@ func defaultExpandKey(sharedSecret []byte, protocol string, info []byte) ([]byte
if err != nil {
return nil, fmt.Errorf("failed to get key size: %w", err)
}
if (keySize <= len(sharedSecret)) {
if keySize <= len(sharedSecret) {
return sharedSecret[:keySize], nil
}

// Use HKDF with SHA-256
hkdf := hkdf.New(sha256.New, sharedSecret, nil, info)
key := make([]byte, keySize)
if _, err := io.ReadFull(hkdf, key); err != nil {
return nil, fmt.Errorf("failed to expand key: %w", err)
}
// Use HKDF with SHA-256
hkdf := hkdf.New(sha256.New, sharedSecret, nil, info)

key := make([]byte, keySize)
if _, err := io.ReadFull(hkdf, key); err != nil {
return nil, fmt.Errorf("failed to expand key: %w", err)
}

return key, nil
return key, nil
}

// ExpandKey derives protocol-specific keys from the shared secret using HKDF
var ExpandKey ExpandKeyFunc = defaultExpandKey

// For XChaCha20Poly1305ReKey, helper to split the expanded key into key and nonce
func SplitKeyAndNonce(expandedKey []byte) (key, nonce []byte) {
if len(expandedKey) != KeySizeXChaCha20Poly1305ReKey {
return nil, nil
}
return expandedKey[:32], expandedKey[32:]
if len(expandedKey) != KeySizeXChaCha20Poly1305ReKey {
return nil, nil
}
return expandedKey[:32], expandedKey[32:]
}

// For AESGCMReKey, helper to split the expanded key into key and counter mask
func SplitKeyAndCounterMask(expandedKey []byte) (key, counterMask []byte) {
if len(expandedKey) != KeySizeAESGCMReKey {
return nil, nil
}
return expandedKey[:32], expandedKey[32:]
if len(expandedKey) != KeySizeAESGCMReKey {
return nil, nil
}
return expandedKey[:32], expandedKey[32:]
}
2 changes: 1 addition & 1 deletion pkg/raptorq/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func GetIDFiles(ctx context.Context, file []byte, ic uint32, max uint32) (ids []

idFiles = append(idFiles, compressedData)

hash, err := utils.Sha3256hash(compressedData)
hash, err := utils.Blake3Hash(compressedData)
if err != nil {
return ids, idFiles, errors.Errorf("sha3-256-hash error getting an id file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/healthcheck_challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ type HealthCheckChallengeMessages []HealthCheckMessage
// Hash returns the hash of the health-check-challenge challenge log data
func (mdl HealthCheckChallengeMessages) Hash() string {
data, _ := json.Marshal(mdl)
hash, _ := utils.Sha3256hash(data)
hash, _ := utils.Blake3Hash(data)

return string(hash)
}
2 changes: 1 addition & 1 deletion pkg/types/self_healing.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ type SelfHealingChallengeEvent struct {
// Hash returns the hash of the self-healing challenge reports
func (s SelfHealingReports) Hash() string {
data, _ := json.Marshal(s)
hash, _ := utils.Sha3256hash(data)
hash, _ := utils.Blake3Hash(data)

return string(hash)
}
Loading