Skip to content

Commit 231e09a

Browse files
committed
xmr subaddresses
1 parent 09e3664 commit 231e09a

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

assets_js_lib_crypto_utils.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,22 @@ function weierstrass(x) {
507507
return mod(x ** 3n + CURVE.b);
508508
}
509509

510-
// Implements extended Euclidean algorithm to find Bézout's identity coefficients
510+
// Implements Extended Euclidean Algorithm to find GCD and Bézout's identity coefficients
511511
function egcd(a, b) {
512-
if (a === 0n) return [b, 0n, 1n];
513-
let [g, x1, y1] = egcd(b % a, a);
514-
return [g, y1 - (b / a) * x1, x1];
512+
if (typeof a === 'number') a = BigInt(a);
513+
if (typeof b === 'number') b = BigInt(b);
514+
515+
let [x, y, u, v] = [0n, 1n, 1n, 0n];
516+
while (a !== 0n) {
517+
const q = b / a,
518+
r = b % a;
519+
let m = x - u * q,
520+
n = y - v * q;
521+
[b, a] = [a, r];
522+
[x, y] = [u, v];
523+
[u, v] = [m, n];
524+
}
525+
return [b, x, y];
515526
}
516527

517528
// Calculates the modular multiplicative inverse using extended Euclidean algorithm
@@ -801,6 +812,19 @@ function pow_mod(base, exponent, modulus) {
801812
return result;
802813
}
803814

815+
// Computes modular exponentiation using square-and-multiply algorithm with optional modulus P
816+
function xpow_mod(a, power, m = xmr_CURVE.P) {
817+
let res = 1n;
818+
while (power > 0n) {
819+
if (power & 1n) {
820+
res = xmod(res * a, m);
821+
}
822+
power >>= 1n;
823+
a = xmod(a * a, m);
824+
}
825+
return res;
826+
}
827+
804828
// Validates and normalizes private key to BigInt within curve order range
805829
function normalize_privatekey(privateKey) {
806830
let key = null;

assets_js_lib_xmr_utils.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ const l = 7237005577332262213973186563042994240857116359379907606001950938285454
9393
// ** Mathematical Foundations: **
9494
//xmod
9595
//xpow_mod
96-
//xmr_egcd
9796
//xmr_invert
9897
//xmr_invert_batch
9998

@@ -173,7 +172,8 @@ function uint32_hex(value) {
173172

174173
// Converts a positive integer to hexadecimal with leading zero padding
175174
function xmr_number_to_hex(num) {
176-
return num.toString(16).padStart(2, "0");
175+
const hex = num.toString(16);
176+
return hex.length % 2 === 1 ? "0" + hex : hex;
177177
}
178178

179179
// ** Mathematical Foundations: **
@@ -197,28 +197,12 @@ function xpow_mod(a, power, m = xmr_CURVE.P) {
197197
return res;
198198
}
199199

200-
// Implements Extended Euclidean Algorithm to find GCD and Bézout's identity coefficients
201-
function xmr_egcd(a, b) {
202-
let [x, y, u, v] = [0n, 1n, 1n, 0n];
203-
while (a !== 0n) {
204-
let q = b / a,
205-
r = b % a,
206-
m = x - u * q,
207-
n = y - v * q;
208-
[b, a] = [a, r];
209-
[x, y] = [u, v];
210-
[u, v] = [m, n];
211-
}
212-
let gcd = b;
213-
return [gcd, x, y];
214-
}
215-
216200
// Computes modular multiplicative inverse using Extended Euclidean Algorithm with optional curve modulus
217201
function xmr_invert(number, modulo = xmr_CURVE.P) {
218202
if (number === 0n || modulo <= 0n) {
219203
throw new Error("invert: expected positive integers");
220204
}
221-
let [gcd, x] = xmr_egcd(xmod(number, modulo), modulo);
205+
let [gcd, x] = egcd(xmod(number, modulo), modulo);
222206
if (gcd !== 1n) {
223207
throw new Error("invert: does not exist");
224208
}

0 commit comments

Comments
 (0)