module Secp256k1::Signature

Overview

Implements ECDSASignature generation and verification for Secp256k1 elliptic curves. Ref: cryptobook.nakov.com/digital-signatures/ecdsa-sign-verify-messages

Defined in:

signature.cr

Class Method Summary

Class Method Detail

def self.sign(msg : String, priv : BigInt) #

Signs a message and creates a signature proof using a private key.

The ECDSA signing algorithm (RFC-6979) takes as input a message msg and a private key priv. It produces as output a signature, which consists of pair of integers (r, s), where r is the x-coordinate of a random point on our curve and s is the signature proof.

Parameters:

  • msg (String): A message string to sign.
  • priv (BigInt): A private key to sign with.
sig = Secp256k1::Signature.sign "Hello, World!", BigInt.new("b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268", 16)
sig.r
# => "63945398370917837063250848409972066837033757647691696776146735867163610886143"
sig.s
# => "20291418537568297129028959685291490143232574306335372594306006819765182564103"

[View source]
def self.verify(msg : String, sig : ECDSASignature, pub : ECPoint) #

Verifies a signature of a message against a public key.

The algorithm to verify an ECDSA signature takes as input the signed message msg and the signature (r, s) produced from .sign and the public key pub, corresponding to the signer's private key. The result is boolean.

Parameters:

  • msg (String): A message string to verify.
  • sig (ECDSASignature): A signature to verify the message.
  • pub (ECPoint): A public key to verify the signature against.
pub = Secp256k1::Util.restore_public_key "03d885aed4bcaf3a8c95a57e3be08caa1bd6a060a68b9795c03129073597fcb19a"
msg = "Hello, World!"
sig = Secp256k1::ECDSASignature.new BigInt.new("63945398370917837063250848409972066837033757647691696776146735867163610886143"), BigInt.new("20291418537568297129028959685291490143232574306335372594306006819765182564103")

Secp256k1::Signature.verify msg, sig, pub
# => true

[View source]
def self.verify_hash(hash : BigInt, sig : ECDSASignature, pub : ECPoint) #

Verifies a signature of a message hash against a public key.

Same as .verify, just using the hashed message directly.

Parameters:

  • hash (BigInt): A SHA-256 hash of the message to verify.
  • sig (ECDSASignature): A signature to verify the message.
  • pub (ECPoint): A public key to verify the signature against.

Returns true if signature is valid. See .verify for usage example.


[View source]