package gnarlring import "testing" func TestCommitmentFrameRoundTrip(t *testing.T) { pk, _ := NTRUKeyGen() cc := NewChildCommitment(17, pk, nil) data := MarshalCommitmentFrame(cc) if len(data) != CommitmentFrameSize { t.Fatalf("frame size = %d, want %d", len(data), CommitmentFrameSize) } cc2 := UnmarshalCommitmentFrame(data) if cc2 == nil { t.Fatal("UnmarshalCommitmentFrame returned nil") } if cc2.Index != cc.Index { t.Fatalf("index mismatch: %d vs %d", cc2.Index, cc.Index) } if !Equal(cc2.PubKey, cc.PubKey) { t.Fatal("PubKey mismatch") } if !Equal(cc2.W, cc.W) { t.Fatal("W mismatch") } } func TestEpochFrameRoundTrip(t *testing.T) { pkCoord, skCoord := NTRUKeyGen() es := StartEpoch(42, pkCoord) for i := 0; i < N; i++ { es.AddCommitment(NewChildCommitment(i, skCoord.PK, nil)) } es.Finalize(skCoord, []byte("frame-test")) data := MarshalEpochFrame(es) if len(data) != EpochFrameSize { t.Fatalf("epoch frame size = %d, want %d", len(data), EpochFrameSize) } es2 := UnmarshalEpochFrame(data) if es2 == nil { t.Fatal("UnmarshalEpochFrame returned nil") } if es2.Counter != es.Counter { t.Fatalf("counter mismatch") } if !Equal(es2.Coordinator.H, pkCoord.H) { t.Fatal("coordinator PK mismatch") } if !es2.IsFinalized() { t.Fatal("epoch should be finalized after unmarshal") } if !es2.Verify(pkCoord, []byte("frame-test")) { t.Fatal("epoch verification after unmarshal failed") } } func TestEpochCheckFrameRoundTrip(t *testing.T) { pkCoord, skCoord := NTRUKeyGen() es := StartEpoch(99, pkCoord) for i := 0; i < N; i++ { es.AddCommitment(NewChildCommitment(i, skCoord.PK, nil)) } es.Finalize(skCoord, []byte("check-test")) data := MarshalEpochCheckFrame(es) if len(data) != EpochCheckFrameSize { t.Fatalf("check frame size = %d, want %d", len(data), EpochCheckFrameSize) } cf, err := UnmarshalEpochCheckFrame(data) if err != nil { t.Fatal(err) } if cf.Counter != es.Counter { t.Fatalf("counter mismatch") } if !Equal(cf.PK, es.Coordinator.H) { t.Fatal("pk mismatch") } } func TestUnmarshalShortFrames(t *testing.T) { if UnmarshalCommitmentFrame([]byte{0, 0}) != nil { t.Fatal("short commitment frame should return nil") } if UnmarshalEpochFrame([]byte{0, 0}) != nil { t.Fatal("short epoch frame should return nil") } _, err := UnmarshalEpochCheckFrame([]byte{0, 0}) if err == nil { t.Fatal("short check frame should return error") } }