@session.js/blinded-session-id 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -4
- package/dist/blinding.js +5 -5
- package/dist/scalar-math.d.ts +7 -7
- package/dist/scalar-math.js +22 -74
- package/dist/unblinding.js +4 -4
- package/dist/utils.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -167,10 +167,7 @@ getBlindingK(sogsPublicKey);
|
|
|
167
167
|
|
|
168
168
|
## Acknowledgements
|
|
169
169
|
|
|
170
|
-
Credit to li0ard for [https://github.com/theinfinityway/session_id/](https://github.com/theinfinityway/session_id/) (MIT license)
|
|
171
|
-
|
|
172
|
-
Credit to [xHD-Wallet-API-ts](https://github.com/algorandfoundation/xHD-Wallet-API-ts) for scalar multiplication functions implementations (Apache-2.0 license)
|
|
173
|
-
|
|
170
|
+
Credit to li0ard for [https://github.com/theinfinityway/session_id/](https://github.com/theinfinityway/session_id/) (MIT license) and src/scalar-math.ts
|
|
174
171
|
## Made for Session.js
|
|
175
172
|
|
|
176
173
|
Use Session messenger programmatically with [Session.js](https://git.hloth.dev/session.js/client): Session bots, custom Session clients, and more.
|
package/dist/blinding.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { hexToBytes } from "@noble/curves/utils.js";
|
|
2
2
|
import { SessionValidationError, SessionValidationErrorCode } from "@session.js/errors";
|
|
3
|
-
import {
|
|
3
|
+
import { multiplyPointToScalar, curve25519ToEd25519, ed25519ToCurve25519 } from "./scalar-math";
|
|
4
4
|
import { getBlindingK, hexRegex, keyToSessionId } from "./utils";
|
|
5
5
|
export function blindKey15({ ed25519PublicKey, serverPublicKey, }) {
|
|
6
6
|
const blindingKInput = serverPublicKey;
|
|
7
7
|
const k = getBlindingK(blindingKInput);
|
|
8
|
-
const kA =
|
|
8
|
+
const kA = multiplyPointToScalar(k, ed25519PublicKey);
|
|
9
9
|
return kA;
|
|
10
10
|
}
|
|
11
11
|
export function blindKey25({ x25519PublicKey, ed25519PublicKey, serverPublicKey, }) {
|
|
12
12
|
const blindingKInput = new Uint8Array([0x05, ...x25519PublicKey, ...serverPublicKey]);
|
|
13
13
|
const k = getBlindingK(blindingKInput);
|
|
14
|
-
const kA =
|
|
14
|
+
const kA = multiplyPointToScalar(k, ed25519PublicKey);
|
|
15
15
|
return kA;
|
|
16
16
|
}
|
|
17
17
|
export function blindSessionId(options) {
|
|
@@ -26,7 +26,7 @@ export function blindSessionId(options) {
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
ed25519PublicKey = options.ed25519PublicKey;
|
|
29
|
-
x25519PublicKey =
|
|
29
|
+
x25519PublicKey = ed25519ToCurve25519(ed25519PublicKey);
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
32
32
|
if ("sessionId" in options) {
|
|
@@ -60,7 +60,7 @@ export function blindSessionId(options) {
|
|
|
60
60
|
}
|
|
61
61
|
x25519PublicKey = options.x25519PublicKey;
|
|
62
62
|
}
|
|
63
|
-
ed25519PublicKey =
|
|
63
|
+
ed25519PublicKey = curve25519ToEd25519(x25519PublicKey);
|
|
64
64
|
}
|
|
65
65
|
if (typeof options.sogsPublicKey === "string") {
|
|
66
66
|
if (!hexRegex.test(options.sogsPublicKey) || options.sogsPublicKey.length % 2 !== 0) {
|
package/dist/scalar-math.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export declare function
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
1
|
+
export declare function scalarAdd(scalarA: Uint8Array, scalarB: Uint8Array): Uint8Array;
|
|
2
|
+
export declare function scalarMul(scalarA: Uint8Array, scalarB: Uint8Array): Uint8Array;
|
|
3
|
+
export declare function scalarReduce(scalar: Uint8Array): Uint8Array;
|
|
4
|
+
export declare function invertScalar(scalar: Uint8Array): Uint8Array;
|
|
5
|
+
export declare function multiplyPointToScalar(scalar: Uint8Array, point: Uint8Array): Uint8Array;
|
|
6
|
+
export declare function ed25519ToCurve25519(ed25519Pk: Uint8Array): Uint8Array;
|
|
7
|
+
export declare function curve25519ToEd25519(x25519Pk: Uint8Array): Uint8Array;
|
package/dist/scalar-math.js
CHANGED
|
@@ -1,95 +1,43 @@
|
|
|
1
|
-
// Credit: https://github.com/algorandfoundation/xHD-Wallet-API-ts/blob/2c5afbf6a1bed04ed952b65b754a36ed31669872/src/sumo.facade.ts
|
|
2
|
-
// License: Apache 2.0: https://github.com/algorandfoundation/xHD-Wallet-API-ts/blob/main/LICENSE
|
|
3
1
|
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
4
2
|
import { mod } from "@noble/curves/abstract/modular.js";
|
|
5
3
|
import { bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
6
|
-
|
|
7
|
-
export function crypto_scalarmult_ed25519_base_noclamp(scalar) {
|
|
8
|
-
if (scalar.length !== crypto_scalarmult_ed25519_SCALARBYTES) {
|
|
9
|
-
throw new Error(`scalar must be ${crypto_scalarmult_ed25519_SCALARBYTES} bytes`);
|
|
10
|
-
}
|
|
11
|
-
const scalarBigint = bytesToNumberLE(scalar);
|
|
12
|
-
try {
|
|
13
|
-
const point = ed25519.Point.BASE.multiply(scalarBigint);
|
|
14
|
-
return point.toBytes();
|
|
15
|
-
}
|
|
16
|
-
catch {
|
|
17
|
-
if (scalarBigint === 0n) {
|
|
18
|
-
const identity = new Uint8Array(32);
|
|
19
|
-
identity[0] = 1;
|
|
20
|
-
return identity;
|
|
21
|
-
}
|
|
22
|
-
const reducedScalar = mod(scalarBigint, ed25519.Point.Fn.ORDER);
|
|
23
|
-
if (reducedScalar === 0n) {
|
|
24
|
-
const identity = new Uint8Array(32);
|
|
25
|
-
identity[0] = 1;
|
|
26
|
-
return identity;
|
|
27
|
-
}
|
|
28
|
-
const point = ed25519.Point.BASE.multiply(reducedScalar);
|
|
29
|
-
return point.toBytes();
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export function crypto_core_ed25519_scalar_add(scalarA, scalarB) {
|
|
4
|
+
export function scalarAdd(scalarA, scalarB) {
|
|
33
5
|
const a = bytesToNumberLE(scalarA);
|
|
34
6
|
const b = bytesToNumberLE(scalarB);
|
|
35
|
-
const result =
|
|
7
|
+
const result = ed25519.Point.Fn.add(a, b);
|
|
36
8
|
return numberToBytesLE(result, 32);
|
|
37
9
|
}
|
|
38
|
-
export function
|
|
10
|
+
export function scalarMul(scalarA, scalarB) {
|
|
39
11
|
const a = bytesToNumberLE(scalarA);
|
|
40
12
|
const b = bytesToNumberLE(scalarB);
|
|
41
|
-
const result =
|
|
13
|
+
const result = ed25519.Point.Fn.mul(a, b);
|
|
42
14
|
return numberToBytesLE(result, 32);
|
|
43
15
|
}
|
|
44
|
-
export function
|
|
16
|
+
export function scalarReduce(scalar) {
|
|
45
17
|
const scalarNum = bytesToNumberLE(scalar);
|
|
46
18
|
const result = mod(scalarNum, ed25519.Point.Fn.ORDER);
|
|
47
19
|
return numberToBytesLE(result, 32);
|
|
48
20
|
}
|
|
49
|
-
export function
|
|
50
|
-
const
|
|
51
|
-
|
|
21
|
+
export function invertScalar(scalar) {
|
|
22
|
+
const s = bytesToNumberLE(scalar);
|
|
23
|
+
const inverted = ed25519.Point.Fn.inv(s);
|
|
24
|
+
return numberToBytesLE(inverted, 32);
|
|
52
25
|
}
|
|
53
|
-
export function
|
|
54
|
-
const P = 2n ** 255n - 19n;
|
|
55
|
-
let u = 0n;
|
|
56
|
-
for (let i = 0; i < x25519Pk.length; i++) {
|
|
57
|
-
u += BigInt(x25519Pk[i]) << (8n * BigInt(i));
|
|
58
|
-
}
|
|
59
|
-
const modPow = (base, exp, mod) => {
|
|
60
|
-
let result = 1n;
|
|
61
|
-
base = base % mod;
|
|
62
|
-
while (exp > 0n) {
|
|
63
|
-
if (exp % 2n === 1n)
|
|
64
|
-
result = (result * base) % mod;
|
|
65
|
-
exp = exp >> 1n;
|
|
66
|
-
base = (base * base) % mod;
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
69
|
-
};
|
|
70
|
-
const modInv = (a) => modPow(a, P - 2n, P);
|
|
71
|
-
const y = (((u - 1n + P) % P) * modInv((u + 1n) % P)) % P;
|
|
72
|
-
const yBytes = new Uint8Array(32);
|
|
73
|
-
let yTemp = y;
|
|
74
|
-
for (let i = 0; i < 32; i++) {
|
|
75
|
-
yBytes[i] = Number(yTemp & 0xffn);
|
|
76
|
-
yTemp >>= 8n;
|
|
77
|
-
}
|
|
78
|
-
yBytes[31] &= 0x7f;
|
|
79
|
-
return yBytes;
|
|
80
|
-
}
|
|
81
|
-
export function crypto_scalarmult_ed25519_noclamp(scalar32, point32) {
|
|
82
|
-
if (scalar32.length !== 32) {
|
|
83
|
-
throw new Error(`crypto_scalarmult_ed25519_noclamp: expected 32-byte scalar, got ${scalar32.length}`);
|
|
84
|
-
}
|
|
85
|
-
if (point32.length !== 32) {
|
|
86
|
-
throw new Error(`crypto_scalarmult_ed25519_noclamp: expected 32-byte point, got ${point32.length}`);
|
|
87
|
-
}
|
|
26
|
+
export function multiplyPointToScalar(scalar, point) {
|
|
88
27
|
const L = ed25519.Point.Fn.ORDER;
|
|
89
|
-
const s = bytesToNumberLE(
|
|
90
|
-
const P = ed25519.Point.fromBytes(
|
|
28
|
+
const s = mod(bytesToNumberLE(scalar), L);
|
|
29
|
+
const P = ed25519.Point.fromBytes(point);
|
|
91
30
|
if (P.isSmallOrder()) {
|
|
92
|
-
throw new Error("
|
|
31
|
+
throw new Error("scalarMultEd25519NoClamp: invalid point (small order)");
|
|
93
32
|
}
|
|
94
33
|
return P.multiply(s).toBytes();
|
|
95
34
|
}
|
|
35
|
+
export function ed25519ToCurve25519(ed25519Pk) {
|
|
36
|
+
const seed = ed25519Pk.slice(0, 32);
|
|
37
|
+
return ed25519.utils.toMontgomerySecret(seed);
|
|
38
|
+
}
|
|
39
|
+
export function curve25519ToEd25519(x25519Pk) {
|
|
40
|
+
const f = ed25519.Point.Fp;
|
|
41
|
+
const x = f.fromBytes(x25519Pk);
|
|
42
|
+
return f.toBytes(f.div(f.sub(x, f.ONE), f.add(x, f.ONE)));
|
|
43
|
+
}
|
package/dist/unblinding.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
2
|
-
import {
|
|
2
|
+
import { hexToBytes } from "@noble/curves/utils.js";
|
|
3
3
|
import { SessionValidationError, SessionValidationErrorCode } from "@session.js/errors";
|
|
4
|
-
import {
|
|
4
|
+
import { invertScalar, multiplyPointToScalar } from "./scalar-math";
|
|
5
5
|
import { getBlindingK, hexRegex, keyToSessionId } from "./utils";
|
|
6
6
|
export function unblindKey15({ blindedKey, serverPublicKey, }) {
|
|
7
7
|
const blindingKInput = serverPublicKey;
|
|
8
8
|
const k = getBlindingK(blindingKInput);
|
|
9
|
-
const kInverted =
|
|
10
|
-
const kA =
|
|
9
|
+
const kInverted = invertScalar(k);
|
|
10
|
+
const kA = multiplyPointToScalar(kInverted, blindedKey);
|
|
11
11
|
const x25519PublicKey = ed25519.utils.toMontgomery(kA);
|
|
12
12
|
return x25519PublicKey;
|
|
13
13
|
}
|
package/dist/utils.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { blake2b } from "@noble/hashes/blake2.js";
|
|
2
2
|
import { bytesToHex } from "@noble/curves/utils.js";
|
|
3
|
-
import {
|
|
3
|
+
import { scalarReduce } from "./scalar-math";
|
|
4
4
|
export const hexRegex = /^[0-9a-fA-F]+$/i;
|
|
5
5
|
export function getBlindingK(input) {
|
|
6
6
|
const serverPkHash = blake2b(input, {
|
|
7
7
|
dkLen: 64,
|
|
8
8
|
});
|
|
9
|
-
const k =
|
|
9
|
+
const k = scalarReduce(serverPkHash);
|
|
10
10
|
return k;
|
|
11
11
|
}
|
|
12
12
|
export function keyToSessionId(prefix, key) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@session.js/blinded-session-id",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Utility JavaScript library with methods to work with Session's blinded Session ID",
|
|
5
5
|
"homepage": "https://git.hloth.dev/session.js/blinded-session-id#readme",
|
|
6
6
|
"bugs": {
|