@semaphore-protocol/identity 3.15.2 → 4.0.0-alpha.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Ethereum Foundation
3
+ Copyright (c) 2024 Ethereum Foundation
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -49,8 +49,8 @@
49
49
  </h4>
50
50
  </div>
51
51
 
52
- | This library provides a class that can be used to create identities compatible with the Semaphore [circuits](https://github.com/semaphore-protocol/semaphore/tree/main/circuits). Each identity contains two secret values: _trapdoor_ and _nullifier_, and one public value: _commitment_. The Poseidon hash of the secret values is the identity secret, and its hash is the identity commitment. |
53
- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52
+ | This library provides a class that can be used to create identities compatible with the Semaphore [circuits](https://github.com/semaphore-protocol/semaphore/tree/main/circuits). Each identity contains an [EdDSA](https://www.rfc-editor.org/rfc/rfc8032) private key, its public key, and the identity commitment, which is the [Poseidon](https://eprint.iacr.org/2019/458.pdf) hash of the public key. |
53
+ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54
54
 
55
55
  ## 🛠 Install
56
56
 
@@ -70,20 +70,51 @@ yarn add @semaphore-protocol/identity
70
70
 
71
71
  ## 📜 Usage
72
72
 
73
- \# **new Identity**(identityOrMessage?: _string_): _Identity_
73
+ \# **new Identity**(privateKey?: _BigNumberish_): _Identity_
74
74
 
75
75
  ```typescript
76
76
  import { Identity } from "@semaphore-protocol/identity"
77
77
 
78
- // The identity can be generated randomly.
79
- const identity1 = new Identity()
78
+ // The identity will be generated randomly.
79
+ const { privateKey, publicKey, commitment } = new Identity()
80
80
 
81
- // Deterministically from a secret message.
82
- const identity2 = new Identity("secret-message")
81
+ // Alternatively, you can pass your private key.
82
+ const identity = new Identity("your-private-key")
83
+ ```
84
+
85
+ \# **identity.signMessage**(message: _BigNumberish_): _Signature\<string>_
86
+
87
+ ```typescript
88
+ import { Identity } from "@semaphore-protocol/identity"
89
+
90
+ const message = "message"
91
+ const identity = new Identity()
92
+
93
+ const signature = identity.signMessage(message)
94
+ ```
95
+
96
+ \# **identity.verifySignature**(message: _BigNumberish_, signature: _Signature_): _boolean_
97
+
98
+ ```typescript
99
+ import { Identity } from "@semaphore-protocol/identity"
100
+
101
+ const message = "message"
102
+ const identity = new Identity()
103
+
104
+ const signature = identity.signMessage(message)
105
+
106
+ identity.verifySignature(message, signature)
107
+ ```
108
+
109
+ \# **Identity.verifySignature**(message: _BigNumberish_, signature: _Signature_, publicKey: _Point_): _boolean_
110
+
111
+ ```typescript
112
+ import { Identity } from "@semaphore-protocol/identity"
113
+
114
+ const message = "message"
115
+ const identity = new Identity()
83
116
 
84
- // Or it can be retrieved from an existing identity.
85
- const identity3 = new Identity(identity1.toString())
117
+ const signature = identity.signMessage("message", signature)
86
118
 
87
- // Trapdoor, nullifier and commitment are the attributes (e.g. JS getters).
88
- const { trapdoor, nullifier, commitment } = identity1
119
+ Identity.verifySignature(message, signature, identity.publicKey)
89
120
  ```
package/dist/index.mjs CHANGED
@@ -1,16 +1,15 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 3.15.2
3
+ * @version 4.0.0-alpha.1
4
4
  * @file A library to create Semaphore identities.
5
- * @copyright Ethereum Foundation 2022
5
+ * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/identity}
8
8
  */
9
- import { BigNumber } from '@ethersproject/bignumber';
10
- import hash from 'js-sha512';
11
- import { randomBytes } from '@ethersproject/random';
9
+ import { signMessage, verifySignature, deriveSecretScalar, derivePublicKey } from '@zk-kit/eddsa-poseidon';
10
+ import { randomBytes } from 'crypto';
12
11
 
13
- var poseidon1$1 = {};
12
+ var poseidon2$1 = {};
14
13
 
15
14
  const F = BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617');
16
15
  const N_ROUNDS_F = 8;
@@ -81,33 +80,6 @@ function unstringifyBigInts(o) {
81
80
  return BigInt(`0x${hex}`);
82
81
  }
83
82
 
84
- var _1 = {};
85
-
86
- Object.defineProperty(_1, "__esModule", {
87
- value: true
88
- });
89
- _1.default = void 0;
90
- var _default$1 = {
91
- C: ['CcRunsaOm9T+H6q6KUy6OKcaoXdTTN0bbH3A29Cr16c=', 'DANWUwiW7sQql+2TfzE1z8UUKzrkBbg0PB2D/6YEy4E=', 'Hiih2TVpitEULlEYK7VM9KAOpaq9Ymi9MX6pd8wVSjA=', 'J68tgxqdJ0gICWXbMOKY5A5XV8PgCNuWTPnisSuRJR8=', 'Hm8RzmD8j1E6ajz+Fq4XWkEpFGLyFM0Iear0NUW3TgM=', 'Kmc4TTu9XkOFQYGctoHwvgRGLtFMNhPY9xkgYmjRQtM=', 'C2b981YJOmEWCfjhL7/s8LmF44HwJRiJNkCPXVyfRdA=', 'AS7j7B541HCDDGEJPCreNwsmyDzFzr7t2qaFLb2wniE=', 'AlK6X2dgv739iPZ/gXXj/WzRxDGwmba7LRCOe0Rbsbk=', 'F5R0zOyl/2dsa+w871QpY1Q5Gok1/3HW71rqrXypMvE=', 'LCQmE3mlG/qSKP9KUD/U7Zwfl0omSWmzfholibvtK5E=', 'HMHXtiaS5j6sLyiL0GlbQ8L2P1AB/A/FU+ZsBVGAGwU=', 'JVBZMBqtqYuy7VX4UpeelgB4Tb8X+6zQXZ7/X9nJG1Y=', 'KEN746wcsuR54fXA7M0ys66iQjSXCoGTsRwpzn5Z79k=', 'KCFqRC8uH3EcpPprU3ZusRhUjaj7T3jUM4diw39fIEM=', 'LB9HzRf6Wt8fOfTnBW3QP+7h784DCUWBEx8jdzI0gsk=', 'B6utAr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nvOynC9KvetU=', 'K51LQRDJrpl3guFQmx0P2yCnwCu9i+pzBUYrn4Elseg='], ['DMV827CFB9Yr9npEk8wmL7bAnVVwE//x9XP0MSIfj/k=', 'EnTmSaMu01WjGm7WlyThra3oV+hutcOhIbzRR5QyA8g=']]
93
- };
94
- _1.default = _default$1;
95
-
96
- Object.defineProperty(poseidon1$1, "__esModule", {
97
- value: true
98
- });
99
- var poseidon1_2 = poseidon1$1.poseidon1 = poseidon1;
100
- var _poseidon$1 = _interopRequireDefault$1(poseidon_1);
101
- var _unstringify$1 = _interopRequireDefault$1(unstringify);
102
- var _$1 = _interopRequireDefault$1(_1);
103
- function _interopRequireDefault$1(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
104
- const c$1 = (0, _unstringify$1.default)(_$1.default);
105
- function poseidon1(inputs) {
106
- return (0, _poseidon$1.default)(inputs, c$1);
107
- }
108
-
109
- var poseidon2$1 = {};
110
-
111
83
  var _2 = {};
112
84
 
113
85
  Object.defineProperty(_2, "__esModule", {
@@ -133,118 +105,51 @@ function poseidon2(inputs) {
133
105
  return (0, _poseidon.default)(inputs, c);
134
106
  }
135
107
 
136
- function checkParameter(value, name, type) {
137
- if (typeof value !== type) {
138
- throw new TypeError("Parameter '".concat(name, "' is not a ").concat(type));
139
- }
140
- }
141
-
142
- /**
143
- * Generates a random big number.
144
- * @param numberOfBytes The number of bytes of the number.
145
- * @returns The generated random number.
146
- */
147
- function genRandomNumber(numberOfBytes) {
148
- if (numberOfBytes === void 0) { numberOfBytes = 31; }
149
- return BigNumber.from(randomBytes(numberOfBytes)).toBigInt();
150
- }
151
- /**
152
- * Checks if a string is a JSON.
153
- * @param jsonString The JSON string.
154
- * @returns True or false.
155
- */
156
- function isJsonArray(jsonString) {
157
- try {
158
- return Array.isArray(JSON.parse(jsonString));
159
- }
160
- catch (error) {
161
- return false;
162
- }
163
- }
164
-
165
108
  var Identity = /** @class */ (function () {
166
109
  /**
167
- * Initializes the class attributes based on the strategy passed as parameter.
168
- * @param identityOrMessage Additional data needed to create identity for given strategy.
110
+ * Initializes the class attributes based on the parameters.
111
+ * @param privateKey The secret value used to generate an EdDSA public key.
169
112
  */
170
- function Identity(identityOrMessage) {
171
- if (identityOrMessage === undefined) {
172
- this._trapdoor = genRandomNumber();
173
- this._nullifier = genRandomNumber();
174
- this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
175
- this._commitment = poseidon1_2([this._secret]);
176
- return;
177
- }
178
- checkParameter(identityOrMessage, "identityOrMessage", "string");
179
- if (!isJsonArray(identityOrMessage)) {
180
- var h = hash.sha512(identityOrMessage).padStart(128, "0");
181
- // alt_bn128 is 253.6 bits, so we can safely use 253 bits.
182
- this._trapdoor = BigInt("0x".concat(h.slice(64))) >> BigInt(3);
183
- this._nullifier = BigInt("0x".concat(h.slice(0, 64))) >> BigInt(3);
184
- this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
185
- this._commitment = poseidon1_2([this._secret]);
186
- return;
187
- }
188
- var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
189
- this._trapdoor = BigNumber.from(trapdoor).toBigInt();
190
- this._nullifier = BigNumber.from(nullifier).toBigInt();
191
- this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
192
- this._commitment = poseidon1_2([this._secret]);
113
+ function Identity(privateKey) {
114
+ if (privateKey === void 0) { privateKey = BigInt("0x".concat(randomBytes(32).toString("hex"))).toString(); }
115
+ this._privateKey = privateKey;
116
+ this._secretScalar = deriveSecretScalar(privateKey);
117
+ this._publicKey = derivePublicKey(privateKey);
118
+ this._commitment = poseidon2_2(this._publicKey).toString();
193
119
  }
194
- Object.defineProperty(Identity.prototype, "trapdoor", {
120
+ Object.defineProperty(Identity.prototype, "privateKey", {
195
121
  /**
196
- * Returns the identity trapdoor.
197
- * @returns The identity trapdoor.
122
+ * Returns the private key.
123
+ * @returns The private key.
198
124
  */
199
125
  get: function () {
200
- return this._trapdoor;
126
+ return this._privateKey;
201
127
  },
202
128
  enumerable: false,
203
129
  configurable: true
204
130
  });
205
- /**
206
- * Returns the identity trapdoor.
207
- * @returns The identity trapdoor.
208
- */
209
- Identity.prototype.getTrapdoor = function () {
210
- return this._trapdoor;
211
- };
212
- Object.defineProperty(Identity.prototype, "nullifier", {
131
+ Object.defineProperty(Identity.prototype, "secretScalar", {
213
132
  /**
214
- * Returns the identity nullifier.
215
- * @returns The identity nullifier.
133
+ * Returns the secret scalar.
134
+ * @returns The secret scalar.
216
135
  */
217
136
  get: function () {
218
- return this._nullifier;
137
+ return this._secretScalar;
219
138
  },
220
139
  enumerable: false,
221
140
  configurable: true
222
141
  });
223
- /**
224
- * Returns the identity nullifier.
225
- * @returns The identity nullifier.
226
- */
227
- Identity.prototype.getNullifier = function () {
228
- return this._nullifier;
229
- };
230
- Object.defineProperty(Identity.prototype, "secret", {
142
+ Object.defineProperty(Identity.prototype, "publicKey", {
231
143
  /**
232
- * Returns the identity secret.
233
- * @returns The identity secret.
144
+ * Returns the public key.
145
+ * @returns The public key.
234
146
  */
235
147
  get: function () {
236
- return this._secret;
148
+ return this._publicKey;
237
149
  },
238
150
  enumerable: false,
239
151
  configurable: true
240
152
  });
241
- /**
242
- * Returns the identity secret.
243
- * @returns The identity secret.
244
- */
245
- Identity.prototype.getSecret = function () {
246
- return this._secret;
247
- };
248
153
  Object.defineProperty(Identity.prototype, "commitment", {
249
154
  /**
250
155
  * Returns the identity commitment.
@@ -256,20 +161,14 @@ var Identity = /** @class */ (function () {
256
161
  enumerable: false,
257
162
  configurable: true
258
163
  });
259
- /**
260
- * Returns the identity commitment.
261
- * @returns The identity commitment.
262
- */
263
- Identity.prototype.getCommitment = function () {
264
- return this._commitment;
164
+ Identity.prototype.signMessage = function (message) {
165
+ return signMessage(this.privateKey, message);
265
166
  };
266
- /**
267
- * Returns a JSON string with trapdoor and nullifier. It can be used
268
- * to export the identity and reuse it later.
269
- * @returns The string representation of the identity.
270
- */
271
- Identity.prototype.toString = function () {
272
- return JSON.stringify(["0x".concat(this._trapdoor.toString(16)), "0x".concat(this._nullifier.toString(16))]);
167
+ Identity.prototype.verifySignature = function (message, signature) {
168
+ return verifySignature(message, signature, this._publicKey);
169
+ };
170
+ Identity.verifySignature = function (message, signature, publicKey) {
171
+ return verifySignature(message, signature, publicKey);
273
172
  };
274
173
  return Identity;
275
174
  }());
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 3.15.2
3
+ * @version 4.0.0-alpha.1
4
4
  * @file A library to create Semaphore identities.
5
- * @copyright Ethereum Foundation 2022
5
+ * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/identity}
8
8
  */
@@ -10,15 +10,10 @@
10
10
 
11
11
  Object.defineProperty(exports, '__esModule', { value: true });
12
12
 
13
- var bignumber = require('@ethersproject/bignumber');
14
- var hash = require('js-sha512');
15
- var random = require('@ethersproject/random');
13
+ var eddsaPoseidon = require('@zk-kit/eddsa-poseidon');
14
+ var crypto = require('crypto');
16
15
 
17
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
-
19
- var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash);
20
-
21
- var poseidon1$1 = {};
16
+ var poseidon2$1 = {};
22
17
 
23
18
  const F = BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617');
24
19
  const N_ROUNDS_F = 8;
@@ -89,33 +84,6 @@ function unstringifyBigInts(o) {
89
84
  return BigInt(`0x${hex}`);
90
85
  }
91
86
 
92
- var _1 = {};
93
-
94
- Object.defineProperty(_1, "__esModule", {
95
- value: true
96
- });
97
- _1.default = void 0;
98
- var _default$1 = {
99
- C: ['CcRunsaOm9T+H6q6KUy6OKcaoXdTTN0bbH3A29Cr16c=', 'DANWUwiW7sQql+2TfzE1z8UUKzrkBbg0PB2D/6YEy4E=', 'Hiih2TVpitEULlEYK7VM9KAOpaq9Ymi9MX6pd8wVSjA=',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TnSC4LA4=', 'Lww6EV1DF9GRuom40T0YBsIKD5sk+MXtwJHirlZWWYQ=', 'DE7neP98FFUwBu0iDPnIEAigz/ZwsiuC2MU4odyVjGE=', 'FwTydm1G+Cw2k/AEQMzDYJQk7SbArMZiJ8PXSF3nTGk=', 'Ly0ZzD6l146noCwbUdJEq/B2nJ+FROQCObZv6QCcPPo=', 'GuA4U7dfyrpQU/ES4qjo3N1+5suc/tnH1sdmqAb8Zik=', 'CXGqv3lSQd9R0THQ+mGqXzVWkhstbwFOTkGobdrwVtU=', 'FAjDFuYBThqR1M9rbg3nPtpiT4OA3xyHX1wp97/i9kY=', 'Fmfz/i7b6FAkir5CtUMJO2yJ8fdz7yhTQWkfOYIu9b0=', 'E798XQ0sQ3akiwoDVXzfkVuBcYQJ5cEzQkxpV2UA/jc=', 'B2IKbfsLbOwwFq3z01M8JAJLlTR4VreXGbwLp0OmLCw=', 'FXTH7wxDVF82qMoIvb3YsHXSlZ4vMitzFnXePhmCtNA=', 'Jp5LW3oushr9VnlwpxfO7FvUGEVxwlT9wG4Dp/+DePA='],
100
- M: [['Bm9vhdb2ioXsEDRTUaI6Oq8H84r4yVKnvOynC9KvetU=', 'K51LQRDJrpl3guFQmx0P2yCnwCu9i+pzBUYrn4Elseg='], ['DMV827CFB9Yr9npEk8wmL7bAnVVwE//x9XP0MSIfj/k=', 'EnTmSaMu01WjGm7WlyThra3oV+hutcOhIbzRR5QyA8g=']]
101
- };
102
- _1.default = _default$1;
103
-
104
- Object.defineProperty(poseidon1$1, "__esModule", {
105
- value: true
106
- });
107
- var poseidon1_2 = poseidon1$1.poseidon1 = poseidon1;
108
- var _poseidon$1 = _interopRequireDefault$1(poseidon_1);
109
- var _unstringify$1 = _interopRequireDefault$1(unstringify);
110
- var _$1 = _interopRequireDefault$1(_1);
111
- function _interopRequireDefault$1(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
112
- const c$1 = (0, _unstringify$1.default)(_$1.default);
113
- function poseidon1(inputs) {
114
- return (0, _poseidon$1.default)(inputs, c$1);
115
- }
116
-
117
- var poseidon2$1 = {};
118
-
119
87
  var _2 = {};
120
88
 
121
89
  Object.defineProperty(_2, "__esModule", {
@@ -141,118 +109,51 @@ function poseidon2(inputs) {
141
109
  return (0, _poseidon.default)(inputs, c);
142
110
  }
143
111
 
144
- function checkParameter(value, name, type) {
145
- if (typeof value !== type) {
146
- throw new TypeError("Parameter '".concat(name, "' is not a ").concat(type));
147
- }
148
- }
149
-
150
- /**
151
- * Generates a random big number.
152
- * @param numberOfBytes The number of bytes of the number.
153
- * @returns The generated random number.
154
- */
155
- function genRandomNumber(numberOfBytes) {
156
- if (numberOfBytes === void 0) { numberOfBytes = 31; }
157
- return bignumber.BigNumber.from(random.randomBytes(numberOfBytes)).toBigInt();
158
- }
159
- /**
160
- * Checks if a string is a JSON.
161
- * @param jsonString The JSON string.
162
- * @returns True or false.
163
- */
164
- function isJsonArray(jsonString) {
165
- try {
166
- return Array.isArray(JSON.parse(jsonString));
167
- }
168
- catch (error) {
169
- return false;
170
- }
171
- }
172
-
173
112
  var Identity = /** @class */ (function () {
174
113
  /**
175
- * Initializes the class attributes based on the strategy passed as parameter.
176
- * @param identityOrMessage Additional data needed to create identity for given strategy.
114
+ * Initializes the class attributes based on the parameters.
115
+ * @param privateKey The secret value used to generate an EdDSA public key.
177
116
  */
178
- function Identity(identityOrMessage) {
179
- if (identityOrMessage === undefined) {
180
- this._trapdoor = genRandomNumber();
181
- this._nullifier = genRandomNumber();
182
- this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
183
- this._commitment = poseidon1_2([this._secret]);
184
- return;
185
- }
186
- checkParameter(identityOrMessage, "identityOrMessage", "string");
187
- if (!isJsonArray(identityOrMessage)) {
188
- var h = hash__default["default"].sha512(identityOrMessage).padStart(128, "0");
189
- // alt_bn128 is 253.6 bits, so we can safely use 253 bits.
190
- this._trapdoor = BigInt("0x".concat(h.slice(64))) >> BigInt(3);
191
- this._nullifier = BigInt("0x".concat(h.slice(0, 64))) >> BigInt(3);
192
- this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
193
- this._commitment = poseidon1_2([this._secret]);
194
- return;
195
- }
196
- var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
197
- this._trapdoor = bignumber.BigNumber.from(trapdoor).toBigInt();
198
- this._nullifier = bignumber.BigNumber.from(nullifier).toBigInt();
199
- this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
200
- this._commitment = poseidon1_2([this._secret]);
117
+ function Identity(privateKey) {
118
+ if (privateKey === void 0) { privateKey = BigInt("0x".concat(crypto.randomBytes(32).toString("hex"))).toString(); }
119
+ this._privateKey = privateKey;
120
+ this._secretScalar = eddsaPoseidon.deriveSecretScalar(privateKey);
121
+ this._publicKey = eddsaPoseidon.derivePublicKey(privateKey);
122
+ this._commitment = poseidon2_2(this._publicKey).toString();
201
123
  }
202
- Object.defineProperty(Identity.prototype, "trapdoor", {
124
+ Object.defineProperty(Identity.prototype, "privateKey", {
203
125
  /**
204
- * Returns the identity trapdoor.
205
- * @returns The identity trapdoor.
126
+ * Returns the private key.
127
+ * @returns The private key.
206
128
  */
207
129
  get: function () {
208
- return this._trapdoor;
130
+ return this._privateKey;
209
131
  },
210
132
  enumerable: false,
211
133
  configurable: true
212
134
  });
213
- /**
214
- * Returns the identity trapdoor.
215
- * @returns The identity trapdoor.
216
- */
217
- Identity.prototype.getTrapdoor = function () {
218
- return this._trapdoor;
219
- };
220
- Object.defineProperty(Identity.prototype, "nullifier", {
135
+ Object.defineProperty(Identity.prototype, "secretScalar", {
221
136
  /**
222
- * Returns the identity nullifier.
223
- * @returns The identity nullifier.
137
+ * Returns the secret scalar.
138
+ * @returns The secret scalar.
224
139
  */
225
140
  get: function () {
226
- return this._nullifier;
141
+ return this._secretScalar;
227
142
  },
228
143
  enumerable: false,
229
144
  configurable: true
230
145
  });
231
- /**
232
- * Returns the identity nullifier.
233
- * @returns The identity nullifier.
234
- */
235
- Identity.prototype.getNullifier = function () {
236
- return this._nullifier;
237
- };
238
- Object.defineProperty(Identity.prototype, "secret", {
146
+ Object.defineProperty(Identity.prototype, "publicKey", {
239
147
  /**
240
- * Returns the identity secret.
241
- * @returns The identity secret.
148
+ * Returns the public key.
149
+ * @returns The public key.
242
150
  */
243
151
  get: function () {
244
- return this._secret;
152
+ return this._publicKey;
245
153
  },
246
154
  enumerable: false,
247
155
  configurable: true
248
156
  });
249
- /**
250
- * Returns the identity secret.
251
- * @returns The identity secret.
252
- */
253
- Identity.prototype.getSecret = function () {
254
- return this._secret;
255
- };
256
157
  Object.defineProperty(Identity.prototype, "commitment", {
257
158
  /**
258
159
  * Returns the identity commitment.
@@ -264,20 +165,14 @@ var Identity = /** @class */ (function () {
264
165
  enumerable: false,
265
166
  configurable: true
266
167
  });
267
- /**
268
- * Returns the identity commitment.
269
- * @returns The identity commitment.
270
- */
271
- Identity.prototype.getCommitment = function () {
272
- return this._commitment;
168
+ Identity.prototype.signMessage = function (message) {
169
+ return eddsaPoseidon.signMessage(this.privateKey, message);
273
170
  };
274
- /**
275
- * Returns a JSON string with trapdoor and nullifier. It can be used
276
- * to export the identity and reuse it later.
277
- * @returns The string representation of the identity.
278
- */
279
- Identity.prototype.toString = function () {
280
- return JSON.stringify(["0x".concat(this._trapdoor.toString(16)), "0x".concat(this._nullifier.toString(16))]);
171
+ Identity.prototype.verifySignature = function (message, signature) {
172
+ return eddsaPoseidon.verifySignature(message, signature, this._publicKey);
173
+ };
174
+ Identity.verifySignature = function (message, signature, publicKey) {
175
+ return eddsaPoseidon.verifySignature(message, signature, publicKey);
281
176
  };
282
177
  return Identity;
283
178
  }());
@@ -1,57 +1,35 @@
1
+ import { BigNumberish, Point, Signature } from "@zk-kit/eddsa-poseidon";
1
2
  export default class Identity {
2
- private _trapdoor;
3
- private _nullifier;
4
- private _secret;
3
+ private _privateKey;
4
+ private _secretScalar;
5
+ private _publicKey;
5
6
  private _commitment;
6
7
  /**
7
- * Initializes the class attributes based on the strategy passed as parameter.
8
- * @param identityOrMessage Additional data needed to create identity for given strategy.
8
+ * Initializes the class attributes based on the parameters.
9
+ * @param privateKey The secret value used to generate an EdDSA public key.
9
10
  */
10
- constructor(identityOrMessage?: string);
11
+ constructor(privateKey?: BigNumberish);
11
12
  /**
12
- * Returns the identity trapdoor.
13
- * @returns The identity trapdoor.
13
+ * Returns the private key.
14
+ * @returns The private key.
14
15
  */
15
- get trapdoor(): bigint;
16
+ get privateKey(): BigNumberish;
16
17
  /**
17
- * Returns the identity trapdoor.
18
- * @returns The identity trapdoor.
18
+ * Returns the secret scalar.
19
+ * @returns The secret scalar.
19
20
  */
20
- getTrapdoor(): bigint;
21
+ get secretScalar(): string;
21
22
  /**
22
- * Returns the identity nullifier.
23
- * @returns The identity nullifier.
23
+ * Returns the public key.
24
+ * @returns The public key.
24
25
  */
25
- get nullifier(): bigint;
26
- /**
27
- * Returns the identity nullifier.
28
- * @returns The identity nullifier.
29
- */
30
- getNullifier(): bigint;
31
- /**
32
- * Returns the identity secret.
33
- * @returns The identity secret.
34
- */
35
- get secret(): bigint;
36
- /**
37
- * Returns the identity secret.
38
- * @returns The identity secret.
39
- */
40
- getSecret(): bigint;
26
+ get publicKey(): Point<string>;
41
27
  /**
42
28
  * Returns the identity commitment.
43
29
  * @returns The identity commitment.
44
30
  */
45
- get commitment(): bigint;
46
- /**
47
- * Returns the identity commitment.
48
- * @returns The identity commitment.
49
- */
50
- getCommitment(): bigint;
51
- /**
52
- * Returns a JSON string with trapdoor and nullifier. It can be used
53
- * to export the identity and reuse it later.
54
- * @returns The string representation of the identity.
55
- */
56
- toString(): string;
31
+ get commitment(): string;
32
+ signMessage(message: BigNumberish): Signature<string>;
33
+ verifySignature(message: BigNumberish, signature: Signature): boolean;
34
+ static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean;
57
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "3.15.2",
3
+ "version": "4.0.0-alpha.1",
4
4
  "description": "A library to create Semaphore identities.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
@@ -32,15 +32,11 @@
32
32
  "devDependencies": {
33
33
  "@rollup/plugin-commonjs": "^24.0.1",
34
34
  "@rollup/plugin-node-resolve": "^15.0.1",
35
- "poseidon-lite": "^0.2.0",
36
35
  "rollup-plugin-cleanup": "^3.2.1",
37
36
  "rollup-plugin-typescript2": "^0.31.2"
38
37
  },
39
38
  "dependencies": {
40
- "@ethersproject/bignumber": "^5.5.0",
41
- "@ethersproject/keccak256": "^5.7.0",
42
- "@ethersproject/random": "^5.5.1",
43
- "@ethersproject/strings": "^5.6.1",
44
- "js-sha512": "^0.8.0"
39
+ "@zk-kit/eddsa-poseidon": "0.4.1",
40
+ "poseidon-lite": "^0.2.0"
45
41
  }
46
42
  }
package/src/identity.ts CHANGED
@@ -1,121 +1,75 @@
1
- import { BigNumber } from "@ethersproject/bignumber"
2
- import hash from "js-sha512"
3
- import { poseidon1 } from "poseidon-lite/poseidon1"
1
+ import {
2
+ BigNumberish,
3
+ Point,
4
+ Signature,
5
+ derivePublicKey,
6
+ deriveSecretScalar,
7
+ signMessage,
8
+ verifySignature
9
+ } from "@zk-kit/eddsa-poseidon"
10
+ import { randomBytes } from "crypto"
4
11
  import { poseidon2 } from "poseidon-lite/poseidon2"
5
- import checkParameter from "./checkParameter"
6
- import { genRandomNumber, isJsonArray } from "./utils"
7
12
 
8
13
  export default class Identity {
9
- private _trapdoor: bigint
10
- private _nullifier: bigint
11
- private _secret: bigint
12
- private _commitment: bigint
14
+ private _privateKey: BigNumberish
15
+ private _secretScalar: string
16
+ private _publicKey: Point<string>
17
+ private _commitment: string
13
18
 
14
19
  /**
15
- * Initializes the class attributes based on the strategy passed as parameter.
16
- * @param identityOrMessage Additional data needed to create identity for given strategy.
20
+ * Initializes the class attributes based on the parameters.
21
+ * @param privateKey The secret value used to generate an EdDSA public key.
17
22
  */
18
- constructor(identityOrMessage?: string) {
19
- if (identityOrMessage === undefined) {
20
- this._trapdoor = genRandomNumber()
21
- this._nullifier = genRandomNumber()
22
- this._secret = poseidon2([this._nullifier, this._trapdoor])
23
- this._commitment = poseidon1([this._secret])
23
+ constructor(privateKey: BigNumberish = BigInt(`0x${randomBytes(32).toString("hex")}`).toString()) {
24
+ this._privateKey = privateKey
25
+ this._secretScalar = deriveSecretScalar(privateKey)
24
26
 
25
- return
26
- }
27
+ this._publicKey = derivePublicKey(privateKey)
27
28
 
28
- checkParameter(identityOrMessage, "identityOrMessage", "string")
29
-
30
- if (!isJsonArray(identityOrMessage)) {
31
- const h = hash.sha512(identityOrMessage).padStart(128, "0")
32
- // alt_bn128 is 253.6 bits, so we can safely use 253 bits.
33
- this._trapdoor = BigInt(`0x${h.slice(64)}`) >> BigInt(3)
34
- this._nullifier = BigInt(`0x${h.slice(0, 64)}`) >> BigInt(3)
35
- this._secret = poseidon2([this._nullifier, this._trapdoor])
36
- this._commitment = poseidon1([this._secret])
37
-
38
- return
39
- }
40
-
41
- const [trapdoor, nullifier] = JSON.parse(identityOrMessage)
42
-
43
- this._trapdoor = BigNumber.from(trapdoor).toBigInt()
44
- this._nullifier = BigNumber.from(nullifier).toBigInt()
45
- this._secret = poseidon2([this._nullifier, this._trapdoor])
46
- this._commitment = poseidon1([this._secret])
29
+ this._commitment = poseidon2(this._publicKey).toString()
47
30
  }
48
31
 
49
32
  /**
50
- * Returns the identity trapdoor.
51
- * @returns The identity trapdoor.
33
+ * Returns the private key.
34
+ * @returns The private key.
52
35
  */
53
- public get trapdoor(): bigint {
54
- return this._trapdoor
36
+ public get privateKey(): BigNumberish {
37
+ return this._privateKey
55
38
  }
56
39
 
57
40
  /**
58
- * Returns the identity trapdoor.
59
- * @returns The identity trapdoor.
41
+ * Returns the secret scalar.
42
+ * @returns The secret scalar.
60
43
  */
61
- public getTrapdoor(): bigint {
62
- return this._trapdoor
44
+ public get secretScalar(): string {
45
+ return this._secretScalar
63
46
  }
64
47
 
65
48
  /**
66
- * Returns the identity nullifier.
67
- * @returns The identity nullifier.
49
+ * Returns the public key.
50
+ * @returns The public key.
68
51
  */
69
- public get nullifier(): bigint {
70
- return this._nullifier
71
- }
72
-
73
- /**
74
- * Returns the identity nullifier.
75
- * @returns The identity nullifier.
76
- */
77
- public getNullifier(): bigint {
78
- return this._nullifier
79
- }
80
-
81
- /**
82
- * Returns the identity secret.
83
- * @returns The identity secret.
84
- */
85
- public get secret(): bigint {
86
- return this._secret
87
- }
88
-
89
- /**
90
- * Returns the identity secret.
91
- * @returns The identity secret.
92
- */
93
- public getSecret(): bigint {
94
- return this._secret
52
+ public get publicKey(): Point<string> {
53
+ return this._publicKey
95
54
  }
96
55
 
97
56
  /**
98
57
  * Returns the identity commitment.
99
58
  * @returns The identity commitment.
100
59
  */
101
- public get commitment(): bigint {
60
+ public get commitment(): string {
102
61
  return this._commitment
103
62
  }
104
63
 
105
- /**
106
- * Returns the identity commitment.
107
- * @returns The identity commitment.
108
- */
109
- public getCommitment(): bigint {
110
- return this._commitment
64
+ public signMessage(message: BigNumberish): Signature<string> {
65
+ return signMessage(this.privateKey, message)
111
66
  }
112
67
 
113
- /**
114
- * Returns a JSON string with trapdoor and nullifier. It can be used
115
- * to export the identity and reuse it later.
116
- * @returns The string representation of the identity.
117
- */
118
- public toString(): string {
119
- return JSON.stringify([`0x${this._trapdoor.toString(16)}`, `0x${this._nullifier.toString(16)}`])
68
+ public verifySignature(message: BigNumberish, signature: Signature): boolean {
69
+ return verifySignature(message, signature, this._publicKey)
70
+ }
71
+
72
+ static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
73
+ return verifySignature(message, signature, publicKey)
120
74
  }
121
75
  }
@@ -1 +0,0 @@
1
- export default function checkParameter(value: any, name: string, type: string): void;
@@ -1 +0,0 @@
1
- export {};
@@ -1,12 +0,0 @@
1
- /**
2
- * Generates a random big number.
3
- * @param numberOfBytes The number of bytes of the number.
4
- * @returns The generated random number.
5
- */
6
- export declare function genRandomNumber(numberOfBytes?: number): bigint;
7
- /**
8
- * Checks if a string is a JSON.
9
- * @param jsonString The JSON string.
10
- * @returns True or false.
11
- */
12
- export declare function isJsonArray(jsonString: string): boolean;
@@ -1,5 +0,0 @@
1
- export default function checkParameter(value: any, name: string, type: string) {
2
- if (typeof value !== type) {
3
- throw new TypeError(`Parameter '${name}' is not a ${type}`)
4
- }
5
- }
@@ -1,124 +0,0 @@
1
- import { BigNumber } from "@ethersproject/bignumber"
2
- import Identity from "./identity"
3
-
4
- describe("Identity", () => {
5
- describe("# Identity", () => {
6
- it("Should not create a identity if the parameter is not valid", () => {
7
- const fun1 = () => new Identity(13 as any)
8
- const fun2 = () => new Identity(true as any)
9
- const fun3 = () => new Identity((() => true) as any)
10
-
11
- expect(fun1).toThrow("Parameter 'identityOrMessage' is not a string")
12
- expect(fun2).toThrow("Parameter 'identityOrMessage' is not a string")
13
- expect(fun3).toThrow("Parameter 'identityOrMessage' is not a string")
14
- })
15
-
16
- it("Should create random identities", () => {
17
- const identity1 = new Identity()
18
- const identity2 = new Identity()
19
-
20
- expect(identity1.trapdoor).not.toBe(identity2.getTrapdoor())
21
- expect(identity1.nullifier).not.toBe(identity2.getNullifier())
22
- expect(identity1.secret).not.toBe(identity2.getSecret())
23
- expect(identity1.commitment).not.toBe(identity2.getCommitment())
24
- })
25
-
26
- it("Should create deterministic identities from a message", () => {
27
- const identity1 = new Identity("message")
28
- const identity2 = new Identity("message")
29
-
30
- expect(identity1.trapdoor).toBe(identity2.getTrapdoor())
31
- expect(identity1.nullifier).toBe(identity2.getNullifier())
32
- })
33
-
34
- it("Should create deterministic identities from number/boolean messages", () => {
35
- const identity1 = new Identity("true")
36
- const identity2 = new Identity("true")
37
- const identity3 = new Identity("7")
38
- const identity4 = new Identity("7")
39
-
40
- expect(identity1.trapdoor).toBe(identity2.getTrapdoor())
41
- expect(identity1.nullifier).toBe(identity2.getNullifier())
42
- expect(identity3.trapdoor).toBe(identity4.getTrapdoor())
43
- expect(identity3.nullifier).toBe(identity4.getNullifier())
44
- })
45
-
46
- it("Should not recreate an existing invalid identity", () => {
47
- const fun = () => new Identity('[true, "01323"]')
48
-
49
- expect(fun).toThrow("invalid BigNumber value")
50
- })
51
-
52
- it("Should recreate an existing identity", () => {
53
- const identity1 = new Identity("message")
54
-
55
- const identity2 = new Identity(identity1.toString())
56
-
57
- expect(identity1.trapdoor).toBe(identity2.getTrapdoor())
58
- expect(identity1.nullifier).toBe(identity2.getNullifier())
59
- })
60
- })
61
-
62
- describe("# getTrapdoor", () => {
63
- it("Should return the identity trapdoor", () => {
64
- const identity = new Identity("message")
65
-
66
- const trapdoor = identity.getTrapdoor()
67
-
68
- expect(trapdoor.toString()).toBe(
69
- "11566083507498623434013707198824105161167204201250008419741119866456392774309"
70
- )
71
- })
72
- })
73
-
74
- describe("# getNullifier", () => {
75
- it("Should return the identity nullifier", () => {
76
- const identity = new Identity("message")
77
-
78
- const nullifier = identity.getNullifier()
79
-
80
- expect(nullifier.toString()).toBe(
81
- "14070056666392584007908120012103355272369511035580155843212703537125048345255"
82
- )
83
- })
84
- })
85
-
86
- describe("# getSecret", () => {
87
- it("Should return an identity secret", () => {
88
- const { secret } = new Identity("message")
89
-
90
- expect(secret.toString()).toBe(
91
- "17452394798940441025978193762953691632066258438336130543532009665042636950194"
92
- )
93
- })
94
- })
95
-
96
- describe("# getCommitment", () => {
97
- it("Should return an identity commitment", () => {
98
- const { commitment } = new Identity("message")
99
-
100
- expect(commitment.toString()).toBe(
101
- "19361462367798001240039467285882167157718016385695743307694056771074972404368"
102
- )
103
- })
104
- })
105
-
106
- describe("# toString", () => {
107
- it("Should return a string", () => {
108
- const identity = new Identity("message")
109
-
110
- const identityString = identity.toString()
111
-
112
- expect(typeof identityString).toBe("string")
113
- })
114
-
115
- it("Should return a valid identity string", () => {
116
- const identity = new Identity("message")
117
-
118
- const [trapdoor, nullifier] = JSON.parse(identity.toString())
119
-
120
- expect(BigNumber.from(trapdoor).toBigInt()).toBe(identity.trapdoor)
121
- expect(BigNumber.from(nullifier).toBigInt()).toBe(identity.nullifier)
122
- })
123
- })
124
- })
package/src/utils.ts DELETED
@@ -1,24 +0,0 @@
1
- import { BigNumber } from "@ethersproject/bignumber"
2
- import { randomBytes } from "@ethersproject/random"
3
-
4
- /**
5
- * Generates a random big number.
6
- * @param numberOfBytes The number of bytes of the number.
7
- * @returns The generated random number.
8
- */
9
- export function genRandomNumber(numberOfBytes = 31): bigint {
10
- return BigNumber.from(randomBytes(numberOfBytes)).toBigInt()
11
- }
12
-
13
- /**
14
- * Checks if a string is a JSON.
15
- * @param jsonString The JSON string.
16
- * @returns True or false.
17
- */
18
- export function isJsonArray(jsonString: string) {
19
- try {
20
- return Array.isArray(JSON.parse(jsonString))
21
- } catch (error) {
22
- return false
23
- }
24
- }