ED256
Overview
License: MIT
library ED256
Cryptography module
Elliptic curve arithmetic over any 256-bit prime field (Twisted Edwards curve ax^2 + y^2 = 1 + dx^2 * y^2 (mod p)).
Structs info
Curve
struct Curve {
uint256 a;
uint256 d;
uint256 p;
uint256 n;
uint256 gx;
uint256 gy;
}
256-bit curve parameters.
Parameters:
| Name | Type | Description |
|---|---|---|
| a | uint256 | The curve coefficient a. |
| d | uint256 | The curve coefficient d. |
| 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. |
PPoint
struct PPoint {
uint256 x;
uint256 y;
uint256 z;
}
Projective representation of a curve point.
Parameters:
| Name | Type | Description |
|---|---|---|
| x | uint256 | The projective X coordinate. |
| y | uint256 | The projective Y coordinate. |
| z | uint256 | The projective Z coordinate. |
Functions info
basepoint
function basepoint(
ED256.Curve memory ec
) internal pure returns (ED256.APoint memory aPoint_)
Returns the generator (base) point of the curve in affine form.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
Return values:
| Name | Type | Description |
|---|---|---|
| aPoint_ | struct ED256.APoint | The basepoint (gx, gy). |
pBasepoint
function pBasepoint(
ED256.Curve memory ec
) internal pure returns (ED256.PPoint memory pPoint_)
Returns the generator (base) point of the curve in projective form.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint_ | struct ED256.PPoint | The basepoint (gx, gy, 1). |
toScalar
function toScalar(
ED256.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 ED256.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(
ED256.Curve memory ec,
ED256.APoint memory aPoint_
) internal pure returns (bool result_)
Checks whether an affine point lies on the curve.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| aPoint_ | struct ED256.APoint | The affine point to test. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if aPoint_ satisfies ax^2 + y^2 = 1 + dx^2y^2 (mod p). |
isValidScalar
function isValidScalar(
ED256.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 ED256.Curve | The curve parameters. |
| scalar_ | uint256 | The scalar to test. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if scalar < n. |
toAffine
function toAffine(
ED256.Curve memory ec,
ED256.PPoint memory pPoint_
) internal view returns (ED256.APoint memory aPoint_)
Converts a point from projective to affine coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint_ | struct ED256.PPoint | The projective point (X, Y, Z). |
Return values:
| Name | Type | Description |
|---|---|---|
| aPoint_ | struct ED256.APoint | The equivalent affine point (x, y). |
toProjective
function toProjective(
ED256.APoint memory aPoint_
) internal pure returns (ED256.PPoint memory pPoint_)
Converts an affine point to projective coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
| aPoint_ | struct ED256.APoint | The affine point (x, y). |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint_ | struct ED256.PPoint | The projective representation (x, y, 1). |
isProjectiveInfinity
function isProjectiveInfinity(
ED256.PPoint memory pPoint_
) internal pure returns (bool result_)
Checks whether a projective point is the point at infinity.
Parameters:
| Name | Type | Description |
|---|---|---|
| pPoint_ | struct ED256.PPoint | The projective point to test. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if X == 0 && Y == Z. |
pInfinity
function pInfinity() internal pure returns (ED256.PPoint memory pPoint_)
Returns the projective representation of the point at infinity.
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint_ | struct ED256.PPoint | The point at infinity (0, 1, 1). |
pEqual
function pEqual(
ED256.Curve memory ec,
ED256.PPoint memory pPoint1_,
ED256.PPoint memory pPoint2_
) internal view returns (bool result_)
Compares two projective points for equality in affine coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint1_ | struct ED256.PPoint | The first projective point. |
| pPoint2_ | struct ED256.PPoint | The second projective point. |
Return values:
| Name | Type | Description |
|---|---|---|
| result_ | bool | True if their affine representations match. |
pNegatePoint
function pNegatePoint(
ED256.Curve memory ec,
ED256.PPoint memory pPoint_
) internal pure returns (ED256.PPoint memory pNegated_)
Computes the negation of a projective point: -P = (-X, Y, Z).
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint_ | struct ED256.PPoint | The projective point P. |
Return values:
| Name | Type | Description |
|---|---|---|
| pNegated_ | struct ED256.PPoint | The projective representation of -P. |
pMultShamir
function pMultShamir(
ED256.Curve memory ec,
ED256.PPoint memory pPoint_,
uint256 scalar_
) internal pure returns (ED256.PPoint memory pPoint2_)
Point multiplication: R = u*P using 4-bit windowed method.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint_ | struct ED256.PPoint | The projective point P. |
| scalar_ | uint256 | The scalar u. |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint2_ | struct ED256.PPoint | The projective representation of result point R. |
pMultShamir2
function pMultShamir2(
ED256.Curve memory ec,
ED256.PPoint memory pPoint1_,
ED256.PPoint memory pPoint2_,
uint256 scalar1_,
uint256 scalar2_
) internal pure returns (ED256.PPoint memory pPoint3_)
Simultaneous double-scalar multiplication: R = u1P1 + u2P2 via Strauss–Shamir.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint1_ | struct ED256.PPoint | The first projective point P1. |
| pPoint2_ | struct ED256.PPoint | The second projective point P2. |
| scalar1_ | uint256 | The first scalar u1. |
| scalar2_ | uint256 | The second scalar u2. |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint3_ | struct ED256.PPoint | The projective representation of result point R. |
pAddPoint
function pAddPoint(
ED256.Curve memory ec,
ED256.PPoint memory pPoint1_,
ED256.PPoint memory pPoint2_
) internal pure returns (ED256.PPoint memory pPoint3_)
Adds two projective points: R = P1 + P2.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint1_ | struct ED256.PPoint | The first projective point P1. |
| pPoint2_ | struct ED256.PPoint | The second projective point P2. |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint3_ | struct ED256.PPoint | The projective representation of result point R. |
pSubPoint
function pSubPoint(
ED256.Curve memory ec,
ED256.PPoint memory pPoint1_,
ED256.PPoint memory pPoint2_
) internal pure returns (ED256.PPoint memory pPoint3_)
Subtracts one projective point from another: R = P1 - P2 = P1 + (-P2).
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint1_ | struct ED256.PPoint | The first projective point P1. |
| pPoint2_ | struct ED256.PPoint | The second projective point P2. |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint3_ | struct ED256.PPoint | The projective representation of result point R. |
pDoublePoint
function pDoublePoint(
ED256.Curve memory ec,
ED256.PPoint memory pPoint1_
) internal pure returns (ED256.PPoint memory pPoint2_)
Doubles a projective point: R = 2*P.
Parameters:
| Name | Type | Description |
|---|---|---|
| ec | struct ED256.Curve | The curve parameters. |
| pPoint1_ | struct ED256.PPoint | The projective point P to double. |
Return values:
| Name | Type | Description |
|---|---|---|
| pPoint2_ | struct ED256.PPoint | The projective representation of result point R. |
_pNegate
function _pNegate(
ED256.PPoint memory pPoint_,
uint256 p_
) internal pure returns (ED256.PPoint memory pNegated_)
Internal projective point negation.