module Secp256k1::Util
Overview
A collection of utilities for Secp256k1
key management, e.g., private key
generation, public key conversions, key formatting, or hex padding.
Defined in:
util.crClass Method Summary
-
.decode_compressed_public_key(pub : String, prime = EC_PRIME_P)
Decodes a public key as
ECPoint
from a compressed public key string. -
.new_private_key
A helper function to generate 32 pseudo-random bytes within the elliptic curve field size of
EC_ORDER_N
. -
.public_key_compressed_prefix(p : ECPoint)
Exports the compressed public key from an
ECPoint
with either the prefix"02"
or"03"
. -
.public_key_from_private(priv : BigInt)
Gets a public key from a private key.
-
.public_key_uncompressed(p : ECPoint)
Exports the uncompressed public key from an
ECPoint
without prefix. -
.public_key_uncompressed_prefix(p : ECPoint)
Exports the uncompressed public key from an
ECPoint
with prefix"04"
. -
.restore_public_key(pub : String, prime = EC_PRIME_P)
Detects public key type and tries to restore the
ECPoint
from it. -
.to_padded_hex_01(i : Int32)
A generic utility to encode single hex bytes as strings, e.g., "07"
-
.to_padded_hex_32(i : BigInt)
An utility tool to ensure hex keys are always 32 bytes; it pads the number with leading zeros if it's shorter.
Class Method Detail
Decodes a public key as ECPoint
from a compressed public key string.
If unsure, .restore_public_key
should be used.
Parameters:
pub
(String
): the public key in prefixed compressed format.prime
(BigInt
): the prime number that shapes the field, default:EC_PRIME_P
.
Secp256k1::Util.decode_compressed_public_key "03d885aed4bcaf3a8c95a57e3be08caa1bd6a060a68b9795c03129073597fcb19a"
Returns an ECPoint
containing the public key.
Raises if compressed public key is malformed or comes with invalid prefix.
A helper function to generate 32 pseudo-random bytes within the elliptic
curve field size of EC_ORDER_N
.
Secp256k1::Util.new_private_key
# => "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"
Exports the compressed public key from an ECPoint
with either the
prefix "02"
or "03"
.
The prefix can be later used to recover the y
coordinate of the public key,
see .decode_compressed_public_key
. Bitcoin
uses this format
to generate shorter addresses as compared to using uncompressed keys.
Parameters:
p
(ECPoint
): the public key point which shall be compressed.
Secp256k1::Util.public_key_compressed_prefix my_public_key
# => "03d885aed4bcaf3a8c95a57e3be08caa1bd6a060a68b9795c03129073597fcb19a"
Gets a public key from a private key.
This is basically a wrapper function to perform an elliptic curve
multiplication with the generator point g
and a provided private key priv
.
Parameters:
priv
(BigInt
): the private key to be used.
Secp256k1::Util.public_key_from_private BigInt.new("b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268", 16)
Returns an ECPoint
containing the public key.
Exports the uncompressed public key from an ECPoint
without prefix.
Ethereum
uses this format to generate addresses. For prefixed
uncompressed public keys, see .public_key_uncompressed_prefix
.
Parameters:
p
(ECPoint
): the public key point which shall be uncompressed.
Secp256k1::Util.public_key_uncompressed my_public_key
# => "d885aed4bcaf3a8c95a57e3be08caa1bd6a060a68b9795c03129073597fcb19a67299d1cf25955e9b6425583cbc33f4ab831f5a31ef88c7167e9eb714cc758a5"
Exports the uncompressed public key from an ECPoint
with prefix "04"
.
Bitcoin
uses this format to generate uncompressed addresses.
For unprefixed public keys, see .public_key_uncompressed
.
Parameters:
p
(ECPoint
): the public key point which shall be uncompressed.
Secp256k1::Util.public_key_uncompressed_prefix my_public_key
# => "04d885aed4bcaf3a8c95a57e3be08caa1bd6a060a68b9795c03129073597fcb19a67299d1cf25955e9b6425583cbc33f4ab831f5a31ef88c7167e9eb714cc758a5"
Detects public key type and tries to restore the ECPoint
from it.
Parameters:
pub
(String
): the public key in any format.prime
(BigInt
): the prime number that shapes the field, default:EC_PRIME_P
.
Secp256k1::Util.restore_public_key "d885aed4bcaf3a8c95a57e3be08caa1bd6a060a68b9795c03129073597fcb19a67299d1cf25955e9b6425583cbc33f4ab831f5a31ef88c7167e9eb714cc758a5"
Returns an ECPoint
containing the public key.
Raises if public key format is unknown.
A generic utility to encode single hex bytes as strings, e.g., "07"
Parameters:
i
(Int32
): the integer to be formatted as padded hex byte.
Secp256k1::Util.to_padded_hex_01 7
# => "07"
An utility tool to ensure hex keys are always 32 bytes; it pads the number with leading zeros if it's shorter.
Parameters:
i
(BigInt
): the integer to be formatted as padded hex byte string.
Secp256k1::Util.to_padded_hex_32 BigInt.new 7
# => "0000000000000000000000000000000000000000000000000000000000000007"