github.com/cloudflare/circl@v1.5.0/abe/cpabe/tkn20/internal/tkn/matrixGT_test.go (about)

     1  package tkn
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	pairing "github.com/cloudflare/circl/ecc/bls12381"
     8  )
     9  
    10  func TestRightMultLinearityGT(t *testing.T) {
    11  	zpMat1, err := randomMatrixZp(rand.Reader, 2, 3)
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  	zpMat2, err := randomMatrixZp(rand.Reader, 2, 3)
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	zpMat1p2 := newMatrixZp(3, 2)
    20  	zpMat1p2.add(zpMat1, zpMat2)
    21  	a, err := randomMatrixGT(rand.Reader, 3, 2)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	s := newMatrixGT(3, 3)
    26  	u := newMatrixGT(3, 3)
    27  	r := newMatrixGT(3, 3)
    28  	tmp := newMatrixGT(3, 3)
    29  	r.rightMult(a, zpMat1)
    30  	s.rightMult(a, zpMat2)
    31  	u.rightMult(a, zpMat1p2)
    32  	tmp.add(r, s)
    33  	if !tmp.Equal(u) {
    34  		t.Fatal("failure of linearity")
    35  	}
    36  }
    37  
    38  func TestLeftMultLinearityGT(t *testing.T) {
    39  	zpMat1, err := randomMatrixZp(rand.Reader, 3, 3)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	zpMat2, err := randomMatrixZp(rand.Reader, 3, 3)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	zpMat1p2 := newMatrixZp(3, 3)
    48  	zpMat1p2.add(zpMat1, zpMat2)
    49  	a, err := randomMatrixGT(rand.Reader, 3, 2)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	s := newMatrixGT(0, 0)
    54  	u := newMatrixGT(0, 0)
    55  	r := newMatrixGT(0, 0)
    56  	tmp := newMatrixGT(0, 0)
    57  	r.leftMult(zpMat1, a)
    58  	s.leftMult(zpMat2, a)
    59  	u.leftMult(zpMat1p2, a)
    60  	tmp.add(r, s)
    61  	if !tmp.Equal(u) {
    62  		t.Fatal("failure of linearity")
    63  	}
    64  }
    65  
    66  func TestLeftMultActionGT(t *testing.T) {
    67  	zpMat1, err := randomMatrixZp(rand.Reader, 3, 3)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	zpMat2, err := randomMatrixZp(rand.Reader, 3, 3)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	zpMat2m1 := newMatrixZp(3, 3)
    76  	zpMat2m1.mul(zpMat2, zpMat1)
    77  	a, err := randomMatrixGT(rand.Reader, 3, 2)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	s := newMatrixGT(0, 0)
    82  	u := newMatrixGT(0, 0)
    83  	r := newMatrixGT(0, 0)
    84  	r.leftMult(zpMat1, a)
    85  	s.leftMult(zpMat2, r)
    86  	u.leftMult(zpMat2m1, a)
    87  	if !s.Equal(u) {
    88  		t.Fatal("failure of action")
    89  	}
    90  }
    91  
    92  func TestRightMultActionGT(t *testing.T) {
    93  	zpMat1, err := randomMatrixZp(rand.Reader, 2, 2)
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	zpMat2, err := randomMatrixZp(rand.Reader, 2, 2)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	zpMat1m2 := newMatrixZp(2, 2)
   102  	zpMat1m2.mul(zpMat1, zpMat2)
   103  	a, err := randomMatrixGT(rand.Reader, 3, 2)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	s := newMatrixGT(0, 0)
   108  	u := newMatrixGT(0, 0)
   109  	r := newMatrixGT(0, 0)
   110  	r.rightMult(a, zpMat1)
   111  	s.rightMult(r, zpMat2)
   112  	u.rightMult(a, zpMat1m2)
   113  	if !s.Equal(u) {
   114  		t.Fatal("failure of action")
   115  	}
   116  }
   117  
   118  func TestExpGT(t *testing.T) {
   119  	a, err := randomMatrixZp(rand.Reader, 2, 2)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	b, err := randomMatrixZp(rand.Reader, 2, 2)
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	ab := newMatrixZp(2, 2)
   128  	ab.mul(a, b)
   129  	aexp := newMatrixGT(0, 0)
   130  	aexp.exp(a)
   131  	abexp := newMatrixGT(0, 0)
   132  	abexp.exp(ab)
   133  	abres := newMatrixGT(0, 0)
   134  	abres.rightMult(aexp, b)
   135  	if !abres.Equal(abexp) {
   136  		t.Fatal("action and exp failure")
   137  	}
   138  }
   139  
   140  func TestExpGTLinearity(t *testing.T) {
   141  	a, err := randomMatrixZp(rand.Reader, 2, 2)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	b, err := randomMatrixZp(rand.Reader, 2, 2)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	ab := newMatrixZp(2, 2)
   150  	ab.add(a, b)
   151  	aexp := newMatrixGT(0, 0)
   152  	aexp.exp(a)
   153  	bexp := newMatrixGT(0, 0)
   154  	bexp.exp(b)
   155  	abexp := newMatrixGT(0, 0)
   156  	absum := newMatrixGT(0, 0)
   157  	absum.add(aexp, bexp)
   158  	abexp.exp(ab)
   159  	if !abexp.Equal(absum) {
   160  		t.Fatal("linearity of exponentiation broken")
   161  	}
   162  }
   163  
   164  func TestExpKnownAnswer(t *testing.T) {
   165  	a := eye(2)
   166  	b := newMatrixGT(0, 0)
   167  	b.exp(a)
   168  	one := &pairing.Gt{}
   169  	one.SetIdentity()
   170  	if !b.entries[0].IsEqual(gtBaseVal) {
   171  		t.Fatal("failure of 0")
   172  	}
   173  	if !b.entries[1].IsEqual(one) {
   174  		t.Fatal("failure of 1")
   175  	}
   176  	if !b.entries[2].IsEqual(one) {
   177  		t.Fatal("failure of 2")
   178  	}
   179  	if !b.entries[3].IsEqual(gtBaseVal) {
   180  		t.Fatal("failure of 3")
   181  	}
   182  }
   183  
   184  func TestMarshalGt(t *testing.T) {
   185  	a, err := randomMatrixGT(rand.Reader, 7, 9)
   186  	if err != nil {
   187  		t.Fatal(err)
   188  	}
   189  	data, err := a.marshalBinary()
   190  	if err != nil {
   191  		t.Fatalf("failure to serialize: %s", err)
   192  	}
   193  	b := newMatrixGT(0, 0)
   194  	err = b.unmarshalBinary(data)
   195  	if err != nil {
   196  		t.Fatalf("failure to deserialize: %s", err)
   197  	}
   198  	if !a.Equal(b) {
   199  		t.Fatal("failure to roundtrip")
   200  	}
   201  	c := newMatrixGT(0, 0)
   202  	err = c.unmarshalBinary(data[0 : len(data)-2])
   203  	if err == nil {
   204  		t.Fatalf("data is too short, deserialization should fail")
   205  	}
   206  }
   207  
   208  func TestAliasLeftMultGT(t *testing.T) {
   209  	a, err := randomMatrixGT(rand.Reader, 4, 4)
   210  	if err != nil {
   211  		t.Fatalf("error: %v", err)
   212  	}
   213  	b, err := randomMatrixZp(rand.Reader, 4, 4)
   214  	if err != nil {
   215  		t.Fatalf("error: %v", err)
   216  	}
   217  	aCopy := newMatrixGT(4, 4)
   218  	aCopy.set(a)
   219  	bCopy := newMatrixZp(4, 4)
   220  	bCopy.set(b)
   221  	a.leftMult(b, a)
   222  	res := newMatrixGT(0, 0)
   223  	res.leftMult(bCopy, aCopy)
   224  	if !res.Equal(a) {
   225  		t.Fatalf("failure of leftMult to be alias safe")
   226  	}
   227  }
   228  
   229  func TestAliasRightMultGT(t *testing.T) {
   230  	a, err := randomMatrixGT(rand.Reader, 4, 4)
   231  	if err != nil {
   232  		t.Fatalf("error: %v", err)
   233  	}
   234  	b, err := randomMatrixZp(rand.Reader, 4, 4)
   235  	if err != nil {
   236  		t.Fatalf("error: %v", err)
   237  	}
   238  	aCopy := newMatrixGT(4, 4)
   239  	aCopy.set(a)
   240  	bCopy := newMatrixZp(4, 4)
   241  	bCopy.set(b)
   242  	a.rightMult(a, b)
   243  	res := newMatrixGT(0, 0)
   244  	res.rightMult(aCopy, bCopy)
   245  	if !res.Equal(a) {
   246  		t.Fatalf("failure of rightMult to be alias safe")
   247  	}
   248  }
   249  
   250  func TestAliasAddGt(t *testing.T) {
   251  	a, err := randomMatrixG1(rand.Reader, 4, 4)
   252  	if err != nil {
   253  		t.Fatalf("error: %v", err)
   254  	}
   255  	b, err := randomMatrixG1(rand.Reader, 4, 4)
   256  	if err != nil {
   257  		t.Fatalf("error: %v", err)
   258  	}
   259  	aCopy := newMatrixG1(4, 4)
   260  	aCopy.set(a)
   261  	bCopy := newMatrixG1(4, 4)
   262  	bCopy.set(b)
   263  	a.add(b, a)
   264  	res := newMatrixG1(0, 0)
   265  	res.add(bCopy, aCopy)
   266  	if !res.Equal(a) {
   267  		t.Fatalf("failure of add to be alias safe")
   268  	}
   269  }