|
| 1 | +package nodekey |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "crypto/subtle" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + |
| 10 | + secp256k1 "github.com/btcsuite/btcd/btcec/v2" |
| 11 | + "github.com/cometbft/cometbft/crypto" |
| 12 | + "github.com/cometbft/cometbft/privval" |
| 13 | + |
| 14 | + cmtjson "github.com/cometbft/cometbft/libs/json" |
| 15 | + gethcrypto "github.com/ethereum/go-ethereum/crypto" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + PrivKeyName = "comet/PrivKeySecp256k1Uncompressed" |
| 20 | + PubKeyName = "comet/PubKeySecp256k1Uncompressed" |
| 21 | + |
| 22 | + KeyType = "secp256k1" |
| 23 | + PrivKeySize = 32 |
| 24 | + // PubKeySize (uncompressed) is composed of 65 bytes for two field elements (x and y) |
| 25 | + // and a prefix byte (0x04) to indicate that it is uncompressed. |
| 26 | + PubKeySize = 65 |
| 27 | + // SigSize is the size of the ECDSA signature. |
| 28 | + SigSize = 65 |
| 29 | +) |
| 30 | + |
| 31 | +var _ crypto.PrivKey = PrivKey{} |
| 32 | +var _ crypto.PubKey = PubKey{} |
| 33 | + |
| 34 | +// ------------------------------------- |
| 35 | +// PrivKey type |
| 36 | +// ------------------------------------- |
| 37 | + |
| 38 | +type PrivKey []byte |
| 39 | + |
| 40 | +// Bytes marshals the private key using amino encoding. |
| 41 | +func (privKey PrivKey) Bytes() []byte { |
| 42 | + return []byte(privKey) |
| 43 | +} |
| 44 | + |
| 45 | +// PubKey performs the point-scalar multiplication from the privKey on the |
| 46 | +// generator point to get the pubkey. |
| 47 | +func (privKey PrivKey) PubKey() crypto.PubKey { |
| 48 | + privateObject, err := gethcrypto.ToECDSA(privKey) |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + |
| 53 | + pk := gethcrypto.FromECDSAPub(&privateObject.PublicKey) |
| 54 | + |
| 55 | + return PubKey(pk) |
| 56 | +} |
| 57 | + |
| 58 | +// Equals - you probably don't need to use this. |
| 59 | +// Runs in constant time based on length of the keys. |
| 60 | +func (privKey PrivKey) Equals(other crypto.PrivKey) bool { |
| 61 | + if otherSecp, ok := other.(PrivKey); ok { |
| 62 | + return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1 |
| 63 | + } |
| 64 | + return false |
| 65 | +} |
| 66 | + |
| 67 | +func (privKey PrivKey) Type() string { |
| 68 | + return KeyType |
| 69 | +} |
| 70 | + |
| 71 | +// Sign creates an ECDSA signature on curve Secp256k1, using SHA256 on the msg. |
| 72 | +// The returned signature will be of the form R || S || V (in lower-S form). |
| 73 | +func (privKey PrivKey) Sign(msg []byte) ([]byte, error) { |
| 74 | + privateObject, err := gethcrypto.ToECDSA(privKey) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + return gethcrypto.Sign(gethcrypto.Keccak256(msg), privateObject) |
| 80 | +} |
| 81 | + |
| 82 | +// ------------------------------------- |
| 83 | +// PubKey type |
| 84 | +// ------------------------------------- |
| 85 | + |
| 86 | +type PubKey []byte |
| 87 | + |
| 88 | +// Bytes returns the pubkey marshaled with amino encoding. |
| 89 | +func (pubKey PubKey) Bytes() []byte { |
| 90 | + return []byte(pubKey) |
| 91 | +} |
| 92 | + |
| 93 | +// Address returns a Ethereym style addresses: Last_20_Bytes(KECCAK256(pubkey)) |
| 94 | +func (pubKey PubKey) Address() crypto.Address { |
| 95 | + if len(pubKey) != PubKeySize { |
| 96 | + panic(fmt.Sprintf("length of pubkey is incorrect %d != %d", len(pubKey), PubKeySize)) |
| 97 | + } |
| 98 | + return gethcrypto.Keccak256(pubKey[1:])[12:] |
| 99 | +} |
| 100 | + |
| 101 | +func (pubKey PubKey) Equals(other crypto.PubKey) bool { |
| 102 | + if otherSecp, ok := other.(PubKey); ok { |
| 103 | + return bytes.Equal(pubKey[:], otherSecp[:]) |
| 104 | + } |
| 105 | + return false |
| 106 | +} |
| 107 | + |
| 108 | +func (pubKey PubKey) Type() string { |
| 109 | + return KeyType |
| 110 | +} |
| 111 | + |
| 112 | +// VerifySignature verifies a signature of the form R || S || V. |
| 113 | +// It rejects signatures which are not in lower-S form. |
| 114 | +func (pubKey PubKey) VerifySignature(msg []byte, sigStr []byte) bool { |
| 115 | + if len(sigStr) != SigSize { |
| 116 | + |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + hash := gethcrypto.Keccak256(msg) |
| 121 | + return gethcrypto.VerifySignature(pubKey, hash, sigStr[:64]) |
| 122 | +} |
| 123 | + |
| 124 | +func init() { |
| 125 | + cmtjson.RegisterType(PubKey{}, PubKeyName) |
| 126 | + cmtjson.RegisterType(PrivKey{}, PrivKeyName) |
| 127 | +} |
| 128 | + |
| 129 | +// Generate an secp256k1 private key from a secret. |
| 130 | +// Most of the logic has been copy/pasted from 0xPolygon/cometbft's fork. |
| 131 | +// https://github.com/0xPolygon/cometbft/blob/v0.1.2-beta-polygon/crypto/secp256k1/secp256k1.go |
| 132 | +// Notes: |
| 133 | +// - It is not possible to import the package yet because go.mod declares its path as github.com/cometbft/cometbft instead of github.com/0xpolygon/cometbft. |
| 134 | +// - This logic will need to be updated to support newer versions. |
| 135 | +func generateSecp256k1PrivateKey(secret []byte) PrivKey { |
| 136 | + // To guarantee that we have a valid field element, we use the approach of: "Suite B Implementer’s Guide to FIPS 186-3", A.2.1 |
| 137 | + // https://apps.nsa.gov/iaarchive/library/ia-guidance/ia-solutions-for-classified/algorithm-guidance/suite-b-implementers-guide-to-fips-186-3-ecdsa.cfm |
| 138 | + // See also https://github.com/golang/go/blob/0380c9ad38843d523d9c9804fe300cb7edd7cd3c/src/crypto/ecdsa/ecdsa.go#L89-L101 |
| 139 | + secretHash := sha256.Sum256(secret) |
| 140 | + fe := new(big.Int).SetBytes(secretHash[:]) |
| 141 | + |
| 142 | + one := new(big.Int).SetInt64(1) |
| 143 | + n := new(big.Int).Sub(secp256k1.S256().N, one) |
| 144 | + fe.Mod(fe, n) |
| 145 | + fe.Add(fe, one) |
| 146 | + |
| 147 | + feB := fe.Bytes() |
| 148 | + privKey32 := make([]byte, PrivKeySize) |
| 149 | + // Copy feB over to fixed 32 byte privKey32 and pad (if necessary). |
| 150 | + copy(privKey32[32-len(feB):32], feB) |
| 151 | + |
| 152 | + return PrivKey(privKey32) |
| 153 | +} |
| 154 | + |
| 155 | +func displayHeimdallV2PrivValidatorKey(privKey crypto.PrivKey) error { |
| 156 | + nodeKey := privval.FilePVKey{ |
| 157 | + Address: privKey.PubKey().Address(), |
| 158 | + PubKey: privKey.PubKey(), |
| 159 | + PrivKey: privKey, |
| 160 | + } |
| 161 | + jsonBytes, err := cmtjson.MarshalIndent(nodeKey, "", " ") |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + fmt.Println(string(jsonBytes)) |
| 166 | + return nil |
| 167 | +} |
0 commit comments