🖋️ECDSA

Elliptic Curve Digital Signature Algorithm

Signature algorithm

  • Use the secret scalar ee to compute the public pointPP, by doing a scalar multiplication with GG: eG=PeG = P.

  • Pick a random (secret) scalar kk, and perform a scalar multiplication with GG to get a random pointRR. kG=RkG = R

  • Use the above variables in the general equation of ECDSA is: uG+vP=RuG + vP = R, where, u=z/su = z/s, v=r/sv=r/s zz is the 256-bit message being signed

  • Simplify the resulting equation to get the ss component of the signature:

    s=(z+re)/ks = (z + re) / k

from random import randint

@dataclass
class PrivateKey:
    secret: int
    
    def sign(self, z: int) -> "Signature":
        e = self.secret
        k = randint(0, N)
        R = k * G
        r = R.x.value
        k_inv = pow(k, -1, N)  # Python 3.8+
        s = ((z + r*e) * k_inv) % N
        
        return Signature(r, s)

Verification algorithm

  • Given: (r, s) is the signature, z is the 256 bit message being signed, and P is the public key of the signer.

  • Calculate: u=z/su = z/s, v=r/sv=r/s.

  • Calculate uG+vP=RuG + vP = R.

  • Signature is valid is RxRx is equal to rr.

Testing our ECDSA implementation

Resources

Last updated

Was this helpful?