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

     1  package tkn20_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/sha256"
     6  	"fmt"
     7  	"io"
     8  	"testing"
     9  
    10  	cpabe "github.com/cloudflare/circl/abe/cpabe/tkn20"
    11  	"github.com/cloudflare/circl/internal/test"
    12  	"github.com/cloudflare/circl/xof"
    13  )
    14  
    15  func TestLongPlaintext(t *testing.T) {
    16  	// Fixed PRNG for test reproducibility.
    17  	prng := xof.SHAKE128.New()
    18  
    19  	pk, msk, err := cpabe.Setup(prng)
    20  	test.CheckNoErr(t, err, "setup failed")
    21  
    22  	attrs := cpabe.Attributes{}
    23  	attrs.FromMap(map[string]string{
    24  		"occupation": "doctor",
    25  		"country":    "US",
    26  		"age":        "16",
    27  	})
    28  
    29  	sk, err := msk.KeyGen(prng, attrs)
    30  	test.CheckNoErr(t, err, "master key generation failed")
    31  
    32  	policy := cpabe.Policy{}
    33  	err = policy.FromString(`(occupation: doctor) and (country: US)`)
    34  	test.CheckNoErr(t, err, "policy parsing failed")
    35  
    36  	const N = 20 // 2^N bytes of plaintext
    37  	buffer := make([]byte, 1<<N)
    38  	_, err = io.ReadFull(prng, buffer)
    39  	test.CheckNoErr(t, err, "reading message from prgn failed")
    40  
    41  	for i := 0; i < N; i++ {
    42  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    43  			msg := buffer[:(1 << i)]
    44  
    45  			ct, err := pk.Encrypt(prng, policy, msg)
    46  			test.CheckNoErr(t, err, "encryption failed")
    47  
    48  			t.Logf("length pt: %v ct: %v", len(msg), len(ct))
    49  
    50  			pt, err := sk.Decrypt(ct)
    51  			test.CheckNoErr(t, err, "decryption failed")
    52  
    53  			got := sha256.Sum256(pt)
    54  			want := sha256.Sum256(msg)
    55  			if !bytes.Equal(got[:], want[:]) {
    56  				test.ReportError(t, got, want)
    57  			}
    58  		})
    59  	}
    60  }