Skip to content

Module BSV::Primitives::ECDSA

Deterministic ECDSA signing and verification on secp256k1.

Implements RFC 6979 deterministic nonce generation to produce signatures that are fully reproducible from the same (key, hash) pair. All signatures are normalised to low-S form (BIP-62 rule 5).

Typically used indirectly via {PrivateKey#sign} and {PublicKey#verify} rather than calling this module directly.

Constants

BYTE_LEN

Byte length of a secp256k1 scalar (256 bits).

Public Class Methods

recover_public_key(hash, signature, recovery_id)

Recover a public key from a signature and recovery ID.

Given a message hash, signature, and the recovery ID produced during signing, reconstructs the public key that created the signature. - @param hash [String] 32-byte message digest that was signed - @param signature [Signature] the ECDSA signature - @param recovery_id [Integer] recovery ID (0-3) - @raise [ArgumentError] if the recovered point is at infinity - @return [PublicKey] the recovered public key

sign(hash, private_key_bn)

Sign a 32-byte message hash with a private key. - @param hash [String] 32-byte message digest - @param private_key_bn [OpenSSL::BN] the private key scalar - @return [Signature] a low-S normalised signature

sign_recoverable(hash, private_key_bn)

Sign a hash and return both the signature and recovery ID.

The recovery ID (0-3) allows the public key to be recovered from the signature without knowing it in advance, as used by Bitcoin Signed Messages (BSM) and compact signature formats. - @param hash [String] 32-byte message digest - @param private_key_bn [OpenSSL::BN] the private key scalar - @return [Array(Signature, Integer)] the signature and recovery ID

verify(hash, signature, public_key_point)

Verify an ECDSA signature against a message hash and public key. - @param hash [String] 32-byte message digest - @param signature [Signature] the signature to verify - @param public_key_point [OpenSSL::PKey::EC::Point] the signer's public key point - @return [Boolean] +true+ if the signature is valid