params.mx raw

   1  // Lattice-based cryptographic parameter sets.
   2  package hamcrypto
   3  
   4  import "git.smesh.lol/gnarl-hamadryad/moxie/ratio"
   5  
   6  // SecurityLevel specifies cryptographic strength.
   7  type SecurityLevel uint8
   8  
   9  const (
  10  	Security128 SecurityLevel = iota
  11  	Security192
  12  	Security256
  13  )
  14  
  15  // Params holds the parameters for a lattice-based cryptographic instance.
  16  type Params struct {
  17  	N                 int32
  18  	Q                 ratio.Ratio
  19  	SmoothingParam    ratio.Ratio
  20  	NoiseWidth        ratio.Ratio
  21  	MaxWalkSteps      int32
  22  	DissolutionPasses int32
  23  }
  24  
  25  // DefaultParams returns parameters for a given security level.
  26  func DefaultParams(level SecurityLevel) (p Params) {
  27  	switch level {
  28  	case Security192:
  29  		return Params{
  30  			N:                 384,
  31  			Q:                 ratio.FromInt(251),
  32  			SmoothingParam:    ratio.New(2, 10),
  33  			NoiseWidth:        ratio.New(3, 10),
  34  			MaxWalkSteps:      1600,
  35  			DissolutionPasses: 3,
  36  		}
  37  	case Security256:
  38  		return Params{
  39  			N:                 512,
  40  			Q:                 ratio.FromInt(509),
  41  			SmoothingParam:    ratio.New(2, 10),
  42  			NoiseWidth:        ratio.New(3, 10),
  43  			MaxWalkSteps:      2000,
  44  			DissolutionPasses: 4,
  45  		}
  46  	default:
  47  		return Params{
  48  			N:                 256,
  49  			Q:                 ratio.FromInt(127),
  50  			SmoothingParam:    ratio.New(2, 10),
  51  			NoiseWidth:        ratio.New(3, 10),
  52  			MaxWalkSteps:      1024,
  53  			DissolutionPasses: 2,
  54  		}
  55  	}
  56  }
  57  
  58  // Valid reports whether the parameters are internally consistent.
  59  func (p *Params) Valid() (ok bool) {
  60  	if p.N <= 0 {
  61  		return false
  62  	}
  63  	if !p.Q.IsPositive() || p.Q.Denom != 1 {
  64  		return false
  65  	}
  66  	if !p.SmoothingParam.IsPositive() {
  67  		return false
  68  	}
  69  	if !p.NoiseWidth.IsPositive() {
  70  		return false
  71  	}
  72  	if !p.SmoothingParam.Less(&p.NoiseWidth) {
  73  		return false
  74  	}
  75  	if !p.NoiseWidth.Less(&ratio.One) {
  76  		return false
  77  	}
  78  	if p.MaxWalkSteps <= 0 {
  79  		return false
  80  	}
  81  	if p.DissolutionPasses <= 0 {
  82  		return false
  83  	}
  84  	return true
  85  }
  86