Skip to content

Commit

Permalink
[bootstrap] define & doc consts for magic numbers in decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker committed Aug 24, 2021
1 parent 44c7a67 commit 596f946
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion cmd/bootstrap/utils/key_generation.go
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/onflow/flow-go/model/flow"
)

// these constants are defined in X9.62 section 4.2 and 4.3
// see https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.202.2977&rep=rep1&type=pdf
// they indicate if the conversion to/from a public key (point) in compressed form must involve an inversion of the ordinate coordinate
const X962_NO_INVERSION = uint8(0x02)
const X962_INVERSION = uint8(0x03)

func GenerateMachineAccountKey(seed []byte) (crypto.PrivateKey, error) {
keys, err := GenerateKeys(crypto.ECDSAP256, 1, [][]byte{seed})
if err != nil {
Expand All @@ -38,7 +44,7 @@ func drawUnstakedKey(seed []byte) (crypto.PrivateKey, error) {
if err != nil {
// this should not happen
return nil, err
} else if key.PublicKey().EncodeCompressed()[0] == 0x03 {
} else if key.PublicKey().EncodeCompressed()[0] == X962_INVERSION {
// negative key -> unsuitable
return nil, fmt.Errorf("Unsuitable negative key")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/bootstrap/utils/key_generation_test.go
Expand Up @@ -19,13 +19,13 @@ func TestGenerateUnstakedNetworkingKey(t *testing.T) {
key, err := GenerateUnstakedNetworkingKey(unittest.SeedFixture(crypto.KeyGenSeedMinLenECDSASecp256k1))
require.NoError(t, err)
assert.Equal(t, crypto.ECDSASecp256k1, key.Algorithm())
assert.Equal(t, uint8(0x02), key.PublicKey().EncodeCompressed()[0])
assert.Equal(t, X962_NO_INVERSION, key.PublicKey().EncodeCompressed()[0])

keys, err := GenerateUnstakedNetworkingKeys(20, unittest.SeedFixtures(20, crypto.KeyGenSeedMinLenECDSASecp256k1))
require.NoError(t, err)
for _, key := range keys {
assert.Equal(t, crypto.ECDSASecp256k1, key.Algorithm())
assert.Equal(t, uint8(0x02), key.PublicKey().EncodeCompressed()[0])
assert.Equal(t, X962_NO_INVERSION, key.PublicKey().EncodeCompressed()[0])
}

}
Expand Down

0 comments on commit 596f946

Please sign in to comment.