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

     1  package tkn
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	pairing "github.com/cloudflare/circl/ecc/bls12381"
     8  	"github.com/cloudflare/circl/ecc/bls12381/ff"
     9  )
    10  
    11  func TestSampleDlin(t *testing.T) {
    12  	_, err := sampleDlin(rand.Reader)
    13  	if err != nil {
    14  		t.Errorf("failure of dlin: %s", err)
    15  	}
    16  }
    17  
    18  func TestAdditionAndTranspose(t *testing.T) {
    19  	a, err := sampleDlin(rand.Reader)
    20  	if err != nil {
    21  		t.Errorf("failure of dlin: %s", err)
    22  	}
    23  	b, err := sampleDlin(rand.Reader)
    24  	if err != nil {
    25  		t.Errorf("failure of dlin: %s", err)
    26  	}
    27  	at := new(matrixZp)
    28  	bt := new(matrixZp)
    29  	aplusb := new(matrixZp)
    30  	atplusbt := new(matrixZp)
    31  	lhs := new(matrixZp)
    32  
    33  	at.transpose(a)
    34  	bt.transpose(b)
    35  	aplusb.add(a, b)
    36  	atplusbt.add(at, bt)
    37  
    38  	lhs.transpose(atplusbt)
    39  	if !aplusb.Equal(lhs) {
    40  		t.Errorf("failure of equality")
    41  	}
    42  }
    43  
    44  func TestMultiplication(t *testing.T) {
    45  	a, err := sampleDlin(rand.Reader)
    46  	if err != nil {
    47  		t.Errorf("failure of diln: %s", err)
    48  	}
    49  	b, err := sampleDlin(rand.Reader)
    50  	if err != nil {
    51  		t.Errorf("failure of diln: %s", err)
    52  	}
    53  	bta := new(matrixZp)
    54  	atb := new(matrixZp)
    55  	tmp := new(matrixZp)
    56  	tmp.transpose(b)
    57  	bta.mul(tmp, a)
    58  	tmp.transpose(a)
    59  	atb.mul(tmp, b)
    60  	tmp.transpose(atb)
    61  	if !tmp.Equal(bta) {
    62  		t.Errorf("failure of multiplication")
    63  	}
    64  }
    65  
    66  func TestInverse(t *testing.T) {
    67  	a, err := sampleDlin(rand.Reader)
    68  	if err != nil {
    69  		t.Errorf("failure of diln: %s", err)
    70  	}
    71  	aa := new(matrixZp)
    72  	at := new(matrixZp)
    73  	ainv := new(matrixZp)
    74  	res := new(matrixZp)
    75  	at.transpose(a)
    76  	aa.mul(at, a)
    77  	err = ainv.inverse(aa)
    78  	if err != nil {
    79  		t.Errorf("failure to compute inverse")
    80  	}
    81  	res.mul(ainv, aa)
    82  	expected := eye(res.cols)
    83  	if !expected.Equal(res) {
    84  		t.Errorf("failure of inverse value: expected:\n%v\n got:\n%v", expected, res)
    85  		t.Errorf("inversion was of %v\n", aa)
    86  	}
    87  	res.mul(aa, ainv)
    88  	if !expected.Equal(res) {
    89  		t.Errorf("failure of reversed mult: got\n%v\n", res)
    90  	}
    91  }
    92  
    93  func TestInverse2x2(t *testing.T) {
    94  	A, err := randomMatrixZp(rand.Reader, 2, 2)
    95  	if err != nil {
    96  		t.Fatalf("failure of random: %v", err)
    97  	}
    98  
    99  	B := newMatrixZp(2, 2)
   100  	C := newMatrixZp(2, 2)
   101  	err = C.inverse(A)
   102  	if err != nil {
   103  		t.Fatalf("ah, we have a problem: try again")
   104  	}
   105  	var a pairing.Scalar
   106  	var b pairing.Scalar
   107  	var c pairing.Scalar
   108  	var d pairing.Scalar
   109  	var det pairing.Scalar
   110  	var tmp pairing.Scalar
   111  	a.Set(&A.entries[0])
   112  	b.Set(&A.entries[1])
   113  	c.Set(&A.entries[2])
   114  	d.Set(&A.entries[3])
   115  	det.Mul(&a, &d)
   116  	tmp.Mul(&b, &c)
   117  	det.Sub(&det, &tmp)
   118  	tmp.Inv(&det)
   119  	B.entries[0].Set(&d)
   120  	B.entries[1].Set(&b)
   121  	B.entries[1].Neg()
   122  	B.entries[2].Set(&c)
   123  	B.entries[2].Neg()
   124  	B.entries[3].Set(&a)
   125  	B.scalarmul(&tmp, B)
   126  	if !C.Equal(B) {
   127  		t.Errorf("failure to agree with explicit formula: got:\n%v, wanted\n%v\n", B, C)
   128  	}
   129  	expected := eye(2)
   130  	res := newMatrixZp(0, 0)
   131  	res.mul(B, A)
   132  	if !res.Equal(expected) {
   133  		t.Errorf("explicit formula wrong: got:\n%v\n as inverse of\n%v\n", B, A)
   134  	}
   135  }
   136  
   137  func TestPRF(t *testing.T) {
   138  	m1, m2, err := prf([]byte("test key do not use"), []byte("some input"))
   139  	if err != nil {
   140  		t.Errorf("failure of prf: %s", err)
   141  	}
   142  	if m1.Equal(m2) {
   143  		t.Errorf("prf fails to have distinct outputs")
   144  	}
   145  	m3, _, err := prf([]byte("test key do not use"), []byte("some other input"))
   146  	if err != nil {
   147  		t.Errorf("failure of prf: %s", err)
   148  	}
   149  
   150  	if m1.Equal(m3) {
   151  		t.Errorf("prf ignores input Value")
   152  	}
   153  	m4, _, err := prf([]byte("test key"), []byte("some input"))
   154  	if err != nil {
   155  		t.Errorf("failure of prf: %s", err)
   156  	}
   157  	if m1.Equal(m4) {
   158  		t.Errorf("prf ignores key")
   159  	}
   160  }
   161  
   162  func TestColsel(t *testing.T) {
   163  	m1, err := randomMatrixZp(rand.Reader, 17, 17)
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	res := new(matrixZp)
   168  	res.colsel(m1, []int{0, 1, 2})
   169  	if res.rows != m1.rows {
   170  		t.Errorf("wrong number of rows")
   171  	}
   172  	if res.cols != 3 {
   173  		t.Errorf("wrong number of columns")
   174  	}
   175  }
   176  
   177  func TestAliasMul(t *testing.T) {
   178  	a, err := randomMatrixZp(rand.Reader, 4, 4)
   179  	if err != nil {
   180  		t.Fatalf("error: %v", err)
   181  	}
   182  	b, err := randomMatrixZp(rand.Reader, 4, 4)
   183  	if err != nil {
   184  		t.Fatalf("error: %v", err)
   185  	}
   186  	aCopy := newMatrixZp(4, 4)
   187  	aCopy.set(a)
   188  	bCopy := newMatrixZp(4, 4)
   189  	bCopy.set(b)
   190  	a.mul(b, a)
   191  	res := newMatrixZp(0, 0)
   192  	res.mul(bCopy, aCopy)
   193  	if !res.Equal(a) {
   194  		t.Fatalf("failure of mul to be alias safe")
   195  	}
   196  }
   197  
   198  func TestAliasAddZp(t *testing.T) {
   199  	a, err := randomMatrixZp(rand.Reader, 4, 4)
   200  	if err != nil {
   201  		t.Fatalf("error: %v", err)
   202  	}
   203  	b, err := randomMatrixZp(rand.Reader, 4, 4)
   204  	if err != nil {
   205  		t.Fatalf("error: %v", err)
   206  	}
   207  	aCopy := newMatrixZp(4, 4)
   208  	aCopy.set(a)
   209  	bCopy := newMatrixZp(4, 4)
   210  	bCopy.set(b)
   211  	a.add(b, a)
   212  	res := newMatrixZp(0, 0)
   213  	res.add(bCopy, aCopy)
   214  	if !res.Equal(a) {
   215  		t.Fatalf("failure of add to be alias safe")
   216  	}
   217  }
   218  
   219  func TestAliasSubZp(t *testing.T) {
   220  	a, err := randomMatrixZp(rand.Reader, 4, 4)
   221  	if err != nil {
   222  		t.Fatalf("error: %v", err)
   223  	}
   224  	b, err := randomMatrixZp(rand.Reader, 4, 4)
   225  	if err != nil {
   226  		t.Fatalf("error: %v", err)
   227  	}
   228  	aCopy := newMatrixZp(4, 4)
   229  	aCopy.set(a)
   230  	bCopy := newMatrixZp(4, 4)
   231  	bCopy.set(b)
   232  	a.sub(b, a)
   233  	res := newMatrixZp(0, 0)
   234  	res.sub(bCopy, aCopy)
   235  	if !res.Equal(a) {
   236  		t.Fatalf("failure of sub to be alias safe")
   237  	}
   238  }
   239  
   240  func TestMarshalZp(t *testing.T) {
   241  	a, err := randomMatrixZp(rand.Reader, 7, 9)
   242  	if err != nil {
   243  		t.Fatal(err)
   244  	}
   245  	data, err := a.marshalBinary()
   246  	if err != nil {
   247  		t.Fatalf("failure to serialize: %s", err)
   248  	}
   249  	b := newMatrixZp(0, 0)
   250  	err = b.unmarshalBinary(data)
   251  	if err != nil {
   252  		t.Fatalf("failure to deserialize: %s", err)
   253  	}
   254  	if !a.Equal(b) {
   255  		t.Fatal("failure to roundtrip")
   256  	}
   257  
   258  	// test failure to set entry bytes
   259  	scOrder := ff.ScalarOrder()
   260  	scOrder[len(scOrder)-1] += 1
   261  	copy(data[4:pairing.ScalarSize+4], scOrder[:])
   262  	d := newMatrixZp(0, 0)
   263  	err = d.unmarshalBinary(data)
   264  	if err == nil {
   265  		t.Fatal("deserialization of matrixZp with entry larger than scalar order must fail")
   266  	}
   267  }