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

     1  package tkn
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  )
     7  
     8  func TestShare(t *testing.T) {
     9  	f := Formula{
    10  		Gates: []Gate{
    11  			{Andgate, 2, 3, 4},
    12  			{Andgate, 0, 1, 3},
    13  		},
    14  	}
    15  	k, err := randomMatrixZp(rand.Reader, 1, 17)
    16  	if err != nil {
    17  		t.Fatalf("error generating vector: %s", err)
    18  	}
    19  	res, err := f.share(rand.Reader, k)
    20  	if err != nil {
    21  		t.Fatalf("error sharing: %s", err)
    22  	}
    23  	if len(res) != 3 {
    24  		t.Errorf("res wrong size")
    25  	}
    26  	acc := newMatrixZp(1, 17)
    27  	for i := 0; i < len(res); i++ {
    28  		acc.add(acc, res[i])
    29  	}
    30  	if !acc.Equal(k) {
    31  		t.Errorf("incorrect share")
    32  	}
    33  }
    34  
    35  func TestFormulaMarshal(t *testing.T) {
    36  	f := Formula{
    37  		Gates: []Gate{
    38  			{Andgate, 0, 1, 3},
    39  			{Andgate, 2, 3, 4},
    40  		},
    41  	}
    42  	data, err := f.MarshalBinary()
    43  	if err != nil {
    44  		t.Fatalf("error marshalling: %s", err)
    45  	}
    46  	g := &Formula{}
    47  	err = g.UnmarshalBinary(data)
    48  	if err != nil {
    49  		t.Fatalf("error unmarshalling: %s", err)
    50  	}
    51  	if !f.Equal(*g) {
    52  		t.Fatal("failure to recover formula")
    53  	}
    54  }