generate_test.go raw
1 package crypto
2
3 import (
4 "testing"
5 )
6
7 func TestGenerate(t *testing.T) {
8 params := DefaultParams(Security128)
9 tags := []string{"word", "punct", "space"}
10
11 kp, err := Generate(params, tags, testFactory)
12 if err != nil {
13 t.Fatalf("Generate: %v", err)
14 }
15
16 if kp.Public.Basis == nil {
17 t.Error("basis should not be nil")
18 }
19 if kp.Public.SporeHash == "" {
20 t.Error("spore hash should not be empty")
21 }
22 if kp.Private.Lattice == nil {
23 t.Error("private lattice should not be nil")
24 }
25 if kp.Private.ConstraintFactory == nil {
26 t.Error("constraint factory should not be nil")
27 }
28 }
29
30 func TestGenerateDimension(t *testing.T) {
31 params := DefaultParams(Security128)
32 tags := []string{"word", "punct"}
33
34 kp, err := Generate(params, tags, testFactory)
35 if err != nil {
36 t.Fatalf("Generate: %v", err)
37 }
38
39 // N=256, 2 tags, 128 nodes per tag = 256 total.
40 if kp.Private.Lattice.Size() != 256 {
41 t.Errorf("lattice size = %d, want 256", kp.Private.Lattice.Size())
42 }
43 if kp.Public.Basis.Dimension != 256 {
44 t.Errorf("basis dimension = %d, want 256", kp.Public.Basis.Dimension)
45 }
46 }
47
48 func TestGenerateHasOccupancy(t *testing.T) {
49 params := DefaultParams(Security128)
50 tags := []string{"word", "punct"}
51
52 kp, err := Generate(params, tags, testFactory)
53 if err != nil {
54 t.Fatalf("Generate: %v", err)
55 }
56
57 occ := occupiedCount(kp.Private.Lattice)
58 if occ == 0 {
59 t.Error("generated lattice should have occupied nodes")
60 }
61 // Should have meaningful occupancy (seeded ~50%).
62 if occ < kp.Private.Lattice.Size()/4 {
63 t.Errorf("occupancy too low: %d / %d", occ, kp.Private.Lattice.Size())
64 }
65 }
66
67 func TestGenerateInvalidParams(t *testing.T) {
68 _, err := Generate(Params{}, []string{"word"}, testFactory)
69 if err == nil {
70 t.Error("expected error with invalid params")
71 }
72 }
73
74 func TestGenerateNoTags(t *testing.T) {
75 _, err := Generate(DefaultParams(Security128), nil, testFactory)
76 if err == nil {
77 t.Error("expected error with no tags")
78 }
79 }
80
81 func TestGenerateNilFactory(t *testing.T) {
82 _, err := Generate(DefaultParams(Security128), []string{"word"}, nil)
83 if err == nil {
84 t.Error("expected error with nil factory")
85 }
86 }
87
88 func TestCloneLattice(t *testing.T) {
89 l := buildMatureLattice()
90 srcOcc := occupiedCount(l)
91 srcSize := l.Size()
92
93 clone := cloneLattice(l, testFactory)
94
95 if clone.Size() != srcSize {
96 t.Errorf("clone size = %d, want %d", clone.Size(), srcSize)
97 }
98 cloneOcc := occupiedCount(clone)
99 if cloneOcc != srcOcc {
100 t.Errorf("clone occupancy = %d, want %d", cloneOcc, srcOcc)
101 }
102
103 // Mutating the clone should not affect the original.
104 clone.Nodes()[0].Dissolve()
105 if occupiedCount(l) != srcOcc {
106 t.Error("dissolving clone node affected original lattice")
107 }
108 }
109
110 func TestCloneLatticeNeighbors(t *testing.T) {
111 l := buildMatureLattice()
112 clone := cloneLattice(l, testFactory)
113
114 // Check that neighbor relationships are preserved.
115 for i, n := range l.Nodes() {
116 srcNbCount := len(n.Neighbors())
117 cloneNbCount := len(clone.Nodes()[i].Neighbors())
118 if srcNbCount != cloneNbCount {
119 t.Errorf("node %d: src neighbors=%d, clone neighbors=%d",
120 i, srcNbCount, cloneNbCount)
121 }
122 }
123 }
124