keypair_test.go raw

   1  package crypto
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.mleku.dev/mleku/dendrite/pkg/axiom"
   7  	"git.mleku.dev/mleku/dendrite/pkg/lattice"
   8  )
   9  
  10  func testFactory(tag string) axiom.Constraint {
  11  	return tagConstraint{tag}
  12  }
  13  
  14  func buildMatureLattice() *lattice.Lattice {
  15  	l := lattice.New()
  16  	var nodes []*lattice.Node
  17  	for range 20 {
  18  		n := l.AddNode([]axiom.Constraint{tagConstraint{"word"}})
  19  		nodes = append(nodes, n)
  20  	}
  21  	for range 10 {
  22  		n := l.AddNode([]axiom.Constraint{tagConstraint{"punct"}})
  23  		nodes = append(nodes, n)
  24  	}
  25  	// Ring within first 20.
  26  	for i := 0; i < 20; i++ {
  27  		l.Connect(nodes[i], nodes[(i+1)%20])
  28  	}
  29  	// Ring within last 10.
  30  	for i := 20; i < 30; i++ {
  31  		l.Connect(nodes[i], nodes[20+(i+1-20)%10])
  32  	}
  33  	// Cross-connect.
  34  	l.Connect(nodes[0], nodes[20])
  35  	l.Connect(nodes[5], nodes[25])
  36  
  37  	// Bond some elements.
  38  	for i := 0; i < 15; i++ {
  39  		nodes[i].Bond(testElem{"word", string(rune('a' + i))})
  40  	}
  41  	for i := 20; i < 27; i++ {
  42  		nodes[i].Bond(testElem{"punct", "."})
  43  	}
  44  	return l
  45  }
  46  
  47  func TestGenerateKeyPair(t *testing.T) {
  48  	l := buildMatureLattice()
  49  	params := DefaultParams(Security128)
  50  	kp := GenerateKeyPair(l, params, testFactory)
  51  
  52  	if kp.Public.SporeHash == "" {
  53  		t.Error("spore hash should not be empty")
  54  	}
  55  	if kp.Public.Basis == nil {
  56  		t.Error("basis should not be nil")
  57  	}
  58  	if kp.Public.Spore == nil {
  59  		t.Error("spore should not be nil")
  60  	}
  61  	if kp.Private.Lattice != l {
  62  		t.Error("private key should reference the lattice")
  63  	}
  64  	if kp.Private.ConstraintFactory == nil {
  65  		t.Error("constraint factory should not be nil")
  66  	}
  67  }
  68  
  69  func TestKeyPairBasisDimension(t *testing.T) {
  70  	l := buildMatureLattice()
  71  	params := DefaultParams(Security128)
  72  	kp := GenerateKeyPair(l, params, testFactory)
  73  
  74  	if kp.Public.Basis.Dimension != l.Size() {
  75  		t.Errorf("basis dimension = %d, want %d", kp.Public.Basis.Dimension, l.Size())
  76  	}
  77  }
  78  
  79  func TestKeyPairBasisTags(t *testing.T) {
  80  	l := buildMatureLattice()
  81  	params := DefaultParams(Security128)
  82  	kp := GenerateKeyPair(l, params, testFactory)
  83  
  84  	if len(kp.Public.Basis.Tags) != 2 {
  85  		t.Errorf("expected 2 tags, got %d", len(kp.Public.Basis.Tags))
  86  	}
  87  	// Tags should be sorted: punct, word.
  88  	if kp.Public.Basis.Tags[0] != "punct" || kp.Public.Basis.Tags[1] != "word" {
  89  		t.Errorf("tags = %v, want [punct, word]", kp.Public.Basis.Tags)
  90  	}
  91  }
  92  
  93  func TestKeyPairFactoryProducesValidConstraints(t *testing.T) {
  94  	l := buildMatureLattice()
  95  	params := DefaultParams(Security128)
  96  	kp := GenerateKeyPair(l, params, testFactory)
  97  
  98  	for _, tag := range kp.Public.Basis.Tags {
  99  		c := kp.Private.ConstraintFactory(tag)
 100  		if c.Tag() != tag {
 101  			t.Errorf("factory(%q).Tag() = %q", tag, c.Tag())
 102  		}
 103  		// Constraint should admit matching elements.
 104  		e := testElem{tag, "test"}
 105  		if !c.Admits(e) {
 106  			t.Errorf("factory(%q) does not admit matching element", tag)
 107  		}
 108  		// Should reject mismatched elements.
 109  		e2 := testElem{"other", "test"}
 110  		if c.Admits(e2) {
 111  			t.Errorf("factory(%q) admits mismatched element", tag)
 112  		}
 113  	}
 114  }
 115