Skip to main content

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:

NameTypeDescription
auint256The curve coefficient a.
buint256The curve coefficient b.
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.

JPoint​

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

Jacobian representation of a curve point.

Parameters:

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

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.

Return values:

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

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.

Return values:

NameTypeDescription
jPoint_struct EC256.JPointThe 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:

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

Return values:

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

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

Return values:

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

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

Return values:

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

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint_struct EC256.JPointThe Jacobian point (X, Y, Z).

Return values:

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

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

Return values:

NameTypeDescription
jPoint_struct EC256.JPointThe 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:

NameTypeDescription
jPoint_struct EC256.JPointThe Jacobian point to test.

Return values:

NameTypeDescription
result_boolTrue if Z == 0.

jinfinity​

function jinfinity() internal pure returns (EC256.JPoint memory jPoint_)

Returns the Jacobian representation of the point at infinity.

Return values:

NameTypeDescription
jPoint_struct EC256.JPointThe 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:

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint1_struct EC256.JPointThe first Jacobian point.
jPoint2_struct EC256.JPointThe second Jacobian point.

Return values:

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

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint_struct EC256.JPointThe Jacobian point P.

Return values:

NameTypeDescription
jNegated_struct EC256.JPointThe 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:

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint_struct EC256.JPointThe Jacobian point P.
scalar_uint256The scalar u.

Return values:

NameTypeDescription
jPoint2_struct EC256.JPointThe 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:

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint1_struct EC256.JPointThe first Jacobian point P1.
jPoint2_struct EC256.JPointThe second Jacobian point P2.
scalar1_uint256The first scalar u1.
scalar2_uint256The second scalar u2.

Return values:

NameTypeDescription
jPoint3_struct EC256.JPointThe 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:

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint1_struct EC256.JPointThe first Jacobian point P1.
jPoint2_struct EC256.JPointThe second Jacobian point P2.

Return values:

NameTypeDescription
jPoint3_struct EC256.JPointThe 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:

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint1_struct EC256.JPointThe first Jacobian point P1.
jPoint2_struct EC256.JPointThe second Jacobian point P2.

Return values:

NameTypeDescription
jPoint3_struct EC256.JPointThe 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:

NameTypeDescription
ecstruct EC256.CurveThe curve parameters.
jPoint1_struct EC256.JPointThe Jacobian point P to double.

Return values:

NameTypeDescription
jPoint2_struct EC256.JPointThe 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.