File: rounding_test.go

package info (click to toggle)
golang-github-cloudflare-circl 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,064 kB
  • sloc: asm: 20,492; ansic: 1,292; makefile: 68
file content (81 lines) | stat: -rw-r--r-- 1,628 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Code generated from mode3/internal/rounding_test.go by gen.go

package internal

import (
	"flag"
	"testing"

	common "github.com/cloudflare/circl/sign/internal/dilithium"
)

var runVeryLongTest = flag.Bool("very-long", false, "runs very long tests")

func TestDecompose(t *testing.T) {
	for a := uint32(0); a < common.Q; a++ {
		a0PlusQ, a1 := decompose(a)
		a0 := int32(a0PlusQ) - int32(common.Q)
		recombined := a0 + int32(Alpha*a1)
		if a1 == 0 && recombined < 0 {
			recombined += common.Q
			if -(Alpha/2) > a0 || a0 >= 0 {
				t.Fatalf("decompose(%v): a0 out of bounds", a)
			}
		} else {
			if (-(Alpha / 2) >= a0) || (a0 > Alpha/2) {
				t.Fatalf("decompose(%v): a0 out of bounds", a)
			}
		}
		if int32(a) != recombined {
			t.Fatalf("decompose(%v) doesn't recombine %v %v", a, a0, a1)
		}
	}
}

func TestMakeHint(t *testing.T) {
	if !*runVeryLongTest {
		t.SkipNow()
	}
	for w := uint32(0); w < common.Q; w++ {
		w0, w1 := decompose(w)
		for fn := uint32(0); fn <= Gamma2; fn++ {
			fsign := false
			for {
				var f uint32
				if fsign {
					if fn == 0 {
						break
					}
					f = common.Q - fn
				} else {
					f = fn
				}

				hint := makeHint(common.ReduceLe2Q(w0+common.Q-f), w1)
				w1p := useHint(common.ReduceLe2Q(w+common.Q-f), hint)
				if w1p != w1 {
					t.Fatal()
				}

				if fsign {
					break
				}
				fsign = true
			}
		}
	}
}

func BenchmarkDecompose(b *testing.B) {
	var p, p0, p1 common.Poly
	for i := 0; i < b.N; i++ {
		PolyDecompose(&p, &p0, &p1)
	}
}

func BenchmarkMakeHint(b *testing.B) {
	var p, p0, p1 common.Poly
	for i := 0; i < b.N; i++ {
		PolyMakeHint(&p, &p0, &p1)
	}
}