class Secp256k1::Num
- Secp256k1::Num
- Reference
- Object
Overview
Provides a class to conveniently handle big numbers on the elliptic curve. It allows to easily access decimal, hexadecimal, and binary representations of the numeric. In addition, it implements some utilities such as zpadding or asserting hexadecimal strings. It's suited to temporarily handle unencrypted private keys.
Properties:
#hex
(String
): the hexadecimal string representation of the number.#dec
(BigInt
): the decimal big-integer representation of the number.#bin
(Bytes
): the binary bytes-slice represenation of the number.
Defined in:
secp256k1/num.crConstructors
-
.new(hex : String)
Creates a number from a hexadecimal string literal.
-
.new(num : BigInt)
Creates a number from a big integer numeric.
-
.new(bin : Slice(UInt8))
Creates a number from a binary bytes slice.
-
.new
Creates a random number using
Random::Secure
that can be used as a secret (private key).
Instance Method Summary
-
#bin : Slice(UInt8)
The binary bytes-slice represenation of the number.
-
#dec : BigInt
The decimal big-integer representation of the number.
-
#hex : String
The hexadecimal string representation of the number.
-
#to_big : BigInt
Returns a big-integer representation of the number.
-
#to_bytes : Bytes
Returns a binary byte-slice representation of the number.
-
#to_hex : String
Returns an unprefixed hexadecimal string representation.
-
#to_prefixed_hex : String
Returns an
0x
-prefixed hexadecimal string representation. -
#to_zpadded_bytes(length = 32) : Bytes
Returns a z-padded byte-slice binary representation.
-
#to_zpadded_hex(length = 32) : String
Returns a z-padded hexadecimal string representation.
Constructor Detail
Creates a number from a hexadecimal string literal.
Parameters:
#hex
(String
): a hexadecimal string representating the number.
Num.new "568a0f505bde902db4a6afd207c794c7845fe7715da5999bb276d453c702a46d"
# => #<Secp256k1::Num:0x7fb934585480
# @hex="568a0f505bde902db4a6afd207c794c7845fe7715da5999bb276d453c702a46d",
# @dec=39142835565766237398843902819171565157710677457569850027793715608438337348717,
# @bin=Bytes[86, 138, 15, 80, 91, 222, 144, 45, 180, 166, 175, 210, 7, 199, 148, 199, 132, 95, 231, 113, 93, 165, 153, 155, 178, 118, 212, 83, 199, 2, 164, 109]>
Creates a number from a big integer numeric.
Parameters:
#dec
(BigInt
): the decimal big-integer representating the number.
Num.new BigInt.new "39142835565766237398843902819171565157710677457569850027793715608438337348717"
# => #<Secp256k1::Num:0x7fb934585480
# @hex="568a0f505bde902db4a6afd207c794c7845fe7715da5999bb276d453c702a46d",
# @dec=39142835565766237398843902819171565157710677457569850027793715608438337348717,
# @bin=Bytes[86, 138, 15, 80, 91, 222, 144, 45, 180, 166, 175, 210, 7, 199, 148, 199, 132, 95, 231, 113, 93, 165, 153, 155, 178, 118, 212, 83, 199, 2, 164, 109]>
Creates a number from a binary bytes slice.
Parameters:
#bin
(Bytes
): the binary bytes-slice represenating the number.
Num.new Bytes[86, 138, 15, 80, 91, 222, 144, 45, 180, 166, 175, 210, 7, 199, 148, 199, 132, 95, 231, 113, 93, 165, 153, 155, 178, 118, 212, 83, 199, 2, 164, 109]
# => #<Secp256k1::Num:0x7fb934585480
# @hex="568a0f505bde902db4a6afd207c794c7845fe7715da5999bb276d453c702a46d",
# @dec=39142835565766237398843902819171565157710677457569850027793715608438337348717,
# @bin=Bytes[86, 138, 15, 80, 91, 222, 144, 45, 180, 166, 175, 210, 7, 199, 148, 199, 132, 95, 231, 113, 93, 165, 153, 155, 178, 118, 212, 83, 199, 2, 164, 109]>
Creates a random number using Random::Secure
that can be used as
a secret (private key).
Num.new
# => #<Secp256k1::Num:0x7ff3d98013c0
# @hex="568a0f505bde902db4a6afd207c794c7845fe7715da5999bb276d453c702a46d",
# @dec=39142835565766237398843902819171565157710677457569850027793715608438337348717,
# @bin=Bytes[86, 138, 15, 80, 91, 222, 144, 45, 180, 166, 175, 210, 7, 199, 148, 199, 132, 95, 231, 113, 93, 165, 153, 155, 178, 118, 212, 83, 199, 2, 164, 109]>
Instance Method Detail
Returns a big-integer representation of the number.
Num.new(Bytes[137]).to_big
# => 137
Returns a binary byte-slice representation of the number.
Num.new("0x89").to_bytes
# => Bytes[137]
Returns an unprefixed hexadecimal string representation.
Num.new(Bytes[137]).to_hex
# => "89"
Returns an 0x
-prefixed hexadecimal string representation.
Num.new(Bytes[137]).to_prefixed_hex
# => "0x89"
Returns a z-padded byte-slice binary representation.
Parameters:
length
(Int
): the byte-size of the final z-padded slice (default32
).
Num.new(Bytes[137]).to_zpadded_bytes
# => Bytes[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137]
Returns a z-padded hexadecimal string representation.
Parameters:
length
(Int
): the byte-size of the final z-padded hex-string (default32
).
Num.new(Bytes[137]).to_zpadded_hex
# => "0000000000000000000000000000000000000000000000000000000000000089"