đź‘®
Contract Cops
  • Mastering Ethereum Book
    • What is ethereum?
    • Tokens
    • Oracles
    • Decenralized Applications(DApps)
    • The Ethereum virtual machine
    • Ethereum basics
    • Ethereum clients
    • Cryptography
    • Wallets
    • Transactions
    • Chapter 7 - Smart Contracts & Solidity
    • Side Notes
      • Tokens
      • Smart Contracts and Solidity
  • Cryptography
    • Ethereum Cryptography - Cheatsheet
    • Assymetric vs symmetric cryptography
    • ECDSA vs RSA
    • Elliptic curves and ECDSA
    • Sha-256 Example
    • Sha-256
    • What are the different steps in SHA-256?
  • Ethereum Blocks
    • Block Headers
  • Learning Solidity
    • Storage vs memory
    • Upgradeable contracts
      • Proxy pattern in smart contracts
  • PoS
    • Proof of stake
  • PoW
    • PoW
  • Tokens
    • ERC-1155
    • ERC20
  • Cryptonomics
    • Automated market makers
    • Collateral Tokens
    • Collateralized Stablecoin
    • Fiat currency
    • Liquidity pool
    • Open Position: Meaning and Risk in Trading
    • Slippage
    • Spot price
  • Common Attack Vectors
    • Checking access control
    • Access control issues on critical functions
    • Account Existence Check for low level calls
    • Account Existence Check
    • Common attacks with contract/EOA addresses
    • Arithmetic under/overflow
    • Assert Attack
    • Assert require revert
    • Assert Violation
    • Bad Interface DOS
    • Bad pragma and compiler
    • Block Timestamp Manipulation
    • Bypassing contract check
    • Code With No Effects
    • Code size check vulnerability
    • Constructors with Care
    • Default Visibilities
    • Delegatecall
    • Delegatecall
    • Denial of Service (DoS)
    • DoS with block gas limit
    • Entropy Illusion
    • External contract referencing
    • Flash Loan Attack
    • Floating Point and Precision
    • Function selector abuse
    • Function selector abuse
    • Smart contract gas griefing
    • Hash collision parameters
    • Hash Collisions With Multiple Variable Length Arguments
    • Imprecise arithmetic
    • Improper Array Deletion
    • Incorrect array deletion
    • Incorrect interface
    • Insufficient Gas Griefing
    • Loop through long arrays
    • Message call with hardcoded gas amount
    • Not enough gas for ether transfer
    • Precision Loss in Calculations
    • Oracle Manipulation
    • Public Burn Function
    • Read-only reentrancy
    • Race Conditions/Front Running
    • Reentrancy Attacks
    • Reentrancy
    • Requirement Violation
    • Right-To-Left-Override control character (U+202E)
    • Shadowing State Variables
    • Short Address / Parameter attack
    • Signature Malleability
    • Signature Replay
    • Transaction Order Dependence
    • Tx.Origin Authentication
    • Unchecked CALL Return Values
    • Unexpected ether
    • Uninitialized Storage Pointers
    • Unsafe Ownership Transfer
  • EIP's
    • EIP155
    • EIP55
  • PoW
    • Ethash
    • Scrypt - RFC 7914
  • Questions for self evaluation
    • Questions 23/04/2023 (Nr: 84)
    • Usability guide for questions
  • Frequently asked questions
    • What is the difference between transaction and message?
    • What is the use of a interface or function without implementation?
  • UsefulResources
Powered by GitBook
On this page
  • ECDSA is short for Elliptic Curve Digital Signature Algorithm
  • Key Generation
  1. Cryptography

Elliptic curves and ECDSA

PreviousECDSA vs RSANextSha-256 Example

Last updated 2 years ago

ECDSA is short for Elliptic Curve Digital Signature Algorithm

The idea is that if you are going to send a message to someone, you could send the message + signature + public key to them, and the signature is computed using the private key.

The recipient could then verify the authenticity of the message with the signature and the public key

It is possible to authenticate the sender without revealing the private key.

  • It is a digital signature scheme, based on the elliptic-curve cryptography(ECC).

  • relies on the math of the cyclic groups of elliptic curves over finite fields and on the difficulty of the ECDLP problem (elliptic-curve discrete logarithm problem)

  • The ECDSA sign / verify algorithm relies on EC point multiplication

  • ECDSA keys and signatures are shorter than in RSA for the same security level

  • 256-bit ECDSA signature has the same security strength like 3072-bit RSA signature

Elliptic curves, used in cryptography, define:

1.Generator point G, used for scalar multiplication on the curve (multiply integer by EC point), e.g. G{x = 123, y = 456}

2.Order n of the subgroup of EC points, generated by G, which defines the length of the private keys (e.g. 256 bits) e.g. n = 123123… (it’s a big prime number)

Key Generation

The ECDSA key-pair consists of:

Private key (integer): privateKey

Public key (EC point): publicKey = privateKey * G

The private key is generated as a random number in the range of [1…n-1].

  • The public key is a point on the elliptic curve, calculate by the EC point multiplication:

    publicKey = privateKey * G , in other words: the private key, multiplied by the generator point G.

  • The public key EC point {x, y} can be compressed to just one of the coordinates + 1 bit

E.g. -> for the secp256k1 curve: The private key is 256-bit integer (32 bytes) The compressed public key is 257-bit integer (~33 bytes)

🖊️ECDSA Sign

  • The signing algorithm takes as an input:

    Message - msg Private key - privateKey

    👉And produces an output:

    Signature - {r, s} := r and s are pair of integers

Steps:

1.Hash the message with for example SHA-256

h = SHA-256(msg)

2.Generate securely a random number k in the range [1…n-1]

2.1 In case of deterministic-ECDSA the value of k is HMAC derived from h + privateKey

👉k derived from h + privateKey

3.Calculate the random point R = k * G and take its x-coordinate: 👉 r = R*x

4.Calculate the signature proof:

s = k^(-1) * (h + r * privateKey) (mod n)

4.1 The modular inverse k^(-1) (mod n) is an integer such that:

k * k^-1 = 1 (mod n)

âś…Return the signature {r, s}

📓The calculated signature {r, s} is a pair of integers, each in the range [1…n-1]. It encodes the random point R = k * G, along with a proof s, confirming that the signer knows the message h and the private key privateKey. The proof s is by idea verifiable using the corresponding publicKey.

❕ECDSA signatures are 2 times longer than the signer's private key for the curve used during the signing process.

🔎ECDSA Verify Signature

To verify a ECDSA signature, take an input of

👉msg

👉signature {r, s} - produced by the signing algorithm

👉public key - corresponding to the signer’s private key```<br>` The output is: boolean valid || invalid signature``

🪜Steps:

👉Calculate the message hash, with the same hashing function used during signing

👉Calculate the modular inverse of the signature proof:

s1 = s^(-1) (mod n)

👉Recover the random point used during the signing:

R’ = (h * s1) * G + (r * s1) * publicKey

👉Take from R’ it’s x-coordinate:

r’ = R’ * x

👉Calculate the signature validation result by comparing whether

r’ == r

❗The general idea of the signature verification is to recover the point R’ using the public key and check whether it is same point R, generated randomly during the signing process.

Cheatsheet 1️.The signing encodes random point R (represented by its x-coordinate only) through elliptic curve transformations using the private key and the message’s hash into a number s, which is the proof that the message signer knows the private key. The signature {r, s} cannot reveal the private key due to the difficulty of the ECDLP problem

2️.The signature verification decodes the proof number s from the signature back to its original point R, using the public key and the message’s hash and compares the x-coordinate of the recovered R with the r value from the signature

ECDSA