Caustic v0.1.13 Caustic.Secp256k1 View Source

Convenience functions for the elliptic curve secp256k1 used in Bitcoin.

Link to this section Summary

Functions

a()

The constant a in the elliptic curve equation y^2 = x^3 + ax + b

b()

The constant b in the elliptic curve equation y^2 = x^3 + ax + b

Signs a message using ECDSA

Verifies whether a given ECDSA signature is correct

g()

The generator point G used in public key calculation P = eG

The x component of the generator point G

The y component of the generator point G

Generate a random private key k with 1 <= k <= priv_key_max

Create a point in the secp256k1 curve. If you supply it with x and y coordinates outside of the curve it will throw an error

n()

The number of possible private keys, because when you consider private keys e >= n it will just loop to the same public keys

p()

The order of the finite field used in secp256k1

The largest possible private key. Equals to the constant n - 1

Calculate the public key of a private key k

Link to this section Functions

The constant a in the elliptic curve equation y^2 = x^3 + ax + b.

Examples

iex> {a, _p} = Caustic.Secp256k1.a()
iex> a
0

The constant b in the elliptic curve equation y^2 = x^3 + ax + b.

Examples

iex> {b, _p} = Caustic.Secp256k1.b()
iex> b
7

Signs a message using ECDSA.

Examples

iex> message = "Hello, world!!!"
iex> z = Caustic.Utils.hash256(message)
iex> e = Caustic.Secp256k1.generate_private_key()
iex> signature = Caustic.Secp256k1.ecdsa_sign(z, e)
iex> pubkey = Caustic.Secp256k1.public_key(e)
iex> Caustic.Secp256k1.ecdsa_verify?(pubkey, z, signature)
true
Link to this function ecdsa_verify?(pubkey, z, sig) View Source

Verifies whether a given ECDSA signature is correct.

Examples

iex> z = 0xbc62d4b80d9e36da29c16c5d4d9f11731f36052c72401a76c23c0fb5a9b74423
iex> r = 0x37206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c6
iex> s = 0x8ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec
iex> pubkey_x = 0x04519fac3d910ca7e7138f7013706f619fa8f033e6ec6e09370ea38cee6a7574
iex> pubkey_y = 0x82b51eab8c27c66e26c858a079bcdf4f1ada34cec420cafc7eac1a42216fb6c4
iex> pubkey = Caustic.Secp256k1.make_point(pubkey_x, pubkey_y)
iex> Caustic.Secp256k1.ecdsa_verify?(pubkey, z, {r, s})
true

The generator point G used in public key calculation P = eG.

Examples

iex> g = Caustic.Secp256k1.g()
iex> n = Caustic.Secp256k1.n()
iex> Caustic.ECPoint.mul(n, g) == Caustic.Secp256k1.make_point_infinity()
true

The x component of the generator point G.

The y component of the generator point G.

Generate a random private key k with 1 <= k <= priv_key_max.

Create a point in the secp256k1 curve. If you supply it with x and y coordinates outside of the curve it will throw an error.

The number of possible private keys, because when you consider private keys e >= n it will just loop to the same public keys.

The order of the finite field used in secp256k1.

The largest possible private key. Equals to the constant n - 1.

Calculate the public key of a private key k.