github.com/cloudflare/circl@v1.5.0/tss/rsa/signShare_test.go (about)

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