@semaphore-protocol/proof 4.0.0-alpha.5 → 4.0.0-alpha.7

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/README.md CHANGED
@@ -73,8 +73,8 @@ yarn add @semaphore-protocol/identity @semaphore-protocol/group @semaphore-proto
73
73
  \# **generateProof**(
74
74
  identity: _Identity_,
75
75
  group: _Group_,
76
- message: _BytesLike | Hexable | number | bigint_,
77
- scope: _BytesLike | Hexable | number | bigint_,
76
+ message: _BigNumberish_ | _Uint8Array_ | string,
77
+ scope: _BigNumberish_ | _Uint8Array_ | string,
78
78
  merkleTreeDepth: _number_,
79
79
  snarkArtifacts?: _SnarkArtifacts_
80
80
  ): Promise\<_SemaphoreProof_>
@@ -83,23 +83,23 @@ snarkArtifacts?: _SnarkArtifacts_
83
83
  import { Identity } from "@semaphore-protocol/identity"
84
84
  import { Group } from "@semaphore-protocol/group"
85
85
  import { generateProof } from "@semaphore-protocol/proof"
86
- import { utils } from "ethers"
87
86
 
88
- const identity = new Identity()
89
- const group = new Group()
87
+ const identity1 = new Identity()
88
+ const identity2 = new Identity()
89
+ const identity3 = new Identity()
90
90
 
91
- const scope = utils.formatBytes32String("Topic")
92
- const message = utils.formatBytes32String("Hello world")
91
+ const group = new Group([identity1.commitment, identity2.commitment, identity3.commitment])
93
92
 
94
- group.addMembers([...identityCommitments, identity.generateCommitment()])
93
+ const message = "Hello world"
94
+ const scope = "Semaphore"
95
95
 
96
- const proof1 = await generateProof(identity, group, message, scope)
96
+ const proof1 = await generateProof(identity1, group, message, scope)
97
97
 
98
98
  // You can also specify the maximum tree depth supported by the proof.
99
- const proof2 = await generateProof(identity, group, message, scope, 20)
99
+ const proof2 = await generateProof(identity2, group, message, scope, 20)
100
100
 
101
101
  // You can also specify the default zkey/wasm files.
102
- const proof3 = await generateProof(identity, group, message, scope, 20, {
102
+ const proof3 = await generateProof(identity3, group, message, scope, 20, {
103
103
  wasmFilePath: "./semaphore.wasm",
104
104
  zkeyFilePath: "./semaphore.zkey"
105
105
  })
@@ -1,15 +1,13 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 4.0.0-alpha.5
3
+ * @version 4.0.0-alpha.7
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/proof}
8
8
  */
9
- import { BigNumber } from '@ethersproject/bignumber';
10
9
  import { groth16 } from 'snarkjs';
11
- import { zeroPad } from '@ethersproject/bytes';
12
- import { keccak256 } from '@ethersproject/keccak256';
10
+ import { keccak256, toBeHex, toBigInt as toBigInt$1, encodeBytes32String } from 'ethers';
13
11
 
14
12
  /******************************************************************************
15
13
  Copyright (c) Microsoft Corporation.
@@ -99,9 +97,7 @@ function getSnarkArtifacts(treeDepth) {
99
97
  * @returns The message digest.
100
98
  */
101
99
  function hash(message) {
102
- message = BigNumber.from(message).toTwos(256).toHexString();
103
- message = zeroPad(message, 32);
104
- return (BigInt(keccak256(message)) >> BigInt(8)).toString();
100
+ return (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString();
105
101
  }
106
102
 
107
103
  /**
@@ -122,12 +118,24 @@ function packPoints(proof) {
122
118
  ];
123
119
  }
124
120
 
121
+ function toBigInt(value) {
122
+ try {
123
+ return toBigInt$1(value);
124
+ }
125
+ catch (error) {
126
+ if (typeof value === "string") {
127
+ return toBigInt$1(encodeBytes32String(value));
128
+ }
129
+ throw TypeError(error.message);
130
+ }
131
+ }
132
+
125
133
  /**
126
134
  * Generates a Semaphore proof.
127
135
  * @param identity The Semaphore identity.
128
136
  * @param groupOrMerkleProof The Semaphore group or its Merkle proof.
129
- * @param scope The external nullifier.
130
- * @param message The Semaphore signal.
137
+ * @param scope The Semaphore scope.
138
+ * @param message The Semaphore message.
131
139
  * @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
132
140
  * @param snarkArtifacts The SNARK artifacts.
133
141
  * @returns The Semaphore proof ready to be verified.
@@ -138,6 +146,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
138
146
  return __generator(this, function (_b) {
139
147
  switch (_b.label) {
140
148
  case 0:
149
+ message = toBigInt(message);
150
+ scope = toBigInt(scope);
141
151
  if ("siblings" in groupOrMerkleProof) {
142
152
  merkleProof = groupOrMerkleProof;
143
153
  }
@@ -182,8 +192,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
182
192
  merkleTreeDepth: merkleTreeDepth,
183
193
  merkleTreeRoot: publicSignals[0],
184
194
  nullifier: publicSignals[1],
185
- message: BigNumber.from(message).toString(),
186
- scope: BigNumber.from(scope).toString(),
195
+ message: message.toString(),
196
+ scope: scope.toString(),
187
197
  points: packPoints(proof)
188
198
  }];
189
199
  }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 4.0.0-alpha.5
3
+ * @version 4.0.0-alpha.7
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -8,13 +8,11 @@
8
8
  */
9
9
  'use strict';
10
10
 
11
- var bignumber = require('@ethersproject/bignumber');
12
11
  var snarkjs = require('snarkjs');
13
12
  var download = require('download');
14
13
  var fs = require('fs');
15
14
  var tmp = require('tmp');
16
- var bytes = require('@ethersproject/bytes');
17
- var keccak256 = require('@ethersproject/keccak256');
15
+ var ethers = require('ethers');
18
16
 
19
17
  /******************************************************************************
20
18
  Copyright (c) Microsoft Corporation.
@@ -118,9 +116,7 @@ function getSnarkArtifacts(treeDepth) {
118
116
  * @returns The message digest.
119
117
  */
120
118
  function hash(message) {
121
- message = bignumber.BigNumber.from(message).toTwos(256).toHexString();
122
- message = bytes.zeroPad(message, 32);
123
- return (BigInt(keccak256.keccak256(message)) >> BigInt(8)).toString();
119
+ return (BigInt(ethers.keccak256(ethers.toBeHex(message, 32))) >> BigInt(8)).toString();
124
120
  }
125
121
 
126
122
  /**
@@ -141,12 +137,24 @@ function packPoints(proof) {
141
137
  ];
142
138
  }
143
139
 
140
+ function toBigInt(value) {
141
+ try {
142
+ return ethers.toBigInt(value);
143
+ }
144
+ catch (error) {
145
+ if (typeof value === "string") {
146
+ return ethers.toBigInt(ethers.encodeBytes32String(value));
147
+ }
148
+ throw TypeError(error.message);
149
+ }
150
+ }
151
+
144
152
  /**
145
153
  * Generates a Semaphore proof.
146
154
  * @param identity The Semaphore identity.
147
155
  * @param groupOrMerkleProof The Semaphore group or its Merkle proof.
148
- * @param scope The external nullifier.
149
- * @param message The Semaphore signal.
156
+ * @param scope The Semaphore scope.
157
+ * @param message The Semaphore message.
150
158
  * @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
151
159
  * @param snarkArtifacts The SNARK artifacts.
152
160
  * @returns The Semaphore proof ready to be verified.
@@ -157,6 +165,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
157
165
  return __generator(this, function (_b) {
158
166
  switch (_b.label) {
159
167
  case 0:
168
+ message = toBigInt(message);
169
+ scope = toBigInt(scope);
160
170
  if ("siblings" in groupOrMerkleProof) {
161
171
  merkleProof = groupOrMerkleProof;
162
172
  }
@@ -201,8 +211,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
201
211
  merkleTreeDepth: merkleTreeDepth,
202
212
  merkleTreeRoot: publicSignals[0],
203
213
  nullifier: publicSignals[1],
204
- message: bignumber.BigNumber.from(message).toString(),
205
- scope: bignumber.BigNumber.from(scope).toString(),
214
+ message: message.toString(),
215
+ scope: scope.toString(),
206
216
  points: packPoints(proof)
207
217
  }];
208
218
  }
@@ -1,18 +1,16 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 4.0.0-alpha.5
3
+ * @version 4.0.0-alpha.7
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/proof}
8
8
  */
9
- import { BigNumber } from '@ethersproject/bignumber';
10
9
  import { groth16 } from 'snarkjs';
11
10
  import download from 'download';
12
11
  import fs from 'fs';
13
12
  import tmp from 'tmp';
14
- import { zeroPad } from '@ethersproject/bytes';
15
- import { keccak256 } from '@ethersproject/keccak256';
13
+ import { keccak256, toBeHex, toBigInt as toBigInt$1, encodeBytes32String } from 'ethers';
16
14
 
17
15
  /******************************************************************************
18
16
  Copyright (c) Microsoft Corporation.
@@ -116,9 +114,7 @@ function getSnarkArtifacts(treeDepth) {
116
114
  * @returns The message digest.
117
115
  */
118
116
  function hash(message) {
119
- message = BigNumber.from(message).toTwos(256).toHexString();
120
- message = zeroPad(message, 32);
121
- return (BigInt(keccak256(message)) >> BigInt(8)).toString();
117
+ return (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString();
122
118
  }
123
119
 
124
120
  /**
@@ -139,12 +135,24 @@ function packPoints(proof) {
139
135
  ];
140
136
  }
141
137
 
138
+ function toBigInt(value) {
139
+ try {
140
+ return toBigInt$1(value);
141
+ }
142
+ catch (error) {
143
+ if (typeof value === "string") {
144
+ return toBigInt$1(encodeBytes32String(value));
145
+ }
146
+ throw TypeError(error.message);
147
+ }
148
+ }
149
+
142
150
  /**
143
151
  * Generates a Semaphore proof.
144
152
  * @param identity The Semaphore identity.
145
153
  * @param groupOrMerkleProof The Semaphore group or its Merkle proof.
146
- * @param scope The external nullifier.
147
- * @param message The Semaphore signal.
154
+ * @param scope The Semaphore scope.
155
+ * @param message The Semaphore message.
148
156
  * @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
149
157
  * @param snarkArtifacts The SNARK artifacts.
150
158
  * @returns The Semaphore proof ready to be verified.
@@ -155,6 +163,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
155
163
  return __generator(this, function (_b) {
156
164
  switch (_b.label) {
157
165
  case 0:
166
+ message = toBigInt(message);
167
+ scope = toBigInt(scope);
158
168
  if ("siblings" in groupOrMerkleProof) {
159
169
  merkleProof = groupOrMerkleProof;
160
170
  }
@@ -199,8 +209,8 @@ function generateProof(identity, groupOrMerkleProof, message, scope, merkleTreeD
199
209
  merkleTreeDepth: merkleTreeDepth,
200
210
  merkleTreeRoot: publicSignals[0],
201
211
  nullifier: publicSignals[1],
202
- message: BigNumber.from(message).toString(),
203
- scope: BigNumber.from(scope).toString(),
212
+ message: message.toString(),
213
+ scope: scope.toString(),
204
214
  points: packPoints(proof)
205
215
  }];
206
216
  }
@@ -1,15 +1,14 @@
1
- import { BytesLike, Hexable } from "@ethersproject/bytes";
2
1
  import type { Group, MerkleProof } from "@semaphore-protocol/group";
3
2
  import type { Identity } from "@semaphore-protocol/identity";
4
- import { SemaphoreProof, SnarkArtifacts } from "./types";
3
+ import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types";
5
4
  /**
6
5
  * Generates a Semaphore proof.
7
6
  * @param identity The Semaphore identity.
8
7
  * @param groupOrMerkleProof The Semaphore group or its Merkle proof.
9
- * @param scope The external nullifier.
10
- * @param message The Semaphore signal.
8
+ * @param scope The Semaphore scope.
9
+ * @param message The Semaphore message.
11
10
  * @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
12
11
  * @param snarkArtifacts The SNARK artifacts.
13
12
  * @returns The Semaphore proof ready to be verified.
14
13
  */
15
- export default function generateProof(identity: Identity, groupOrMerkleProof: Group | MerkleProof, message: BytesLike | Hexable | number | bigint, scope: BytesLike | Hexable | number | bigint, merkleTreeDepth?: number, snarkArtifacts?: SnarkArtifacts): Promise<SemaphoreProof>;
14
+ export default function generateProof(identity: Identity, groupOrMerkleProof: Group | MerkleProof, message: BigNumberish | Uint8Array | string, scope: BigNumberish | Uint8Array | string, merkleTreeDepth?: number, snarkArtifacts?: SnarkArtifacts): Promise<SemaphoreProof>;
@@ -1,8 +1,8 @@
1
- import { BytesLike, Hexable } from "@ethersproject/bytes";
2
1
  import { NumericString } from "snarkjs";
2
+ import { BigNumberish } from "./types";
3
3
  /**
4
4
  * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
5
5
  * @param message The message to be hashed.
6
6
  * @returns The message digest.
7
7
  */
8
- export default function hash(message: BytesLike | Hexable | number | bigint): NumericString;
8
+ export default function hash(message: BigNumberish): NumericString;
@@ -0,0 +1,2 @@
1
+ import { BigNumberish } from "./types";
2
+ export default function toBigInt(value: BigNumberish | Uint8Array | string): bigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/proof",
3
- "version": "4.0.0-alpha.5",
3
+ "version": "4.0.0-alpha.7",
4
4
  "description": "A library to generate and verify Semaphore proofs.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -50,15 +50,13 @@
50
50
  "rollup-plugin-typescript2": "^0.36.0"
51
51
  },
52
52
  "peerDependencies": {
53
- "@semaphore-protocol/group": "4.0.0-alpha.5",
54
- "@semaphore-protocol/identity": "4.0.0-alpha.5"
53
+ "@semaphore-protocol/group": "4.0.0-alpha.7",
54
+ "@semaphore-protocol/identity": "4.0.0-alpha.7"
55
55
  },
56
56
  "dependencies": {
57
- "@ethersproject/bignumber": "5.7.0",
58
- "@ethersproject/bytes": "5.7.0",
59
- "@ethersproject/keccak256": "5.7.0",
60
57
  "@types/snarkjs": "0.7.8",
61
58
  "download": "8.0.0",
59
+ "ethers": "6.10.0",
62
60
  "snarkjs": "0.7.3",
63
61
  "tmp": "0.2.1"
64
62
  }
@@ -1,19 +1,18 @@
1
- import { BigNumber } from "@ethersproject/bignumber"
2
- import { BytesLike, Hexable } from "@ethersproject/bytes"
3
1
  import type { Group, MerkleProof } from "@semaphore-protocol/group"
4
2
  import type { Identity } from "@semaphore-protocol/identity"
5
3
  import { NumericString, groth16 } from "snarkjs"
6
4
  import getSnarkArtifacts from "./get-snark-artifacts.node"
7
5
  import hash from "./hash"
8
6
  import packPoints from "./pack-points"
9
- import { SemaphoreProof, SnarkArtifacts } from "./types"
7
+ import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types"
8
+ import toBigInt from "./to-bigint"
10
9
 
11
10
  /**
12
11
  * Generates a Semaphore proof.
13
12
  * @param identity The Semaphore identity.
14
13
  * @param groupOrMerkleProof The Semaphore group or its Merkle proof.
15
- * @param scope The external nullifier.
16
- * @param message The Semaphore signal.
14
+ * @param scope The Semaphore scope.
15
+ * @param message The Semaphore message.
17
16
  * @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
18
17
  * @param snarkArtifacts The SNARK artifacts.
19
18
  * @returns The Semaphore proof ready to be verified.
@@ -21,11 +20,14 @@ import { SemaphoreProof, SnarkArtifacts } from "./types"
21
20
  export default async function generateProof(
22
21
  identity: Identity,
23
22
  groupOrMerkleProof: Group | MerkleProof,
24
- message: BytesLike | Hexable | number | bigint,
25
- scope: BytesLike | Hexable | number | bigint,
23
+ message: BigNumberish | Uint8Array | string,
24
+ scope: BigNumberish | Uint8Array | string,
26
25
  merkleTreeDepth?: number,
27
26
  snarkArtifacts?: SnarkArtifacts
28
27
  ): Promise<SemaphoreProof> {
28
+ message = toBigInt(message)
29
+ scope = toBigInt(scope)
30
+
29
31
  let merkleProof
30
32
 
31
33
  if ("siblings" in groupOrMerkleProof) {
@@ -81,8 +83,8 @@ export default async function generateProof(
81
83
  merkleTreeDepth,
82
84
  merkleTreeRoot: publicSignals[0],
83
85
  nullifier: publicSignals[1],
84
- message: BigNumber.from(message).toString() as NumericString,
85
- scope: BigNumber.from(scope).toString() as NumericString,
86
+ message: message.toString() as NumericString,
87
+ scope: scope.toString() as NumericString,
86
88
  points: packPoints(proof)
87
89
  }
88
90
  }
package/src/hash.ts CHANGED
@@ -1,16 +1,12 @@
1
- import { BigNumber } from "@ethersproject/bignumber"
2
- import { BytesLike, Hexable, zeroPad } from "@ethersproject/bytes"
3
- import { keccak256 } from "@ethersproject/keccak256"
1
+ import { keccak256, toBeHex } from "ethers"
4
2
  import { NumericString } from "snarkjs"
3
+ import { BigNumberish } from "./types"
5
4
 
6
5
  /**
7
6
  * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
8
7
  * @param message The message to be hashed.
9
8
  * @returns The message digest.
10
9
  */
11
- export default function hash(message: BytesLike | Hexable | number | bigint): NumericString {
12
- message = BigNumber.from(message).toTwos(256).toHexString()
13
- message = zeroPad(message, 32)
14
-
15
- return (BigInt(keccak256(message)) >> BigInt(8)).toString() as NumericString
10
+ export default function hash(message: BigNumberish): NumericString {
11
+ return (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString()
16
12
  }
@@ -0,0 +1,14 @@
1
+ import { encodeBytes32String, toBigInt as _toBigInt } from "ethers"
2
+ import { BigNumberish } from "./types"
3
+
4
+ export default function toBigInt(value: BigNumberish | Uint8Array | string): bigint {
5
+ try {
6
+ return _toBigInt(value)
7
+ } catch (error: any) {
8
+ if (typeof value === "string") {
9
+ return _toBigInt(encodeBytes32String(value))
10
+ }
11
+
12
+ throw TypeError(error.message)
13
+ }
14
+ }