light: ensure trust level is strictly less than 1 (#6447)

This commit is contained in:
Callum Waters
2021-05-11 15:13:52 +02:00
committed by GitHub
parent d1c9463bef
commit 811dbe439f
2 changed files with 14 additions and 13 deletions

View File

@@ -167,10 +167,10 @@ func Verify(
// ValidateTrustLevel checks that trustLevel is within the allowed range [1/3,
// 1]. If not, it returns an error. 1/3 is the minimum amount of trust needed
// which does not break the security model.
// which does not break the security model. Must be strictly less than 1.
func ValidateTrustLevel(lvl tmmath.Fraction) error {
if lvl.Numerator*3 < lvl.Denominator || // < 1/3
lvl.Numerator > lvl.Denominator || // > 1
lvl.Numerator >= lvl.Denominator || // >= 1
lvl.Denominator == 0 {
return fmt.Errorf("trustLevel must be within [1/3, 1], given %v", lvl)
}

View File

@@ -311,25 +311,26 @@ func TestValidateTrustLevel(t *testing.T) {
valid bool
}{
// valid
0: {tmmath.Fraction{Numerator: 1, Denominator: 1}, true},
1: {tmmath.Fraction{Numerator: 1, Denominator: 3}, true},
2: {tmmath.Fraction{Numerator: 2, Denominator: 3}, true},
3: {tmmath.Fraction{Numerator: 3, Denominator: 3}, true},
4: {tmmath.Fraction{Numerator: 4, Denominator: 5}, true},
0: {tmmath.Fraction{Numerator: 1, Denominator: 3}, true},
1: {tmmath.Fraction{Numerator: 2, Denominator: 3}, true},
2: {tmmath.Fraction{Numerator: 4, Denominator: 5}, true},
3: {tmmath.Fraction{Numerator: 99, Denominator: 100}, true},
// invalid
4: {tmmath.Fraction{Numerator: 3, Denominator: 3}, false},
5: {tmmath.Fraction{Numerator: 6, Denominator: 5}, false},
6: {tmmath.Fraction{Numerator: 0, Denominator: 1}, false},
7: {tmmath.Fraction{Numerator: 0, Denominator: 0}, false},
8: {tmmath.Fraction{Numerator: 1, Denominator: 0}, false},
6: {tmmath.Fraction{Numerator: 3, Denominator: 10}, false},
7: {tmmath.Fraction{Numerator: 0, Denominator: 1}, false},
8: {tmmath.Fraction{Numerator: 0, Denominator: 0}, false},
9: {tmmath.Fraction{Numerator: 1, Denominator: 0}, false},
}
for _, tc := range testCases {
for idx, tc := range testCases {
err := light.ValidateTrustLevel(tc.lvl)
if !tc.valid {
assert.Error(t, err)
assert.Error(t, err, idx)
} else {
assert.NoError(t, err)
assert.NoError(t, err, idx)
}
}
}