github.com/cloudflare/circl@v1.5.0/abe/cpabe/tkn20/internal/tkn/tk_test.go (about) 1 package tkn 2 3 import ( 4 "crypto/rand" 5 "fmt" 6 "testing" 7 ) 8 9 func TestEncryptionSimple(t *testing.T) { 10 for i, suite := range encTestCases { 11 t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) { 12 public, secret, err := GenerateParams(rand.Reader) 13 if err != nil { 14 t.Fatalf("error generating parameters: %s", err) 15 } 16 userKey, err := deriveAttributeKeys(rand.Reader, secret, suite.a) 17 if err != nil { 18 t.Fatalf("error generating attribute keys: %s", err) 19 } 20 21 header, shared, err := encapsulate(rand.Reader, public, suite.p) 22 if err != nil { 23 t.Fatalf("error encrypting message: %s", err) 24 } 25 headerSerialized, err := header.marshalBinary() 26 if err != nil { 27 t.Fatalf("cannot serialize: %s", err) 28 } 29 newHeader := &ciphertextHeader{} 30 err = newHeader.unmarshalBinary(headerSerialized) 31 if err != nil { 32 t.Fatalf("unmarshaling failed: %s", err) 33 } 34 35 recovered, err := decapsulate(newHeader, userKey) 36 if err != nil { 37 t.Fatalf("error decrypting message: %s", err) 38 } 39 if !recovered.IsEqual(shared) { 40 t.Fatalf("decryption is incorrect") 41 } 42 }) 43 } 44 } 45 46 func TestMarshalPublicParams(t *testing.T) { 47 a, _, err := GenerateParams(rand.Reader) 48 if err != nil { 49 t.Fatal(err) 50 } 51 data, err := a.MarshalBinary() 52 if err != nil { 53 t.Fatalf("failure to serialize: %s", err) 54 } 55 b := &PublicParams{} 56 err = b.UnmarshalBinary(data) 57 if err != nil { 58 t.Fatalf("failure to deserialize: %s", err) 59 } 60 if !a.Equal(b) { 61 t.Fatal("failure to roundtrip") 62 } 63 } 64 65 func TestMarshalSecretParams(t *testing.T) { 66 _, a, err := GenerateParams(rand.Reader) 67 if err != nil { 68 t.Fatal(err) 69 } 70 data, err := a.MarshalBinary() 71 if err != nil { 72 t.Fatalf("failure to serialize: %s", err) 73 } 74 b := &SecretParams{} 75 err = b.UnmarshalBinary(data) 76 if err != nil { 77 t.Fatalf("failure to deserialize: %s", err) 78 } 79 if !a.Equal(b) { 80 t.Fatal("failure to roundtrip") 81 } 82 } 83 84 func TestMarshalAttributesKey(t *testing.T) { 85 _, sp, err := GenerateParams(rand.Reader) 86 if err != nil { 87 t.Fatal(err) 88 } 89 attrs := &Attributes{ 90 "d": { 91 wild: false, 92 Value: ToScalar(4), 93 }, 94 "c": { 95 wild: true, 96 Value: ToScalar(3), 97 }, 98 "b": { 99 wild: false, 100 Value: ToScalar(2), 101 }, 102 "a": { 103 wild: true, 104 Value: ToScalar(2), 105 }, 106 } 107 a, err := deriveAttributeKeys(rand.Reader, sp, attrs) 108 if err != nil { 109 t.Fatalf("error generating attribute keys: %s", err) 110 } 111 data, err := a.MarshalBinary() 112 if err != nil { 113 t.Fatalf("failure to serialize: %s", err) 114 } 115 b := &AttributesKey{} 116 err = b.UnmarshalBinary(data) 117 if err != nil { 118 t.Fatalf("failure to deserialize: %s", err) 119 } 120 if !a.Equal(b) { 121 t.Fatal("failure to roundtrip") 122 } 123 124 // ensure we can deserialize into non-empty struct 125 cAttrs := &Attributes{ 126 "evil": { 127 wild: false, 128 Value: ToScalar(0), 129 }, 130 } 131 c, err := deriveAttributeKeys(rand.Reader, sp, cAttrs) 132 if err != nil { 133 t.Fatalf("error generating attribute keys: %s", err) 134 } 135 err = c.UnmarshalBinary(data) 136 if err != nil { 137 t.Fatalf("failure to deserialize into non-empty struct: %s", err) 138 } 139 if !a.Equal(c) { 140 t.Fatal("failure to roundtrip") 141 } 142 } 143 144 func TestEqualAttributesKey(t *testing.T) { 145 _, sp, err := GenerateParams(rand.Reader) 146 if err != nil { 147 t.Fatal(err) 148 } 149 attrs := &Attributes{ 150 "d": { 151 wild: false, 152 Value: ToScalar(4), 153 }, 154 } 155 a, err := deriveAttributeKeys(rand.Reader, sp, attrs) 156 if err != nil { 157 t.Fatalf("error generating attribute keys: %s", err) 158 } 159 160 _, sp2, err := GenerateParams(rand.Reader) 161 if err != nil { 162 t.Fatal(err) 163 } 164 attrs2 := &Attributes{ 165 "k": { 166 wild: false, 167 Value: ToScalar(4), 168 }, 169 } 170 b, err := deriveAttributeKeys(rand.Reader, sp2, attrs2) 171 if err != nil { 172 t.Fatalf("error generating attribute keys: %s", err) 173 } 174 if a.Equal(b) { 175 t.Fatalf("shouldnt be equal") 176 } 177 178 // deep copy 179 data, err := a.MarshalBinary() 180 if err != nil { 181 t.Fatalf("failure to serialize: %s", err) 182 } 183 c := &AttributesKey{} 184 err = c.UnmarshalBinary(data) 185 if err != nil { 186 t.Fatalf("failure to deserialize: %s", err) 187 } 188 if !a.Equal(c) { 189 t.Fatalf("failure to roundtrip") 190 } 191 192 for k, v := range a.k3 { 193 c.k3[k], err = randomMatrixG1(rand.Reader, v.rows, v.cols) 194 if err != nil { 195 t.Fatal(err) 196 } 197 break 198 } 199 if a.Equal(c) { 200 t.Fatalf("shouldnt be equal") 201 } 202 }