params_test.go raw

   1  package crypto
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/gnarl-hamadryad/ratio"
   7  )
   8  
   9  func TestDefaultParamsValid(t *testing.T) {
  10  	for _, level := range []SecurityLevel{Security128, Security192, Security256} {
  11  		p := DefaultParams(level)
  12  		if !p.Valid() {
  13  			t.Errorf("DefaultParams(%d) is not valid", level)
  14  		}
  15  	}
  16  }
  17  
  18  func TestParamsDimensions(t *testing.T) {
  19  	cases := []struct {
  20  		level SecurityLevel
  21  		n     int
  22  		q     int64
  23  	}{
  24  		{Security128, 256, 127},
  25  		{Security192, 384, 251},
  26  		{Security256, 512, 509},
  27  	}
  28  	for _, tc := range cases {
  29  		p := DefaultParams(tc.level)
  30  		if p.N != tc.n {
  31  			t.Errorf("level %d: N = %d, want %d", tc.level, p.N, tc.n)
  32  		}
  33  		if !p.Q.Equal(ratio.FromInt(tc.q)) {
  34  			t.Errorf("level %d: Q = %s, want %d/1", tc.level, p.Q, tc.q)
  35  		}
  36  	}
  37  }
  38  
  39  func TestParamsSmoothingLessThanNoise(t *testing.T) {
  40  	for _, level := range []SecurityLevel{Security128, Security192, Security256} {
  41  		p := DefaultParams(level)
  42  		if !p.SmoothingParam.Less(p.NoiseWidth) {
  43  			t.Errorf("level %d: smoothing %s not less than noise %s",
  44  				level, p.SmoothingParam, p.NoiseWidth)
  45  		}
  46  	}
  47  }
  48  
  49  func TestParamsNoiseLessThanOne(t *testing.T) {
  50  	for _, level := range []SecurityLevel{Security128, Security192, Security256} {
  51  		p := DefaultParams(level)
  52  		if !p.NoiseWidth.Less(ratio.One) {
  53  			t.Errorf("level %d: noise %s not less than 1", level, p.NoiseWidth)
  54  		}
  55  	}
  56  }
  57  
  58  func TestInvalidParams(t *testing.T) {
  59  	cases := []struct {
  60  		name string
  61  		p    Params
  62  	}{
  63  		{"zero N", Params{N: 0, Q: ratio.FromInt(127),
  64  			SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10),
  65  			MaxWalkSteps: 1000, DissolutionPasses: 2}},
  66  		{"negative N", Params{N: -1, Q: ratio.FromInt(127),
  67  			SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10),
  68  			MaxWalkSteps: 1000, DissolutionPasses: 2}},
  69  		{"non-integer Q", Params{N: 256, Q: ratio.New(1, 2),
  70  			SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10),
  71  			MaxWalkSteps: 1000, DissolutionPasses: 2}},
  72  		{"smoothing >= noise", Params{N: 256, Q: ratio.FromInt(127),
  73  			SmoothingParam: ratio.New(5, 10), NoiseWidth: ratio.New(3, 10),
  74  			MaxWalkSteps: 1000, DissolutionPasses: 2}},
  75  		{"noise >= 1", Params{N: 256, Q: ratio.FromInt(127),
  76  			SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.FromInt(1),
  77  			MaxWalkSteps: 1000, DissolutionPasses: 2}},
  78  		{"zero walk steps", Params{N: 256, Q: ratio.FromInt(127),
  79  			SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10),
  80  			MaxWalkSteps: 0, DissolutionPasses: 2}},
  81  		{"zero dissolution", Params{N: 256, Q: ratio.FromInt(127),
  82  			SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10),
  83  			MaxWalkSteps: 1000, DissolutionPasses: 0}},
  84  	}
  85  	for _, tc := range cases {
  86  		if tc.p.Valid() {
  87  			t.Errorf("%s: expected invalid, got valid", tc.name)
  88  		}
  89  	}
  90  }
  91