Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

Commit

Permalink
Possible fix to SecretKeyShare.UnmarshalBinary (#52)
Browse files Browse the repository at this point in the history
* add test

* fix #51

* add test to lib_test

* remove test/unmarshal_test.go
  • Loading branch information
dB2510 committed Mar 16, 2022
1 parent af6ab33 commit 269410e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions pkg/signatures/bls/bls_sig/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (sks *SecretKeyShare) UnmarshalBinary(data []byte) error {
}
l := len(data)
sks.identifier = data[l-1]
sks.value = make([]byte, SecretKeySize)
copy(sks.value, data[:l])
return nil
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/signatures/bls/bls_sig/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,37 @@ func TestThresholdizeSecretKeyCountsCorrect(t *testing.T) {
}
}
}

func TestSecretKeyShareUnmarshalBinary(t *testing.T) {
sk := genSecretKey(t)
sks, err := thresholdizeSecretKey(sk, 3, 5)
if err != nil {
t.Errorf("Expected thresholdizeSecretKey to pass but failed.")
}

for i, sh := range sks {
b1, err := sh.MarshalBinary()
if err != nil {
t.Errorf("%d - expected MarshalBinary to pass but failed. sh=%v", i, sh)
}

// UnmarshalBinary b1 to new SecretKeyShare
sh1 := new(SecretKeyShare)
err = sh1.UnmarshalBinary(b1)
if err != nil {
t.Errorf("%d - expected UnmarshalBinary to pass but failed. sh=%v", i, sh)
}

// zero bytes slice with length equal to SecretKeySize
zeros := make([]byte, SecretKeySize)
b2, err := sh1.MarshalBinary()
if err != nil {
t.Errorf("%d - expected MarshalBinary to pass but failed. sh1=%v", i, sh1)
}

// Check if []bytes from UnmarshalBinary != zeros && Initial bytes(b1) == Final bytes(b2)
if bytes.Equal(zeros, b2) && !bytes.Equal(b1, b2) {
t.Errorf("%d - expected UnmarshalBinary to give non zeros value but failed. sh1=%v, sh=%v", i, sh1, sh)
}
}
}
28 changes: 0 additions & 28 deletions test/unmarshal_test.go

This file was deleted.

0 comments on commit 269410e

Please sign in to comment.