github.com/cloudflare/circl@v1.5.0/abe/cpabe/tkn20/internal/tkn/bk_test.go (about) 1 package tkn 2 3 import ( 4 "crypto/rand" 5 "testing" 6 ) 7 8 var encTestCases = []TestCase{ 9 { 10 &Policy{ 11 Inputs: []Wire{ 12 {"a", "", ToScalar(0), true}, 13 }, 14 F: Formula{ 15 Gates: []Gate{}, 16 }, 17 }, 18 &Attributes{ 19 "a": { 20 wild: false, 21 Value: ToScalar(0), 22 }, 23 }, 24 }, 25 { 26 &Policy{ 27 Inputs: []Wire{ 28 {"a", "", ToScalar(1), true}, 29 {"b", "", ToScalar(2), true}, 30 {"c", "", ToScalar(3), true}, 31 }, 32 F: Formula{ 33 Gates: []Gate{ 34 {Andgate, 0, 1, 3}, 35 {Andgate, 2, 3, 4}, 36 }, 37 }, 38 }, 39 &Attributes{ 40 "d": { 41 wild: false, 42 Value: ToScalar(4), 43 }, 44 "c": { 45 wild: false, 46 Value: ToScalar(3), 47 }, 48 "b": { 49 wild: false, 50 Value: ToScalar(2), 51 }, 52 "a": { 53 wild: false, 54 Value: ToScalar(1), 55 }, 56 }, 57 }, 58 { 59 &Policy{ 60 Inputs: []Wire{ 61 {"a", "", ToScalar(1), false}, 62 {"b", "", ToScalar(2), true}, 63 {"c", "", ToScalar(3), true}, 64 }, 65 F: Formula{ 66 Gates: []Gate{ 67 {Andgate, 0, 1, 3}, 68 {Andgate, 2, 3, 4}, 69 }, 70 }, 71 }, 72 &Attributes{ 73 "d": { 74 wild: false, 75 Value: ToScalar(4), 76 }, 77 "c": { 78 wild: false, 79 Value: ToScalar(3), 80 }, 81 "b": { 82 wild: false, 83 Value: ToScalar(2), 84 }, 85 "a": { 86 wild: false, 87 Value: ToScalar(2), 88 }, 89 }, 90 }, 91 { 92 &Policy{ 93 Inputs: []Wire{ 94 {"a", "", ToScalar(1), false}, 95 {"c", "", ToScalar(4), true}, 96 }, 97 F: Formula{ 98 Gates: []Gate{ 99 {Andgate, 0, 1, 2}, 100 }, 101 }, 102 }, 103 &Attributes{ 104 "d": { 105 wild: false, 106 Value: ToScalar(4), 107 }, 108 "c": { 109 wild: true, 110 Value: ToScalar(3), 111 }, 112 "b": { 113 wild: false, 114 Value: ToScalar(2), 115 }, 116 "a": { 117 wild: true, 118 Value: ToScalar(2), 119 }, 120 }, 121 }, 122 { 123 &Policy{ 124 Inputs: []Wire{ 125 {"a", "", ToScalar(1), true}, 126 {"b", "", ToScalar(2), true}, 127 {"c", "", ToScalar(3), true}, 128 }, 129 F: Formula{ 130 Gates: []Gate{ 131 {Andgate, 0, 1, 3}, 132 {Orgate, 2, 3, 4}, 133 }, 134 }, 135 }, 136 &Attributes{ 137 "d": { 138 wild: false, 139 Value: ToScalar(4), 140 }, 141 "c": { 142 wild: false, 143 Value: ToScalar(3), 144 }, 145 "b": { 146 wild: false, 147 Value: ToScalar(2), 148 }, 149 "a": { 150 wild: false, 151 Value: ToScalar(1), 152 }, 153 }, 154 }, 155 { 156 &Policy{ 157 Inputs: []Wire{ 158 {"blocked", "", ToScalar(1), false}, 159 {"secure", "", ToScalar(1), true}, 160 {"eu", "", ToScalar(1), true}, 161 {"us", "", ToScalar(1), true}, 162 }, 163 F: Formula{ 164 Gates: []Gate{ 165 {Orgate, 2, 3, 4}, 166 {Andgate, 0, 4, 5}, 167 {Andgate, 1, 5, 6}, 168 }, 169 }, 170 }, 171 &Attributes{ 172 "blocked": { 173 wild: false, 174 Value: ToScalar(0), 175 }, 176 "secure": { 177 wild: false, 178 Value: ToScalar(1), 179 }, 180 "us": { 181 wild: false, 182 Value: ToScalar(1), 183 }, 184 "irrelevantAttr": { 185 wild: false, 186 Value: ToScalar(1), 187 }, 188 }, 189 }, 190 { 191 &Policy{ 192 Inputs: []Wire{ 193 {"a", "", ToScalar(1), true}, 194 {"a", "", ToScalar(2), true}, 195 }, 196 F: Formula{ 197 Gates: []Gate{ 198 {Orgate, 0, 1, 2}, 199 }, 200 }, 201 }, 202 &Attributes{ 203 "a": { 204 false, 205 ToScalar(1), 206 }, 207 }, 208 }, 209 { 210 &Policy{ 211 Inputs: []Wire{ 212 {"a", "", ToScalar(1), true}, 213 {"a", "", ToScalar(2), true}, 214 }, 215 F: Formula{ 216 Gates: []Gate{ 217 {Orgate, 0, 1, 2}, 218 }, 219 }, 220 }, 221 &Attributes{ 222 "a": { 223 false, 224 ToScalar(2), 225 }, 226 }, 227 }, 228 } 229 230 func TestEncryptionBk(t *testing.T) { 231 msg := []byte("drink your ovaltine") 232 233 for _, suite := range encTestCases { 234 public, secret, err := GenerateParams(rand.Reader) 235 if err != nil { 236 t.Fatalf("error generating parameters: %s", err) 237 } 238 userKey, err := DeriveAttributeKeysCCA(rand.Reader, secret, suite.a) 239 if err != nil { 240 t.Fatalf("error generating Attribute keys: %s", err) 241 } 242 243 ciphertext, err := EncryptCCA(rand.Reader, public, suite.p, msg) 244 if err != nil { 245 t.Fatalf("error encrypting: %s", err) 246 } 247 recovered, err := DecryptCCA(ciphertext, userKey) 248 if err != nil { 249 t.Fatalf("error decrypting: %s", err) 250 } 251 if string(recovered) != string(msg) { 252 t.Fatalf("expected: %s, got %s", string(recovered), string(msg)) 253 } 254 } 255 } 256 257 var benchPolicy = &Policy{ 258 Inputs: []Wire{ 259 {"blocked", "", ToScalar(1), false}, 260 {"secure", "", ToScalar(1), true}, 261 {"eu", "", ToScalar(1), true}, 262 {"us", "", ToScalar(1), true}, 263 }, 264 F: Formula{ 265 Gates: []Gate{ 266 {Orgate, 2, 3, 4}, 267 {Andgate, 0, 4, 5}, 268 {Andgate, 1, 5, 6}, 269 }, 270 }, 271 } 272 273 var benchAttrs = &Attributes{ 274 "blocked": { 275 wild: false, 276 Value: ToScalar(0), 277 }, 278 "secure": { 279 wild: false, 280 Value: ToScalar(1), 281 }, 282 "us": { 283 wild: false, 284 Value: ToScalar(1), 285 }, 286 "irrelevantAttr": { 287 wild: false, 288 Value: ToScalar(1), 289 }, 290 } 291 292 func BenchmarkTkDecryption(b *testing.B) { 293 msg := []byte("drink your ovaltine") 294 295 public, secret, err := GenerateParams(rand.Reader) 296 if err != nil { 297 b.Fatalf("error generating parameters: %s", err) 298 } 299 userKey, err := DeriveAttributeKeysCCA(rand.Reader, secret, benchAttrs) 300 if err != nil { 301 b.Fatalf("error generating Attribute keys: %s", err) 302 } 303 304 ciphertext, err := EncryptCCA(rand.Reader, public, benchPolicy, msg) 305 if err != nil { 306 b.Fatalf("error encrypting: %s", err) 307 } 308 b.ResetTimer() 309 for i := 0; i < b.N; i++ { 310 _, err = DecryptCCA(ciphertext, userKey) 311 if err != nil { 312 b.Fatalf("mismatch: %s", err) 313 } 314 } 315 } 316 317 func BenchmarkTkEncryption(b *testing.B) { 318 msg := []byte("drink your ovaltine") 319 public, _, err := GenerateParams(rand.Reader) 320 if err != nil { 321 b.Fatalf("error generating parameters: %s", err) 322 } 323 b.ResetTimer() 324 for i := 0; i < b.N; i++ { 325 _, err := EncryptCCA(rand.Reader, public, benchPolicy, msg) 326 if err != nil { 327 b.Fatalf("error encrypting: %s", err) 328 } 329 } 330 } 331 332 func BenchmarkTkDerivation(b *testing.B) { 333 _, secret, err := GenerateParams(rand.Reader) 334 if err != nil { 335 b.Fatalf("error generating parameters: %s", err) 336 } 337 b.ResetTimer() 338 for i := 0; i < b.N; i++ { 339 _, err := DeriveAttributeKeysCCA(rand.Reader, secret, benchAttrs) 340 if err != nil { 341 b.Fatalf("error generating Attribute keys: %s", err) 342 } 343 } 344 }