bitcoin-elixir v0.0.2 Bitcoin.DERSig

DER Signature.

DER encoded signatures are used in Bitcoin scripts (with sighash byte at the end).

This module provides parsing, serialization, normalization and checking if the signature meets BIP66 requirements.

We need to normalize signatures before passing them to erlang’s :crypto.verify because it will return false if R or S are zero padded (while libsecp256k1 returns true).

DER Signature format:

<<type, total_length, r_type, r_length, r :: binary, s_type, s_length, s :: binary>>

Plus sighash byte at the end for the signatures present in the script, but this module deals with signatures that already have the sighash byte stripped.

In strict DER signature type should be 0x30 (compound), and r_encoding and s_encoding should equal 0x02 (integer).

  • https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
  • https://en.wikipedia.org/wiki/X.690
  • https://www.itu.int/rec/T-REC-X.690/en

Summary

Functions

Returns false when S > order/2

Normalize DER signature

Parse binary signature into %DERSig{} struct

Serialize signature struct into binary

Check if the signature is a strict DER signature (BIP66)

Types

t()
t() :: %Bitcoin.DERSig{length: term, r: term, r_type: term, s: term, s_type: term, type: term}

Functions

low_s?(sig)
low_s?(t | binary) :: boolean

Returns false when S > order/2

See https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#Low_S_values_in_signatures for details.

normalize(sig)
normalize(t | binary) :: t | binary

Normalize DER signature.

  • remove leading null bytes from R and S
  • fix total_length if it’s incorrect
  • fix negative S
  • fix negative R
  • ensure low S
parse(sig)
parse(binary) :: t

Parse binary signature into %DERSig{} struct.

serialize(der)
serialize(t) :: binary

Serialize signature struct into binary.

length from the struct is used in serialization, even if it’s incorrect.

strict?(sig)
strict?(binary) :: boolean

Check if the signature is a strict DER signature (BIP66)

Note that we operate on sig that already has the sighash byte stripped.