github.com/cloudflare/circl@v1.5.0/sign/dilithium/mode3/internal/pack_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"testing"
     5  
     6  	common "github.com/cloudflare/circl/sign/internal/dilithium"
     7  )
     8  
     9  func TestPolyPackLeqEta(t *testing.T) {
    10  	var p1, p2 common.Poly
    11  	var seed [64]byte
    12  	var buf [PolyLeqEtaSize]byte
    13  
    14  	for i := uint16(0); i < 100; i++ {
    15  		// Note that DeriveUniformLeqEta sets p to the right kind of
    16  		// unnormalized vector.
    17  		PolyDeriveUniformLeqEta(&p1, &seed, i)
    18  		for j := 0; j < PolyLeqEtaSize; j++ {
    19  			if p1[j] < common.Q-Eta || p1[j] > common.Q+Eta {
    20  				t.Fatalf("DerveUniformLeqEta out of bounds")
    21  			}
    22  		}
    23  		PolyPackLeqEta(&p1, buf[:])
    24  		PolyUnpackLeqEta(&p2, buf[:])
    25  		if p1 != p2 {
    26  			t.Fatalf("%v != %v", p1, p2)
    27  		}
    28  	}
    29  }
    30  
    31  func TestPolyPackT1(t *testing.T) {
    32  	var p1, p2 common.Poly
    33  	var seed [32]byte
    34  	var buf [common.PolyT1Size]byte
    35  
    36  	for i := uint16(0); i < 100; i++ {
    37  		PolyDeriveUniform(&p1, &seed, i)
    38  		p1.Normalize()
    39  		for j := 0; j < common.N; j++ {
    40  			p1[j] &= 0x1ff
    41  		}
    42  		p1.PackT1(buf[:])
    43  		p2.UnpackT1(buf[:])
    44  		if p1 != p2 {
    45  			t.Fatalf("%v != %v", p1, p2)
    46  		}
    47  	}
    48  }
    49  
    50  func TestPolyPackT0(t *testing.T) {
    51  	var p, p0, p1, p2 common.Poly
    52  	var seed [32]byte
    53  	var buf [common.PolyT0Size]byte
    54  
    55  	for i := uint16(0); i < 100; i++ {
    56  		PolyDeriveUniform(&p, &seed, i)
    57  		p.Normalize()
    58  		p.Power2Round(&p0, &p1)
    59  
    60  		p0.PackT0(buf[:])
    61  		p2.UnpackT0(buf[:])
    62  		if p0 != p2 {
    63  			t.Fatalf("%v !=\n%v", p0, p2)
    64  		}
    65  	}
    66  }
    67  
    68  func BenchmarkUnpackLeGamma1(b *testing.B) {
    69  	var p common.Poly
    70  	var buf [PolyLeGamma1Size]byte
    71  	for i := 0; i < b.N; i++ {
    72  		PolyUnpackLeGamma1(&p, buf[:])
    73  	}
    74  }
    75  
    76  func TestPolyPackLeGamma1(t *testing.T) {
    77  	var p0, p1 common.Poly
    78  	var seed [64]byte
    79  	var buf [PolyLeGamma1Size]byte
    80  
    81  	for i := uint16(0); i < 100; i++ {
    82  		PolyDeriveUniformLeGamma1(&p0, &seed, i)
    83  		p0.Normalize()
    84  
    85  		PolyPackLeGamma1(&p0, buf[:])
    86  		PolyUnpackLeGamma1(&p1, buf[:])
    87  		if p0 != p1 {
    88  			t.Fatalf("%v != %v", p0, p1)
    89  		}
    90  	}
    91  }