EC256
Overview
License: MIT
library EC256
Cryptography module
Elliptic curve arithmetic over a 256-bit prime field (Weierstrass curve y^2 = x^3 + ax + b (mod p)).
Structs info
Curve
struct Curve {
uint256 a;
uint256 b;
uint256 p;
uint256 n;
uint256 gx;
uint256 gy;
}
256-bit curve parameters.
Parameters:
| Name | Type | Description |
|---|---|---|
| a | uint256 | The curve coefficient a. |
| b | uint256 | The curve coefficient b. |
| p | uint256 | The base field size. |
| n | uint256 | The scalar field size. |
| gx | uint256 | The x-coordinate of the basepoint G. |
| gy | uint256 | The y-coordinate of the basepoint G. |
APoint
struct APoint {
uint256 x;
uint256 y;
}
Affine representation of a curve point.
Parameters:
| Name | Type | Description |
|---|---|---|
| x | uint256 | The x-coordinate. |
| y | uint256 | The y-coordinate. |
JPoint
struct JPoint {
uint256 x;
uint256 y;
uint256 z;
}
Jacobian representation of a curve point.
Parameters:
| Name | Type | Description |
|---|---|---|
| x | uint256 | The Jacobian X coordinate. |
| y | uint256 | The Jacobian Y coordinate. |
| z | uint256 | The Jacobian Z coordinate. |
Functions info
basepoint
function basepoint(
EC256.Curve memory ec
) internal pure returns (EC256.APoint memory aPoint_)
Returns the generator (base) point of the curve in affine form.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
Return values:
| Name | Type | Description |
|---|---|---|
| aPoint_ | struct EC256.APoint | The basepoint (gx, gy). |
jbasepoint
function jbasepoint(
EC256.Curve memory ec
) internal pure returns (EC256.JPoint memory jPoint_)
Returns the generator (base) point of the curve in jacobian form.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint_ | struct EC256.JPoint | The basepoint (gx, gy, 1). |
toScalar
function toScalar(
EC256.Curve memory ec,
uint256 u256_
) internal pure returns (uint256 scalar_)
Reduces an arbitrary uint256 into the scalar field [0, n).
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| u256_ | uint256 | The integer to reduce. |
Return values:
| Name | Type | Description |
|---|---|---|
| scalar_ | uint256 | The result of u256_ mod n. |
isOnCurve
function isOnCurve(
EC256.Curve memory ec,
EC256.APoint memory aPoint_
) internal pure returns (bool result_)
Checks whether an affine point lies on the curve.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| aPoint_ | struct EC256.APoint | The affine point to test. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if aPoint_ satisfies y^2 = x^3 + ax + b (mod p). |
isValidScalar
function isValidScalar(
EC256.Curve memory ec,
uint256 scalar_
) internal pure returns (bool result_)
Checks whether a scalar is in the valid range [0, n).
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| scalar_ | uint256 | The scalar to test. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if scalar < n. |
toAffine
function toAffine(
EC256.Curve memory ec,
EC256.JPoint memory jPoint_
) internal view returns (EC256.APoint memory aPoint_)
Converts a point from Jacobian to affine coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint_ | struct EC256.JPoint | The Jacobian point (X, Y, Z). |
Return values:
| Name | Type | Description |
|---|---|---|
| aPoint_ | struct EC256.APoint | The equivalent affine point (x, y). |
toJacobian
function toJacobian(
EC256.APoint memory aPoint_
) internal pure returns (EC256.JPoint memory jPoint_)
Converts an affine point to Jacobian coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
| aPoint_ | struct EC256.APoint | The affine point (x, y). |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint_ | struct EC256.JPoint | The Jacobian representation (x, y, 1). |
isJacobianInfinity
function isJacobianInfinity(
EC256.JPoint memory jPoint_
) internal pure returns (bool result_)
Checks whether a Jacobian point is the point at infinity.
Parameters:
| Name | Type | Description |
|---|---|---|
| jPoint_ | struct EC256.JPoint | The Jacobian point to test. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if Z == 0. |
jinfinity
function jinfinity() internal pure returns (EC256.JPoint memory jPoint_)
Returns the Jacobian representation of the point at infinity.
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint_ | struct EC256.JPoint | The point at infinity (0, 0, 0). |
jEqual
function jEqual(
EC256.Curve memory ec,
EC256.JPoint memory jPoint1_,
EC256.JPoint memory jPoint2_
) internal view returns (bool result_)
Compares two Jacobian points for equality in affine coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint1_ | struct EC256.JPoint | The first Jacobian point. |
| jPoint2_ | struct EC256.JPoint | The second Jacobian point. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if their affine representations match. |
jNegatePoint
function jNegatePoint(
EC256.Curve memory ec,
EC256.JPoint memory jPoint_
) internal pure returns (EC256.JPoint memory jNegated_)
Computes the negation of a Jacobian point: -P = (X, -Y, Z).
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint_ | struct EC256.JPoint | The Jacobian point P. |
Return values:
| Name | Type | Description |
|---|---|---|
| jNegated_ | struct EC256.JPoint | The Jacobian representation of -P. |
jMultShamir
function jMultShamir(
EC256.Curve memory ec,
EC256.JPoint memory jPoint_,
uint256 scalar_
) internal pure returns (EC256.JPoint memory jPoint2_)
Point multiplication: R = u*P using 4-bit windowed method.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint_ | struct EC256.JPoint | The Jacobian point P. |
| scalar_ | uint256 | The scalar u. |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint2_ | struct EC256.JPoint | The Jacobian representation of result point R. |
jMultShamir2
function jMultShamir2(
EC256.Curve memory ec,
EC256.JPoint memory jPoint1_,
EC256.JPoint memory jPoint2_,
uint256 scalar1_,
uint256 scalar2_
) internal pure returns (EC256.JPoint memory jPoint3_)
Simultaneous double-scalar multiplication: R = u1P1 + u2P2 via Strauss–Shamir.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint1_ | struct EC256.JPoint | The first Jacobian point P1. |
| jPoint2_ | struct EC256.JPoint | The second Jacobian point P2. |
| scalar1_ | uint256 | The first scalar u1. |
| scalar2_ | uint256 | The second scalar u2. |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint3_ | struct EC256.JPoint | The Jacobian representation of result point R. |
jAddPoint
function jAddPoint(
EC256.Curve memory ec,
EC256.JPoint memory jPoint1_,
EC256.JPoint memory jPoint2_
) internal pure returns (EC256.JPoint memory jPoint3_)
Adds two Jacobian points: R = P1 + P2.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint1_ | struct EC256.JPoint | The first Jacobian point P1. |
| jPoint2_ | struct EC256.JPoint | The second Jacobian point P2. |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint3_ | struct EC256.JPoint | The Jacobian representation of result point R. |
jSubPoint
function jSubPoint(
EC256.Curve memory ec,
EC256.JPoint memory jPoint1_,
EC256.JPoint memory jPoint2_
) internal pure returns (EC256.JPoint memory jPoint3_)
Subtracts one Jacobian point from another: R = P1 - P2 = P1 + (-P2).
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint1_ | struct EC256.JPoint | The first Jacobian point P1. |
| jPoint2_ | struct EC256.JPoint | The second Jacobian point P2. |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint3_ | struct EC256.JPoint | The Jacobian representation of result point R. |
jDoublePoint
function jDoublePoint(
EC256.Curve memory ec,
EC256.JPoint memory jPoint1_
) internal pure returns (EC256.JPoint memory jPoint2_)
Doubles a Jacobian point: R = 2*P.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct EC256.Curve | The curve parameters. |
| jPoint1_ | struct EC256.JPoint | The Jacobian point P to double. |
Return values:
| Name | Type | Description |
|---|---|---|
| jPoint2_ | struct EC256.JPoint | The Jacobian representation of result point R. |
_jNegate
function _jNegate(
EC256.JPoint memory jPoint_,
uint256 p_
) internal pure returns (EC256.JPoint memory jNegated_)
Internal Jacobian point negation.