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

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