Skip to content

Commit

Permalink
Added claim signature message
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Apr 24, 2024
1 parent 147d82d commit 36c20d2
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pkg/tecdsa/inactivity/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inactivity

import (
"crypto/ecdsa"
"fmt"
"math/big"

"github.com/keep-network/keep-core/pkg/protocol/group"
Expand All @@ -20,3 +21,19 @@ const ClaimSignatureHashByteSize = 32
// ClaimSignatureHash is a signature hash of the inactivity claim. The hashing
// algorithm used depends on the client code.
type ClaimSignatureHash [ClaimSignatureHashByteSize]byte

// ClaimSignatureHashFromBytes converts bytes slice to ClaimSignatureHash.
// It requires provided bytes slice size to be exactly
// ClaimSignatureHashByteSize.
func ClaimSignatureHashFromBytes(bytes []byte) (ClaimSignatureHash, error) {
var hash ClaimSignatureHash

if len(bytes) != ClaimSignatureHashByteSize {
return hash, fmt.Errorf(
"bytes length is not equal %v", ClaimSignatureHashByteSize,
)
}
copy(hash[:], bytes[:])

return hash, nil
}
185 changes: 185 additions & 0 deletions pkg/tecdsa/inactivity/gen/pb/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/tecdsa/inactivity/gen/pb/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

option go_package = "./pb";
package inactivity;

message ClaimSignatureMessage {
uint32 senderID = 1;
bytes claimHash = 2;
bytes signature = 3;
bytes publicKey = 4;
string sessionID = 5;
}
48 changes: 45 additions & 3 deletions pkg/tecdsa/inactivity/marshalling.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
package inactivity

import (
"fmt"

"google.golang.org/protobuf/proto"

"github.com/keep-network/keep-core/pkg/protocol/group"
"github.com/keep-network/keep-core/pkg/tecdsa/inactivity/gen/pb"
)

func validateMemberIndex(protoIndex uint32) error {
// Protobuf does not have uint8 type, so we are using uint32. When
// unmarshalling message, we need to make sure we do not overflow.
if protoIndex > group.MaxMemberIndex {
return fmt.Errorf("invalid member index value: [%v]", protoIndex)
}
return nil
}

// Marshal converts this claimSignatureMessage to a byte array suitable
// for network communication.
func (csm *claimSignatureMessage) Marshal() ([]byte, error) {
// TODO: Implement
return nil, nil
return proto.Marshal(&pb.ClaimSignatureMessage{
SenderID: uint32(csm.senderID),
ClaimHash: csm.claimHash[:],
Signature: csm.signature,
PublicKey: csm.publicKey,
SessionID: csm.sessionID,
})
}

// Unmarshal converts a byte array produced by Marshal to a
// claimSignatureMessage.
func (csm *claimSignatureMessage) Unmarshal(bytes []byte) error {
// TODO: Implement
pbMsg := pb.ClaimSignatureMessage{}
if err := proto.Unmarshal(bytes, &pbMsg); err != nil {
return err
}

if err := validateMemberIndex(pbMsg.SenderID); err != nil {
return err
}
csm.senderID = group.MemberIndex(pbMsg.SenderID)

claimHash, err := ClaimSignatureHashFromBytes(pbMsg.ClaimHash)
if err != nil {
return err
}
csm.claimHash = claimHash

csm.signature = pbMsg.Signature
csm.publicKey = pbMsg.PublicKey
csm.sessionID = pbMsg.SessionID

return nil
}
65 changes: 65 additions & 0 deletions pkg/tecdsa/inactivity/marshalling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package inactivity

import (
"reflect"
"testing"

fuzz "github.com/google/gofuzz"

"github.com/keep-network/keep-core/pkg/internal/pbutils"
"github.com/keep-network/keep-core/pkg/protocol/group"
)

func TestClaimSignatureMessage_MarshalingRoundtrip(t *testing.T) {
msg := &claimSignatureMessage{
senderID: 123,
claimHash: [32]byte{0: 11, 10: 22, 31: 33},
signature: []byte("signature"),
publicKey: []byte("pubkey"),
sessionID: "session-1",
}
unmarshaled := &claimSignatureMessage{}

err := pbutils.RoundTrip(msg, unmarshaled)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(msg, unmarshaled) {
t.Fatalf("unexpected content of unmarshaled message")
}
}

func TestFuzzClaimSignatureMessage_MarshalingRoundtrip(t *testing.T) {
for i := 0; i < 10; i++ {
var (
senderID group.MemberIndex
claimHash ClaimSignatureHash
signature []byte
publicKey []byte
sessionID string
)

f := fuzz.New().NilChance(0.1).NumElements(0, 512)

f.Fuzz(&senderID)
f.Fuzz(&claimHash)
f.Fuzz(&signature)
f.Fuzz(&publicKey)
f.Fuzz(&sessionID)

message := &claimSignatureMessage{
senderID: senderID,
claimHash: claimHash,
signature: signature,
publicKey: publicKey,
sessionID: sessionID,
}

_ = pbutils.RoundTrip(message, &claimSignatureMessage{})
}
}

func TestFuzzClaimSignatureMessage_Unmarshaler(t *testing.T) {
pbutils.FuzzUnmarshaler(&claimSignatureMessage{})
}

0 comments on commit 36c20d2

Please sign in to comment.