threshold-elgamal 0.1.21 → 0.1.22

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/dist/elgamal.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { modPow, modInv } from 'bigint-mod-arith';
2
- import { getRandomBigIntegerInRange, getGroup } from './utils';
2
+ import { getRandomBigIntegerInRange, getGroup } from "./utils.js";
3
3
  /**
4
4
  * Generates the parameters for the ElGamal encryption, including the prime, generator,
5
5
  * and key pair (public and private keys).
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { generateParameters, encrypt, decrypt } from './elgamal';
2
- import { generateSingleKeyShare, generateKeyShares, combinePublicKeys, createDecryptionShare, combineDecryptionShares, thresholdDecrypt, } from './thresholdElgamal';
3
- import { getRandomBigIntegerInRange, multiplyEncryptedValues, getGroup, } from './utils';
1
+ import { generateParameters, encrypt, decrypt } from "./elgamal.js";
2
+ import { generateSingleKeyShare, generateKeyShares, combinePublicKeys, createDecryptionShare, combineDecryptionShares, thresholdDecrypt, } from "./thresholdElgamal.js";
3
+ import { getRandomBigIntegerInRange, multiplyEncryptedValues, getGroup, } from "./utils.js";
4
4
  export { generateParameters, encrypt, decrypt, generateSingleKeyShare, generateKeyShares, combinePublicKeys, createDecryptionShare, combineDecryptionShares, thresholdDecrypt, getRandomBigIntegerInRange, multiplyEncryptedValues, getGroup, };
@@ -0,0 +1 @@
1
+ export declare const randomBigint: (bits: number) => bigint;
@@ -0,0 +1,65 @@
1
+ // limit of Crypto.getRandomValues()
2
+ // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
3
+ const MAX_BYTES = 65536;
4
+ // Node supports requesting up to this number of bytes
5
+ // https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js#L48
6
+ const MAX_UINT32 = 4294967295;
7
+ const randomBytes = (size) => {
8
+ if (size > MAX_UINT32) {
9
+ throw new RangeError('requested too many random bytes');
10
+ }
11
+ const bytes = Buffer.allocUnsafe(size);
12
+ // getRandomValues fails on IE if size == 0
13
+ if (size > 0) {
14
+ // this is the max bytes crypto.getRandomValues
15
+ if (size > MAX_BYTES) {
16
+ // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
17
+ for (let generated = 0; generated < size; generated += MAX_BYTES) {
18
+ // buffer.slice automatically checks if the end is past the end of
19
+ // the buffer so we don't have to here
20
+ crypto.getRandomValues(bytes.slice(generated, Math.min(generated + MAX_BYTES, size)));
21
+ }
22
+ }
23
+ else {
24
+ // Directly fill the buffer if within the limit
25
+ crypto.getRandomValues(bytes);
26
+ }
27
+ }
28
+ return bytes;
29
+ };
30
+ const bytes2bigint = (bytes) => {
31
+ let result = 0n;
32
+ const n = bytes.length;
33
+ // Read input in 8 byte slices. This is, on average and at the time
34
+ // of writing, about 35x faster for large inputs than processing them
35
+ // one byte at a time.
36
+ if (n >= 8) {
37
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
38
+ for (let i = 0, k = n & ~7; i < k; i += 8) {
39
+ const x = view.getBigUint64(i, false);
40
+ result = (result << 64n) + x;
41
+ }
42
+ }
43
+ // Now mop up any remaining bytes.
44
+ for (let i = n & ~7; i < n; i++)
45
+ result = result * 256n + BigInt(bytes[i]);
46
+ return result;
47
+ };
48
+ // Note: mutates the contents of |bytes|.
49
+ function maskBits(m, bytes) {
50
+ // Mask off bits from the MSB that are > log2(bits).
51
+ // |bytes| is treated as a big-endian bigint so byte 0 is the MSB.
52
+ if (bytes.length > 0)
53
+ bytes[0] &= m;
54
+ }
55
+ export const randomBigint = (bits) => {
56
+ if (bits < 0)
57
+ throw new RangeError('bits < 0');
58
+ const n = (bits >>> 3) + (bits & 7 ? 1 : 0); // Round up to next byte.
59
+ const r = 8 * n - bits;
60
+ const s = 8 - r;
61
+ const m = (1 << s) - 1; // Bits to mask off from MSB.
62
+ const bytes = randomBytes(n);
63
+ maskBits(m, bytes);
64
+ return bytes2bigint(bytes);
65
+ };
package/dist/testUtils.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { expect } from 'vitest';
2
- import { encrypt } from './elgamal';
3
- import { generateKeyShares, combinePublicKeys, createDecryptionShare, combineDecryptionShares, thresholdDecrypt, } from './thresholdElgamal';
4
- import { multiplyEncryptedValues, getGroup } from './utils';
2
+ import { encrypt } from "./elgamal.js";
3
+ import { generateKeyShares, combinePublicKeys, createDecryptionShare, combineDecryptionShares, thresholdDecrypt, } from "./thresholdElgamal.js";
4
+ import { multiplyEncryptedValues, getGroup } from "./utils.js";
5
5
  export const getRandomScore = (min = 1, max = 10) => Math.floor(Math.random() * (max - min + 1)) + min;
6
6
  export const thresholdSetup = (partiesCount, threshold, primeBits = 2048) => {
7
7
  const { prime, generator } = getGroup(primeBits);
@@ -1,5 +1,5 @@
1
1
  import { modPow, modInv } from 'bigint-mod-arith';
2
- import { generatePolynomial, getGroup } from './utils';
2
+ import { generatePolynomial, getGroup } from "./utils.js";
3
3
  /**
4
4
  * Evaluates a polynomial at a given point using modular arithmetic.
5
5
  *
package/dist/utils.js CHANGED
@@ -1,5 +1,5 @@
1
- import randomBigint from 'random-bigint';
2
- import { GROUPS } from './constants';
1
+ import { GROUPS } from "./constants.js";
2
+ import { randomBigint } from "./randomBigInt.js";
3
3
  /**
4
4
  * Retrieves the group parameters for a given prime bit length.
5
5
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "threshold-elgamal",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "author": "Piotr Piech <piotr@piech.dev>",
5
5
  "license": "MIT",
6
6
  "description": "Threshold ElGamal in TypeScript",
@@ -15,6 +15,7 @@
15
15
  },
16
16
  "homepage": "https://tenemo.github.io/threshold-elgamal/",
17
17
  "scripts": {
18
+ "prepare": "ts-patch install -s",
18
19
  "eslint": "eslint . -c eslint.config.js",
19
20
  "eslint:fix": "eslint . --fix -c eslint.config.js",
20
21
  "tsc": "tsc",
@@ -52,15 +53,16 @@
52
53
  "globals": "^15.0.0",
53
54
  "prettier": "^3.2.5",
54
55
  "prettier-eslint": "^16.3.0",
56
+ "ts-patch": "^3.1.2",
55
57
  "tsx": "^4.7.2",
56
58
  "typedoc": "^0.25.13",
57
59
  "typedoc-plugin-markdown": "^3.17.1",
58
60
  "typescript": "^5.4.4",
61
+ "typescript-transformer-esm": "^1.1.0",
59
62
  "vitest": "^1.4.0"
60
63
  },
61
64
  "dependencies": {
62
- "bigint-mod-arith": "^3.3.1",
63
- "random-bigint": "^0.0.1"
65
+ "bigint-mod-arith": "^3.3.1"
64
66
  },
65
67
  "keywords": [
66
68
  "cryptography",