package crypto import ( "testing" "git.smesh.lol/gnarl-hamadryad/ratio" ) func TestDefaultParamsValid(t *testing.T) { for _, level := range []SecurityLevel{Security128, Security192, Security256} { p := DefaultParams(level) if !p.Valid() { t.Errorf("DefaultParams(%d) is not valid", level) } } } func TestParamsDimensions(t *testing.T) { cases := []struct { level SecurityLevel n int q int64 }{ {Security128, 256, 127}, {Security192, 384, 251}, {Security256, 512, 509}, } for _, tc := range cases { p := DefaultParams(tc.level) if p.N != tc.n { t.Errorf("level %d: N = %d, want %d", tc.level, p.N, tc.n) } if !p.Q.Equal(ratio.FromInt(tc.q)) { t.Errorf("level %d: Q = %s, want %d/1", tc.level, p.Q, tc.q) } } } func TestParamsSmoothingLessThanNoise(t *testing.T) { for _, level := range []SecurityLevel{Security128, Security192, Security256} { p := DefaultParams(level) if !p.SmoothingParam.Less(p.NoiseWidth) { t.Errorf("level %d: smoothing %s not less than noise %s", level, p.SmoothingParam, p.NoiseWidth) } } } func TestParamsNoiseLessThanOne(t *testing.T) { for _, level := range []SecurityLevel{Security128, Security192, Security256} { p := DefaultParams(level) if !p.NoiseWidth.Less(ratio.One) { t.Errorf("level %d: noise %s not less than 1", level, p.NoiseWidth) } } } func TestInvalidParams(t *testing.T) { cases := []struct { name string p Params }{ {"zero N", Params{N: 0, Q: ratio.FromInt(127), SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10), MaxWalkSteps: 1000, DissolutionPasses: 2}}, {"negative N", Params{N: -1, Q: ratio.FromInt(127), SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10), MaxWalkSteps: 1000, DissolutionPasses: 2}}, {"non-integer Q", Params{N: 256, Q: ratio.New(1, 2), SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10), MaxWalkSteps: 1000, DissolutionPasses: 2}}, {"smoothing >= noise", Params{N: 256, Q: ratio.FromInt(127), SmoothingParam: ratio.New(5, 10), NoiseWidth: ratio.New(3, 10), MaxWalkSteps: 1000, DissolutionPasses: 2}}, {"noise >= 1", Params{N: 256, Q: ratio.FromInt(127), SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.FromInt(1), MaxWalkSteps: 1000, DissolutionPasses: 2}}, {"zero walk steps", Params{N: 256, Q: ratio.FromInt(127), SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10), MaxWalkSteps: 0, DissolutionPasses: 2}}, {"zero dissolution", Params{N: 256, Q: ratio.FromInt(127), SmoothingParam: ratio.New(2, 10), NoiseWidth: ratio.New(3, 10), MaxWalkSteps: 1000, DissolutionPasses: 0}}, } for _, tc := range cases { if tc.p.Valid() { t.Errorf("%s: expected invalid, got valid", tc.name) } } }