@session.js/blinded-session-id 1.0.6 → 1.0.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @session.js/blinded-session-id
2
2
 
3
- Utility JavaScript library with methods to work with Session's blinded Session ID. Uses [noble v2](https://www.npmjs.com/package/@noble/ciphers) under the hood.
3
+ Utility JavaScript library with methods to work with Session's blinded Session ID. Uses @noble/curves v2, @noble/hashes v2 and @session.js/scalars under the hood.
4
4
 
5
5
  Example of unblinded Session ID (05-prefixed):
6
6
  `057aeb66e45660c3bdfb7c62706f6440226af43ec13f3b6f899c1dd4db1b8fce5b`
@@ -23,7 +23,6 @@ Blinded IDs are used on Session SOGS to conceal identity of room's users.
23
23
  - [Unblinding 25-prefixed Session IDs](#unblinding-25-prefixed-session-ids)
24
24
  - [Advanced usage](#advanced-usage)
25
25
  - [getBlindingK](#getblindingk)
26
- - [Acknowledgements](#acknowledgements)
27
26
  - [Made for Session.js](#made-for-sessionjs)
28
27
  - [Donate](#donate)
29
28
  - [License](#license)
@@ -155,7 +154,7 @@ Unblinding 25-prefixed Session IDs is impossible.
155
154
  generates a hash for blinding/unblinding using blake2b
156
155
 
157
156
  ```ts
158
- import { getBlindingK } from "@session.js/blinded-session-id/utils.js";
157
+ import { getBlindingK } from "@session.js/blinded-session-id/utils";
159
158
 
160
159
  const sogsPublicKey = new Uint8Array([
161
160
  203, 79, 214, 25, 155, 132, 220, 54, 100, 240, 55, 51, 84, 52, 26, 1, 0, 126, 202, 169, 154, 56,
@@ -165,12 +164,6 @@ getBlindingK(sogsPublicKey);
165
164
  // => Uint8Array(32) [ 27, 203, 111, 10, 221, 88, 187, 146, 221, 11, 206, 55, 7, 86, 218, 223, 21, 123, 29, 214, 198, 182, 3, 40, 188, 123, 190, 73, 35, 122, 140, 13 ]
166
165
  ```
167
166
 
168
- ## Acknowledgements
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
-
174
167
  ## Made for Session.js
175
168
 
176
169
  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 { crypto_scalarmult_ed25519_noclamp, crypto_sign_curve25519_pk_to_ed25519, } from "./scalar-math";
3
+ import { multiplyPointToScalar, curve25519ToEd25519, ed25519ToCurve25519 } from "@session.js/scalars";
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 = crypto_scalarmult_ed25519_noclamp(k, ed25519PublicKey);
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 = crypto_scalarmult_ed25519_noclamp(k, ed25519PublicKey);
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 = crypto_sign_curve25519_pk_to_ed25519(ed25519PublicKey);
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 = crypto_sign_curve25519_pk_to_ed25519(x25519PublicKey);
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) {
@@ -1,13 +1,13 @@
1
1
  import { ed25519 } from "@noble/curves/ed25519.js";
2
- import { bytesToNumberLE, numberToBytesLE, hexToBytes } from "@noble/curves/utils.js";
2
+ import { hexToBytes } from "@noble/curves/utils.js";
3
3
  import { SessionValidationError, SessionValidationErrorCode } from "@session.js/errors";
4
- import { crypto_scalarmult_ed25519_noclamp } from "./scalar-math";
4
+ import { invertScalar, multiplyPointToScalar } from "@session.js/scalars";
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 = numberToBytesLE(ed25519.Point.Fn.inv(bytesToNumberLE(k)), 32);
10
- const kA = crypto_scalarmult_ed25519_noclamp(kInverted, blindedKey);
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 { crypto_core_ed25519_scalar_reduce } from "./scalar-math";
3
+ import { scalarReduce } from "@session.js/scalars";
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 = crypto_core_ed25519_scalar_reduce(serverPkHash);
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.6",
3
+ "version": "1.0.8",
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": {
@@ -14,6 +14,16 @@
14
14
  "license": "MIT",
15
15
  "author": "Viktor Shchelochkov <hi@hloth.dev> (https://hloth.dev)",
16
16
  "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "import": "./dist/index.js",
20
+ "types": "./dist/index.d.ts"
21
+ },
22
+ "./utils": {
23
+ "import": "./dist/utils.js",
24
+ "types": "./dist/utils.d.ts"
25
+ }
26
+ },
17
27
  "main": "dist/index.js",
18
28
  "types": "dist/index.d.ts",
19
29
  "files": [
@@ -28,7 +38,8 @@
28
38
  "dependencies": {
29
39
  "@noble/curves": "^2.0.1",
30
40
  "@noble/hashes": "^2.0.1",
31
- "@session.js/errors": "^1.0.11"
41
+ "@session.js/errors": "^1.0.11",
42
+ "@session.js/scalars": "^1.0.0"
32
43
  },
33
44
  "devDependencies": {
34
45
  "@eslint/compat": "^2.0.1",
@@ -1,7 +0,0 @@
1
- export declare function crypto_scalarmult_ed25519_base_noclamp(scalar: Uint8Array): Uint8Array;
2
- export declare function crypto_core_ed25519_scalar_add(scalarA: Uint8Array, scalarB: Uint8Array): Uint8Array;
3
- export declare function crypto_core_ed25519_scalar_mul(scalarA: Uint8Array, scalarB: Uint8Array): Uint8Array;
4
- export declare function crypto_core_ed25519_scalar_reduce(scalar: Uint8Array): Uint8Array;
5
- export declare function crypto_sign_ed25519_sk_to_curve25519(edPrivKey: Uint8Array): Uint8Array;
6
- export declare function crypto_sign_curve25519_pk_to_ed25519(x25519Pk: Uint8Array): Uint8Array;
7
- export declare function crypto_scalarmult_ed25519_noclamp(scalar32: Uint8Array, point32: Uint8Array): Uint8Array;
@@ -1,95 +0,0 @@
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
- import { ed25519 } from "@noble/curves/ed25519.js";
4
- import { mod } from "@noble/curves/abstract/modular.js";
5
- import { bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
6
- const crypto_scalarmult_ed25519_SCALARBYTES = 32;
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) {
33
- const a = bytesToNumberLE(scalarA);
34
- const b = bytesToNumberLE(scalarB);
35
- const result = mod(a + b, ed25519.Point.Fn.ORDER);
36
- return numberToBytesLE(result, 32);
37
- }
38
- export function crypto_core_ed25519_scalar_mul(scalarA, scalarB) {
39
- const a = bytesToNumberLE(scalarA);
40
- const b = bytesToNumberLE(scalarB);
41
- const result = mod(a * b, ed25519.Point.Fn.ORDER);
42
- return numberToBytesLE(result, 32);
43
- }
44
- export function crypto_core_ed25519_scalar_reduce(scalar) {
45
- const scalarNum = bytesToNumberLE(scalar);
46
- const result = mod(scalarNum, ed25519.Point.Fn.ORDER);
47
- return numberToBytesLE(result, 32);
48
- }
49
- export function crypto_sign_ed25519_sk_to_curve25519(edPrivKey) {
50
- const seed = edPrivKey.slice(0, 32);
51
- return ed25519.utils.toMontgomerySecret(seed);
52
- }
53
- export function crypto_sign_curve25519_pk_to_ed25519(x25519Pk) {
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
- }
88
- const L = ed25519.Point.Fn.ORDER;
89
- const s = bytesToNumberLE(scalar32) % L;
90
- const P = ed25519.Point.fromBytes(point32);
91
- if (P.isSmallOrder()) {
92
- throw new Error("crypto_scalarmult_ed25519_noclamp: invalid point (small order)");
93
- }
94
- return P.multiply(s).toBytes();
95
- }