class Secp256k1::Num

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:

Defined in:

secp256k1/num.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(hex : String) #

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]>

[View source]
def self.new(num : BigInt) #

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]>

[View source]
def self.new(bin : Slice(UInt8)) #

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]>

[View source]
def self.new #

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]>

[View source]

Instance Method Detail

def bin : Slice(UInt8) #

The binary bytes-slice represenation of the number.


[View source]
def dec : BigInt #

The decimal big-integer representation of the number.


[View source]
def hex : String #

The hexadecimal string representation of the number.


[View source]
def to_big : BigInt #

Returns a big-integer representation of the number.

Num.new(Bytes[137]).to_big
# => 137

[View source]
def to_bytes : Bytes #

Returns a binary byte-slice representation of the number.

Num.new("0x89").to_bytes
# => Bytes[137]

[View source]
def to_hex : String #

Returns an unprefixed hexadecimal string representation.

Num.new(Bytes[137]).to_hex
# => "89"

[View source]
def to_prefixed_hex : String #

Returns an 0x-prefixed hexadecimal string representation.

Num.new(Bytes[137]).to_prefixed_hex
# => "0x89"

[View source]
def to_zpadded_bytes(length = 32) : Bytes #

Returns a z-padded byte-slice binary representation.

Parameters:

  • length (Int): the byte-size of the final z-padded slice (default 32).
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]

[View source]
def to_zpadded_hex(length = 32) : String #

Returns a z-padded hexadecimal string representation.

Parameters:

  • length (Int): the byte-size of the final z-padded hex-string (default 32).
Num.new(Bytes[137]).to_zpadded_hex
# => "0000000000000000000000000000000000000000000000000000000000000089"

[View source]