1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
package rsa
import (
"crypto/rand"
"crypto/rsa"
"math/big"
"testing"
)
func marshalTestSignShare(share SignShare, t *testing.T) {
marshall, err := share.MarshalBinary()
if err != nil {
t.Fatal(err)
}
share2 := SignShare{}
err = share2.UnmarshalBinary(marshall)
if err != nil {
t.Fatal(err)
}
if share.Players != share2.Players {
t.Fatalf("Players did not match, expected %d, found %d", share.Players, share2.Players)
}
if share.Threshold != share2.Threshold {
t.Fatalf("Threshold did not match, expected %d, found %d", share.Threshold, share2.Threshold)
}
if share.Index != share2.Index {
t.Fatalf("Index did not match, expected %d, found %d", share.Index, share2.Index)
}
if share.xi.Cmp(share2.xi) != 0 {
t.Fatalf("si did not match, expected %v, found %v", share.xi.Bytes(), share2.xi.Bytes())
}
}
func unmarshalSignShareTest(t *testing.T, input []byte) {
share := SignShare{}
err := share.UnmarshalBinary(input)
if err == nil {
t.Fatalf("unmarshall succeeded when it shouldn't have")
}
}
func TestMarshallSignShare(t *testing.T) {
marshalTestSignShare(SignShare{
xi: big.NewInt(10),
Index: 30,
Players: 16,
Threshold: 18,
}, t)
marshalTestSignShare(SignShare{
xi: big.NewInt(0),
Index: 0,
Players: 0,
Threshold: 0,
}, t)
unmarshalSignShareTest(t, []byte{})
unmarshalSignShareTest(t, []byte{0, 0, 0})
unmarshalSignShareTest(t, []byte{0, 0, 0, 0, 0, 0, 0, 0})
unmarshalSignShareTest(t, []byte{0, 0, 0, 0, 0, 0, 0, 1})
unmarshalSignShareTest(t, []byte{0, 0, 0, 0, 0, 0, 0, 2, 1})
}
func TestMarshallFullSignShare(t *testing.T) {
const players = 3
const threshold = 2
const bits = 4096
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
t.Fatal(err)
}
keys, err := Deal(rand.Reader, players, threshold, key, false)
if err != nil {
t.Fatal(err)
}
for _, share := range keys {
keyshare, err := share.Sign(rand.Reader, &key.PublicKey, []byte("Cloudflare!"), true)
if err != nil {
t.Fatal(err)
}
_, err = keyshare.MarshalBinary()
if err != nil {
t.Fatal(err)
}
}
}
|