Module SECP
Elliptic Curve Point Arithmetic for secp256k1 (SECP)
Point arithmetic over the secp256k1 curve used by Bitcoin and BIP-340 Schnorr.
This module is independent of the global ECP class (which remains BLS381). Scalars and coordinates are exchanged as 32-byte OCTET values; zenroom.BIG is not accepted for SECP operations because the global BIG factory is sized for BLS381.
Serialization: - compressed: 33 bytes (0x02/0x03 + x) - uncompressed: 65 bytes (0x04 + x + y) - SECP:octet() returns compressed (the common secp256k1 wire format)
Info:
- Copyright: Dyne.org foundation 2024-2026
- License: AGPLv3
Functions
| new (OCTET) | Create a new SECP point from an OCTET containing compressed (33-byte) or uncompressed (65-byte) serialized point coordinates. |
| from_xy (x, y) | Create a SECP point from affine x and y coordinates (32-byte OCTET each). |
| generator () | Return the generator of the secp256k1 curve. |
| infinity () | Return the point at infinity. |
| order () | Give the order of the secp256k1 curve as a 32-byte OCTET. |
| prime () | Give the field prime p of secp256k1 as a 32-byte OCTET. |
| rhs (x) | Compute the right-hand side y^2 of the curve equation for a given x. |
| validate (OCTET) | Validate an OCTET as a secp256k1 point. |
| affine () | Make an existing SECP point affine. |
| isinf () | Check if a point is the point at infinity. |
| add (other) | Add two SECP points. |
| sub (other) | Subtract one SECP point from another. |
| negative () | Return the negation of a SECP point. |
| double () | Double a SECP point. |
| mul (scalar) | Multiply a SECP point by a scalar (32-byte OCTET). |
| eq (other) | Compare two SECP points for equality. |
| octet () | Return the compressed (33-byte) serialization of the SECP point. |
| compressed () | Return the compressed (33-byte) serialization. |
| uncompressed () | Return the uncompressed (65-byte) serialization (0x04 + x + y). |
| xonly () | Return the x-only (32-byte) serialization of the point. |
| x () | Return the x coordinate as a 32-byte OCTET. |
| y () | Return the y coordinate as a 32-byte OCTET. |
| __tostring () | String representation: hex-encoded compressed point. |
| bip340_seckey_valid (sk) | Validate a BIP-340 secret key: exactly 32 bytes, 1 <= d < n. |
| bip340_tagged_hash (tag, data) | Compute BIP-0340 tagged hash: sha256(sha256(tag) || sha256(tag) || data). |
| bip340_lift_x (x) | Lift an x-only (32-byte) octet to a SECP point with even y. |
| bip340_scalar_add (a, b) | Add two 32-byte scalars modulo n. |
| bip340_scalar_mul (a, b) | Multiply two 32-byte scalars modulo n. |
| bip340_scalar_negate (x) | Negate a 32-byte scalar modulo n. |
| bip340_challenge_reduce (hash32) | Reduce a 32-byte hash value modulo the curve order. |
| mapit (OCTET) | Map an OCTET of exactly 64 bytes to a point on the secp256k1 curve. |
Functions
- new (OCTET)
-
Create a new SECP point from an OCTET containing compressed (33-byte) or
uncompressed (65-byte) serialized point coordinates.
Parameters:
- OCTET 33-byte compressed or 65-byte uncompressed point
Returns:
-
a new SECP point on secp256k1
See also:
- from_xy (x, y)
-
Create a SECP point from affine x and y coordinates (32-byte OCTET each).
Parameters:
- x 32-byte OCTET x coordinate
- y 32-byte OCTET y coordinate
Returns:
-
a new SECP point, or nil if coordinates are not on the curve
- generator ()
-
Return the generator of the secp256k1 curve.
Returns:
-
the generator point G
- infinity ()
-
Return the point at infinity.
Returns:
-
SECP point at infinity
- order ()
-
Give the order of the secp256k1 curve as a 32-byte OCTET.
Returns:
-
32-byte OCTET containing the curve order
- prime ()
-
Give the field prime p of secp256k1 as a 32-byte OCTET.
Returns:
-
32-byte OCTET containing the field prime
- rhs (x)
-
Compute the right-hand side y^2 of the curve equation for a given x.
Parameters:
- x 32-byte OCTET x coordinate
Returns:
-
32-byte OCTET containing rhs = x^3 + 7 (mod p)
- validate (OCTET)
-
Validate an OCTET as a secp256k1 point.
Parameters:
- OCTET point bytes (33 or 65 bytes)
Returns:
-
true if valid, false otherwise
- affine ()
-
Make an existing SECP point affine.
Returns:
-
a new affine SECP point
- isinf ()
-
Check if a point is the point at infinity.
Returns:
-
true if infinity, false otherwise
- add (other)
-
Add two SECP points.
Parameters:
- other another SECP point
Returns:
-
sum P + Q
- sub (other)
-
Subtract one SECP point from another.
Parameters:
- other SECP point to subtract
Returns:
-
difference P - Q
- negative ()
-
Return the negation of a SECP point.
Returns:
-
-P
- double ()
-
Double a SECP point.
Returns:
-
2*P
- mul (scalar)
-
Multiply a SECP point by a scalar (32-byte OCTET).
Parameters:
- scalar 32-byte OCTET scalar (0 <= k < order)
Returns:
-
k * P
- eq (other)
-
Compare two SECP points for equality.
Parameters:
- other another SECP point
Returns:
-
true if equal, false otherwise
- octet ()
-
Return the compressed (33-byte) serialization of the SECP point.
Returns:
-
33-byte compressed OCTET (0x02 for even y, 0x03 for odd y)
This is the default serialization (alias for compressed).
- compressed ()
-
Return the compressed (33-byte) serialization.
Returns:
-
33-byte compressed OCTET
- uncompressed ()
-
Return the uncompressed (65-byte) serialization (0x04 + x + y).
Returns:
-
65-byte uncompressed OCTET
- xonly ()
-
Return the x-only (32-byte) serialization of the point.
Returns:
-
32-byte OCTET containing the x coordinate
- x ()
-
Return the x coordinate as a 32-byte OCTET.
Returns:
-
32-byte OCTET containing the x coordinate
- y ()
-
Return the y coordinate as a 32-byte OCTET.
Returns:
-
32-byte OCTET containing the y coordinate
- __tostring ()
-
String representation: hex-encoded compressed point.
Returns:
-
hex string of compressed serialization
- bip340_seckey_valid (sk)
-
Validate a BIP-340 secret key: exactly 32 bytes, 1 <= d < n.
Parameters:
- sk 32-byte OCTET secret key
Returns:
-
true if valid, false otherwise
- bip340_tagged_hash (tag, data)
-
Compute BIP-0340 tagged hash: sha256(sha256(tag) || sha256(tag) || data).
Parameters:
- tag a string (e.g. "BIP0340/challenge")
- data an OCTET to hash
Returns:
-
32-byte OCTET hash digest
- bip340_lift_x (x)
-
Lift an x-only (32-byte) octet to a SECP point with even y.
Parameters:
- x 32-byte OCTET x coordinate
Returns:
-
SECP point with even y, or nil if x is not on the curve
- bip340_scalar_add (a, b)
-
Add two 32-byte scalars modulo n. Returns 32-byte OCTET.
Parameters:
- a 32-byte OCTET
- b 32-byte OCTET
Returns:
-
32-byte OCTET (a + b) mod n
- bip340_scalar_mul (a, b)
-
Multiply two 32-byte scalars modulo n. Returns 32-byte OCTET.
Parameters:
- a 32-byte OCTET
- b 32-byte OCTET
Returns:
-
32-byte OCTET (a * b) mod n
- bip340_scalar_negate (x)
-
Negate a 32-byte scalar modulo n. Returns 32-byte OCTET.
Parameters:
- x 32-byte OCTET
Returns:
-
32-byte OCTET n - x mod n
- bip340_challenge_reduce (hash32)
-
Reduce a 32-byte hash value modulo the curve order.
Parameters:
- hash32 32-byte OCTET (SHA-256 output)
Returns:
-
32-byte OCTET hash32 mod n
- mapit (OCTET)
-
Map an OCTET of exactly 64 bytes to a point on the secp256k1 curve.
Uses Milagro's hash-to-point mapping.
Parameters:
- OCTET 64-byte hash output
Returns:
-
SECP point on secp256k1