github.com/cloudflare/circl@v1.5.0/abe/cpabe/tkn20/internal/tkn/matrixG2_test.go (about) 1 package tkn 2 3 import ( 4 "crypto/rand" 5 "testing" 6 ) 7 8 func TestRightMultLinearityG2(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 := randomMatrixG2(rand.Reader, 3, 2) 20 if err != nil { 21 t.Fatal(err) 22 } 23 s := newMatrixG2(3, 3) 24 u := newMatrixG2(3, 3) 25 r := newMatrixG2(3, 3) 26 tmp := newMatrixG2(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 TestLeftMultLinearityG2(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 := randomMatrixG2(rand.Reader, 3, 2) 48 if err != nil { 49 t.Fatal(err) 50 } 51 s := newMatrixG2(0, 0) 52 u := newMatrixG2(0, 0) 53 r := newMatrixG2(0, 0) 54 tmp := newMatrixG2(0, 0) 55 r.leftMult(zpMat1, a) 56 s.leftMult(zpMat2, a) 57 u.leftMult(zpMat1p2, a) 58 tmp.add(r, s) 59 if !tmp.Equal(u) { 60 t.Fatal("failure of linearity") 61 } 62 } 63 64 func TestLeftMultActionG2(t *testing.T) { 65 zpMat1, err := randomMatrixZp(rand.Reader, 3, 3) 66 if err != nil { 67 t.Fatal(err) 68 } 69 zpMat2, err := randomMatrixZp(rand.Reader, 3, 3) 70 if err != nil { 71 t.Fatal(err) 72 } 73 zpMat2m1 := newMatrixZp(3, 3) 74 zpMat2m1.mul(zpMat2, zpMat1) 75 a, err := randomMatrixG2(rand.Reader, 3, 2) 76 if err != nil { 77 t.Fatal(err) 78 } 79 s := newMatrixG2(0, 0) 80 u := newMatrixG2(0, 0) 81 r := newMatrixG2(0, 0) 82 r.leftMult(zpMat1, a) 83 s.leftMult(zpMat2, r) 84 u.leftMult(zpMat2m1, a) 85 if !s.Equal(u) { 86 t.Fatal("failure of action") 87 } 88 } 89 90 func TestRightMultActionG2(t *testing.T) { 91 zpMat1, err := randomMatrixZp(rand.Reader, 2, 2) 92 if err != nil { 93 t.Fatal(err) 94 } 95 zpMat2, err := randomMatrixZp(rand.Reader, 2, 2) 96 if err != nil { 97 t.Fatal(err) 98 } 99 zpMat1m2 := newMatrixZp(2, 2) 100 zpMat1m2.mul(zpMat1, zpMat2) 101 a, err := randomMatrixG2(rand.Reader, 3, 2) 102 if err != nil { 103 t.Fatal(err) 104 } 105 s := newMatrixG2(0, 0) 106 u := newMatrixG2(0, 0) 107 r := newMatrixG2(0, 0) 108 r.rightMult(a, zpMat1) 109 s.rightMult(r, zpMat2) 110 u.rightMult(a, zpMat1m2) 111 if !s.Equal(u) { 112 t.Fatal("failure of action") 113 } 114 } 115 116 func TestExpG2(t *testing.T) { 117 a, err := randomMatrixZp(rand.Reader, 2, 2) 118 if err != nil { 119 t.Fatal(err) 120 } 121 b, err := randomMatrixZp(rand.Reader, 2, 2) 122 if err != nil { 123 t.Fatal(err) 124 } 125 ab := newMatrixZp(2, 2) 126 ab.mul(a, b) 127 aexp := newMatrixG2(0, 0) 128 aexp.exp(a) 129 abexp := newMatrixG2(0, 0) 130 abexp.exp(ab) 131 abres := newMatrixG2(0, 0) 132 abres.rightMult(aexp, b) 133 if !abres.Equal(abexp) { 134 t.Fatal("action and exp failure") 135 } 136 } 137 138 func TestMarshalG2(t *testing.T) { 139 a, err := randomMatrixG2(rand.Reader, 7, 9) 140 if err != nil { 141 t.Fatal(err) 142 } 143 data, err := a.marshalBinary() 144 if err != nil { 145 t.Fatalf("failure to serialize: %s", err) 146 } 147 b := newMatrixG2(0, 0) 148 err = b.unmarshalBinary(data) 149 if err != nil { 150 t.Fatalf("failure to deserialize: %s", err) 151 } 152 if !a.Equal(b) { 153 t.Fatal("failure to roundtrip") 154 } 155 c := newMatrixG2(0, 0) 156 err = c.unmarshalBinary(append(data, 0)) 157 if err == nil { 158 t.Fatalf("data has excess bytes, deserialization should fail") 159 } 160 } 161 162 func TestAliasLeftMultG2(t *testing.T) { 163 a, err := randomMatrixG2(rand.Reader, 4, 4) 164 if err != nil { 165 t.Fatalf("error: %v", err) 166 } 167 b, err := randomMatrixZp(rand.Reader, 4, 4) 168 if err != nil { 169 t.Fatalf("error: %v", err) 170 } 171 aCopy := newMatrixG2(4, 4) 172 aCopy.set(a) 173 bCopy := newMatrixZp(4, 4) 174 bCopy.set(b) 175 a.leftMult(b, a) 176 res := newMatrixG2(0, 0) 177 res.leftMult(bCopy, aCopy) 178 if !res.Equal(a) { 179 t.Fatalf("failure of leftMult to be alias safe") 180 } 181 } 182 183 func TestAliasRightMultG2(t *testing.T) { 184 a, err := randomMatrixG2(rand.Reader, 4, 4) 185 if err != nil { 186 t.Fatalf("error: %v", err) 187 } 188 b, err := randomMatrixZp(rand.Reader, 4, 4) 189 if err != nil { 190 t.Fatalf("error: %v", err) 191 } 192 aCopy := newMatrixG2(4, 4) 193 aCopy.set(a) 194 bCopy := newMatrixZp(4, 4) 195 bCopy.set(b) 196 a.rightMult(a, b) 197 res := newMatrixG2(0, 0) 198 res.rightMult(aCopy, bCopy) 199 if !res.Equal(a) { 200 t.Fatalf("failure of rightMult to be alias safe") 201 } 202 } 203 204 func TestAliasAddG2(t *testing.T) { 205 a, err := randomMatrixG2(rand.Reader, 4, 4) 206 if err != nil { 207 t.Fatalf("error: %v", err) 208 } 209 b, err := randomMatrixG2(rand.Reader, 4, 4) 210 if err != nil { 211 t.Fatalf("error: %v", err) 212 } 213 aCopy := newMatrixG2(4, 4) 214 aCopy.set(a) 215 bCopy := newMatrixG2(4, 4) 216 bCopy.set(b) 217 a.add(b, a) 218 res := newMatrixG2(0, 0) 219 res.add(bCopy, aCopy) 220 if !res.Equal(a) { 221 t.Fatalf("failure of add to be alias safe") 222 } 223 }