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

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