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

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