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:
parent
418e5eba2d
commit
92feebd272
@ -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
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user