keypair.go raw

   1  package crypto
   2  
   3  import (
   4  	"git.mleku.dev/mleku/dendrite/pkg/axiom"
   5  	"git.mleku.dev/mleku/dendrite/pkg/lattice"
   6  	"git.mleku.dev/mleku/dendrite/pkg/spore"
   7  )
   8  
   9  // PublicKey is the lattice structure visible to anyone.
  10  // It is derived from a spore: portable, compact, and sufficient
  11  // to nucleate a lattice for encryption.
  12  type PublicKey struct {
  13  	Basis     *Basis
  14  	SporeHash string       // SHA-256 of the generating spore
  15  	Spore     *spore.Spore // the full spore for nucleation
  16  }
  17  
  18  // PrivateKey is the constraint envelope — the trapdoor.
  19  // Knowing the specific Constraint implementations allows
  20  // direct reading of the bonding pattern (efficient CVP).
  21  type PrivateKey struct {
  22  	// ConstraintFactory produces a constraint for a given type tag.
  23  	// This is the short basis — the trapdoor that makes decryption
  24  	// and signing efficient.
  25  	ConstraintFactory func(tag string) axiom.Constraint
  26  
  27  	// Lattice is the live lattice with all constraints loaded.
  28  	Lattice *lattice.Lattice
  29  }
  30  
  31  // KeyPair bundles public and private keys.
  32  type KeyPair struct {
  33  	Public  PublicKey
  34  	Private PrivateKey
  35  }
  36  
  37  // GenerateKeyPair produces a keypair from a mature lattice.
  38  // The lattice must have been through at least one grow/dissolve cycle
  39  // (i.e., it should have bonded elements and structural history).
  40  //
  41  // Algorithm:
  42  //  1. Extract spore from lattice
  43  //  2. Public key = spore's structural fingerprint
  44  //  3. Private key = the lattice itself + its constraint factory
  45  func GenerateKeyPair(l *lattice.Lattice, params Params, factory func(string) axiom.Constraint) *KeyPair {
  46  	s := spore.Extract(l)
  47  	basis := FromSpore(s, params.Q)
  48  
  49  	return &KeyPair{
  50  		Public: PublicKey{
  51  			Basis:     basis,
  52  			SporeHash: s.Hash(),
  53  			Spore:     s,
  54  		},
  55  		Private: PrivateKey{
  56  			ConstraintFactory: factory,
  57  			Lattice:           l,
  58  		},
  59  	}
  60  }
  61