params.go raw

   1  // Package crypto implements lattice-based cryptographic primitives using
   2  // dendrite's accretion/dissolution dynamics as the physical substrate.
   3  //
   4  // The structural identity: dendrite's lattice IS a lattice-crypto system.
   5  // Constraint envelopes are the short basis (trapdoor). Spore fingerprints
   6  // are the public basis. ContextualLockIn is the BDD margin. Dissolution
   7  // is discrete Gaussian sampling. The CVP decision is Node.Bond().
   8  //
   9  // Security rests on the assumption that recovering specific constraint
  10  // implementations from a spore's structural fingerprint is at least as
  11  // hard as CVP on the constraint graph. This is plausible but unproven —
  12  // see PLAN-lattice-crypto.md for the honest assessment.
  13  package crypto
  14  
  15  import "git.mleku.dev/mleku/dendrite/pkg/ratio"
  16  
  17  // SecurityLevel specifies cryptographic strength.
  18  type SecurityLevel uint8
  19  
  20  const (
  21  	Security128 SecurityLevel = iota
  22  	Security192
  23  	Security256
  24  )
  25  
  26  // Params holds the parameters for a DendriteLWE instance.
  27  type Params struct {
  28  	// N is the lattice dimension — number of constraint sites.
  29  	// Higher N = harder CVP = more security.
  30  	N int
  31  
  32  	// Q is the modulus for arithmetic operations.
  33  	// Integer-valued ratio (Denom == 1).
  34  	Q ratio.Ratio
  35  
  36  	// SmoothingParam is the dissolution threshold below which
  37  	// the lattice structure disappears into noise. This is eta_eps.
  38  	// Elements with ContextualLockIn < SmoothingParam are dissolved.
  39  	SmoothingParam ratio.Ratio
  40  
  41  	// NoiseWidth controls the spread of the discrete Gaussian.
  42  	// More noise = more security, less noise = more reliable decryption.
  43  	// Must satisfy: SmoothingParam < NoiseWidth < 1.
  44  	NoiseWidth ratio.Ratio
  45  
  46  	// MaxWalkSteps limits Brownian walk length during encryption.
  47  	MaxWalkSteps int
  48  
  49  	// DissolutionPasses is how many dissolution scans to run
  50  	// after bonding, to add the LWE noise term.
  51  	DissolutionPasses int
  52  }
  53  
  54  // DefaultParams returns parameters for a given security level.
  55  //
  56  //	128-bit: N=256,  Q=127/1, noise=3/10, smoothing=2/10
  57  //	192-bit: N=384,  Q=251/1, noise=3/10, smoothing=2/10
  58  //	256-bit: N=512,  Q=509/1, noise=3/10, smoothing=2/10
  59  func DefaultParams(level SecurityLevel) Params {
  60  	switch level {
  61  	case Security192:
  62  		return Params{
  63  			N:                 384,
  64  			Q:                 ratio.FromInt(251),
  65  			SmoothingParam:    ratio.New(2, 10),
  66  			NoiseWidth:        ratio.New(3, 10),
  67  			MaxWalkSteps:      1600, // 10^2 × 2^4 — epoch-aligned
  68  			DissolutionPasses: 3,
  69  		}
  70  	case Security256:
  71  		return Params{
  72  			N:                 512,
  73  			Q:                 ratio.FromInt(509),
  74  			SmoothingParam:    ratio.New(2, 10),
  75  			NoiseWidth:        ratio.New(3, 10),
  76  			MaxWalkSteps:      2000, // 10^3 × 2^1 — already epoch-aligned
  77  			DissolutionPasses: 4,
  78  		}
  79  	default: // Security128
  80  		return Params{
  81  			N:                 256,
  82  			Q:                 ratio.FromInt(127),
  83  			SmoothingParam:    ratio.New(2, 10),
  84  			NoiseWidth:        ratio.New(3, 10),
  85  			MaxWalkSteps:      1024, // 2^10 — epoch-aligned with N=256=2^8
  86  			DissolutionPasses: 2,
  87  		}
  88  	}
  89  }
  90  
  91  // Valid reports whether the parameters are internally consistent.
  92  func (p Params) Valid() bool {
  93  	if p.N <= 0 {
  94  		return false
  95  	}
  96  	if !p.Q.IsPositive() || p.Q.Denom != 1 {
  97  		return false // Q must be a positive integer
  98  	}
  99  	if !p.SmoothingParam.IsPositive() {
 100  		return false
 101  	}
 102  	if !p.NoiseWidth.IsPositive() {
 103  		return false
 104  	}
 105  	// SmoothingParam < NoiseWidth < 1
 106  	if !p.SmoothingParam.Less(p.NoiseWidth) {
 107  		return false
 108  	}
 109  	if !p.NoiseWidth.Less(ratio.One) {
 110  		return false
 111  	}
 112  	if p.MaxWalkSteps <= 0 {
 113  		return false
 114  	}
 115  	if p.DissolutionPasses <= 0 {
 116  		return false
 117  	}
 118  	return true
 119  }
 120