Cryptography
Transactions on the BSV blockchain use hash functions to fingerprint data and elliptic curve signatures to authorize spends. A hash function gives a fixed size output that cannot be reversed, and a signature lets a key holder approve a spend without revealing the private key. On this page you can hash text with the four hash functions, run two short scripts that hash data and sign a message with @bsv/sdk, and see which function each part of a transaction uses.
The examples run with Node.js after npm install @bsv/sdk and were written against @bsv/sdk 4.0.1.
Hash functions
A hash function turns any input into a fixed size output that cannot be reversed, which is why it works as a fingerprint. Hash some text below and change one character to see how the output reacts.
Type a text and pick a function. Change one character and about half of the bits flip.
The four functions
The BSV blockchain uses two hash functions and two combinations of them:
| Function | Output | Used for |
|---|---|---|
| SHA-256 | 32 bytes | The first step of HASH256 and HASH160 |
| RIPEMD-160 | 20 bytes | Shortening a public key, after SHA-256 |
HASH256 | 32 bytes | SHA-256 applied twice. Proof of work, block hashes, transaction IDs and Merkle treesA tree of hashes in which pairs are hashed together until one hash is left |
HASH160 | 20 bytes | RIPEMD-160 of the SHA-256 of the data. Addresses and P2PKHPay to Public Key Hash locking scripts |
import { Hash, Utils } from '@bsv/sdk'
const data = Utils.toArray('abc', 'utf8')
console.log('sha256 ', Utils.toHex(Hash.sha256(data)))
console.log('hash256 ', Utils.toHex(Hash.hash256(data)))
console.log('ripemd160', Utils.toHex(Hash.ripemd160(data)))
console.log('hash160 ', Utils.toHex(Hash.hash160(data)))sha256 ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
hash256 4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358
ripemd160 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
hash160 bb1be98c142444d7a56aa3981c3942a978e4dc33Hashes in Script
HASH256 and HASH160 are also opcodes in Script, so a locking scriptThe part of a transaction output that sets the condition to spend it can check that a value hashes to a given digest. How Script works covers locking scripts.
Private keys, public keys and addresses
A key pair is a private key and the public key derived from it.
Private keys
A private key is a 256 bit number, and valid values run from 1 up to one less than the order of the curve, described in the next section. The public key is derived from it. A wallet holds private keys and uses them to sign, and the keys themselves stay inside it. BRC-100 wallet interface explains why an app never holds them.
Never put a private key that holds value in code or in version control. The examples on this page that need a key generate a new random one on every run, only to try the functions.
Public keys and addresses
A compressed public key is 33 bytes. An address is built from the public key: HASH160 of the key, with a version prefix and a checksum, written in base58A text encoding that leaves out look-alike characters. A private key can also be written as a Wallet Import FormatA text form of a private key that includes a checksum string, which for a compressed key is 52 characters long. In a pay to public key hash output the address stands for a hash of the key, and the key itself shows up on the chain only when the output is spent.
Generate keys with PrivateKey.fromRandom(). The SDK reduces any value it is given modulo the order of the curve. It does not reject a value that is out of range, so a value at or above the order turns into a different key without an error.
How keys and identity work covers key derivation.
The curve
BSV blockchain public keys live on the elliptic curve secp256k1The elliptic curve that BSV blockchain public keys live on, the same curve Bitcoin uses. The curve is y² = x³ + 7 over a prime field. Two constants matter to a developer:
G, the base point, which every public key is built from. Its compressed form is02 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798.n, the order ofGand the limit for private keys,FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141.
A public key is the private key times G, which means adding G to itself that many times. Going back from the public key to the private key is the discrete logarithm problem, and no efficient solution is known on this curve. The signature scheme depends on this one way property.
Digital signatures
A signature proves that the holder of a private key approved a message.
Signing and verifying
The BSV blockchain uses the Elliptic Curve Digital Signature Algorithm, ECDSA. The signer hashes a message, signs the hash with the private key, and anyone with the public key can check that the signature matches that message.
import { PrivateKey } from '@bsv/sdk'
const key = PrivateKey.fromRandom()
const publicKey = key.toPublicKey()
const signature = key.sign('hello')
console.log(publicKey.toString().length / 2, 'byte public key')
console.log(publicKey.verify('hello', signature))
console.log(publicKey.verify('hullo', signature))33 byte public key
true
falseSignatures in a transaction
A signature is two numbers, and transactions carry them in DERDistinguished Encoding Rules encoding, a standard byte format that starts with 0x30. The length of a signature varies by a byte or two. In a transaction the signature sits in the unlocking scriptThe part of an input that meets the condition set by the locking script of an input, with one extra byte for the sighash flagA byte that says which parts of the transaction a signature covers that says which parts of the transaction it covers. How Script works covers how a spend is checked, and How transactions and fees work shows what a signature covers.
Related articles
To learn more about cryptography on the BSV blockchain, review these articles: