Skip to content

Commit

Permalink
complete the function to create twisted curves with cofactor and endo
Browse files Browse the repository at this point in the history
  • Loading branch information
querolita committed Dec 10, 2024
1 parent 0cc506e commit df65376
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion crypto/elliptic-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,12 @@ type TwistedCurveParams = {
* Scalar field modulus = group order
*/
order: bigint;
/**
* Cofactor = size of EC / order
*
* This can be left undefined if the cofactor is 1.
*/
cofactor?: bigint;
/**
* Generator point
*/
Expand All @@ -746,6 +752,8 @@ type TwistedCurveParams = {
* The `d` parameter in the curve equation ax^2 + y^2 = 1 + dx^2y^2
*/
d: bigint;
endoBase?: bigint;
endoScalar?: bigint;
};

function twistedOnCurve(
Expand Down Expand Up @@ -973,13 +981,17 @@ function createCurveTwisted({
name,
modulus: p,
order,
cofactor,
generator,
a,
d,
}: TwistedCurveParams) {
let hasCofactor = cofactor !== undefined && cofactor !== 1n;

const Field = createField(p);
const Scalar = createField(order);
const one = { ...generator, infinity: false };
const Endo = undefined; // for Ed25519

assert(a !== 0n, 'a must not be zero');
assert(d !== 0n, 'd must not be zero');
Expand All @@ -999,11 +1011,19 @@ function createCurveTwisted({
modulus: p,
order,
a,
b,
d,
cofactor,
hasCofactor,

zero: twistedZero,
one,

hasEndomorphism: Endo !== undefined,
get Endo() {
if (Endo === undefined) throw Error(`no endomorphism defined on ${name}`);
return Endo;
},

from(g: { x: bigint; y: bigint }): GroupTwisted {
if (g.x === 0n && g.y === 1n) return twistedZero;
return { ...g, infinity: false };
Expand Down

0 comments on commit df65376

Please sign in to comment.