File: scalar_test.go

package info (click to toggle)
golang-github-cloudflare-circl 1.0.0%2B20200724-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,788 kB
  • sloc: asm: 19,418; ansic: 1,289; makefile: 54
file content (107 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package goldilocks_test

import (
	"crypto/rand"
	"encoding/binary"
	"math/big"
	"testing"

	"github.com/cloudflare/circl/ecc/goldilocks"
	"github.com/cloudflare/circl/internal/conv"
	"github.com/cloudflare/circl/internal/test"
)

func TestReduceModOrder(t *testing.T) {
	order := goldilocks.Curve{}.Order()
	bigOrder := conv.BytesLe2BigInt(order[:])
	const max = 3*goldilocks.ScalarSize - 1
	var b [max]byte
	_, _ = rand.Read(b[:])
	var z goldilocks.Scalar
	for i := 0; i < max; i++ {
		x := b[0:i]
		bigX := conv.BytesLe2BigInt(x)

		z.FromBytes(x)
		got := conv.BytesLe2BigInt(z[:])
		got.Mod(got, bigOrder)

		want := bigX.Mod(bigX, bigOrder)

		if got.Cmp(want) != 0 {
			test.ReportError(t, got, want, x, i)
		}
	}
}

func testOp(t *testing.T,
	f func(z, x, y *goldilocks.Scalar),
	g func(z, x, y *big.Int)) {
	const testTimes = 1 << 8
	var x, y, z goldilocks.Scalar
	order := goldilocks.Curve{}.Order()
	want := new(big.Int)
	bigOrder := conv.BytesLe2BigInt(order[:])

	for i := 0; i < testTimes; i++ {
		_, _ = rand.Read(x[:])
		_, _ = rand.Read(y[:])
		bigX := conv.BytesLe2BigInt(x[:])
		bigY := conv.BytesLe2BigInt(y[:])

		f(&z, &x, &y)
		got := conv.BytesLe2BigInt(z[:])

		g(want, bigX, bigY)
		want.Mod(want, bigOrder)
		if got.Cmp(want) != 0 {
			test.ReportError(t, got.Text(16), want.Text(16),
				conv.BytesLe2Hex(x[:]),
				conv.BytesLe2Hex(y[:]))
		}
	}
}

func TestScalar(t *testing.T) {
	t.Run("Add", func(t *testing.T) {
		testOp(t,
			func(z, x, y *goldilocks.Scalar) { z.Add(x, y) },
			func(z, x, y *big.Int) { z.Add(x, y) })
	})
	t.Run("Sub", func(t *testing.T) {
		testOp(t,
			func(z, x, y *goldilocks.Scalar) { z.Sub(x, y) },
			func(z, x, y *big.Int) { z.Sub(x, y) })
	})
	t.Run("Mul", func(t *testing.T) {
		testOp(t,
			func(z, x, y *goldilocks.Scalar) { z.Mul(x, y) },
			func(z, x, y *big.Int) { z.Mul(x, y) })
	})
}

func BenchmarkScalar(b *testing.B) {
	var k [2 * goldilocks.ScalarSize]byte
	var x, y, z goldilocks.Scalar
	_ = binary.Read(rand.Reader, binary.LittleEndian, x[:])
	b.Run("Add", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			z.Add(&x, &y)
		}
	})
	b.Run("Sub", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			z.Sub(&x, &y)
		}
	})
	b.Run("Mul", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			z.Mul(&x, &y)
		}
	})
	b.Run("Red", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			z.FromBytes(k[:])
		}
	})
}