File: xwing_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 (79 lines) | stat: -rw-r--r-- 1,593 bytes parent folder | download | duplicates (2)
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
package xwing

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	"github.com/cloudflare/circl/internal/sha3"
)

func writeHex(w io.Writer, prefix string, val interface{}) {
	indent := "  "
	width := 74
	hex := fmt.Sprintf("%x", val)
	if len(prefix)+len(hex)+5 < width {
		fmt.Fprintf(w, "%s     %s\n", prefix, hex)
		return
	}
	fmt.Fprintf(w, "%s\n", prefix)
	for len(hex) != 0 {
		var toPrint string
		if len(hex) < width-len(indent) {
			toPrint = hex
			hex = ""
		} else {
			toPrint = hex[:width-len(indent)]
			hex = hex[width-len(indent):]
		}
		fmt.Fprintf(w, "%s%s\n", indent, toPrint)
	}
}

func TestVectors(t *testing.T) {
	h := sha3.NewShake128()
	w := new(bytes.Buffer)

	for i := 0; i < 3; i++ {
		var seed [SeedSize]byte
		_, _ = h.Read(seed[:])
		writeHex(w, "seed", seed)

		sk, pk := DeriveKeyPairPacked(seed[:])
		writeHex(w, "sk", sk)
		writeHex(w, "pk", pk)

		var eseed [EncapsulationSeedSize]byte
		_, _ = h.Read(eseed[:])
		writeHex(w, "eseed", eseed)

		ss, ct, err := Encapsulate(pk, eseed[:])
		if err != nil {
			t.Fatal(err)
		}
		writeHex(w, "ct", ct)
		writeHex(w, "ss", ss)

		ss2 := Decapsulate(ct, sk)
		if !bytes.Equal(ss, ss2) {
			t.Fatal()
		}

		fmt.Fprintf(w, "\n")
	}

	t.Logf("%s", w.String())
	h.Reset()
	_, _ = h.Write(w.Bytes())
	var cs [32]byte
	_, _ = h.Read(cs[:])
	got := fmt.Sprintf("%x", cs)

	// shake128 of spec/test-vectors.txt from X-Wing spec at
	// https://github.com/dconnolly/draft-connolly-cfrg-xwing-kem
	want := "1bcd0057d861d6b866239936cadcaeee1ec0164dedc181c386e9e54fe46156fe"
	if got != want {
		t.Fatalf("%s ≠ %s", got, want)
	}
}