@semaphore-protocol/identity 3.4.0 → 3.6.0-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/dist/index.mjs +33 -21
- package/dist/index.node.js +33 -21
- package/dist/types/identity.d.ts +11 -0
- package/dist/types/utils.d.ts +0 -7
- package/package.json +3 -2
- package/src/identity.test.ts +13 -2
- package/src/identity.ts +27 -5
- package/src/utils.ts +0 -12
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version 3.
|
|
3
|
+
* @version 3.6.0-1
|
|
4
4
|
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
@@ -8,14 +8,8 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { BigNumber } from '@ethersproject/bignumber';
|
|
10
10
|
import hash from 'js-sha512';
|
|
11
|
-
import { randomBytes } from '@ethersproject/random';
|
|
12
11
|
import require$$1 from 'crypto';
|
|
13
|
-
|
|
14
|
-
function checkParameter(value, name, type) {
|
|
15
|
-
if (typeof value !== type) {
|
|
16
|
-
throw new TypeError("Parameter '".concat(name, "' is not a ").concat(type));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
12
|
+
import { randomBytes } from '@ethersproject/random';
|
|
19
13
|
|
|
20
14
|
var poseidon1$1 = {};
|
|
21
15
|
|
|
@@ -1079,6 +1073,12 @@ function poseidon2(inputs) {
|
|
|
1079
1073
|
return (0, _slow.default)(inputs, c);
|
|
1080
1074
|
}
|
|
1081
1075
|
|
|
1076
|
+
function checkParameter(value, name, type) {
|
|
1077
|
+
if (typeof value !== type) {
|
|
1078
|
+
throw new TypeError("Parameter '".concat(name, "' is not a ").concat(type));
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
1082
|
/**
|
|
1083
1083
|
* Generates a random big number.
|
|
1084
1084
|
* @param numberOfBytes The number of bytes of the number.
|
|
@@ -1088,15 +1088,6 @@ function genRandomNumber(numberOfBytes) {
|
|
|
1088
1088
|
if (numberOfBytes === void 0) { numberOfBytes = 31; }
|
|
1089
1089
|
return BigNumber.from(randomBytes(numberOfBytes)).toBigInt();
|
|
1090
1090
|
}
|
|
1091
|
-
/**
|
|
1092
|
-
* Generates the identity commitment from trapdoor and nullifier.
|
|
1093
|
-
* @param nullifier The identity nullifier.
|
|
1094
|
-
* @param trapdoor The identity trapdoor.
|
|
1095
|
-
* @returns identity commitment
|
|
1096
|
-
*/
|
|
1097
|
-
function generateCommitment(nullifier, trapdoor) {
|
|
1098
|
-
return poseidon1_2([poseidon2_2([nullifier, trapdoor])]);
|
|
1099
|
-
}
|
|
1100
1091
|
/**
|
|
1101
1092
|
* Checks if a string is a JSON.
|
|
1102
1093
|
* @param jsonString The JSON string.
|
|
@@ -1120,22 +1111,25 @@ var Identity = /** @class */ (function () {
|
|
|
1120
1111
|
if (identityOrMessage === undefined) {
|
|
1121
1112
|
this._trapdoor = genRandomNumber();
|
|
1122
1113
|
this._nullifier = genRandomNumber();
|
|
1123
|
-
this.
|
|
1114
|
+
this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
|
|
1115
|
+
this._commitment = poseidon1_2([this._secret]);
|
|
1124
1116
|
return;
|
|
1125
1117
|
}
|
|
1126
1118
|
checkParameter(identityOrMessage, "identityOrMessage", "string");
|
|
1127
1119
|
if (!isJsonArray(identityOrMessage)) {
|
|
1128
1120
|
var h = hash.sha512(identityOrMessage).padStart(128, "0");
|
|
1129
|
-
// alt_bn128 is 253.6 bits, so we can safely use 253 bits
|
|
1121
|
+
// alt_bn128 is 253.6 bits, so we can safely use 253 bits.
|
|
1130
1122
|
this._trapdoor = BigInt("0x".concat(h.slice(64))) >> BigInt(3);
|
|
1131
1123
|
this._nullifier = BigInt("0x".concat(h.slice(0, 64))) >> BigInt(3);
|
|
1132
|
-
this.
|
|
1124
|
+
this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
|
|
1125
|
+
this._commitment = poseidon1_2([this._secret]);
|
|
1133
1126
|
return;
|
|
1134
1127
|
}
|
|
1135
1128
|
var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
|
|
1136
1129
|
this._trapdoor = BigNumber.from(trapdoor).toBigInt();
|
|
1137
1130
|
this._nullifier = BigNumber.from(nullifier).toBigInt();
|
|
1138
|
-
this.
|
|
1131
|
+
this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
|
|
1132
|
+
this._commitment = poseidon1_2([this._secret]);
|
|
1139
1133
|
}
|
|
1140
1134
|
Object.defineProperty(Identity.prototype, "trapdoor", {
|
|
1141
1135
|
/**
|
|
@@ -1173,6 +1167,24 @@ var Identity = /** @class */ (function () {
|
|
|
1173
1167
|
Identity.prototype.getNullifier = function () {
|
|
1174
1168
|
return this._nullifier;
|
|
1175
1169
|
};
|
|
1170
|
+
Object.defineProperty(Identity.prototype, "secret", {
|
|
1171
|
+
/**
|
|
1172
|
+
* Returns the identity secret.
|
|
1173
|
+
* @returns The identity secret.
|
|
1174
|
+
*/
|
|
1175
|
+
get: function () {
|
|
1176
|
+
return this._secret;
|
|
1177
|
+
},
|
|
1178
|
+
enumerable: false,
|
|
1179
|
+
configurable: true
|
|
1180
|
+
});
|
|
1181
|
+
/**
|
|
1182
|
+
* Returns the identity secret.
|
|
1183
|
+
* @returns The identity secret.
|
|
1184
|
+
*/
|
|
1185
|
+
Identity.prototype.getSecret = function () {
|
|
1186
|
+
return this._secret;
|
|
1187
|
+
};
|
|
1176
1188
|
Object.defineProperty(Identity.prototype, "commitment", {
|
|
1177
1189
|
/**
|
|
1178
1190
|
* Returns the identity commitment.
|
package/dist/index.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version 3.
|
|
3
|
+
* @version 3.6.0-1
|
|
4
4
|
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
@@ -12,20 +12,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
12
12
|
|
|
13
13
|
var bignumber = require('@ethersproject/bignumber');
|
|
14
14
|
var hash = require('js-sha512');
|
|
15
|
-
var random$1 = require('@ethersproject/random');
|
|
16
15
|
var require$$1 = require('crypto');
|
|
16
|
+
var random$1 = require('@ethersproject/random');
|
|
17
17
|
|
|
18
18
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
19
19
|
|
|
20
20
|
var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash);
|
|
21
21
|
var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
|
|
22
22
|
|
|
23
|
-
function checkParameter(value, name, type) {
|
|
24
|
-
if (typeof value !== type) {
|
|
25
|
-
throw new TypeError("Parameter '".concat(name, "' is not a ").concat(type));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
23
|
var poseidon1$1 = {};
|
|
30
24
|
|
|
31
25
|
var scalar = {};
|
|
@@ -1088,6 +1082,12 @@ function poseidon2(inputs) {
|
|
|
1088
1082
|
return (0, _slow.default)(inputs, c);
|
|
1089
1083
|
}
|
|
1090
1084
|
|
|
1085
|
+
function checkParameter(value, name, type) {
|
|
1086
|
+
if (typeof value !== type) {
|
|
1087
|
+
throw new TypeError("Parameter '".concat(name, "' is not a ").concat(type));
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
1091
|
/**
|
|
1092
1092
|
* Generates a random big number.
|
|
1093
1093
|
* @param numberOfBytes The number of bytes of the number.
|
|
@@ -1097,15 +1097,6 @@ function genRandomNumber(numberOfBytes) {
|
|
|
1097
1097
|
if (numberOfBytes === void 0) { numberOfBytes = 31; }
|
|
1098
1098
|
return bignumber.BigNumber.from(random$1.randomBytes(numberOfBytes)).toBigInt();
|
|
1099
1099
|
}
|
|
1100
|
-
/**
|
|
1101
|
-
* Generates the identity commitment from trapdoor and nullifier.
|
|
1102
|
-
* @param nullifier The identity nullifier.
|
|
1103
|
-
* @param trapdoor The identity trapdoor.
|
|
1104
|
-
* @returns identity commitment
|
|
1105
|
-
*/
|
|
1106
|
-
function generateCommitment(nullifier, trapdoor) {
|
|
1107
|
-
return poseidon1_2([poseidon2_2([nullifier, trapdoor])]);
|
|
1108
|
-
}
|
|
1109
1100
|
/**
|
|
1110
1101
|
* Checks if a string is a JSON.
|
|
1111
1102
|
* @param jsonString The JSON string.
|
|
@@ -1129,22 +1120,25 @@ var Identity = /** @class */ (function () {
|
|
|
1129
1120
|
if (identityOrMessage === undefined) {
|
|
1130
1121
|
this._trapdoor = genRandomNumber();
|
|
1131
1122
|
this._nullifier = genRandomNumber();
|
|
1132
|
-
this.
|
|
1123
|
+
this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
|
|
1124
|
+
this._commitment = poseidon1_2([this._secret]);
|
|
1133
1125
|
return;
|
|
1134
1126
|
}
|
|
1135
1127
|
checkParameter(identityOrMessage, "identityOrMessage", "string");
|
|
1136
1128
|
if (!isJsonArray(identityOrMessage)) {
|
|
1137
1129
|
var h = hash__default["default"].sha512(identityOrMessage).padStart(128, "0");
|
|
1138
|
-
// alt_bn128 is 253.6 bits, so we can safely use 253 bits
|
|
1130
|
+
// alt_bn128 is 253.6 bits, so we can safely use 253 bits.
|
|
1139
1131
|
this._trapdoor = BigInt("0x".concat(h.slice(64))) >> BigInt(3);
|
|
1140
1132
|
this._nullifier = BigInt("0x".concat(h.slice(0, 64))) >> BigInt(3);
|
|
1141
|
-
this.
|
|
1133
|
+
this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
|
|
1134
|
+
this._commitment = poseidon1_2([this._secret]);
|
|
1142
1135
|
return;
|
|
1143
1136
|
}
|
|
1144
1137
|
var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
|
|
1145
1138
|
this._trapdoor = bignumber.BigNumber.from(trapdoor).toBigInt();
|
|
1146
1139
|
this._nullifier = bignumber.BigNumber.from(nullifier).toBigInt();
|
|
1147
|
-
this.
|
|
1140
|
+
this._secret = poseidon2_2([this._nullifier, this._trapdoor]);
|
|
1141
|
+
this._commitment = poseidon1_2([this._secret]);
|
|
1148
1142
|
}
|
|
1149
1143
|
Object.defineProperty(Identity.prototype, "trapdoor", {
|
|
1150
1144
|
/**
|
|
@@ -1182,6 +1176,24 @@ var Identity = /** @class */ (function () {
|
|
|
1182
1176
|
Identity.prototype.getNullifier = function () {
|
|
1183
1177
|
return this._nullifier;
|
|
1184
1178
|
};
|
|
1179
|
+
Object.defineProperty(Identity.prototype, "secret", {
|
|
1180
|
+
/**
|
|
1181
|
+
* Returns the identity secret.
|
|
1182
|
+
* @returns The identity secret.
|
|
1183
|
+
*/
|
|
1184
|
+
get: function () {
|
|
1185
|
+
return this._secret;
|
|
1186
|
+
},
|
|
1187
|
+
enumerable: false,
|
|
1188
|
+
configurable: true
|
|
1189
|
+
});
|
|
1190
|
+
/**
|
|
1191
|
+
* Returns the identity secret.
|
|
1192
|
+
* @returns The identity secret.
|
|
1193
|
+
*/
|
|
1194
|
+
Identity.prototype.getSecret = function () {
|
|
1195
|
+
return this._secret;
|
|
1196
|
+
};
|
|
1185
1197
|
Object.defineProperty(Identity.prototype, "commitment", {
|
|
1186
1198
|
/**
|
|
1187
1199
|
* Returns the identity commitment.
|
package/dist/types/identity.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export default class Identity {
|
|
2
2
|
private _trapdoor;
|
|
3
3
|
private _nullifier;
|
|
4
|
+
private _secret;
|
|
4
5
|
private _commitment;
|
|
5
6
|
/**
|
|
6
7
|
* Initializes the class attributes based on the strategy passed as parameter.
|
|
@@ -27,6 +28,16 @@ export default class Identity {
|
|
|
27
28
|
* @returns The identity nullifier.
|
|
28
29
|
*/
|
|
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;
|
|
30
41
|
/**
|
|
31
42
|
* Returns the identity commitment.
|
|
32
43
|
* @returns The identity commitment.
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -4,13 +4,6 @@
|
|
|
4
4
|
* @returns The generated random number.
|
|
5
5
|
*/
|
|
6
6
|
export declare function genRandomNumber(numberOfBytes?: number): bigint;
|
|
7
|
-
/**
|
|
8
|
-
* Generates the identity commitment from trapdoor and nullifier.
|
|
9
|
-
* @param nullifier The identity nullifier.
|
|
10
|
-
* @param trapdoor The identity trapdoor.
|
|
11
|
-
* @returns identity commitment
|
|
12
|
-
*/
|
|
13
|
-
export declare function generateCommitment(nullifier: bigint, trapdoor: bigint): bigint;
|
|
14
7
|
/**
|
|
15
8
|
* Checks if a string is a JSON.
|
|
16
9
|
* @param jsonString The JSON string.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semaphore-protocol/identity",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-1",
|
|
4
4
|
"description": "A library to create Semaphore identities.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.node.js",
|
|
@@ -43,5 +43,6 @@
|
|
|
43
43
|
"@ethersproject/random": "^5.5.1",
|
|
44
44
|
"@ethersproject/strings": "^5.6.1",
|
|
45
45
|
"js-sha512": "^0.8.0"
|
|
46
|
-
}
|
|
46
|
+
},
|
|
47
|
+
"stableVersion": "3.5.0"
|
|
47
48
|
}
|
package/src/identity.test.ts
CHANGED
|
@@ -19,6 +19,7 @@ describe("Identity", () => {
|
|
|
19
19
|
|
|
20
20
|
expect(identity1.trapdoor).not.toBe(identity2.getTrapdoor())
|
|
21
21
|
expect(identity1.nullifier).not.toBe(identity2.getNullifier())
|
|
22
|
+
expect(identity1.secret).not.toBe(identity2.getSecret())
|
|
22
23
|
expect(identity1.commitment).not.toBe(identity2.getCommitment())
|
|
23
24
|
})
|
|
24
25
|
|
|
@@ -82,8 +83,18 @@ describe("Identity", () => {
|
|
|
82
83
|
})
|
|
83
84
|
})
|
|
84
85
|
|
|
85
|
-
describe("#
|
|
86
|
-
it("Should
|
|
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", () => {
|
|
87
98
|
const { commitment } = new Identity("message")
|
|
88
99
|
|
|
89
100
|
expect(commitment.toString()).toBe(
|
package/src/identity.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { BigNumber } from "@ethersproject/bignumber"
|
|
2
2
|
import hash from "js-sha512"
|
|
3
|
+
import { poseidon1 } from "poseidon-lite/poseidon1"
|
|
4
|
+
import { poseidon2 } from "poseidon-lite/poseidon2"
|
|
3
5
|
import checkParameter from "./checkParameter"
|
|
4
|
-
import {
|
|
6
|
+
import { genRandomNumber, isJsonArray } from "./utils"
|
|
5
7
|
|
|
6
8
|
export default class Identity {
|
|
7
9
|
private _trapdoor: bigint
|
|
8
10
|
private _nullifier: bigint
|
|
11
|
+
private _secret: bigint
|
|
9
12
|
private _commitment: bigint
|
|
10
13
|
|
|
11
14
|
/**
|
|
@@ -16,7 +19,8 @@ export default class Identity {
|
|
|
16
19
|
if (identityOrMessage === undefined) {
|
|
17
20
|
this._trapdoor = genRandomNumber()
|
|
18
21
|
this._nullifier = genRandomNumber()
|
|
19
|
-
this.
|
|
22
|
+
this._secret = poseidon2([this._nullifier, this._trapdoor])
|
|
23
|
+
this._commitment = poseidon1([this._secret])
|
|
20
24
|
|
|
21
25
|
return
|
|
22
26
|
}
|
|
@@ -25,10 +29,11 @@ export default class Identity {
|
|
|
25
29
|
|
|
26
30
|
if (!isJsonArray(identityOrMessage)) {
|
|
27
31
|
const h = hash.sha512(identityOrMessage).padStart(128, "0")
|
|
28
|
-
// alt_bn128 is 253.6 bits, so we can safely use 253 bits
|
|
32
|
+
// alt_bn128 is 253.6 bits, so we can safely use 253 bits.
|
|
29
33
|
this._trapdoor = BigInt(`0x${h.slice(64)}`) >> BigInt(3)
|
|
30
34
|
this._nullifier = BigInt(`0x${h.slice(0, 64)}`) >> BigInt(3)
|
|
31
|
-
this.
|
|
35
|
+
this._secret = poseidon2([this._nullifier, this._trapdoor])
|
|
36
|
+
this._commitment = poseidon1([this._secret])
|
|
32
37
|
|
|
33
38
|
return
|
|
34
39
|
}
|
|
@@ -37,7 +42,8 @@ export default class Identity {
|
|
|
37
42
|
|
|
38
43
|
this._trapdoor = BigNumber.from(trapdoor).toBigInt()
|
|
39
44
|
this._nullifier = BigNumber.from(nullifier).toBigInt()
|
|
40
|
-
this.
|
|
45
|
+
this._secret = poseidon2([this._nullifier, this._trapdoor])
|
|
46
|
+
this._commitment = poseidon1([this._secret])
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
/**
|
|
@@ -72,6 +78,22 @@ export default class Identity {
|
|
|
72
78
|
return this._nullifier
|
|
73
79
|
}
|
|
74
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
|
|
95
|
+
}
|
|
96
|
+
|
|
75
97
|
/**
|
|
76
98
|
* Returns the identity commitment.
|
|
77
99
|
* @returns The identity commitment.
|
package/src/utils.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { BigNumber } from "@ethersproject/bignumber"
|
|
2
2
|
import { randomBytes } from "@ethersproject/random"
|
|
3
|
-
import { poseidon1 } from "poseidon-lite/poseidon1"
|
|
4
|
-
import { poseidon2 } from "poseidon-lite/poseidon2"
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* Generates a random big number.
|
|
@@ -12,16 +10,6 @@ export function genRandomNumber(numberOfBytes = 31): bigint {
|
|
|
12
10
|
return BigNumber.from(randomBytes(numberOfBytes)).toBigInt()
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
/**
|
|
16
|
-
* Generates the identity commitment from trapdoor and nullifier.
|
|
17
|
-
* @param nullifier The identity nullifier.
|
|
18
|
-
* @param trapdoor The identity trapdoor.
|
|
19
|
-
* @returns identity commitment
|
|
20
|
-
*/
|
|
21
|
-
export function generateCommitment(nullifier: bigint, trapdoor: bigint): bigint {
|
|
22
|
-
return poseidon1([poseidon2([nullifier, trapdoor])])
|
|
23
|
-
}
|
|
24
|
-
|
|
25
13
|
/**
|
|
26
14
|
* Checks if a string is a JSON.
|
|
27
15
|
* @param jsonString The JSON string.
|