@waku/rln 0.1.2-e1679b6 → 0.1.3-f6d5deb
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/bundle/index.js +40915 -39696
- package/dist/codec.d.ts +2 -1
- package/dist/codec.js +7 -1
- package/dist/codec.js.map +1 -1
- package/dist/{constants.js → contract/constants.js} +3 -3
- package/dist/contract/constants.js.map +1 -0
- package/dist/contract/index.d.ts +2 -0
- package/dist/contract/index.js +3 -0
- package/dist/contract/index.js.map +1 -0
- package/dist/{rln_contract.d.ts → contract/rln_contract.d.ts} +3 -2
- package/dist/{rln_contract.js → contract/rln_contract.js} +24 -18
- package/dist/contract/rln_contract.js.map +1 -0
- package/dist/identity.d.ts +9 -0
- package/dist/identity.js +24 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +7 -5
- package/dist/index.js.map +1 -1
- package/dist/keystore/cipher.js +1 -1
- package/dist/keystore/cipher.js.map +1 -1
- package/dist/keystore/credential_validation_generated.js.map +1 -1
- package/dist/keystore/keystore.js +21 -18
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/keystore_validation_generated.js.map +1 -1
- package/dist/keystore/schema_validator.js.map +1 -1
- package/dist/keystore/types.d.ts +1 -1
- package/dist/message.js +7 -4
- package/dist/message.js.map +1 -1
- package/dist/proof.d.ts +21 -0
- package/dist/proof.js +49 -0
- package/dist/proof.js.map +1 -0
- package/dist/resources/verification_key.d.ts +9 -9
- package/dist/resources/witness_calculator.js.map +1 -0
- package/dist/rln.d.ts +6 -48
- package/dist/rln.js +24 -193
- package/dist/rln.js.map +1 -1
- package/dist/root_tracker.js +5 -2
- package/dist/root_tracker.js.map +1 -1
- package/dist/{byte_utils.d.ts → utils/bytes.d.ts} +7 -1
- package/dist/{byte_utils.js → utils/bytes.js} +21 -3
- package/dist/utils/bytes.js.map +1 -0
- package/dist/utils/epoch.js.map +1 -0
- package/dist/utils/hash.d.ts +2 -0
- package/dist/utils/hash.js +13 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/{metamask.js → utils/metamask.js} +1 -0
- package/dist/utils/metamask.js.map +1 -0
- package/dist/zerokit.d.ts +19 -0
- package/dist/zerokit.js +105 -0
- package/dist/zerokit.js.map +1 -0
- package/package.json +18 -23
- package/src/codec.ts +8 -4
- package/src/identity.ts +27 -0
- package/src/index.ts +8 -12
- package/src/message.ts +4 -4
- package/src/proof.ts +67 -0
- package/src/rln.ts +31 -307
- package/src/root_tracker.ts +4 -1
- package/src/zerokit.ts +181 -0
- package/dist/.tsbuildinfo +0 -1
- package/dist/byte_utils.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/epoch.js.map +0 -1
- package/dist/metamask.js.map +0 -1
- package/dist/rln_contract.js.map +0 -1
- package/dist/witness_calculator.js.map +0 -1
- package/src/byte_utils.ts +0 -63
- package/src/constants.ts +0 -68
- package/src/epoch.ts +0 -30
- package/src/metamask.ts +0 -16
- package/src/rln_contract.ts +0 -350
- package/src/witness_calculator.d.ts +0 -8
- package/src/witness_calculator.js +0 -335
- /package/dist/{constants.d.ts → contract/constants.d.ts} +0 -0
- /package/dist/{witness_calculator.d.ts → resources/witness_calculator.d.ts} +0 -0
- /package/dist/{witness_calculator.js → resources/witness_calculator.js} +0 -0
- /package/dist/{epoch.d.ts → utils/epoch.d.ts} +0 -0
- /package/dist/{epoch.js → utils/epoch.js} +0 -0
- /package/dist/{metamask.d.ts → utils/metamask.d.ts} +0 -0
package/src/rln_contract.ts
DELETED
@@ -1,350 +0,0 @@
|
|
1
|
-
import { hexToBytes } from "@waku/utils/bytes";
|
2
|
-
import { ethers } from "ethers";
|
3
|
-
|
4
|
-
import { zeroPadLE } from "./byte_utils.js";
|
5
|
-
import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
|
6
|
-
import type { DecryptedCredentials } from "./keystore/index.js";
|
7
|
-
import { type IdentityCredential, RLNInstance } from "./rln.js";
|
8
|
-
import { MerkleRootTracker } from "./root_tracker.js";
|
9
|
-
|
10
|
-
type Member = {
|
11
|
-
idCommitment: string;
|
12
|
-
index: ethers.BigNumber;
|
13
|
-
};
|
14
|
-
|
15
|
-
type Signer = ethers.Signer;
|
16
|
-
|
17
|
-
type RLNContractOptions = {
|
18
|
-
signer: Signer;
|
19
|
-
registryAddress: string;
|
20
|
-
};
|
21
|
-
|
22
|
-
type RLNStorageOptions = {
|
23
|
-
storageIndex?: number;
|
24
|
-
};
|
25
|
-
|
26
|
-
type RLNContractInitOptions = RLNContractOptions & RLNStorageOptions;
|
27
|
-
|
28
|
-
type FetchMembersOptions = {
|
29
|
-
fromBlock?: number;
|
30
|
-
fetchRange?: number;
|
31
|
-
fetchChunks?: number;
|
32
|
-
};
|
33
|
-
|
34
|
-
export class RLNContract {
|
35
|
-
private registryContract: ethers.Contract;
|
36
|
-
private merkleRootTracker: MerkleRootTracker;
|
37
|
-
|
38
|
-
private deployBlock: undefined | number;
|
39
|
-
private storageIndex: undefined | number;
|
40
|
-
private storageContract: undefined | ethers.Contract;
|
41
|
-
private _membersFilter: undefined | ethers.EventFilter;
|
42
|
-
|
43
|
-
private _members: Map<number, Member> = new Map();
|
44
|
-
|
45
|
-
public static async init(
|
46
|
-
rlnInstance: RLNInstance,
|
47
|
-
options: RLNContractInitOptions
|
48
|
-
): Promise<RLNContract> {
|
49
|
-
const rlnContract = new RLNContract(rlnInstance, options);
|
50
|
-
|
51
|
-
await rlnContract.initStorageContract(options.signer);
|
52
|
-
await rlnContract.fetchMembers(rlnInstance);
|
53
|
-
rlnContract.subscribeToMembers(rlnInstance);
|
54
|
-
|
55
|
-
return rlnContract;
|
56
|
-
}
|
57
|
-
|
58
|
-
constructor(
|
59
|
-
rlnInstance: RLNInstance,
|
60
|
-
{ registryAddress, signer }: RLNContractOptions
|
61
|
-
) {
|
62
|
-
const initialRoot = rlnInstance.getMerkleRoot();
|
63
|
-
|
64
|
-
this.registryContract = new ethers.Contract(
|
65
|
-
registryAddress,
|
66
|
-
RLN_REGISTRY_ABI,
|
67
|
-
signer
|
68
|
-
);
|
69
|
-
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
70
|
-
}
|
71
|
-
|
72
|
-
private async initStorageContract(
|
73
|
-
signer: Signer,
|
74
|
-
options: RLNStorageOptions = {}
|
75
|
-
): Promise<void> {
|
76
|
-
const storageIndex = options?.storageIndex
|
77
|
-
? options.storageIndex
|
78
|
-
: await this.registryContract.usingStorageIndex();
|
79
|
-
const storageAddress = await this.registryContract.storages(storageIndex);
|
80
|
-
|
81
|
-
if (!storageAddress || storageAddress === ethers.constants.AddressZero) {
|
82
|
-
throw Error("No RLN Storage initialized on registry contract.");
|
83
|
-
}
|
84
|
-
|
85
|
-
this.storageIndex = storageIndex;
|
86
|
-
this.storageContract = new ethers.Contract(
|
87
|
-
storageAddress,
|
88
|
-
RLN_STORAGE_ABI,
|
89
|
-
signer
|
90
|
-
);
|
91
|
-
this._membersFilter = this.storageContract.filters.MemberRegistered();
|
92
|
-
|
93
|
-
this.deployBlock = await this.storageContract.deployedBlockNumber();
|
94
|
-
}
|
95
|
-
|
96
|
-
public get registry(): ethers.Contract {
|
97
|
-
if (!this.registryContract) {
|
98
|
-
throw Error("Registry contract was not initialized");
|
99
|
-
}
|
100
|
-
return this.registryContract as ethers.Contract;
|
101
|
-
}
|
102
|
-
|
103
|
-
public get contract(): ethers.Contract {
|
104
|
-
if (!this.storageContract) {
|
105
|
-
throw Error("Storage contract was not initialized");
|
106
|
-
}
|
107
|
-
return this.storageContract as ethers.Contract;
|
108
|
-
}
|
109
|
-
|
110
|
-
public get members(): Member[] {
|
111
|
-
const sortedMembers = Array.from(this._members.values()).sort(
|
112
|
-
(left, right) => left.index.toNumber() - right.index.toNumber()
|
113
|
-
);
|
114
|
-
return sortedMembers;
|
115
|
-
}
|
116
|
-
|
117
|
-
private get membersFilter(): ethers.EventFilter {
|
118
|
-
if (!this._membersFilter) {
|
119
|
-
throw Error("Members filter was not initialized.");
|
120
|
-
}
|
121
|
-
return this._membersFilter as ethers.EventFilter;
|
122
|
-
}
|
123
|
-
|
124
|
-
public async fetchMembers(
|
125
|
-
rlnInstance: RLNInstance,
|
126
|
-
options: FetchMembersOptions = {}
|
127
|
-
): Promise<void> {
|
128
|
-
const registeredMemberEvents = await queryFilter(this.contract, {
|
129
|
-
fromBlock: this.deployBlock,
|
130
|
-
...options,
|
131
|
-
membersFilter: this.membersFilter,
|
132
|
-
});
|
133
|
-
this.processEvents(rlnInstance, registeredMemberEvents);
|
134
|
-
}
|
135
|
-
|
136
|
-
public processEvents(rlnInstance: RLNInstance, events: ethers.Event[]): void {
|
137
|
-
const toRemoveTable = new Map<number, number[]>();
|
138
|
-
const toInsertTable = new Map<number, ethers.Event[]>();
|
139
|
-
|
140
|
-
events.forEach((evt) => {
|
141
|
-
if (!evt.args) {
|
142
|
-
return;
|
143
|
-
}
|
144
|
-
|
145
|
-
if (evt.removed) {
|
146
|
-
const index: ethers.BigNumber = evt.args.index;
|
147
|
-
const toRemoveVal = toRemoveTable.get(evt.blockNumber);
|
148
|
-
if (toRemoveVal != undefined) {
|
149
|
-
toRemoveVal.push(index.toNumber());
|
150
|
-
toRemoveTable.set(evt.blockNumber, toRemoveVal);
|
151
|
-
} else {
|
152
|
-
toRemoveTable.set(evt.blockNumber, [index.toNumber()]);
|
153
|
-
}
|
154
|
-
} else {
|
155
|
-
let eventsPerBlock = toInsertTable.get(evt.blockNumber);
|
156
|
-
if (eventsPerBlock == undefined) {
|
157
|
-
eventsPerBlock = [];
|
158
|
-
}
|
159
|
-
|
160
|
-
eventsPerBlock.push(evt);
|
161
|
-
toInsertTable.set(evt.blockNumber, eventsPerBlock);
|
162
|
-
}
|
163
|
-
});
|
164
|
-
|
165
|
-
this.removeMembers(rlnInstance, toRemoveTable);
|
166
|
-
this.insertMembers(rlnInstance, toInsertTable);
|
167
|
-
}
|
168
|
-
|
169
|
-
private insertMembers(
|
170
|
-
rlnInstance: RLNInstance,
|
171
|
-
toInsert: Map<number, ethers.Event[]>
|
172
|
-
): void {
|
173
|
-
toInsert.forEach((events: ethers.Event[], blockNumber: number) => {
|
174
|
-
events.forEach((evt) => {
|
175
|
-
const _idCommitment = evt?.args?.idCommitment;
|
176
|
-
const index: ethers.BigNumber = evt?.args?.index;
|
177
|
-
|
178
|
-
if (!_idCommitment || !index) {
|
179
|
-
return;
|
180
|
-
}
|
181
|
-
|
182
|
-
const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32);
|
183
|
-
rlnInstance.insertMember(idCommitment);
|
184
|
-
this._members.set(index.toNumber(), {
|
185
|
-
index,
|
186
|
-
idCommitment: _idCommitment?._hex,
|
187
|
-
});
|
188
|
-
});
|
189
|
-
|
190
|
-
const currentRoot = rlnInstance.getMerkleRoot();
|
191
|
-
this.merkleRootTracker.pushRoot(blockNumber, currentRoot);
|
192
|
-
});
|
193
|
-
}
|
194
|
-
|
195
|
-
private removeMembers(
|
196
|
-
rlnInstance: RLNInstance,
|
197
|
-
toRemove: Map<number, number[]>
|
198
|
-
): void {
|
199
|
-
const removeDescending = new Map([...toRemove].sort().reverse());
|
200
|
-
removeDescending.forEach((indexes: number[], blockNumber: number) => {
|
201
|
-
indexes.forEach((index) => {
|
202
|
-
if (this._members.has(index)) {
|
203
|
-
this._members.delete(index);
|
204
|
-
}
|
205
|
-
rlnInstance.deleteMember(index);
|
206
|
-
});
|
207
|
-
|
208
|
-
this.merkleRootTracker.backFill(blockNumber);
|
209
|
-
});
|
210
|
-
}
|
211
|
-
|
212
|
-
public subscribeToMembers(rlnInstance: RLNInstance): void {
|
213
|
-
this.contract.on(this.membersFilter, (_pubkey, _index, event) =>
|
214
|
-
this.processEvents(rlnInstance, [event])
|
215
|
-
);
|
216
|
-
}
|
217
|
-
|
218
|
-
public async registerWithIdentity(
|
219
|
-
identity: IdentityCredential
|
220
|
-
): Promise<DecryptedCredentials | undefined> {
|
221
|
-
if (this.storageIndex === undefined) {
|
222
|
-
throw Error(
|
223
|
-
"Cannot register credential, no storage contract index found."
|
224
|
-
);
|
225
|
-
}
|
226
|
-
const txRegisterResponse: ethers.ContractTransaction =
|
227
|
-
await this.registryContract["register(uint16,uint256)"](
|
228
|
-
this.storageIndex,
|
229
|
-
identity.IDCommitmentBigInt,
|
230
|
-
{ gasLimit: 100000 }
|
231
|
-
);
|
232
|
-
const txRegisterReceipt = await txRegisterResponse.wait();
|
233
|
-
|
234
|
-
// assumption: register(uint16,uint256) emits one event
|
235
|
-
const memberRegistered = txRegisterReceipt?.events?.[0];
|
236
|
-
|
237
|
-
if (!memberRegistered) {
|
238
|
-
return undefined;
|
239
|
-
}
|
240
|
-
|
241
|
-
const decodedData = this.contract.interface.decodeEventLog(
|
242
|
-
"MemberRegistered",
|
243
|
-
memberRegistered.data
|
244
|
-
);
|
245
|
-
|
246
|
-
const network = await this.registryContract.provider.getNetwork();
|
247
|
-
const address = this.registryContract.address;
|
248
|
-
const membershipId = decodedData.index.toNumber();
|
249
|
-
|
250
|
-
return {
|
251
|
-
identity,
|
252
|
-
membership: {
|
253
|
-
address,
|
254
|
-
treeIndex: membershipId,
|
255
|
-
chainId: network.chainId,
|
256
|
-
},
|
257
|
-
};
|
258
|
-
}
|
259
|
-
|
260
|
-
public roots(): Uint8Array[] {
|
261
|
-
return this.merkleRootTracker.roots();
|
262
|
-
}
|
263
|
-
}
|
264
|
-
|
265
|
-
type CustomQueryOptions = FetchMembersOptions & {
|
266
|
-
membersFilter: ethers.EventFilter;
|
267
|
-
};
|
268
|
-
|
269
|
-
// these value should be tested on other networks
|
270
|
-
const FETCH_CHUNK = 5;
|
271
|
-
const BLOCK_RANGE = 3000;
|
272
|
-
|
273
|
-
async function queryFilter(
|
274
|
-
contract: ethers.Contract,
|
275
|
-
options: CustomQueryOptions
|
276
|
-
): Promise<ethers.Event[]> {
|
277
|
-
const {
|
278
|
-
fromBlock,
|
279
|
-
membersFilter,
|
280
|
-
fetchRange = BLOCK_RANGE,
|
281
|
-
fetchChunks = FETCH_CHUNK,
|
282
|
-
} = options;
|
283
|
-
|
284
|
-
if (!fromBlock) {
|
285
|
-
return contract.queryFilter(membersFilter);
|
286
|
-
}
|
287
|
-
|
288
|
-
if (!contract.signer.provider) {
|
289
|
-
throw Error("No provider found on the contract's signer.");
|
290
|
-
}
|
291
|
-
|
292
|
-
const toBlock = await contract.signer.provider.getBlockNumber();
|
293
|
-
|
294
|
-
if (toBlock - fromBlock < fetchRange) {
|
295
|
-
return contract.queryFilter(membersFilter);
|
296
|
-
}
|
297
|
-
|
298
|
-
const events: ethers.Event[][] = [];
|
299
|
-
const chunks = splitToChunks(fromBlock, toBlock, fetchRange);
|
300
|
-
|
301
|
-
for (const portion of takeN<[number, number]>(chunks, fetchChunks)) {
|
302
|
-
const promises = portion.map(([left, right]) =>
|
303
|
-
ignoreErrors(contract.queryFilter(membersFilter, left, right), [])
|
304
|
-
);
|
305
|
-
const fetchedEvents = await Promise.all(promises);
|
306
|
-
events.push(fetchedEvents.flatMap((v) => v));
|
307
|
-
}
|
308
|
-
|
309
|
-
return events.flatMap((v) => v);
|
310
|
-
}
|
311
|
-
|
312
|
-
function splitToChunks(
|
313
|
-
from: number,
|
314
|
-
to: number,
|
315
|
-
step: number
|
316
|
-
): Array<[number, number]> {
|
317
|
-
const chunks = [];
|
318
|
-
|
319
|
-
let left = from;
|
320
|
-
while (left < to) {
|
321
|
-
const right = left + step < to ? left + step : to;
|
322
|
-
|
323
|
-
chunks.push([left, right] as [number, number]);
|
324
|
-
|
325
|
-
left = right;
|
326
|
-
}
|
327
|
-
|
328
|
-
return chunks;
|
329
|
-
}
|
330
|
-
|
331
|
-
function* takeN<T>(array: T[], size: number): Iterable<T[]> {
|
332
|
-
let start = 0;
|
333
|
-
let skip = size;
|
334
|
-
|
335
|
-
while (skip < array.length) {
|
336
|
-
const portion = array.slice(start, skip);
|
337
|
-
|
338
|
-
yield portion;
|
339
|
-
|
340
|
-
start = skip;
|
341
|
-
skip += size;
|
342
|
-
}
|
343
|
-
}
|
344
|
-
|
345
|
-
function ignoreErrors<T>(promise: Promise<T>, defaultValue: T): Promise<T> {
|
346
|
-
return promise.catch((err) => {
|
347
|
-
console.error(`Ignoring an error during query: ${err?.message}`);
|
348
|
-
return defaultValue;
|
349
|
-
});
|
350
|
-
}
|
@@ -1,335 +0,0 @@
|
|
1
|
-
// File generated with https://github.com/iden3/circom
|
2
|
-
// following the instructions from:
|
3
|
-
// https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
|
4
|
-
|
5
|
-
export async function builder(code, options) {
|
6
|
-
|
7
|
-
options = options || {};
|
8
|
-
|
9
|
-
let wasmModule;
|
10
|
-
try {
|
11
|
-
wasmModule = await WebAssembly.compile(code);
|
12
|
-
} catch (err) {
|
13
|
-
console.log(err);
|
14
|
-
console.log("\nTry to run circom --c in order to generate c++ code instead\n");
|
15
|
-
throw new Error(err);
|
16
|
-
}
|
17
|
-
|
18
|
-
let wc;
|
19
|
-
|
20
|
-
let errStr = "";
|
21
|
-
let msgStr = "";
|
22
|
-
|
23
|
-
const instance = await WebAssembly.instantiate(wasmModule, {
|
24
|
-
runtime: {
|
25
|
-
exceptionHandler : function(code) {
|
26
|
-
let err;
|
27
|
-
if (code == 1) {
|
28
|
-
err = "Signal not found.\n";
|
29
|
-
} else if (code == 2) {
|
30
|
-
err = "Too many signals set.\n";
|
31
|
-
} else if (code == 3) {
|
32
|
-
err = "Signal already set.\n";
|
33
|
-
} else if (code == 4) {
|
34
|
-
err = "Assert Failed.\n";
|
35
|
-
} else if (code == 5) {
|
36
|
-
err = "Not enough memory.\n";
|
37
|
-
} else if (code == 6) {
|
38
|
-
err = "Input signal array access exceeds the size.\n";
|
39
|
-
} else {
|
40
|
-
err = "Unknown error.\n";
|
41
|
-
}
|
42
|
-
throw new Error(err + errStr);
|
43
|
-
},
|
44
|
-
printErrorMessage : function() {
|
45
|
-
errStr += getMessage() + "\n";
|
46
|
-
// console.error(getMessage());
|
47
|
-
},
|
48
|
-
writeBufferMessage : function() {
|
49
|
-
const msg = getMessage();
|
50
|
-
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
|
51
|
-
if (msg === "\n") {
|
52
|
-
console.log(msgStr);
|
53
|
-
msgStr = "";
|
54
|
-
} else {
|
55
|
-
// If we've buffered other content, put a space in between the items
|
56
|
-
if (msgStr !== "") {
|
57
|
-
msgStr += " "
|
58
|
-
}
|
59
|
-
// Then append the message to the message we are creating
|
60
|
-
msgStr += msg;
|
61
|
-
}
|
62
|
-
},
|
63
|
-
showSharedRWMemory : function() {
|
64
|
-
printSharedRWMemory ();
|
65
|
-
}
|
66
|
-
|
67
|
-
}
|
68
|
-
});
|
69
|
-
|
70
|
-
const sanityCheck =
|
71
|
-
options
|
72
|
-
// options &&
|
73
|
-
// (
|
74
|
-
// options.sanityCheck ||
|
75
|
-
// options.logGetSignal ||
|
76
|
-
// options.logSetSignal ||
|
77
|
-
// options.logStartComponent ||
|
78
|
-
// options.logFinishComponent
|
79
|
-
// );
|
80
|
-
|
81
|
-
|
82
|
-
wc = new WitnessCalculator(instance, sanityCheck);
|
83
|
-
return wc;
|
84
|
-
|
85
|
-
function getMessage() {
|
86
|
-
var message = "";
|
87
|
-
var c = instance.exports.getMessageChar();
|
88
|
-
while ( c != 0 ) {
|
89
|
-
message += String.fromCharCode(c);
|
90
|
-
c = instance.exports.getMessageChar();
|
91
|
-
}
|
92
|
-
return message;
|
93
|
-
}
|
94
|
-
|
95
|
-
function printSharedRWMemory () {
|
96
|
-
const shared_rw_memory_size = instance.exports.getFieldNumLen32();
|
97
|
-
const arr = new Uint32Array(shared_rw_memory_size);
|
98
|
-
for (let j=0; j<shared_rw_memory_size; j++) {
|
99
|
-
arr[shared_rw_memory_size-1-j] = instance.exports.readSharedRWMemory(j);
|
100
|
-
}
|
101
|
-
|
102
|
-
// If we've buffered other content, put a space in between the items
|
103
|
-
if (msgStr !== "") {
|
104
|
-
msgStr += " "
|
105
|
-
}
|
106
|
-
// Then append the value to the message we are creating
|
107
|
-
msgStr += (fromArray32(arr).toString());
|
108
|
-
}
|
109
|
-
|
110
|
-
};
|
111
|
-
|
112
|
-
class WitnessCalculator {
|
113
|
-
constructor(instance, sanityCheck) {
|
114
|
-
this.instance = instance;
|
115
|
-
|
116
|
-
this.version = this.instance.exports.getVersion();
|
117
|
-
this.n32 = this.instance.exports.getFieldNumLen32();
|
118
|
-
|
119
|
-
this.instance.exports.getRawPrime();
|
120
|
-
const arr = new Uint32Array(this.n32);
|
121
|
-
for (let i=0; i<this.n32; i++) {
|
122
|
-
arr[this.n32-1-i] = this.instance.exports.readSharedRWMemory(i);
|
123
|
-
}
|
124
|
-
this.prime = fromArray32(arr);
|
125
|
-
|
126
|
-
this.witnessSize = this.instance.exports.getWitnessSize();
|
127
|
-
|
128
|
-
this.sanityCheck = sanityCheck;
|
129
|
-
}
|
130
|
-
|
131
|
-
circom_version() {
|
132
|
-
return this.instance.exports.getVersion();
|
133
|
-
}
|
134
|
-
|
135
|
-
async _doCalculateWitness(input, sanityCheck) {
|
136
|
-
//input is assumed to be a map from signals to arrays of bigints
|
137
|
-
this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
|
138
|
-
const keys = Object.keys(input);
|
139
|
-
var input_counter = 0;
|
140
|
-
keys.forEach( (k) => {
|
141
|
-
const h = fnvHash(k);
|
142
|
-
const hMSB = parseInt(h.slice(0,8), 16);
|
143
|
-
const hLSB = parseInt(h.slice(8,16), 16);
|
144
|
-
const fArr = flatArray(input[k]);
|
145
|
-
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
|
146
|
-
if (signalSize < 0){
|
147
|
-
throw new Error(`Signal ${k} not found\n`);
|
148
|
-
}
|
149
|
-
if (fArr.length < signalSize) {
|
150
|
-
throw new Error(`Not enough values for input signal ${k}\n`);
|
151
|
-
}
|
152
|
-
if (fArr.length > signalSize) {
|
153
|
-
throw new Error(`Too many values for input signal ${k}\n`);
|
154
|
-
}
|
155
|
-
for (let i=0; i<fArr.length; i++) {
|
156
|
-
const arrFr = toArray32(BigInt(fArr[i])%this.prime,this.n32)
|
157
|
-
for (let j=0; j<this.n32; j++) {
|
158
|
-
this.instance.exports.writeSharedRWMemory(j,arrFr[this.n32-1-j]);
|
159
|
-
}
|
160
|
-
try {
|
161
|
-
this.instance.exports.setInputSignal(hMSB, hLSB,i);
|
162
|
-
input_counter++;
|
163
|
-
} catch (err) {
|
164
|
-
// console.log(`After adding signal ${i} of ${k}`)
|
165
|
-
throw new Error(err);
|
166
|
-
}
|
167
|
-
}
|
168
|
-
|
169
|
-
});
|
170
|
-
if (input_counter < this.instance.exports.getInputSize()) {
|
171
|
-
throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
|
172
|
-
}
|
173
|
-
}
|
174
|
-
|
175
|
-
async calculateWitness(input, sanityCheck) {
|
176
|
-
|
177
|
-
const w = [];
|
178
|
-
|
179
|
-
await this._doCalculateWitness(input, sanityCheck);
|
180
|
-
|
181
|
-
for (let i=0; i<this.witnessSize; i++) {
|
182
|
-
this.instance.exports.getWitness(i);
|
183
|
-
const arr = new Uint32Array(this.n32);
|
184
|
-
for (let j=0; j<this.n32; j++) {
|
185
|
-
arr[this.n32-1-j] = this.instance.exports.readSharedRWMemory(j);
|
186
|
-
}
|
187
|
-
w.push(fromArray32(arr));
|
188
|
-
}
|
189
|
-
|
190
|
-
return w;
|
191
|
-
}
|
192
|
-
|
193
|
-
|
194
|
-
async calculateBinWitness(input, sanityCheck) {
|
195
|
-
|
196
|
-
const buff32 = new Uint32Array(this.witnessSize*this.n32);
|
197
|
-
const buff = new Uint8Array( buff32.buffer);
|
198
|
-
await this._doCalculateWitness(input, sanityCheck);
|
199
|
-
|
200
|
-
for (let i=0; i<this.witnessSize; i++) {
|
201
|
-
this.instance.exports.getWitness(i);
|
202
|
-
const pos = i*this.n32;
|
203
|
-
for (let j=0; j<this.n32; j++) {
|
204
|
-
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
|
205
|
-
}
|
206
|
-
}
|
207
|
-
|
208
|
-
return buff;
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
async calculateWTNSBin(input, sanityCheck) {
|
213
|
-
|
214
|
-
const buff32 = new Uint32Array(this.witnessSize*this.n32+this.n32+11);
|
215
|
-
const buff = new Uint8Array( buff32.buffer);
|
216
|
-
await this._doCalculateWitness(input, sanityCheck);
|
217
|
-
|
218
|
-
//"wtns"
|
219
|
-
buff[0] = "w".charCodeAt(0)
|
220
|
-
buff[1] = "t".charCodeAt(0)
|
221
|
-
buff[2] = "n".charCodeAt(0)
|
222
|
-
buff[3] = "s".charCodeAt(0)
|
223
|
-
|
224
|
-
//version 2
|
225
|
-
buff32[1] = 2;
|
226
|
-
|
227
|
-
//number of sections: 2
|
228
|
-
buff32[2] = 2;
|
229
|
-
|
230
|
-
//id section 1
|
231
|
-
buff32[3] = 1;
|
232
|
-
|
233
|
-
const n8 = this.n32*4;
|
234
|
-
//id section 1 length in 64bytes
|
235
|
-
const idSection1length = 8 + n8;
|
236
|
-
const idSection1lengthHex = idSection1length.toString(16);
|
237
|
-
buff32[4] = parseInt(idSection1lengthHex.slice(0,8), 16);
|
238
|
-
buff32[5] = parseInt(idSection1lengthHex.slice(8,16), 16);
|
239
|
-
|
240
|
-
//this.n32
|
241
|
-
buff32[6] = n8;
|
242
|
-
|
243
|
-
//prime number
|
244
|
-
this.instance.exports.getRawPrime();
|
245
|
-
|
246
|
-
var pos = 7;
|
247
|
-
for (let j=0; j<this.n32; j++) {
|
248
|
-
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
|
249
|
-
}
|
250
|
-
pos += this.n32;
|
251
|
-
|
252
|
-
// witness size
|
253
|
-
buff32[pos] = this.witnessSize;
|
254
|
-
pos++;
|
255
|
-
|
256
|
-
//id section 2
|
257
|
-
buff32[pos] = 2;
|
258
|
-
pos++;
|
259
|
-
|
260
|
-
// section 2 length
|
261
|
-
const idSection2length = n8*this.witnessSize;
|
262
|
-
const idSection2lengthHex = idSection2length.toString(16);
|
263
|
-
buff32[pos] = parseInt(idSection2lengthHex.slice(0,8), 16);
|
264
|
-
buff32[pos+1] = parseInt(idSection2lengthHex.slice(8,16), 16);
|
265
|
-
|
266
|
-
pos += 2;
|
267
|
-
for (let i=0; i<this.witnessSize; i++) {
|
268
|
-
this.instance.exports.getWitness(i);
|
269
|
-
for (let j=0; j<this.n32; j++) {
|
270
|
-
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
|
271
|
-
}
|
272
|
-
pos += this.n32;
|
273
|
-
}
|
274
|
-
|
275
|
-
return buff;
|
276
|
-
}
|
277
|
-
|
278
|
-
}
|
279
|
-
|
280
|
-
|
281
|
-
function toArray32(rem,size) {
|
282
|
-
const res = []; //new Uint32Array(size); //has no unshift
|
283
|
-
const radix = BigInt(0x100000000);
|
284
|
-
while (rem) {
|
285
|
-
res.unshift( Number(rem % radix));
|
286
|
-
rem = rem / radix;
|
287
|
-
}
|
288
|
-
if (size) {
|
289
|
-
var i = size - res.length;
|
290
|
-
while (i>0) {
|
291
|
-
res.unshift(0);
|
292
|
-
i--;
|
293
|
-
}
|
294
|
-
}
|
295
|
-
return res;
|
296
|
-
}
|
297
|
-
|
298
|
-
function fromArray32(arr) { //returns a BigInt
|
299
|
-
var res = BigInt(0);
|
300
|
-
const radix = BigInt(0x100000000);
|
301
|
-
for (let i = 0; i<arr.length; i++) {
|
302
|
-
res = res*radix + BigInt(arr[i]);
|
303
|
-
}
|
304
|
-
return res;
|
305
|
-
}
|
306
|
-
|
307
|
-
function flatArray(a) {
|
308
|
-
var res = [];
|
309
|
-
fillArray(res, a);
|
310
|
-
return res;
|
311
|
-
|
312
|
-
function fillArray(res, a) {
|
313
|
-
if (Array.isArray(a)) {
|
314
|
-
for (let i=0; i<a.length; i++) {
|
315
|
-
fillArray(res, a[i]);
|
316
|
-
}
|
317
|
-
} else {
|
318
|
-
res.push(a);
|
319
|
-
}
|
320
|
-
}
|
321
|
-
}
|
322
|
-
|
323
|
-
function fnvHash(str) {
|
324
|
-
const uint64_max = BigInt(2) ** BigInt(64);
|
325
|
-
let hash = BigInt("0xCBF29CE484222325");
|
326
|
-
for (var i = 0; i < str.length; i++) {
|
327
|
-
hash ^= BigInt(str[i].charCodeAt());
|
328
|
-
hash *= BigInt(0x100000001B3);
|
329
|
-
hash %= uint64_max;
|
330
|
-
}
|
331
|
-
let shash = hash.toString(16);
|
332
|
-
let n = 16 - shash.length;
|
333
|
-
shash = '0'.repeat(n).concat(shash);
|
334
|
-
return shash;
|
335
|
-
}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|