@semaphore-protocol/identity 2.6.1 → 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 +14 -22
- package/dist/index.node.js +19 -23
- package/dist/types/hash.d.ts +6 -0
- package/dist/types/identity.d.ts +0 -6
- package/dist/types/utils.d.ts +0 -6
- package/package.json +3 -3
- package/src/hash.ts +11 -0
- package/src/identity.test.ts +6 -12
- package/src/identity.ts +5 -14
- package/src/utils.ts +1 -14
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version
|
|
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 {
|
|
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
|
-
*
|
|
23
|
-
* @param message The
|
|
24
|
-
* @returns The
|
|
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
|
|
27
|
-
|
|
28
|
-
|
|
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 =
|
|
77
|
-
this._trapdoor =
|
|
78
|
-
this._nullifier =
|
|
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.
|
package/dist/index.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version
|
|
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
|
|
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
|
-
*
|
|
27
|
-
* @param message The
|
|
28
|
-
* @returns The
|
|
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
|
|
31
|
-
|
|
32
|
-
|
|
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
|
|
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 =
|
|
81
|
-
this._trapdoor =
|
|
82
|
-
this._nullifier =
|
|
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.
|
package/dist/types/identity.d.ts
CHANGED
|
@@ -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.
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -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": "
|
|
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
|
-
"
|
|
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
|
+
}
|
package/src/identity.test.ts
CHANGED
|
@@ -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
|
|
88
|
-
|
|
89
|
-
const commitment = identity.generateCommitment()
|
|
83
|
+
const { commitment } = new Identity("message")
|
|
90
84
|
|
|
91
85
|
expect(commitment).toBe(
|
|
92
|
-
BigInt("
|
|
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.
|
|
112
|
-
expect(BigNumber.from(`0x${nullifier}`).toBigInt()).toBe(identity.
|
|
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
|
|
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 =
|
|
27
|
+
const messageHash = hash(identityOrMessage)
|
|
28
28
|
|
|
29
|
-
this._trapdoor =
|
|
30
|
-
this._nullifier =
|
|
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
|
|
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.
|