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

     1  package rsa
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/rsa"
     6  	"math/big"
     7  	"testing"
     8  )
     9  
    10  func TestKeyShare_Sign(t *testing.T) {
    11  	// delta = 3! = 6
    12  	// n = 253
    13  	// Players = 3
    14  	// kshare = { si: 15, Index: 1 }
    15  	// x = { 150 }
    16  	// x_i = x^{2∆kshare.si} = 150^{2 * 6 * 15} = 150^180 = 243
    17  
    18  	kshare := KeyShare{
    19  		si:      big.NewInt(15),
    20  		Index:   1,
    21  		Players: 3,
    22  	}
    23  	pub := rsa.PublicKey{N: big.NewInt(253)}
    24  	share, err := kshare.Sign(nil, &pub, []byte{150}, false)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if share.xi.Cmp(big.NewInt(243)) != 0 {
    29  		t.Fatalf("share.xi should be 243 but was %d", share.xi)
    30  	}
    31  }
    32  
    33  func testSignBlind(parallel bool, t *testing.T) {
    34  	// delta = 3! = 6
    35  	// n = 253
    36  	// Players = 3
    37  	// kshare = { si: 15, i: 1 }
    38  	// x = { 150 }
    39  	// x_i = x^{2∆kshare.si} = 150^{2 * 6 * 15} = 150^180 = 243
    40  
    41  	kshare := KeyShare{
    42  		si:      big.NewInt(15),
    43  		Index:   1,
    44  		Players: 3,
    45  	}
    46  	pub := rsa.PublicKey{N: big.NewInt(253)}
    47  	share, err := kshare.Sign(rand.Reader, &pub, []byte{150}, parallel)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if share.xi.Cmp(big.NewInt(243)) != 0 {
    52  		t.Fatalf("share.xi should be 243 but was %d", share.xi)
    53  	}
    54  }
    55  
    56  func TestKeyShare_SignBlind(t *testing.T) {
    57  	testSignBlind(false, t)
    58  }
    59  
    60  func TestKeyShare_SignBlindParallel(t *testing.T) {
    61  	testSignBlind(true, t)
    62  }
    63  
    64  func marshalTestKeyShare(share KeyShare, t *testing.T) {
    65  	marshall, err := share.MarshalBinary()
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	share2 := KeyShare{}
    71  	err = share2.UnmarshalBinary(marshall)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	if share.Players != share2.Players {
    77  		t.Fatalf("Players did not match, expected %d, found %d", share.Players, share2.Players)
    78  	}
    79  
    80  	if share.Threshold != share2.Threshold {
    81  		t.Fatalf("Threshold did not match, expected %d, found %d", share.Threshold, share2.Threshold)
    82  	}
    83  
    84  	if share.Index != share2.Index {
    85  		t.Fatalf("Index did not match, expected %d, found %d", share.Index, share2.Index)
    86  	}
    87  
    88  	if (share.twoDeltaSi == nil || share2.twoDeltaSi == nil) && share.twoDeltaSi != share2.twoDeltaSi {
    89  		t.Fatalf("twoDeltaSi did not match, expected %v, found %v", share.twoDeltaSi, share2.twoDeltaSi)
    90  	}
    91  
    92  	if !(share.twoDeltaSi == nil && share2.twoDeltaSi == nil) && share.twoDeltaSi.Cmp(share2.twoDeltaSi) != 0 {
    93  		t.Fatalf("twoDeltaSi did not match, expected %v, found %v", share.twoDeltaSi.Bytes(), share2.twoDeltaSi.Bytes())
    94  	}
    95  
    96  	if share.si.Cmp(share2.si) != 0 {
    97  		t.Fatalf("si did not match, expected %v, found %v", share.si.Bytes(), share2.si.Bytes())
    98  	}
    99  }
   100  
   101  func unmarshalKeyShareTest(t *testing.T, input []byte) {
   102  	share := KeyShare{}
   103  	err := share.UnmarshalBinary(input)
   104  	if err == nil {
   105  		t.Fatalf("unmarshall succeeded when it shouldn't have")
   106  	}
   107  }
   108  
   109  func TestMarshallKeyShare(t *testing.T) {
   110  	marshalTestKeyShare(KeyShare{
   111  		si:         big.NewInt(10),
   112  		twoDeltaSi: big.NewInt(20),
   113  		Index:      30,
   114  		Threshold:  10,
   115  		Players:    2,
   116  	}, t)
   117  
   118  	marshalTestKeyShare(KeyShare{
   119  		si:         big.NewInt(10),
   120  		twoDeltaSi: nil,
   121  		Index:      30,
   122  		Threshold:  0,
   123  		Players:    200,
   124  	}, t)
   125  
   126  	marshalTestKeyShare(KeyShare{
   127  		si:         big.NewInt(0),
   128  		twoDeltaSi: big.NewInt(0),
   129  		Index:      0,
   130  		Threshold:  0,
   131  		Players:    0,
   132  	}, t)
   133  
   134  	unmarshalKeyShareTest(t, []byte{})
   135  	unmarshalKeyShareTest(t, []byte{1, 0, 1})
   136  	unmarshalKeyShareTest(t, []byte{1, 0, 1})
   137  	unmarshalKeyShareTest(t, []byte{0, 1, 0, 1, 0, 1})
   138  	unmarshalKeyShareTest(t, []byte{0, 1, 0, 1, 0, 1, 0, 1})
   139  	unmarshalKeyShareTest(t, []byte{0, 1, 0, 1, 0, 1, 0})
   140  	unmarshalKeyShareTest(t, []byte{0, 1, 0, 1, 0, 1, 0, 2, 1})
   141  	unmarshalKeyShareTest(t, []byte{0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0})
   142  	unmarshalKeyShareTest(t, []byte{0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1})
   143  }
   144  
   145  func TestMarshallKeyShareFull(t *testing.T) {
   146  	const players = 3
   147  	const threshold = 2
   148  	const bits = 4096
   149  
   150  	key, err := rsa.GenerateKey(rand.Reader, bits)
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	keys, err := Deal(rand.Reader, players, threshold, key, false)
   155  	if err != nil {
   156  		t.Fatal(err)
   157  	}
   158  	for _, share := range keys {
   159  		marshalTestKeyShare(share, t)
   160  	}
   161  }