@semaphore-protocol/identity 2.6.0 → 3.0.0-beta.5

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/index.mjs CHANGED
@@ -1,16 +1,16 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 2.6.0
3
+ * @version 3.0.0-beta.5
4
4
  * @file A library to create Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/identity}
8
8
  */
9
9
  import { BigNumber } from '@ethersproject/bignumber';
10
- import { poseidon } from 'circomlibjs';
11
- import { randomBytes } from '@ethersproject/random';
12
- import { sha256 as sha256$1 } from '@ethersproject/sha2';
10
+ import { keccak256 } from '@ethersproject/keccak256';
13
11
  import { toUtf8Bytes } from '@ethersproject/strings';
12
+ import { randomBytes } from '@ethersproject/random';
13
+ import poseidon from 'poseidon-lite';
14
14
 
15
15
  function checkParameter(value, name, type) {
16
16
  if (typeof value !== type) {
@@ -19,14 +19,14 @@ function checkParameter(value, name, type) {
19
19
  }
20
20
 
21
21
  /**
22
- * Returns an hexadecimal sha256 hash of the message passed as parameter.
23
- * @param message The string to hash.
24
- * @returns The hexadecimal hash of the message.
22
+ * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
23
+ * @param message The message to be hashed.
24
+ * @returns The message digest.
25
25
  */
26
- function sha256(message) {
27
- var hash = sha256$1(toUtf8Bytes(message));
28
- return hash;
29
- }
26
+ function hash(message) {
27
+ return BigInt(keccak256(toUtf8Bytes(message))) >> BigInt(8);
28
+ }
29
+
30
30
  /**
31
31
  * Generates a random big number.
32
32
  * @param numberOfBytes The number of bytes of the number.
@@ -73,9 +73,9 @@ var Identity = /** @class */ (function () {
73
73
  }
74
74
  checkParameter(identityOrMessage, "identityOrMessage", "string");
75
75
  if (!isJsonArray(identityOrMessage)) {
76
- var messageHash = sha256(identityOrMessage).slice(2);
77
- this._trapdoor = BigNumber.from(sha256("".concat(messageHash, "identity_trapdoor"))).toBigInt();
78
- this._nullifier = BigNumber.from(sha256("".concat(messageHash, "identity_nullifier"))).toBigInt();
76
+ var messageHash = hash(identityOrMessage);
77
+ this._trapdoor = hash("".concat(messageHash, "identity_trapdoor"));
78
+ this._nullifier = hash("".concat(messageHash, "identity_nullifier"));
79
79
  this._commitment = generateCommitment(this._nullifier, this._trapdoor);
80
80
  return;
81
81
  }
@@ -138,14 +138,6 @@ var Identity = /** @class */ (function () {
138
138
  Identity.prototype.getCommitment = function () {
139
139
  return this._commitment;
140
140
  };
141
- /**
142
- * @deprecated since version 2.6.0
143
- * Generates the identity commitment from trapdoor and nullifier.
144
- * @returns identity commitment
145
- */
146
- Identity.prototype.generateCommitment = function () {
147
- return poseidon([poseidon([this._nullifier, this._trapdoor])]);
148
- };
149
141
  /**
150
142
  * Returns a JSON string with trapdoor and nullifier. It can be used
151
143
  * to export the identity and reuse it later.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 2.6.0
3
+ * @version 3.0.0-beta.5
4
4
  * @file A library to create Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -11,10 +11,14 @@
11
11
  Object.defineProperty(exports, '__esModule', { value: true });
12
12
 
13
13
  var bignumber = require('@ethersproject/bignumber');
14
- var circomlibjs = require('circomlibjs');
15
- var random = require('@ethersproject/random');
16
- var sha2 = require('@ethersproject/sha2');
14
+ var keccak256 = require('@ethersproject/keccak256');
17
15
  var strings = require('@ethersproject/strings');
16
+ var random = require('@ethersproject/random');
17
+ var poseidon = require('poseidon-lite');
18
+
19
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
20
+
21
+ var poseidon__default = /*#__PURE__*/_interopDefaultLegacy(poseidon);
18
22
 
19
23
  function checkParameter(value, name, type) {
20
24
  if (typeof value !== type) {
@@ -23,14 +27,14 @@ function checkParameter(value, name, type) {
23
27
  }
24
28
 
25
29
  /**
26
- * Returns an hexadecimal sha256 hash of the message passed as parameter.
27
- * @param message The string to hash.
28
- * @returns The hexadecimal hash of the message.
30
+ * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
31
+ * @param message The message to be hashed.
32
+ * @returns The message digest.
29
33
  */
30
- function sha256(message) {
31
- var hash = sha2.sha256(strings.toUtf8Bytes(message));
32
- return hash;
33
- }
34
+ function hash(message) {
35
+ return BigInt(keccak256.keccak256(strings.toUtf8Bytes(message))) >> BigInt(8);
36
+ }
37
+
34
38
  /**
35
39
  * Generates a random big number.
36
40
  * @param numberOfBytes The number of bytes of the number.
@@ -47,7 +51,7 @@ function genRandomNumber(numberOfBytes) {
47
51
  * @returns identity commitment
48
52
  */
49
53
  function generateCommitment(nullifier, trapdoor) {
50
- return circomlibjs.poseidon([circomlibjs.poseidon([nullifier, trapdoor])]);
54
+ return poseidon__default["default"]([poseidon__default["default"]([nullifier, trapdoor])]);
51
55
  }
52
56
  /**
53
57
  * Checks if a string is a JSON.
@@ -77,9 +81,9 @@ var Identity = /** @class */ (function () {
77
81
  }
78
82
  checkParameter(identityOrMessage, "identityOrMessage", "string");
79
83
  if (!isJsonArray(identityOrMessage)) {
80
- var messageHash = sha256(identityOrMessage).slice(2);
81
- this._trapdoor = bignumber.BigNumber.from(sha256("".concat(messageHash, "identity_trapdoor"))).toBigInt();
82
- this._nullifier = bignumber.BigNumber.from(sha256("".concat(messageHash, "identity_nullifier"))).toBigInt();
84
+ var messageHash = hash(identityOrMessage);
85
+ this._trapdoor = hash("".concat(messageHash, "identity_trapdoor"));
86
+ this._nullifier = hash("".concat(messageHash, "identity_nullifier"));
83
87
  this._commitment = generateCommitment(this._nullifier, this._trapdoor);
84
88
  return;
85
89
  }
@@ -142,14 +146,6 @@ var Identity = /** @class */ (function () {
142
146
  Identity.prototype.getCommitment = function () {
143
147
  return this._commitment;
144
148
  };
145
- /**
146
- * @deprecated since version 2.6.0
147
- * Generates the identity commitment from trapdoor and nullifier.
148
- * @returns identity commitment
149
- */
150
- Identity.prototype.generateCommitment = function () {
151
- return circomlibjs.poseidon([circomlibjs.poseidon([this._nullifier, this._trapdoor])]);
152
- };
153
149
  /**
154
150
  * Returns a JSON string with trapdoor and nullifier. It can be used
155
151
  * to export the identity and reuse it later.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
3
+ * @param message The message to be hashed.
4
+ * @returns The message digest.
5
+ */
6
+ export default function hash(message: string): bigint;
@@ -37,12 +37,6 @@ export default class Identity {
37
37
  * @returns The identity commitment.
38
38
  */
39
39
  getCommitment(): bigint;
40
- /**
41
- * @deprecated since version 2.6.0
42
- * Generates the identity commitment from trapdoor and nullifier.
43
- * @returns identity commitment
44
- */
45
- generateCommitment(): bigint;
46
40
  /**
47
41
  * Returns a JSON string with trapdoor and nullifier. It can be used
48
42
  * to export the identity and reuse it later.
@@ -1,9 +1,3 @@
1
- /**
2
- * Returns an hexadecimal sha256 hash of the message passed as parameter.
3
- * @param message The string to hash.
4
- * @returns The hexadecimal hash of the message.
5
- */
6
- export declare function sha256(message: string): string;
7
1
  /**
8
2
  * Generates a random big number.
9
3
  * @param numberOfBytes The number of bytes of the number.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "2.6.0",
3
+ "version": "3.0.0-beta.5",
4
4
  "description": "A library to create Semaphore identities.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
@@ -36,9 +36,9 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@ethersproject/bignumber": "^5.5.0",
39
+ "@ethersproject/keccak256": "^5.7.0",
39
40
  "@ethersproject/random": "^5.5.1",
40
- "@ethersproject/sha2": "^5.6.1",
41
41
  "@ethersproject/strings": "^5.6.1",
42
- "circomlibjs": "0.0.8"
42
+ "poseidon-lite": "^0.0.2"
43
43
  }
44
44
  }
package/src/hash.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { keccak256 } from "@ethersproject/keccak256"
2
+ import { toUtf8Bytes } from "@ethersproject/strings"
3
+
4
+ /**
5
+ * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
6
+ * @param message The message to be hashed.
7
+ * @returns The message digest.
8
+ */
9
+ export default function hash(message: string): bigint {
10
+ return BigInt(keccak256(toUtf8Bytes(message))) >> BigInt(8)
11
+ }
@@ -64,9 +64,7 @@ describe("Identity", () => {
64
64
 
65
65
  const trapdoor = identity.getTrapdoor()
66
66
 
67
- expect(trapdoor).toBe(
68
- BigInt("58952291509798197436757858062402199043831251943841934828591473955215726495831")
69
- )
67
+ expect(trapdoor).toBe(BigInt("211007102311354422986775462856672883657031335757695461477990303178796954863"))
70
68
  })
71
69
  })
72
70
 
@@ -76,20 +74,16 @@ describe("Identity", () => {
76
74
 
77
75
  const nullifier = identity.getNullifier()
78
76
 
79
- expect(nullifier).toBe(
80
- BigInt("44673097405870585416457571638073245190425597599743560105244308998175651589997")
81
- )
77
+ expect(nullifier).toBe(BigInt("10282208199720122340759039255952223220417076359839127631923809108800013776"))
82
78
  })
83
79
  })
84
80
 
85
81
  describe("# generateCommitment", () => {
86
82
  it("Should generate an identity commitment", () => {
87
- const identity = new Identity("message")
88
-
89
- const commitment = identity.generateCommitment()
83
+ const { commitment } = new Identity("message")
90
84
 
91
85
  expect(commitment).toBe(
92
- BigInt("1720349790382552497189398984241859233944354304766757200361065203741879866188")
86
+ BigInt("13192222509545780880434144549342414064490325100975031303723930089730328393905")
93
87
  )
94
88
  })
95
89
  })
@@ -108,8 +102,8 @@ describe("Identity", () => {
108
102
 
109
103
  const [trapdoor, nullifier] = JSON.parse(identity.toString())
110
104
 
111
- expect(BigNumber.from(`0x${trapdoor}`).toBigInt()).toBe(identity.getTrapdoor())
112
- expect(BigNumber.from(`0x${nullifier}`).toBigInt()).toBe(identity.getNullifier())
105
+ expect(BigNumber.from(`0x${trapdoor}`).toBigInt()).toBe(identity.trapdoor)
106
+ expect(BigNumber.from(`0x${nullifier}`).toBigInt()).toBe(identity.nullifier)
113
107
  })
114
108
  })
115
109
  })
package/src/identity.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { BigNumber } from "@ethersproject/bignumber"
2
- import { poseidon } from "circomlibjs"
3
2
  import checkParameter from "./checkParameter"
4
- import { generateCommitment, genRandomNumber, isJsonArray, sha256 } from "./utils"
3
+ import hash from "./hash"
4
+ import { generateCommitment, genRandomNumber, isJsonArray } from "./utils"
5
5
 
6
6
  export default class Identity {
7
7
  private _trapdoor: bigint
@@ -24,10 +24,10 @@ export default class Identity {
24
24
  checkParameter(identityOrMessage, "identityOrMessage", "string")
25
25
 
26
26
  if (!isJsonArray(identityOrMessage)) {
27
- const messageHash = sha256(identityOrMessage).slice(2)
27
+ const messageHash = hash(identityOrMessage)
28
28
 
29
- this._trapdoor = BigNumber.from(sha256(`${messageHash}identity_trapdoor`)).toBigInt()
30
- this._nullifier = BigNumber.from(sha256(`${messageHash}identity_nullifier`)).toBigInt()
29
+ this._trapdoor = hash(`${messageHash}identity_trapdoor`)
30
+ this._nullifier = hash(`${messageHash}identity_nullifier`)
31
31
  this._commitment = generateCommitment(this._nullifier, this._trapdoor)
32
32
 
33
33
  return
@@ -88,15 +88,6 @@ export default class Identity {
88
88
  return this._commitment
89
89
  }
90
90
 
91
- /**
92
- * @deprecated since version 2.6.0
93
- * Generates the identity commitment from trapdoor and nullifier.
94
- * @returns identity commitment
95
- */
96
- public generateCommitment(): bigint {
97
- return poseidon([poseidon([this._nullifier, this._trapdoor])])
98
- }
99
-
100
91
  /**
101
92
  * Returns a JSON string with trapdoor and nullifier. It can be used
102
93
  * to export the identity and reuse it later.
package/src/utils.ts CHANGED
@@ -1,19 +1,6 @@
1
1
  import { BigNumber } from "@ethersproject/bignumber"
2
2
  import { randomBytes } from "@ethersproject/random"
3
- import { sha256 as _sha256 } from "@ethersproject/sha2"
4
- import { toUtf8Bytes } from "@ethersproject/strings"
5
- import { poseidon } from "circomlibjs"
6
-
7
- /**
8
- * Returns an hexadecimal sha256 hash of the message passed as parameter.
9
- * @param message The string to hash.
10
- * @returns The hexadecimal hash of the message.
11
- */
12
- export function sha256(message: string): string {
13
- const hash = _sha256(toUtf8Bytes(message))
14
-
15
- return hash
16
- }
3
+ import poseidon from "poseidon-lite"
17
4
 
18
5
  /**
19
6
  * Generates a random big number.