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

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Generates golden files for tests.
     5  package main
     6  
     7  import (
     8  	"encoding"
     9  	"os"
    10  
    11  	cpabe "github.com/cloudflare/circl/abe/cpabe/tkn20"
    12  	"github.com/cloudflare/circl/xof"
    13  )
    14  
    15  func writeToFile(name string, data []byte) {
    16  	err := os.WriteFile("testdata/"+name, data, 0o644)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  }
    21  
    22  func dumpToFile(name string, m encoding.BinaryMarshaler) {
    23  	data, err := m.MarshalBinary()
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	writeToFile(name, data)
    28  }
    29  
    30  func main() {
    31  	// Using fixed PRNG for reproducibility,
    32  	prng := xof.SHAKE128.New()
    33  
    34  	err := os.MkdirAll("testdata", 0o755)
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  
    39  	publicParams, secretParams, err := cpabe.Setup(prng)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	dumpToFile("publicKey", &publicParams)
    45  	dumpToFile("secretKey", &secretParams)
    46  
    47  	attrs := cpabe.Attributes{}
    48  	attrs.FromMap(map[string]string{"country": "NL", "EU": "true"})
    49  
    50  	policy := cpabe.Policy{}
    51  	err = policy.FromString("EU: true")
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	msg := []byte("Be sure to drink your ovaltine!")
    56  	ciphertext, err := publicParams.Encrypt(prng, policy, msg)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  	writeToFile("ciphertext", ciphertext)
    61  
    62  	key, err := secretParams.KeyGen(prng, attrs)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	dumpToFile("attributeKey", &key)
    67  }