Skip to main content

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:

NameTypeDescription
auint256The curve coefficient a.
duint256The curve coefficient d.
puint256The base field size.
nuint256The scalar field size.
gxuint256The x-coordinate of the basepoint G.
gyuint256The y-coordinate of the basepoint G.

APoint​

struct APoint {
uint256 x;
uint256 y;
}

Affine representation of a curve point.

Parameters:

NameTypeDescription
xuint256The x-coordinate.
yuint256The y-coordinate.

PPoint​

struct PPoint {
uint256 x;
uint256 y;
uint256 z;
}

Projective representation of a curve point.

Parameters:

NameTypeDescription
xuint256The projective X coordinate.
yuint256The projective Y coordinate.
zuint256The 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.

Return values:

NameTypeDescription
aPoint_struct ED256.APointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.

Return values:

NameTypeDescription
pPoint_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
u256_uint256The integer to reduce.

Return values:

NameTypeDescription
scalar_uint256The 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
aPoint_struct ED256.APointThe affine point to test.

Return values:

NameTypeDescription
result_boolTrue 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
scalar_uint256The scalar to test.

Return values:

NameTypeDescription
result_boolTrue 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint_struct ED256.PPointThe projective point (X, Y, Z).

Return values:

NameTypeDescription
aPoint_struct ED256.APointThe 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:

NameTypeDescription
aPoint_struct ED256.APointThe affine point (x, y).

Return values:

NameTypeDescription
pPoint_struct ED256.PPointThe 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:

NameTypeDescription
pPoint_struct ED256.PPointThe projective point to test.

Return values:

NameTypeDescription
result_boolTrue 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:

NameTypeDescription
pPoint_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint1_struct ED256.PPointThe first projective point.
pPoint2_struct ED256.PPointThe second projective point.

Return values:

NameTypeDescription
result_boolTrue 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint_struct ED256.PPointThe projective point P.

Return values:

NameTypeDescription
pNegated_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint_struct ED256.PPointThe projective point P.
scalar_uint256The scalar u.

Return values:

NameTypeDescription
pPoint2_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint1_struct ED256.PPointThe first projective point P1.
pPoint2_struct ED256.PPointThe second projective point P2.
scalar1_uint256The first scalar u1.
scalar2_uint256The second scalar u2.

Return values:

NameTypeDescription
pPoint3_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint1_struct ED256.PPointThe first projective point P1.
pPoint2_struct ED256.PPointThe second projective point P2.

Return values:

NameTypeDescription
pPoint3_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint1_struct ED256.PPointThe first projective point P1.
pPoint2_struct ED256.PPointThe second projective point P2.

Return values:

NameTypeDescription
pPoint3_struct ED256.PPointThe 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:

NameTypeDescription
ecstruct ED256.CurveThe curve parameters.
pPoint1_struct ED256.PPointThe projective point P to double.

Return values:

NameTypeDescription
pPoint2_struct ED256.PPointThe 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.