From 92feebd2728d3fd5b07c8a0a43ab63ccc6ce0c31 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 21 Mar 2020 02:22:53 -0500 Subject: [PATCH] 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. --- dcrec/secp256k1/field.go | 2 +- dcrec/secp256k1/field_test.go | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/dcrec/secp256k1/field.go b/dcrec/secp256k1/field.go index 2f28fe36..b54d778d 100644 --- a/dcrec/secp256k1/field.go +++ b/dcrec/secp256k1/field.go @@ -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 diff --git a/dcrec/secp256k1/field_test.go b/dcrec/secp256k1/field_test.go index 28ca0bbc..9ded85ad 100644 --- a/dcrec/secp256k1/field_test.go +++ b/dcrec/secp256k1/field_test.go @@ -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 {