wire_test.go raw
1 package gnarlring
2
3 import "testing"
4
5 func TestCommitmentFrameRoundTrip(t *testing.T) {
6 pk, _ := NTRUKeyGen()
7 cc := NewChildCommitment(17, pk, nil)
8
9 data := MarshalCommitmentFrame(cc)
10 if len(data) != CommitmentFrameSize {
11 t.Fatalf("frame size = %d, want %d", len(data), CommitmentFrameSize)
12 }
13
14 cc2 := UnmarshalCommitmentFrame(data)
15 if cc2 == nil {
16 t.Fatal("UnmarshalCommitmentFrame returned nil")
17 }
18 if cc2.Index != cc.Index {
19 t.Fatalf("index mismatch: %d vs %d", cc2.Index, cc.Index)
20 }
21 if !Equal(cc2.PubKey, cc.PubKey) {
22 t.Fatal("PubKey mismatch")
23 }
24 if !Equal(cc2.W, cc.W) {
25 t.Fatal("W mismatch")
26 }
27 }
28
29 func TestEpochFrameRoundTrip(t *testing.T) {
30 pkCoord, skCoord := NTRUKeyGen()
31 es := StartEpoch(42, pkCoord)
32
33 for i := 0; i < N; i++ {
34 es.AddCommitment(NewChildCommitment(i, skCoord.PK, nil))
35 }
36 es.Finalize(skCoord, []byte("frame-test"))
37
38 data := MarshalEpochFrame(es)
39 if len(data) != EpochFrameSize {
40 t.Fatalf("epoch frame size = %d, want %d", len(data), EpochFrameSize)
41 }
42
43 es2 := UnmarshalEpochFrame(data)
44 if es2 == nil {
45 t.Fatal("UnmarshalEpochFrame returned nil")
46 }
47 if es2.Counter != es.Counter {
48 t.Fatalf("counter mismatch")
49 }
50 if !Equal(es2.Coordinator.H, pkCoord.H) {
51 t.Fatal("coordinator PK mismatch")
52 }
53 if !es2.IsFinalized() {
54 t.Fatal("epoch should be finalized after unmarshal")
55 }
56 if !es2.Verify(pkCoord, []byte("frame-test")) {
57 t.Fatal("epoch verification after unmarshal failed")
58 }
59 }
60
61 func TestEpochCheckFrameRoundTrip(t *testing.T) {
62 pkCoord, skCoord := NTRUKeyGen()
63 es := StartEpoch(99, pkCoord)
64
65 for i := 0; i < N; i++ {
66 es.AddCommitment(NewChildCommitment(i, skCoord.PK, nil))
67 }
68 es.Finalize(skCoord, []byte("check-test"))
69
70 data := MarshalEpochCheckFrame(es)
71 if len(data) != EpochCheckFrameSize {
72 t.Fatalf("check frame size = %d, want %d", len(data), EpochCheckFrameSize)
73 }
74
75 cf, err := UnmarshalEpochCheckFrame(data)
76 if err != nil {
77 t.Fatal(err)
78 }
79 if cf.Counter != es.Counter {
80 t.Fatalf("counter mismatch")
81 }
82 if !Equal(cf.PK, es.Coordinator.H) {
83 t.Fatal("pk mismatch")
84 }
85 }
86
87 func TestUnmarshalShortFrames(t *testing.T) {
88 if UnmarshalCommitmentFrame([]byte{0, 0}) != nil {
89 t.Fatal("short commitment frame should return nil")
90 }
91 if UnmarshalEpochFrame([]byte{0, 0}) != nil {
92 t.Fatal("short epoch frame should return nil")
93 }
94 _, err := UnmarshalEpochCheckFrame([]byte{0, 0})
95 if err == nil {
96 t.Fatal("short check frame should return error")
97 }
98 }
99