File: wycheproof_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 (116 lines) | stat: -rw-r--r-- 2,825 bytes parent folder | download | duplicates (4)
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
108
109
110
111
112
113
114
115
116
package ed448_test

import (
	"bytes"
	"encoding/hex"
	"encoding/json"
	"io"
	"os"
	"testing"

	"github.com/cloudflare/circl/internal/test"
	"github.com/cloudflare/circl/sign/ed448"
)

type group struct {
	Key struct {
		Curve string `json:"curve"`
		Size  int    `json:"keySize"`
		Pk    string `json:"pk"`
		Sk    string `json:"sk"`
		Type  string `json:"type"`
	} `json:"key"`
	Type  string `json:"type"`
	Tests []struct {
		TcID    int      `json:"tcId"`
		Comment string   `json:"comment"`
		Msg     string   `json:"msg"`
		Sig     string   `json:"sig"`
		Result  string   `json:"result"`
		Flags   []string `json:"flags"`
	} `json:"tests"`
}

type Wycheproof struct {
	Alg     string  `json:"algorithm"`
	Version string  `json:"generatorVersion"`
	Num     int     `json:"numberOfTests"`
	Groups  []group `json:"testGroups"`
}

func (kat *Wycheproof) readFile(t *testing.T, fileName string) {
	jsonFile, err := os.Open(fileName)
	if err != nil {
		t.Fatalf("File %v can not be opened. Error: %v", fileName, err)
	}
	defer jsonFile.Close()
	input, err := io.ReadAll(jsonFile)
	if err != nil {
		t.Fatalf("File %v can not be read. Error: %v", fileName, err)
	}

	err = json.Unmarshal(input, &kat)
	if err != nil {
		t.Fatalf("File %v can not be loaded. Error: %v", fileName, err)
	}
}

func (kat *Wycheproof) keyPair(t *testing.T) {
	for i, g := range kat.Groups {
		if g.Key.Curve != "edwards448" {
			t.Errorf("Curve not expected %v", g.Key.Curve)
		}
		private, _ := hex.DecodeString(g.Key.Sk)
		public, _ := hex.DecodeString(g.Key.Pk)
		keys := ed448.NewKeyFromSeed(private)
		got := keys.Public().(ed448.PublicKey)
		want := public

		if !bytes.Equal(got, want) {
			test.ReportError(t, got, want, i, g.Key.Sk)
		}
	}
}

func (kat *Wycheproof) verify(t *testing.T) {
	ctx := []byte{}

	for i, g := range kat.Groups {
		for _, gT := range g.Tests {
			isValid := gT.Result == "valid"
			private, _ := hex.DecodeString(g.Key.Sk)
			public, _ := hex.DecodeString(g.Key.Pk)
			sig, _ := hex.DecodeString(gT.Sig)
			msg, _ := hex.DecodeString(gT.Msg)

			priv := ed448.NewKeyFromSeed(private)
			got := priv.Public().(ed448.PublicKey)
			want := public
			if !bytes.Equal(got, want) {
				test.ReportError(t, got, want, i, gT.TcID)
			}
			if isValid {
				got := ed448.Sign(priv, msg, string(ctx))
				want := sig
				if !bytes.Equal(got, want) {
					test.ReportError(t, got, want, i, gT.TcID)
				}
			}
			{
				got := ed448.Verify(priv.Public().(ed448.PublicKey), msg, sig, string(ctx))
				want := isValid
				if got != want {
					test.ReportError(t, got, want, i, gT.TcID)
				}
			}
		}
	}
}

func TestWycheproof(t *testing.T) {
	// Test vectors from Wycheproof v0.4.12
	var kat Wycheproof
	kat.readFile(t, "testdata/wycheproof_Ed448.json")
	t.Run("EDDSAKeyPair", kat.keyPair)
	t.Run("EDDSAVerify", kat.verify)
}