Skip to content

Commit

Permalink
Rename Fp vars in weierstrass curves
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 22, 2024
1 parent 2f6226c commit 37271bd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/p256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
// NIST secp256r1 aka p256
// https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-256

const Fp = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
const CURVE_A = Fp.create(BigInt('-3'));
const Fp256 = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
const CURVE_A = Fp256.create(BigInt('-3'));
const CURVE_B = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');

// prettier-ignore
export const p256 = createCurve({
a: CURVE_A, // Equation params: a, b
b: CURVE_B,
Fp, // Field: 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n
Fp: Fp256, // Field: 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n
// Curve order, total count of valid points in the field
n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
// Base (generator) point (x, y)
Expand All @@ -28,17 +28,17 @@ export const p256 = createCurve({
export const secp256r1 = p256;

const mapSWU = /* @__PURE__ */ (() =>
mapToCurveSimpleSWU(Fp, {
mapToCurveSimpleSWU(Fp256, {
A: CURVE_A,
B: CURVE_B,
Z: Fp.create(BigInt('-10')),
Z: Fp256.create(BigInt('-10')),
}))();

const htf = /* @__PURE__ */ (() =>
createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
DST: 'P256_XMD:SHA-256_SSWU_RO_',
encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
p: Fp.ORDER,
p: Fp256.ORDER,
m: 1,
k: 128,
expand: 'xmd',
Expand Down
12 changes: 6 additions & 6 deletions src/p384.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
// Field over which we'll do calculations.
// prettier-ignore
const P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff');
const Fp = Field(P);
const CURVE_A = Fp.create(BigInt('-3'));
const Fp384 = Field(P);
const CURVE_A = Fp384.create(BigInt('-3'));
// prettier-ignore
const CURVE_B = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');

// prettier-ignore
export const p384 = createCurve({
a: CURVE_A, // Equation params: a, b
b: CURVE_B,
Fp, // Field: 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
Fp: Fp384, // Field: 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
// Curve order, total count of valid points in the field.
n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
// Base (generator) point (x, y)
Expand All @@ -32,17 +32,17 @@ export const p384 = createCurve({
export const secp384r1 = p384;

const mapSWU = /* @__PURE__ */ (() =>
mapToCurveSimpleSWU(Fp, {
mapToCurveSimpleSWU(Fp384, {
A: CURVE_A,
B: CURVE_B,
Z: Fp.create(BigInt('-12')),
Z: Fp384.create(BigInt('-12')),
}))();

const htf = /* @__PURE__ */ (() =>
createHasher(secp384r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
DST: 'P384_XMD:SHA-384_SSWU_RO_',
encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
p: Fp.ORDER,
p: Fp384.ORDER,
m: 1,
k: 192,
expand: 'xmd',
Expand Down
14 changes: 7 additions & 7 deletions src/p521.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
// Field over which we'll do calculations.
// prettier-ignore
const P = BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
const Fp = Field(P);
const Fp521 = Field(P);

const CURVE = {
a: Fp.create(BigInt('-3')),
a: Fp521.create(BigInt('-3')),
b: BigInt(
'0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
),
Fp,
Fp: Fp521,
n: BigInt(
'0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
),
Expand All @@ -36,7 +36,7 @@ const CURVE = {
export const p521 = createCurve({
a: CURVE.a, // Equation params: a, b
b: CURVE.b,
Fp, // Field: 2n**521n - 1n
Fp: Fp521, // Field: 2n**521n - 1n
// Curve order, total count of valid points in the field
n: CURVE.n,
Gx: CURVE.Gx, // Base point (x, y) aka generator point
Expand All @@ -48,17 +48,17 @@ export const p521 = createCurve({
export const secp521r1 = p521;

const mapSWU = /* @__PURE__ */ (() =>
mapToCurveSimpleSWU(Fp, {
mapToCurveSimpleSWU(Fp521, {
A: CURVE.a,
B: CURVE.b,
Z: Fp.create(BigInt('-4')),
Z: Fp521.create(BigInt('-4')),
}))();

const htf = /* @__PURE__ */ (() =>
createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
DST: 'P521_XMD:SHA-512_SSWU_RO_',
encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
p: Fp.ORDER,
p: Fp521.ORDER,
m: 1,
k: 256,
expand: 'xmd',
Expand Down
16 changes: 8 additions & 8 deletions src/secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ function sqrtMod(y: bigint): bigint {
const t1 = (pow2(b223, _23n, P) * b22) % P;
const t2 = (pow2(t1, _6n, P) * b2) % P;
const root = pow2(t2, _2n, P);
if (!Fp.eql(Fp.sqr(root), y)) throw new Error('Cannot find square root');
if (!Fpk1.eql(Fpk1.sqr(root), y)) throw new Error('Cannot find square root');
return root;
}

const Fp = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
const Fpk1 = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });

/**
* secp256k1 short weierstrass curve and ECDSA signatures over it.
Expand All @@ -58,7 +58,7 @@ export const secp256k1 = createCurve(
{
a: BigInt(0), // equation params: a, b
b: BigInt(7), // Seem to be rigid: bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975
Fp, // Field's prime: 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
Fp: Fpk1, // Field's prime: 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
n: secp256k1N, // Curve order, total count of valid points in the field
// Base point (x, y) aka generator point
Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
Expand Down Expand Up @@ -228,7 +228,7 @@ export const schnorr = /* @__PURE__ */ (() => ({

const isoMap = /* @__PURE__ */ (() =>
isogenyMap(
Fp,
Fpk1,
[
// xNum
[
Expand Down Expand Up @@ -260,22 +260,22 @@ const isoMap = /* @__PURE__ */ (() =>
].map((i) => i.map((j) => BigInt(j))) as [bigint[], bigint[], bigint[], bigint[]]
))();
const mapSWU = /* @__PURE__ */ (() =>
mapToCurveSimpleSWU(Fp, {
mapToCurveSimpleSWU(Fpk1, {
A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
B: BigInt('1771'),
Z: Fp.create(BigInt('-11')),
Z: Fpk1.create(BigInt('-11')),
}))();
const htf = /* @__PURE__ */ (() =>
createHasher(
secp256k1.ProjectivePoint,
(scalars: bigint[]) => {
const { x, y } = mapSWU(Fp.create(scalars[0]));
const { x, y } = mapSWU(Fpk1.create(scalars[0]));
return isoMap(x, y);
},
{
DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
encodeDST: 'secp256k1_XMD:SHA-256_SSWU_NU_',
p: Fp.ORDER,
p: Fpk1.ORDER,
m: 1,
k: 128,
expand: 'xmd',
Expand Down

0 comments on commit 37271bd

Please sign in to comment.