View Source Caustic.Secp256k1 (Caustic v0.1.25)
Convenience functions for the elliptic curve secp256k1 used in Bitcoin.
Link to this section Summary
Functions
The constant a
in the elliptic curve equation y^2 = x^3 + ax + 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.
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.
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
.
Link to this section Functions
@spec a() :: Caustic.FiniteField.finite_field_elem()
The constant a
in the elliptic curve equation y^2 = x^3 + ax + b
.
examples
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
Examples
iex> {b, _p} = Caustic.Secp256k1.b()
iex> b
7
Signs a message using ECDSA.
arguments
Arguments
z
: The hash of the signed message. UseCaustic.Utils.hash256
to hash your message.e
: The private key.
examples
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
Verifies whether a given ECDSA signature is correct.
arguments
Arguments
pubkey
: The public key.z
: The hash of the signed message. UseCaustic.Utils.hash256
to hash your message.sig
: The signature in the format of{r, s}
.
examples
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
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
.