secp256k1: Make field value set int take uint16.

This modifies the SetInt method on a field value to take a uint16
instead of a uint in order to make it impossible to misuse it since it
has a precondition of a max value of 2^26 - 1.  In practice, the code
currently only ever calls it with 0 and 1, but in order to prepare for
exporting the type, it's nicer to prevent the potential misuse.
This commit is contained in:
Dave Collins 2020-03-21 02:22:53 -05:00
parent 418e5eba2d
commit 92feebd272
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2
2 changed files with 9 additions and 13 deletions

View File

@ -171,7 +171,7 @@ func (f *fieldVal) Set(val *fieldVal) *fieldVal {
//
// The field value is returned to support chaining. This enables syntax such
// as f := new(fieldVal).SetInt(2).Mul(f2) so that f = 2 * f2.
func (f *fieldVal) SetInt(ui uint) *fieldVal {
func (f *fieldVal) SetInt(ui uint16) *fieldVal {
f.Zero()
f.n[0] = uint32(ui)
return f

View File

@ -54,24 +54,20 @@ func randIntAndFieldVal(t *testing.T, rng *rand.Rand) (*big.Int, *fieldVal) {
func TestFieldSetInt(t *testing.T) {
tests := []struct {
name string // test description
in uint // test value
in uint16 // test value
expected [10]uint32 // expected raw ints
}{{
name: "one",
in: 1,
expected: [10]uint32{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}, {
name: "five",
in: 5,
expected: [10]uint32{5, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}, {
name: "2^26",
in: 67108864,
expected: [10]uint32{67108864, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}, {
name: "2^26 + 1",
in: 67108865,
expected: [10]uint32{67108865, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}, {
name: "2^32 - 1",
in: 4294967295,
expected: [10]uint32{4294967295, 0, 0, 0, 0, 0, 0, 0, 0, 0},
name: "2^16 - 1",
in: 65535,
expected: [10]uint32{65535, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}}
for _, test := range tests {