-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathxwing_test.go
79 lines (67 loc) · 1.56 KB
/
xwing_test.go
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)
}
}