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

     1  // Code generated from mode3/internal/rounding_test.go by gen.go
     2  
     3  package internal
     4  
     5  import (
     6  	"flag"
     7  	"testing"
     8  
     9  	common "github.com/cloudflare/circl/sign/internal/dilithium"
    10  )
    11  
    12  var runVeryLongTest = flag.Bool("very-long", false, "runs very long tests")
    13  
    14  func TestDecompose(t *testing.T) {
    15  	for a := uint32(0); a < common.Q; a++ {
    16  		a0PlusQ, a1 := decompose(a)
    17  		a0 := int32(a0PlusQ) - int32(common.Q)
    18  		recombined := a0 + int32(Alpha*a1)
    19  		if a1 == 0 && recombined < 0 {
    20  			recombined += common.Q
    21  			if -(Alpha/2) > a0 || a0 >= 0 {
    22  				t.Fatalf("decompose(%v): a0 out of bounds", a)
    23  			}
    24  		} else {
    25  			if (-(Alpha / 2) >= a0) || (a0 > Alpha/2) {
    26  				t.Fatalf("decompose(%v): a0 out of bounds", a)
    27  			}
    28  		}
    29  		if int32(a) != recombined {
    30  			t.Fatalf("decompose(%v) doesn't recombine %v %v", a, a0, a1)
    31  		}
    32  	}
    33  }
    34  
    35  func TestMakeHint(t *testing.T) {
    36  	if !*runVeryLongTest {
    37  		t.SkipNow()
    38  	}
    39  	for w := uint32(0); w < common.Q; w++ {
    40  		w0, w1 := decompose(w)
    41  		for fn := uint32(0); fn <= Gamma2; fn++ {
    42  			fsign := false
    43  			for {
    44  				var f uint32
    45  				if fsign {
    46  					if fn == 0 {
    47  						break
    48  					}
    49  					f = common.Q - fn
    50  				} else {
    51  					f = fn
    52  				}
    53  
    54  				hint := makeHint(common.ReduceLe2Q(w0+common.Q-f), w1)
    55  				w1p := useHint(common.ReduceLe2Q(w+common.Q-f), hint)
    56  				if w1p != w1 {
    57  					t.Fatal()
    58  				}
    59  
    60  				if fsign {
    61  					break
    62  				}
    63  				fsign = true
    64  			}
    65  		}
    66  	}
    67  }
    68  
    69  func BenchmarkDecompose(b *testing.B) {
    70  	var p, p0, p1 common.Poly
    71  	for i := 0; i < b.N; i++ {
    72  		PolyDecompose(&p, &p0, &p1)
    73  	}
    74  }
    75  
    76  func BenchmarkMakeHint(b *testing.B) {
    77  	var p, p0, p1 common.Poly
    78  	for i := 0; i < b.N; i++ {
    79  		PolyMakeHint(&p, &p0, &p1)
    80  	}
    81  }