@waku/rln 0.1.1-9b1e818 → 0.1.1-bafbe01

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 CHANGED
@@ -1639,7 +1639,7 @@ const encodingLength = (int) => {
1639
1639
  * @param {Code} code
1640
1640
  * @param {Uint8Array} digest
1641
1641
  */
1642
- const create$2 = (code, digest) => {
1642
+ const create$1 = (code, digest) => {
1643
1643
  const size = digest.byteLength;
1644
1644
  const sizeOffset = encodingLength(code);
1645
1645
  const digestOffset = sizeOffset + encodingLength(size);
@@ -1692,7 +1692,7 @@ const encode$3 = coerce;
1692
1692
  * @param {Uint8Array} input
1693
1693
  * @returns {Digest.Digest<typeof code, number>}
1694
1694
  */
1695
- const digest = (input) => create$2(code, encode$3(input));
1695
+ const digest = (input) => create$1(code, encode$3(input));
1696
1696
 
1697
1697
  const identity = { code, name, encode: encode$3, digest };
1698
1698
 
@@ -1741,9 +1741,9 @@ class Hasher {
1741
1741
  if (input instanceof Uint8Array) {
1742
1742
  const result = this.encode(input);
1743
1743
  return result instanceof Uint8Array
1744
- ? create$2(this.code, result)
1744
+ ? create$1(this.code, result)
1745
1745
  /* c8 ignore next 1 */
1746
- : result.then(digest => create$2(this.code, digest))
1746
+ : result.then(digest => create$1(this.code, digest))
1747
1747
  } else {
1748
1748
  throw Error('Unknown type, must be binary type')
1749
1749
  /* c8 ignore next 1 */
@@ -1980,9 +1980,7 @@ class RLNEncoder {
1980
1980
  }
1981
1981
  async generateProof(message) {
1982
1982
  const signal = toRLNSignal(this.contentTopic, message);
1983
- console.time("proof_gen_timer");
1984
1983
  const proof = await this.rlnInstance.generateRLNProof(signal, this.index, message.timestamp, this.idSecretHash);
1985
- console.timeEnd("proof_gen_timer");
1986
1984
  return proof;
1987
1985
  }
1988
1986
  get pubsubTopic() {
@@ -2086,6 +2084,14 @@ const SEPOLIA_CONTRACT = {
2086
2084
  abi: RLN_REGISTRY_ABI,
2087
2085
  };
2088
2086
 
2087
+ async function createRLN() {
2088
+ // A dependency graph that contains any wasm must all be imported
2089
+ // asynchronously. This file does the single async import, so
2090
+ // that no one else needs to worry about it again.
2091
+ const rlnModule = await Promise.resolve().then(function () { return rln; });
2092
+ return rlnModule.create();
2093
+ }
2094
+
2089
2095
  var lib = {};
2090
2096
 
2091
2097
  var types = {};
@@ -25387,626 +25393,9 @@ async function __wbg_init(input) {
25387
25393
  return __wbg_finalize_init(instance, module);
25388
25394
  }
25389
25395
 
25390
- const verificationKey = {
25391
- protocol: "groth16",
25392
- curve: "bn128",
25393
- nPublic: 6,
25394
- vk_alpha_1: [
25395
- "20124996762962216725442980738609010303800849578410091356605067053491763969391",
25396
- "9118593021526896828671519912099489027245924097793322973632351264852174143923",
25397
- "1",
25398
- ],
25399
- vk_beta_2: [
25400
- [
25401
- "4693952934005375501364248788849686435240706020501681709396105298107971354382",
25402
- "14346958885444710485362620645446987998958218205939139994511461437152241966681",
25403
- ],
25404
- [
25405
- "16851772916911573982706166384196538392731905827088356034885868448550849804972",
25406
- "823612331030938060799959717749043047845343400798220427319188951998582076532",
25407
- ],
25408
- ["1", "0"],
25409
- ],
25410
- vk_gamma_2: [
25411
- [
25412
- "10857046999023057135944570762232829481370756359578518086990519993285655852781",
25413
- "11559732032986387107991004021392285783925812861821192530917403151452391805634",
25414
- ],
25415
- [
25416
- "8495653923123431417604973247489272438418190587263600148770280649306958101930",
25417
- "4082367875863433681332203403145435568316851327593401208105741076214120093531",
25418
- ],
25419
- ["1", "0"],
25420
- ],
25421
- vk_delta_2: [
25422
- [
25423
- "8353516066399360694538747105302262515182301251524941126222712285088022964076",
25424
- "9329524012539638256356482961742014315122377605267454801030953882967973561832",
25425
- ],
25426
- [
25427
- "16805391589556134376869247619848130874761233086443465978238468412168162326401",
25428
- "10111259694977636294287802909665108497237922060047080343914303287629927847739",
25429
- ],
25430
- ["1", "0"],
25431
- ],
25432
- vk_alphabeta_12: [
25433
- [
25434
- [
25435
- "12608968655665301215455851857466367636344427685631271961542642719683786103711",
25436
- "9849575605876329747382930567422916152871921500826003490242628251047652318086",
25437
- ],
25438
- [
25439
- "6322029441245076030714726551623552073612922718416871603535535085523083939021",
25440
- "8700115492541474338049149013125102281865518624059015445617546140629435818912",
25441
- ],
25442
- [
25443
- "10674973475340072635573101639867487770811074181475255667220644196793546640210",
25444
- "2926286967251299230490668407790788696102889214647256022788211245826267484824",
25445
- ],
25446
- ],
25447
- [
25448
- [
25449
- "9660441540778523475944706619139394922744328902833875392144658911530830074820",
25450
- "19548113127774514328631808547691096362144426239827206966690021428110281506546",
25451
- ],
25452
- [
25453
- "1870837942477655969123169532603615788122896469891695773961478956740992497097",
25454
- "12536105729661705698805725105036536744930776470051238187456307227425796690780",
25455
- ],
25456
- [
25457
- "21811903352654147452884857281720047789720483752548991551595462057142824037334",
25458
- "19021616763967199151052893283384285352200445499680068407023236283004353578353",
25459
- ],
25460
- ],
25461
- ],
25462
- IC: [
25463
- [
25464
- "11992897507809711711025355300535923222599547639134311050809253678876341466909",
25465
- "17181525095924075896332561978747020491074338784673526378866503154966799128110",
25466
- "1",
25467
- ],
25468
- [
25469
- "17018665030246167677911144513385572506766200776123272044534328594850561667818",
25470
- "18601114175490465275436712413925513066546725461375425769709566180981674884464",
25471
- "1",
25472
- ],
25473
- [
25474
- "18799470100699658367834559797874857804183288553462108031963980039244731716542",
25475
- "13064227487174191981628537974951887429496059857753101852163607049188825592007",
25476
- "1",
25477
- ],
25478
- [
25479
- "17432501889058124609368103715904104425610382063762621017593209214189134571156",
25480
- "13406815149699834788256141097399354592751313348962590382887503595131085938635",
25481
- "1",
25482
- ],
25483
- [
25484
- "10320964835612716439094703312987075811498239445882526576970512041988148264481",
25485
- "9024164961646353611176283204118089412001502110138072989569118393359029324867",
25486
- "1",
25487
- ],
25488
- [
25489
- "718355081067365548229685160476620267257521491773976402837645005858953849298",
25490
- "14635482993933988261008156660773180150752190597753512086153001683711587601974",
25491
- "1",
25492
- ],
25493
- [
25494
- "11777720285956632126519898515392071627539405001940313098390150593689568177535",
25495
- "8483603647274280691250972408211651407952870456587066148445913156086740744515",
25496
- "1",
25497
- ],
25498
- ],
25499
- };
25396
+ var bn = {exports: {}};
25500
25397
 
25501
- // File generated with https://github.com/iden3/circom
25502
- // following the instructions from:
25503
- // https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
25504
- async function builder(code, options) {
25505
- options = options || {};
25506
- let wasmModule;
25507
- try {
25508
- wasmModule = await WebAssembly.compile(code);
25509
- }
25510
- catch (err) {
25511
- console.log(err);
25512
- console.log("\nTry to run circom --c in order to generate c++ code instead\n");
25513
- throw new Error(err);
25514
- }
25515
- let wc;
25516
- let errStr = "";
25517
- let msgStr = "";
25518
- const instance = await WebAssembly.instantiate(wasmModule, {
25519
- runtime: {
25520
- exceptionHandler: function (code) {
25521
- let err;
25522
- if (code == 1) {
25523
- err = "Signal not found.\n";
25524
- }
25525
- else if (code == 2) {
25526
- err = "Too many signals set.\n";
25527
- }
25528
- else if (code == 3) {
25529
- err = "Signal already set.\n";
25530
- }
25531
- else if (code == 4) {
25532
- err = "Assert Failed.\n";
25533
- }
25534
- else if (code == 5) {
25535
- err = "Not enough memory.\n";
25536
- }
25537
- else if (code == 6) {
25538
- err = "Input signal array access exceeds the size.\n";
25539
- }
25540
- else {
25541
- err = "Unknown error.\n";
25542
- }
25543
- throw new Error(err + errStr);
25544
- },
25545
- printErrorMessage: function () {
25546
- errStr += getMessage() + "\n";
25547
- // console.error(getMessage());
25548
- },
25549
- writeBufferMessage: function () {
25550
- const msg = getMessage();
25551
- // Any calls to `log()` will always end with a `\n`, so that's when we print and reset
25552
- if (msg === "\n") {
25553
- console.log(msgStr);
25554
- msgStr = "";
25555
- }
25556
- else {
25557
- // If we've buffered other content, put a space in between the items
25558
- if (msgStr !== "") {
25559
- msgStr += " ";
25560
- }
25561
- // Then append the message to the message we are creating
25562
- msgStr += msg;
25563
- }
25564
- },
25565
- showSharedRWMemory: function () {
25566
- printSharedRWMemory();
25567
- }
25568
- }
25569
- });
25570
- const sanityCheck = options;
25571
- // options &&
25572
- // (
25573
- // options.sanityCheck ||
25574
- // options.logGetSignal ||
25575
- // options.logSetSignal ||
25576
- // options.logStartComponent ||
25577
- // options.logFinishComponent
25578
- // );
25579
- wc = new WitnessCalculator(instance, sanityCheck);
25580
- return wc;
25581
- function getMessage() {
25582
- var message = "";
25583
- var c = instance.exports.getMessageChar();
25584
- while (c != 0) {
25585
- message += String.fromCharCode(c);
25586
- c = instance.exports.getMessageChar();
25587
- }
25588
- return message;
25589
- }
25590
- function printSharedRWMemory() {
25591
- const shared_rw_memory_size = instance.exports.getFieldNumLen32();
25592
- const arr = new Uint32Array(shared_rw_memory_size);
25593
- for (let j = 0; j < shared_rw_memory_size; j++) {
25594
- arr[shared_rw_memory_size - 1 - j] = instance.exports.readSharedRWMemory(j);
25595
- }
25596
- // If we've buffered other content, put a space in between the items
25597
- if (msgStr !== "") {
25598
- msgStr += " ";
25599
- }
25600
- // Then append the value to the message we are creating
25601
- msgStr += (fromArray32(arr).toString());
25602
- }
25603
- }
25604
- class WitnessCalculator {
25605
- constructor(instance, sanityCheck) {
25606
- this.instance = instance;
25607
- this.version = this.instance.exports.getVersion();
25608
- this.n32 = this.instance.exports.getFieldNumLen32();
25609
- this.instance.exports.getRawPrime();
25610
- const arr = new Uint32Array(this.n32);
25611
- for (let i = 0; i < this.n32; i++) {
25612
- arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
25613
- }
25614
- this.prime = fromArray32(arr);
25615
- this.witnessSize = this.instance.exports.getWitnessSize();
25616
- this.sanityCheck = sanityCheck;
25617
- }
25618
- circom_version() {
25619
- return this.instance.exports.getVersion();
25620
- }
25621
- async _doCalculateWitness(input, sanityCheck) {
25622
- //input is assumed to be a map from signals to arrays of bigints
25623
- this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
25624
- const keys = Object.keys(input);
25625
- var input_counter = 0;
25626
- keys.forEach((k) => {
25627
- const h = fnvHash(k);
25628
- const hMSB = parseInt(h.slice(0, 8), 16);
25629
- const hLSB = parseInt(h.slice(8, 16), 16);
25630
- const fArr = flatArray(input[k]);
25631
- let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
25632
- if (signalSize < 0) {
25633
- throw new Error(`Signal ${k} not found\n`);
25634
- }
25635
- if (fArr.length < signalSize) {
25636
- throw new Error(`Not enough values for input signal ${k}\n`);
25637
- }
25638
- if (fArr.length > signalSize) {
25639
- throw new Error(`Too many values for input signal ${k}\n`);
25640
- }
25641
- for (let i = 0; i < fArr.length; i++) {
25642
- const arrFr = toArray32(BigInt(fArr[i]) % this.prime, this.n32);
25643
- for (let j = 0; j < this.n32; j++) {
25644
- this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j]);
25645
- }
25646
- try {
25647
- this.instance.exports.setInputSignal(hMSB, hLSB, i);
25648
- input_counter++;
25649
- }
25650
- catch (err) {
25651
- // console.log(`After adding signal ${i} of ${k}`)
25652
- throw new Error(err);
25653
- }
25654
- }
25655
- });
25656
- if (input_counter < this.instance.exports.getInputSize()) {
25657
- throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
25658
- }
25659
- }
25660
- async calculateWitness(input, sanityCheck) {
25661
- const w = [];
25662
- await this._doCalculateWitness(input, sanityCheck);
25663
- for (let i = 0; i < this.witnessSize; i++) {
25664
- this.instance.exports.getWitness(i);
25665
- const arr = new Uint32Array(this.n32);
25666
- for (let j = 0; j < this.n32; j++) {
25667
- arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
25668
- }
25669
- w.push(fromArray32(arr));
25670
- }
25671
- return w;
25672
- }
25673
- async calculateBinWitness(input, sanityCheck) {
25674
- const buff32 = new Uint32Array(this.witnessSize * this.n32);
25675
- const buff = new Uint8Array(buff32.buffer);
25676
- await this._doCalculateWitness(input, sanityCheck);
25677
- for (let i = 0; i < this.witnessSize; i++) {
25678
- this.instance.exports.getWitness(i);
25679
- const pos = i * this.n32;
25680
- for (let j = 0; j < this.n32; j++) {
25681
- buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
25682
- }
25683
- }
25684
- return buff;
25685
- }
25686
- async calculateWTNSBin(input, sanityCheck) {
25687
- const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11);
25688
- const buff = new Uint8Array(buff32.buffer);
25689
- await this._doCalculateWitness(input, sanityCheck);
25690
- //"wtns"
25691
- buff[0] = "w".charCodeAt(0);
25692
- buff[1] = "t".charCodeAt(0);
25693
- buff[2] = "n".charCodeAt(0);
25694
- buff[3] = "s".charCodeAt(0);
25695
- //version 2
25696
- buff32[1] = 2;
25697
- //number of sections: 2
25698
- buff32[2] = 2;
25699
- //id section 1
25700
- buff32[3] = 1;
25701
- const n8 = this.n32 * 4;
25702
- //id section 1 length in 64bytes
25703
- const idSection1length = 8 + n8;
25704
- const idSection1lengthHex = idSection1length.toString(16);
25705
- buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
25706
- buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
25707
- //this.n32
25708
- buff32[6] = n8;
25709
- //prime number
25710
- this.instance.exports.getRawPrime();
25711
- var pos = 7;
25712
- for (let j = 0; j < this.n32; j++) {
25713
- buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
25714
- }
25715
- pos += this.n32;
25716
- // witness size
25717
- buff32[pos] = this.witnessSize;
25718
- pos++;
25719
- //id section 2
25720
- buff32[pos] = 2;
25721
- pos++;
25722
- // section 2 length
25723
- const idSection2length = n8 * this.witnessSize;
25724
- const idSection2lengthHex = idSection2length.toString(16);
25725
- buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
25726
- buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
25727
- pos += 2;
25728
- for (let i = 0; i < this.witnessSize; i++) {
25729
- this.instance.exports.getWitness(i);
25730
- for (let j = 0; j < this.n32; j++) {
25731
- buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
25732
- }
25733
- pos += this.n32;
25734
- }
25735
- return buff;
25736
- }
25737
- }
25738
- function toArray32(rem, size) {
25739
- const res = []; //new Uint32Array(size); //has no unshift
25740
- const radix = BigInt(0x100000000);
25741
- while (rem) {
25742
- res.unshift(Number(rem % radix));
25743
- rem = rem / radix;
25744
- }
25745
- if (size) {
25746
- var i = size - res.length;
25747
- while (i > 0) {
25748
- res.unshift(0);
25749
- i--;
25750
- }
25751
- }
25752
- return res;
25753
- }
25754
- function fromArray32(arr) {
25755
- var res = BigInt(0);
25756
- const radix = BigInt(0x100000000);
25757
- for (let i = 0; i < arr.length; i++) {
25758
- res = res * radix + BigInt(arr[i]);
25759
- }
25760
- return res;
25761
- }
25762
- function flatArray(a) {
25763
- var res = [];
25764
- fillArray(res, a);
25765
- return res;
25766
- function fillArray(res, a) {
25767
- if (Array.isArray(a)) {
25768
- for (let i = 0; i < a.length; i++) {
25769
- fillArray(res, a[i]);
25770
- }
25771
- }
25772
- else {
25773
- res.push(a);
25774
- }
25775
- }
25776
- }
25777
- function fnvHash(str) {
25778
- const uint64_max = BigInt(2) ** BigInt(64);
25779
- let hash = BigInt("0xCBF29CE484222325");
25780
- for (var i = 0; i < str.length; i++) {
25781
- hash ^= BigInt(str[i].charCodeAt());
25782
- hash *= BigInt(0x100000001B3);
25783
- hash %= uint64_max;
25784
- }
25785
- let shash = hash.toString(16);
25786
- let n = 16 - shash.length;
25787
- shash = '0'.repeat(n).concat(shash);
25788
- return shash;
25789
- }
25790
-
25791
- /**
25792
- * Concatenate Uint8Arrays
25793
- * @param input
25794
- * @returns concatenation of all Uint8Array received as input
25795
- */
25796
- function concatenate(...input) {
25797
- let totalLength = 0;
25798
- for (const arr of input) {
25799
- totalLength += arr.length;
25800
- }
25801
- const result = new Uint8Array(totalLength);
25802
- let offset = 0;
25803
- for (const arr of input) {
25804
- result.set(arr, offset);
25805
- offset += arr.length;
25806
- }
25807
- return result;
25808
- }
25809
- const stringEncoder = new TextEncoder();
25810
- const DEPTH = 20;
25811
- async function loadWitnessCalculator() {
25812
- const url = new URL(new URL('assets/rln-6ded2896.wasm', import.meta.url).href, import.meta.url);
25813
- const response = await fetch(url);
25814
- return await builder(new Uint8Array(await response.arrayBuffer()), false);
25815
- }
25816
- async function loadZkey() {
25817
- const url = new URL(new URL('assets/rln_final-8b299152.zkey', import.meta.url).href, import.meta.url);
25818
- const response = await fetch(url);
25819
- return new Uint8Array(await response.arrayBuffer());
25820
- }
25821
- /**
25822
- * Create an instance of RLN
25823
- * @returns RLNInstance
25824
- */
25825
- async function create$1() {
25826
- await __wbg_init?.();
25827
- init_panic_hook();
25828
- const witnessCalculator = await loadWitnessCalculator();
25829
- const zkey = await loadZkey();
25830
- const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
25831
- const zkRLN = newRLN(DEPTH, zkey, vkey);
25832
- return new RLNInstance(zkRLN, witnessCalculator);
25833
- }
25834
- class IdentityCredential {
25835
- constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment, IDCommitmentBigInt) {
25836
- this.IDTrapdoor = IDTrapdoor;
25837
- this.IDNullifier = IDNullifier;
25838
- this.IDSecretHash = IDSecretHash;
25839
- this.IDCommitment = IDCommitment;
25840
- this.IDCommitmentBigInt = IDCommitmentBigInt;
25841
- }
25842
- static fromBytes(memKeys) {
25843
- const idTrapdoor = memKeys.subarray(0, 32);
25844
- const idNullifier = memKeys.subarray(32, 64);
25845
- const idSecretHash = memKeys.subarray(64, 96);
25846
- const idCommitment = memKeys.subarray(96);
25847
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
25848
- return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
25849
- }
25850
- }
25851
- const proofOffset = 128;
25852
- const rootOffset = proofOffset + 32;
25853
- const epochOffset = rootOffset + 32;
25854
- const shareXOffset = epochOffset + 32;
25855
- const shareYOffset = shareXOffset + 32;
25856
- const nullifierOffset = shareYOffset + 32;
25857
- const rlnIdentifierOffset = nullifierOffset + 32;
25858
- class ProofMetadata {
25859
- constructor(nullifier, shareX, shareY, externalNullifier) {
25860
- this.nullifier = nullifier;
25861
- this.shareX = shareX;
25862
- this.shareY = shareY;
25863
- this.externalNullifier = externalNullifier;
25864
- }
25865
- }
25866
- class Proof {
25867
- constructor(proofBytes) {
25868
- if (proofBytes.length < rlnIdentifierOffset)
25869
- throw "invalid proof";
25870
- // parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
25871
- this.proof = proofBytes.subarray(0, proofOffset);
25872
- this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
25873
- this.epoch = proofBytes.subarray(rootOffset, epochOffset);
25874
- this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
25875
- this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
25876
- this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
25877
- this.rlnIdentifier = proofBytes.subarray(nullifierOffset, rlnIdentifierOffset);
25878
- }
25879
- extractMetadata() {
25880
- const externalNullifier = poseidonHash(this.epoch, this.rlnIdentifier);
25881
- return new ProofMetadata(this.nullifier, this.shareX, this.shareY, externalNullifier);
25882
- }
25883
- }
25884
- function proofToBytes(p) {
25885
- return concatenate(p.proof, p.merkleRoot, p.epoch, p.shareX, p.shareY, p.nullifier, p.rlnIdentifier);
25886
- }
25887
- function poseidonHash(...input) {
25888
- const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
25889
- const lenPrefixedData = concatenate(inputLen, ...input);
25890
- return poseidonHash$1(lenPrefixedData);
25891
- }
25892
- function sha256$2(input) {
25893
- const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
25894
- const lenPrefixedData = concatenate(inputLen, input);
25895
- return hash$1(lenPrefixedData);
25896
- }
25897
- class RLNInstance {
25898
- constructor(zkRLN, witnessCalculator) {
25899
- this.zkRLN = zkRLN;
25900
- this.witnessCalculator = witnessCalculator;
25901
- }
25902
- generateIdentityCredentials() {
25903
- const memKeys = generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
25904
- return IdentityCredential.fromBytes(memKeys);
25905
- }
25906
- generateSeededIdentityCredential(seed) {
25907
- const seedBytes = stringEncoder.encode(seed);
25908
- // TODO: rename this function in zerokit rln-wasm
25909
- const memKeys = generateSeededExtendedMembershipKey(this.zkRLN, seedBytes);
25910
- return IdentityCredential.fromBytes(memKeys);
25911
- }
25912
- insertMember(idCommitment) {
25913
- insertMember(this.zkRLN, idCommitment);
25914
- }
25915
- insertMembers(index, ...idCommitments) {
25916
- // serializes a seq of IDCommitments to a byte seq
25917
- // the order of serialization is |id_commitment_len<8>|id_commitment<var>|
25918
- const idCommitmentLen = writeUIntLE(new Uint8Array(8), idCommitments.length, 0, 8);
25919
- const idCommitmentBytes = concatenate(idCommitmentLen, ...idCommitments);
25920
- setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
25921
- }
25922
- deleteMember(index) {
25923
- deleteLeaf(this.zkRLN, index);
25924
- }
25925
- getMerkleRoot() {
25926
- return getRoot(this.zkRLN);
25927
- }
25928
- serializeMessage(uint8Msg, memIndex, epoch, idKey) {
25929
- // calculate message length
25930
- const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
25931
- // Converting index to LE bytes
25932
- const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
25933
- // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
25934
- return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg);
25935
- }
25936
- async generateRLNProof(msg, index, epoch, idSecretHash) {
25937
- if (epoch == undefined) {
25938
- epoch = epochIntToBytes(dateToEpoch(new Date()));
25939
- }
25940
- else if (epoch instanceof Date) {
25941
- epoch = epochIntToBytes(dateToEpoch(epoch));
25942
- }
25943
- if (epoch.length != 32)
25944
- throw "invalid epoch";
25945
- if (idSecretHash.length != 32)
25946
- throw "invalid id secret hash";
25947
- if (index < 0)
25948
- throw "index must be >= 0";
25949
- const serialized_msg = this.serializeMessage(msg, index, epoch, idSecretHash);
25950
- const rlnWitness = getSerializedRLNWitness(this.zkRLN, serialized_msg);
25951
- const inputs = RLNWitnessToJson(this.zkRLN, rlnWitness);
25952
- const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false); // no sanity check being used in zerokit
25953
- const proofBytes = generate_rln_proof_with_witness(this.zkRLN, calculatedWitness, rlnWitness);
25954
- return new Proof(proofBytes);
25955
- }
25956
- verifyRLNProof(proof, msg) {
25957
- let pBytes;
25958
- if (proof instanceof Uint8Array) {
25959
- pBytes = proof;
25960
- }
25961
- else {
25962
- pBytes = proofToBytes(proof);
25963
- }
25964
- // calculate message length
25965
- const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
25966
- return verifyRLNProof(this.zkRLN, concatenate(pBytes, msgLen, msg));
25967
- }
25968
- verifyWithRoots(proof, msg, ...roots) {
25969
- let pBytes;
25970
- if (proof instanceof Uint8Array) {
25971
- pBytes = proof;
25972
- }
25973
- else {
25974
- pBytes = proofToBytes(proof);
25975
- }
25976
- // calculate message length
25977
- const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
25978
- const rootsBytes = concatenate(...roots);
25979
- return verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg), rootsBytes);
25980
- }
25981
- verifyWithNoRoot(proof, msg) {
25982
- let pBytes;
25983
- if (proof instanceof Uint8Array) {
25984
- pBytes = proof;
25985
- }
25986
- else {
25987
- pBytes = proofToBytes(proof);
25988
- }
25989
- // calculate message length
25990
- const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
25991
- return verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg), new Uint8Array());
25992
- }
25993
- }
25994
-
25995
- var rln = /*#__PURE__*/Object.freeze({
25996
- __proto__: null,
25997
- create: create$1,
25998
- IdentityCredential: IdentityCredential,
25999
- ProofMetadata: ProofMetadata,
26000
- Proof: Proof,
26001
- proofToBytes: proofToBytes,
26002
- poseidonHash: poseidonHash,
26003
- sha256: sha256$2,
26004
- RLNInstance: RLNInstance
26005
- });
26006
-
26007
- var bn = {exports: {}};
26008
-
26009
- var _nodeResolve_empty = {};
25398
+ var _nodeResolve_empty = {};
26010
25399
 
26011
25400
  var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
26012
25401
  __proto__: null,
@@ -40986,7 +40375,7 @@ const logger$q = new Logger(version$b);
40986
40375
  function ripemd160(data) {
40987
40376
  return "0x" + (hash.ripemd160().update(arrayify(data)).digest("hex"));
40988
40377
  }
40989
- function sha256$1(data) {
40378
+ function sha256$2(data) {
40990
40379
  return "0x" + (hash.sha256().update(arrayify(data)).digest("hex"));
40991
40380
  }
40992
40381
  function sha512(data) {
@@ -41131,7 +40520,7 @@ function bytes32(value) {
41131
40520
  return hexZeroPad(hexlify(value), 32);
41132
40521
  }
41133
40522
  function base58check(data) {
41134
- return Base58.encode(concat([data, hexDataSlice(sha256$1(sha256$1(data)), 0, 4)]));
40523
+ return Base58.encode(concat([data, hexDataSlice(sha256$2(sha256$2(data)), 0, 4)]));
41135
40524
  }
41136
40525
  function getWordlist(wordlist) {
41137
40526
  if (wordlist == null) {
@@ -41171,7 +40560,7 @@ class HDNode {
41171
40560
  defineReadOnly(this, "publicKey", hexlify(publicKey));
41172
40561
  }
41173
40562
  defineReadOnly(this, "parentFingerprint", parentFingerprint);
41174
- defineReadOnly(this, "fingerprint", hexDataSlice(ripemd160(sha256$1(this.publicKey)), 0, 4));
40563
+ defineReadOnly(this, "fingerprint", hexDataSlice(ripemd160(sha256$2(this.publicKey)), 0, 4));
41175
40564
  defineReadOnly(this, "address", computeAddress(this.publicKey));
41176
40565
  defineReadOnly(this, "chainCode", chainCode);
41177
40566
  defineReadOnly(this, "index", index);
@@ -41377,7 +40766,7 @@ function mnemonicToEntropy(mnemonic, wordlist) {
41377
40766
  const entropyBits = 32 * words.length / 3;
41378
40767
  const checksumBits = words.length / 3;
41379
40768
  const checksumMask = getUpperMask(checksumBits);
41380
- const checksum = arrayify(sha256$1(entropy.slice(0, entropyBits / 8)))[0] & checksumMask;
40769
+ const checksum = arrayify(sha256$2(entropy.slice(0, entropyBits / 8)))[0] & checksumMask;
41381
40770
  if (checksum !== (entropy[entropy.length - 1] & checksumMask)) {
41382
40771
  throw new Error("invalid checksum");
41383
40772
  }
@@ -41409,7 +40798,7 @@ function entropyToMnemonic(entropy, wordlist) {
41409
40798
  }
41410
40799
  // Compute the checksum bits
41411
40800
  const checksumBits = entropy.length / 4;
41412
- const checksum = arrayify(sha256$1(entropy))[0] & getUpperMask(checksumBits);
40801
+ const checksum = arrayify(sha256$2(entropy))[0] & getUpperMask(checksumBits);
41413
40802
  // Shift the checksum into the word indices
41414
40803
  indices[indices.length - 1] <<= checksumBits;
41415
40804
  indices[indices.length - 1] |= (checksum >> (8 - checksumBits));
@@ -44888,7 +44277,7 @@ function bytes32ify(value) {
44888
44277
  }
44889
44278
  // Compute the Base58Check encoded data (checksum is first 4 bytes of sha256d)
44890
44279
  function base58Encode(data) {
44891
- return Base58.encode(concat([data, hexDataSlice(sha256$1(sha256$1(data)), 0, 4)]));
44280
+ return Base58.encode(concat([data, hexDataSlice(sha256$2(sha256$2(data)), 0, 4)]));
44892
44281
  }
44893
44282
  const matcherIpfs = new RegExp("^(ipfs):/\/(.*)$", "i");
44894
44283
  const matchers = [
@@ -49494,8 +48883,8 @@ function pack(types, values) {
49494
48883
  function keccak256(types, values) {
49495
48884
  return keccak256$1(pack(types, values));
49496
48885
  }
49497
- function sha256(types, values) {
49498
- return sha256$1(pack(types, values));
48886
+ function sha256$1(types, values) {
48887
+ return sha256$2(pack(types, values));
49499
48888
  }
49500
48889
 
49501
48890
  const version$1 = "units/5.7.0";
@@ -49652,13 +49041,13 @@ var utils = /*#__PURE__*/Object.freeze({
49652
49041
  computeHmac: computeHmac,
49653
49042
  keccak256: keccak256$1,
49654
49043
  ripemd160: ripemd160,
49655
- sha256: sha256$1,
49044
+ sha256: sha256$2,
49656
49045
  sha512: sha512,
49657
49046
  randomBytes: randomBytes,
49658
49047
  shuffled: shuffled,
49659
49048
  solidityPack: pack,
49660
49049
  solidityKeccak256: keccak256,
49661
- soliditySha256: sha256,
49050
+ soliditySha256: sha256$1,
49662
49051
  splitSignature: splitSignature,
49663
49052
  joinSignature: joinSignature,
49664
49053
  accessListify: accessListify,
@@ -49716,6 +49105,126 @@ try {
49716
49105
  }
49717
49106
  catch (error) { }
49718
49107
 
49108
+ const extractMetaMaskAccount = async () => {
49109
+ const ethereum = window.ethereum;
49110
+ if (!ethereum) {
49111
+ throw Error("Missing or invalid Ethereum provider. Please install a Web3 wallet such as MetaMask.");
49112
+ }
49113
+ await ethereum.request({ method: "eth_requestAccounts" });
49114
+ return new Web3Provider(ethereum, "any");
49115
+ };
49116
+
49117
+ const verificationKey = {
49118
+ protocol: "groth16",
49119
+ curve: "bn128",
49120
+ nPublic: 6,
49121
+ vk_alpha_1: [
49122
+ "20124996762962216725442980738609010303800849578410091356605067053491763969391",
49123
+ "9118593021526896828671519912099489027245924097793322973632351264852174143923",
49124
+ "1",
49125
+ ],
49126
+ vk_beta_2: [
49127
+ [
49128
+ "4693952934005375501364248788849686435240706020501681709396105298107971354382",
49129
+ "14346958885444710485362620645446987998958218205939139994511461437152241966681",
49130
+ ],
49131
+ [
49132
+ "16851772916911573982706166384196538392731905827088356034885868448550849804972",
49133
+ "823612331030938060799959717749043047845343400798220427319188951998582076532",
49134
+ ],
49135
+ ["1", "0"],
49136
+ ],
49137
+ vk_gamma_2: [
49138
+ [
49139
+ "10857046999023057135944570762232829481370756359578518086990519993285655852781",
49140
+ "11559732032986387107991004021392285783925812861821192530917403151452391805634",
49141
+ ],
49142
+ [
49143
+ "8495653923123431417604973247489272438418190587263600148770280649306958101930",
49144
+ "4082367875863433681332203403145435568316851327593401208105741076214120093531",
49145
+ ],
49146
+ ["1", "0"],
49147
+ ],
49148
+ vk_delta_2: [
49149
+ [
49150
+ "8353516066399360694538747105302262515182301251524941126222712285088022964076",
49151
+ "9329524012539638256356482961742014315122377605267454801030953882967973561832",
49152
+ ],
49153
+ [
49154
+ "16805391589556134376869247619848130874761233086443465978238468412168162326401",
49155
+ "10111259694977636294287802909665108497237922060047080343914303287629927847739",
49156
+ ],
49157
+ ["1", "0"],
49158
+ ],
49159
+ vk_alphabeta_12: [
49160
+ [
49161
+ [
49162
+ "12608968655665301215455851857466367636344427685631271961542642719683786103711",
49163
+ "9849575605876329747382930567422916152871921500826003490242628251047652318086",
49164
+ ],
49165
+ [
49166
+ "6322029441245076030714726551623552073612922718416871603535535085523083939021",
49167
+ "8700115492541474338049149013125102281865518624059015445617546140629435818912",
49168
+ ],
49169
+ [
49170
+ "10674973475340072635573101639867487770811074181475255667220644196793546640210",
49171
+ "2926286967251299230490668407790788696102889214647256022788211245826267484824",
49172
+ ],
49173
+ ],
49174
+ [
49175
+ [
49176
+ "9660441540778523475944706619139394922744328902833875392144658911530830074820",
49177
+ "19548113127774514328631808547691096362144426239827206966690021428110281506546",
49178
+ ],
49179
+ [
49180
+ "1870837942477655969123169532603615788122896469891695773961478956740992497097",
49181
+ "12536105729661705698805725105036536744930776470051238187456307227425796690780",
49182
+ ],
49183
+ [
49184
+ "21811903352654147452884857281720047789720483752548991551595462057142824037334",
49185
+ "19021616763967199151052893283384285352200445499680068407023236283004353578353",
49186
+ ],
49187
+ ],
49188
+ ],
49189
+ IC: [
49190
+ [
49191
+ "11992897507809711711025355300535923222599547639134311050809253678876341466909",
49192
+ "17181525095924075896332561978747020491074338784673526378866503154966799128110",
49193
+ "1",
49194
+ ],
49195
+ [
49196
+ "17018665030246167677911144513385572506766200776123272044534328594850561667818",
49197
+ "18601114175490465275436712413925513066546725461375425769709566180981674884464",
49198
+ "1",
49199
+ ],
49200
+ [
49201
+ "18799470100699658367834559797874857804183288553462108031963980039244731716542",
49202
+ "13064227487174191981628537974951887429496059857753101852163607049188825592007",
49203
+ "1",
49204
+ ],
49205
+ [
49206
+ "17432501889058124609368103715904104425610382063762621017593209214189134571156",
49207
+ "13406815149699834788256141097399354592751313348962590382887503595131085938635",
49208
+ "1",
49209
+ ],
49210
+ [
49211
+ "10320964835612716439094703312987075811498239445882526576970512041988148264481",
49212
+ "9024164961646353611176283204118089412001502110138072989569118393359029324867",
49213
+ "1",
49214
+ ],
49215
+ [
49216
+ "718355081067365548229685160476620267257521491773976402837645005858953849298",
49217
+ "14635482993933988261008156660773180150752190597753512086153001683711587601974",
49218
+ "1",
49219
+ ],
49220
+ [
49221
+ "11777720285956632126519898515392071627539405001940313098390150593689568177535",
49222
+ "8483603647274280691250972408211651407952870456587066148445913156086740744515",
49223
+ "1",
49224
+ ],
49225
+ ],
49226
+ };
49227
+
49719
49228
  class RootPerBlock {
49720
49229
  constructor(root, blockNumber) {
49721
49230
  this.root = root;
@@ -49979,13 +49488,522 @@ function ignoreErrors(promise, defaultValue) {
49979
49488
  });
49980
49489
  }
49981
49490
 
49982
- // reexport the create function, dynamically imported from rln.ts
49491
+ // File generated with https://github.com/iden3/circom
49492
+ // following the instructions from:
49493
+ // https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
49494
+ async function builder(code, options) {
49495
+ options = options || {};
49496
+ let wasmModule;
49497
+ try {
49498
+ wasmModule = await WebAssembly.compile(code);
49499
+ }
49500
+ catch (err) {
49501
+ console.log(err);
49502
+ console.log("\nTry to run circom --c in order to generate c++ code instead\n");
49503
+ throw new Error(err);
49504
+ }
49505
+ let wc;
49506
+ let errStr = "";
49507
+ let msgStr = "";
49508
+ const instance = await WebAssembly.instantiate(wasmModule, {
49509
+ runtime: {
49510
+ exceptionHandler: function (code) {
49511
+ let err;
49512
+ if (code == 1) {
49513
+ err = "Signal not found.\n";
49514
+ }
49515
+ else if (code == 2) {
49516
+ err = "Too many signals set.\n";
49517
+ }
49518
+ else if (code == 3) {
49519
+ err = "Signal already set.\n";
49520
+ }
49521
+ else if (code == 4) {
49522
+ err = "Assert Failed.\n";
49523
+ }
49524
+ else if (code == 5) {
49525
+ err = "Not enough memory.\n";
49526
+ }
49527
+ else if (code == 6) {
49528
+ err = "Input signal array access exceeds the size.\n";
49529
+ }
49530
+ else {
49531
+ err = "Unknown error.\n";
49532
+ }
49533
+ throw new Error(err + errStr);
49534
+ },
49535
+ printErrorMessage: function () {
49536
+ errStr += getMessage() + "\n";
49537
+ // console.error(getMessage());
49538
+ },
49539
+ writeBufferMessage: function () {
49540
+ const msg = getMessage();
49541
+ // Any calls to `log()` will always end with a `\n`, so that's when we print and reset
49542
+ if (msg === "\n") {
49543
+ console.log(msgStr);
49544
+ msgStr = "";
49545
+ }
49546
+ else {
49547
+ // If we've buffered other content, put a space in between the items
49548
+ if (msgStr !== "") {
49549
+ msgStr += " ";
49550
+ }
49551
+ // Then append the message to the message we are creating
49552
+ msgStr += msg;
49553
+ }
49554
+ },
49555
+ showSharedRWMemory: function () {
49556
+ printSharedRWMemory();
49557
+ }
49558
+ }
49559
+ });
49560
+ const sanityCheck = options;
49561
+ // options &&
49562
+ // (
49563
+ // options.sanityCheck ||
49564
+ // options.logGetSignal ||
49565
+ // options.logSetSignal ||
49566
+ // options.logStartComponent ||
49567
+ // options.logFinishComponent
49568
+ // );
49569
+ wc = new WitnessCalculator(instance, sanityCheck);
49570
+ return wc;
49571
+ function getMessage() {
49572
+ var message = "";
49573
+ var c = instance.exports.getMessageChar();
49574
+ while (c != 0) {
49575
+ message += String.fromCharCode(c);
49576
+ c = instance.exports.getMessageChar();
49577
+ }
49578
+ return message;
49579
+ }
49580
+ function printSharedRWMemory() {
49581
+ const shared_rw_memory_size = instance.exports.getFieldNumLen32();
49582
+ const arr = new Uint32Array(shared_rw_memory_size);
49583
+ for (let j = 0; j < shared_rw_memory_size; j++) {
49584
+ arr[shared_rw_memory_size - 1 - j] = instance.exports.readSharedRWMemory(j);
49585
+ }
49586
+ // If we've buffered other content, put a space in between the items
49587
+ if (msgStr !== "") {
49588
+ msgStr += " ";
49589
+ }
49590
+ // Then append the value to the message we are creating
49591
+ msgStr += (fromArray32(arr).toString());
49592
+ }
49593
+ }
49594
+ class WitnessCalculator {
49595
+ constructor(instance, sanityCheck) {
49596
+ this.instance = instance;
49597
+ this.version = this.instance.exports.getVersion();
49598
+ this.n32 = this.instance.exports.getFieldNumLen32();
49599
+ this.instance.exports.getRawPrime();
49600
+ const arr = new Uint32Array(this.n32);
49601
+ for (let i = 0; i < this.n32; i++) {
49602
+ arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
49603
+ }
49604
+ this.prime = fromArray32(arr);
49605
+ this.witnessSize = this.instance.exports.getWitnessSize();
49606
+ this.sanityCheck = sanityCheck;
49607
+ }
49608
+ circom_version() {
49609
+ return this.instance.exports.getVersion();
49610
+ }
49611
+ async _doCalculateWitness(input, sanityCheck) {
49612
+ //input is assumed to be a map from signals to arrays of bigints
49613
+ this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
49614
+ const keys = Object.keys(input);
49615
+ var input_counter = 0;
49616
+ keys.forEach((k) => {
49617
+ const h = fnvHash(k);
49618
+ const hMSB = parseInt(h.slice(0, 8), 16);
49619
+ const hLSB = parseInt(h.slice(8, 16), 16);
49620
+ const fArr = flatArray(input[k]);
49621
+ let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
49622
+ if (signalSize < 0) {
49623
+ throw new Error(`Signal ${k} not found\n`);
49624
+ }
49625
+ if (fArr.length < signalSize) {
49626
+ throw new Error(`Not enough values for input signal ${k}\n`);
49627
+ }
49628
+ if (fArr.length > signalSize) {
49629
+ throw new Error(`Too many values for input signal ${k}\n`);
49630
+ }
49631
+ for (let i = 0; i < fArr.length; i++) {
49632
+ const arrFr = toArray32(BigInt(fArr[i]) % this.prime, this.n32);
49633
+ for (let j = 0; j < this.n32; j++) {
49634
+ this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j]);
49635
+ }
49636
+ try {
49637
+ this.instance.exports.setInputSignal(hMSB, hLSB, i);
49638
+ input_counter++;
49639
+ }
49640
+ catch (err) {
49641
+ // console.log(`After adding signal ${i} of ${k}`)
49642
+ throw new Error(err);
49643
+ }
49644
+ }
49645
+ });
49646
+ if (input_counter < this.instance.exports.getInputSize()) {
49647
+ throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
49648
+ }
49649
+ }
49650
+ async calculateWitness(input, sanityCheck) {
49651
+ const w = [];
49652
+ await this._doCalculateWitness(input, sanityCheck);
49653
+ for (let i = 0; i < this.witnessSize; i++) {
49654
+ this.instance.exports.getWitness(i);
49655
+ const arr = new Uint32Array(this.n32);
49656
+ for (let j = 0; j < this.n32; j++) {
49657
+ arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
49658
+ }
49659
+ w.push(fromArray32(arr));
49660
+ }
49661
+ return w;
49662
+ }
49663
+ async calculateBinWitness(input, sanityCheck) {
49664
+ const buff32 = new Uint32Array(this.witnessSize * this.n32);
49665
+ const buff = new Uint8Array(buff32.buffer);
49666
+ await this._doCalculateWitness(input, sanityCheck);
49667
+ for (let i = 0; i < this.witnessSize; i++) {
49668
+ this.instance.exports.getWitness(i);
49669
+ const pos = i * this.n32;
49670
+ for (let j = 0; j < this.n32; j++) {
49671
+ buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
49672
+ }
49673
+ }
49674
+ return buff;
49675
+ }
49676
+ async calculateWTNSBin(input, sanityCheck) {
49677
+ const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11);
49678
+ const buff = new Uint8Array(buff32.buffer);
49679
+ await this._doCalculateWitness(input, sanityCheck);
49680
+ //"wtns"
49681
+ buff[0] = "w".charCodeAt(0);
49682
+ buff[1] = "t".charCodeAt(0);
49683
+ buff[2] = "n".charCodeAt(0);
49684
+ buff[3] = "s".charCodeAt(0);
49685
+ //version 2
49686
+ buff32[1] = 2;
49687
+ //number of sections: 2
49688
+ buff32[2] = 2;
49689
+ //id section 1
49690
+ buff32[3] = 1;
49691
+ const n8 = this.n32 * 4;
49692
+ //id section 1 length in 64bytes
49693
+ const idSection1length = 8 + n8;
49694
+ const idSection1lengthHex = idSection1length.toString(16);
49695
+ buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
49696
+ buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
49697
+ //this.n32
49698
+ buff32[6] = n8;
49699
+ //prime number
49700
+ this.instance.exports.getRawPrime();
49701
+ var pos = 7;
49702
+ for (let j = 0; j < this.n32; j++) {
49703
+ buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
49704
+ }
49705
+ pos += this.n32;
49706
+ // witness size
49707
+ buff32[pos] = this.witnessSize;
49708
+ pos++;
49709
+ //id section 2
49710
+ buff32[pos] = 2;
49711
+ pos++;
49712
+ // section 2 length
49713
+ const idSection2length = n8 * this.witnessSize;
49714
+ const idSection2lengthHex = idSection2length.toString(16);
49715
+ buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
49716
+ buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
49717
+ pos += 2;
49718
+ for (let i = 0; i < this.witnessSize; i++) {
49719
+ this.instance.exports.getWitness(i);
49720
+ for (let j = 0; j < this.n32; j++) {
49721
+ buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
49722
+ }
49723
+ pos += this.n32;
49724
+ }
49725
+ return buff;
49726
+ }
49727
+ }
49728
+ function toArray32(rem, size) {
49729
+ const res = []; //new Uint32Array(size); //has no unshift
49730
+ const radix = BigInt(0x100000000);
49731
+ while (rem) {
49732
+ res.unshift(Number(rem % radix));
49733
+ rem = rem / radix;
49734
+ }
49735
+ if (size) {
49736
+ var i = size - res.length;
49737
+ while (i > 0) {
49738
+ res.unshift(0);
49739
+ i--;
49740
+ }
49741
+ }
49742
+ return res;
49743
+ }
49744
+ function fromArray32(arr) {
49745
+ var res = BigInt(0);
49746
+ const radix = BigInt(0x100000000);
49747
+ for (let i = 0; i < arr.length; i++) {
49748
+ res = res * radix + BigInt(arr[i]);
49749
+ }
49750
+ return res;
49751
+ }
49752
+ function flatArray(a) {
49753
+ var res = [];
49754
+ fillArray(res, a);
49755
+ return res;
49756
+ function fillArray(res, a) {
49757
+ if (Array.isArray(a)) {
49758
+ for (let i = 0; i < a.length; i++) {
49759
+ fillArray(res, a[i]);
49760
+ }
49761
+ }
49762
+ else {
49763
+ res.push(a);
49764
+ }
49765
+ }
49766
+ }
49767
+ function fnvHash(str) {
49768
+ const uint64_max = BigInt(2) ** BigInt(64);
49769
+ let hash = BigInt("0xCBF29CE484222325");
49770
+ for (var i = 0; i < str.length; i++) {
49771
+ hash ^= BigInt(str[i].charCodeAt());
49772
+ hash *= BigInt(0x100000001B3);
49773
+ hash %= uint64_max;
49774
+ }
49775
+ let shash = hash.toString(16);
49776
+ let n = 16 - shash.length;
49777
+ shash = '0'.repeat(n).concat(shash);
49778
+ return shash;
49779
+ }
49780
+
49781
+ /**
49782
+ * Concatenate Uint8Arrays
49783
+ * @param input
49784
+ * @returns concatenation of all Uint8Array received as input
49785
+ */
49786
+ function concatenate(...input) {
49787
+ let totalLength = 0;
49788
+ for (const arr of input) {
49789
+ totalLength += arr.length;
49790
+ }
49791
+ const result = new Uint8Array(totalLength);
49792
+ let offset = 0;
49793
+ for (const arr of input) {
49794
+ result.set(arr, offset);
49795
+ offset += arr.length;
49796
+ }
49797
+ return result;
49798
+ }
49799
+ const stringEncoder = new TextEncoder();
49800
+ const DEPTH = 20;
49801
+ async function loadWitnessCalculator() {
49802
+ const url = new URL(new URL('assets/rln-6ded2896.wasm', import.meta.url).href, import.meta.url);
49803
+ const response = await fetch(url);
49804
+ return await builder(new Uint8Array(await response.arrayBuffer()), false);
49805
+ }
49806
+ async function loadZkey() {
49807
+ const url = new URL(new URL('assets/rln_final-8b299152.zkey', import.meta.url).href, import.meta.url);
49808
+ const response = await fetch(url);
49809
+ return new Uint8Array(await response.arrayBuffer());
49810
+ }
49811
+ /**
49812
+ * Create an instance of RLN
49813
+ * @returns RLNInstance
49814
+ */
49983
49815
  async function create() {
49984
- // A dependency graph that contains any wasm must all be imported
49985
- // asynchronously. This file does the single async import, so
49986
- // that no one else needs to worry about it again.
49987
- const rlnModule = await Promise.resolve().then(function () { return rln; });
49988
- return await rlnModule.create();
49816
+ await __wbg_init?.();
49817
+ init_panic_hook();
49818
+ const witnessCalculator = await loadWitnessCalculator();
49819
+ const zkey = await loadZkey();
49820
+ const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
49821
+ const zkRLN = newRLN(DEPTH, zkey, vkey);
49822
+ return new RLNInstance(zkRLN, witnessCalculator);
49823
+ }
49824
+ class IdentityCredential {
49825
+ constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment, IDCommitmentBigInt) {
49826
+ this.IDTrapdoor = IDTrapdoor;
49827
+ this.IDNullifier = IDNullifier;
49828
+ this.IDSecretHash = IDSecretHash;
49829
+ this.IDCommitment = IDCommitment;
49830
+ this.IDCommitmentBigInt = IDCommitmentBigInt;
49831
+ }
49832
+ static fromBytes(memKeys) {
49833
+ const idTrapdoor = memKeys.subarray(0, 32);
49834
+ const idNullifier = memKeys.subarray(32, 64);
49835
+ const idSecretHash = memKeys.subarray(64, 96);
49836
+ const idCommitment = memKeys.subarray(96);
49837
+ const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
49838
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
49839
+ }
49840
+ }
49841
+ const proofOffset = 128;
49842
+ const rootOffset = proofOffset + 32;
49843
+ const epochOffset = rootOffset + 32;
49844
+ const shareXOffset = epochOffset + 32;
49845
+ const shareYOffset = shareXOffset + 32;
49846
+ const nullifierOffset = shareYOffset + 32;
49847
+ const rlnIdentifierOffset = nullifierOffset + 32;
49848
+ class ProofMetadata {
49849
+ constructor(nullifier, shareX, shareY, externalNullifier) {
49850
+ this.nullifier = nullifier;
49851
+ this.shareX = shareX;
49852
+ this.shareY = shareY;
49853
+ this.externalNullifier = externalNullifier;
49854
+ }
49855
+ }
49856
+ class Proof {
49857
+ constructor(proofBytes) {
49858
+ if (proofBytes.length < rlnIdentifierOffset)
49859
+ throw "invalid proof";
49860
+ // parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
49861
+ this.proof = proofBytes.subarray(0, proofOffset);
49862
+ this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
49863
+ this.epoch = proofBytes.subarray(rootOffset, epochOffset);
49864
+ this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
49865
+ this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
49866
+ this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
49867
+ this.rlnIdentifier = proofBytes.subarray(nullifierOffset, rlnIdentifierOffset);
49868
+ }
49869
+ extractMetadata() {
49870
+ const externalNullifier = poseidonHash(this.epoch, this.rlnIdentifier);
49871
+ return new ProofMetadata(this.nullifier, this.shareX, this.shareY, externalNullifier);
49872
+ }
49873
+ }
49874
+ function proofToBytes(p) {
49875
+ return concatenate(p.proof, p.merkleRoot, p.epoch, p.shareX, p.shareY, p.nullifier, p.rlnIdentifier);
49876
+ }
49877
+ function poseidonHash(...input) {
49878
+ const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
49879
+ const lenPrefixedData = concatenate(inputLen, ...input);
49880
+ return poseidonHash$1(lenPrefixedData);
49989
49881
  }
49882
+ function sha256(input) {
49883
+ const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
49884
+ const lenPrefixedData = concatenate(inputLen, input);
49885
+ return hash$1(lenPrefixedData);
49886
+ }
49887
+ class RLNInstance {
49888
+ constructor(zkRLN, witnessCalculator) {
49889
+ this.zkRLN = zkRLN;
49890
+ this.witnessCalculator = witnessCalculator;
49891
+ this._contract = null;
49892
+ }
49893
+ get contract() {
49894
+ return this._contract;
49895
+ }
49896
+ async start(options = {}) {
49897
+ const provider = options.provider || (await extractMetaMaskAccount());
49898
+ const registryAddress = options.registryAddress || SEPOLIA_CONTRACT.address;
49899
+ this._contract = await RLNContract.init(this, {
49900
+ registryAddress,
49901
+ provider,
49902
+ });
49903
+ }
49904
+ generateIdentityCredentials() {
49905
+ const memKeys = generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
49906
+ return IdentityCredential.fromBytes(memKeys);
49907
+ }
49908
+ generateSeededIdentityCredential(seed) {
49909
+ const seedBytes = stringEncoder.encode(seed);
49910
+ // TODO: rename this function in zerokit rln-wasm
49911
+ const memKeys = generateSeededExtendedMembershipKey(this.zkRLN, seedBytes);
49912
+ return IdentityCredential.fromBytes(memKeys);
49913
+ }
49914
+ insertMember(idCommitment) {
49915
+ insertMember(this.zkRLN, idCommitment);
49916
+ }
49917
+ insertMembers(index, ...idCommitments) {
49918
+ // serializes a seq of IDCommitments to a byte seq
49919
+ // the order of serialization is |id_commitment_len<8>|id_commitment<var>|
49920
+ const idCommitmentLen = writeUIntLE(new Uint8Array(8), idCommitments.length, 0, 8);
49921
+ const idCommitmentBytes = concatenate(idCommitmentLen, ...idCommitments);
49922
+ setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
49923
+ }
49924
+ deleteMember(index) {
49925
+ deleteLeaf(this.zkRLN, index);
49926
+ }
49927
+ getMerkleRoot() {
49928
+ return getRoot(this.zkRLN);
49929
+ }
49930
+ serializeMessage(uint8Msg, memIndex, epoch, idKey) {
49931
+ // calculate message length
49932
+ const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
49933
+ // Converting index to LE bytes
49934
+ const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
49935
+ // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
49936
+ return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg);
49937
+ }
49938
+ async generateRLNProof(msg, index, epoch, idSecretHash) {
49939
+ if (epoch == undefined) {
49940
+ epoch = epochIntToBytes(dateToEpoch(new Date()));
49941
+ }
49942
+ else if (epoch instanceof Date) {
49943
+ epoch = epochIntToBytes(dateToEpoch(epoch));
49944
+ }
49945
+ if (epoch.length != 32)
49946
+ throw "invalid epoch";
49947
+ if (idSecretHash.length != 32)
49948
+ throw "invalid id secret hash";
49949
+ if (index < 0)
49950
+ throw "index must be >= 0";
49951
+ const serialized_msg = this.serializeMessage(msg, index, epoch, idSecretHash);
49952
+ const rlnWitness = getSerializedRLNWitness(this.zkRLN, serialized_msg);
49953
+ const inputs = RLNWitnessToJson(this.zkRLN, rlnWitness);
49954
+ const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false); // no sanity check being used in zerokit
49955
+ const proofBytes = generate_rln_proof_with_witness(this.zkRLN, calculatedWitness, rlnWitness);
49956
+ return new Proof(proofBytes);
49957
+ }
49958
+ verifyRLNProof(proof, msg) {
49959
+ let pBytes;
49960
+ if (proof instanceof Uint8Array) {
49961
+ pBytes = proof;
49962
+ }
49963
+ else {
49964
+ pBytes = proofToBytes(proof);
49965
+ }
49966
+ // calculate message length
49967
+ const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
49968
+ return verifyRLNProof(this.zkRLN, concatenate(pBytes, msgLen, msg));
49969
+ }
49970
+ verifyWithRoots(proof, msg, ...roots) {
49971
+ let pBytes;
49972
+ if (proof instanceof Uint8Array) {
49973
+ pBytes = proof;
49974
+ }
49975
+ else {
49976
+ pBytes = proofToBytes(proof);
49977
+ }
49978
+ // calculate message length
49979
+ const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
49980
+ const rootsBytes = concatenate(...roots);
49981
+ return verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg), rootsBytes);
49982
+ }
49983
+ verifyWithNoRoot(proof, msg) {
49984
+ let pBytes;
49985
+ if (proof instanceof Uint8Array) {
49986
+ pBytes = proof;
49987
+ }
49988
+ else {
49989
+ pBytes = proofToBytes(proof);
49990
+ }
49991
+ // calculate message length
49992
+ const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
49993
+ return verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg), new Uint8Array());
49994
+ }
49995
+ }
49996
+
49997
+ var rln = /*#__PURE__*/Object.freeze({
49998
+ __proto__: null,
49999
+ create: create,
50000
+ IdentityCredential: IdentityCredential,
50001
+ ProofMetadata: ProofMetadata,
50002
+ Proof: Proof,
50003
+ proofToBytes: proofToBytes,
50004
+ poseidonHash: poseidonHash,
50005
+ sha256: sha256,
50006
+ RLNInstance: RLNInstance
50007
+ });
49990
50008
 
49991
- export { IdentityCredential, Keystore, MerkleRootTracker, Proof, ProofMetadata, RLNContract, RLNDecoder, RLNEncoder, RLNInstance, RLN_REGISTRY_ABI, RLN_STORAGE_ABI, SEPOLIA_CONTRACT, create };
50009
+ export { IdentityCredential, Keystore, MerkleRootTracker, Proof, ProofMetadata, RLNContract, RLNDecoder, RLNEncoder, RLNInstance, RLN_REGISTRY_ABI, RLN_STORAGE_ABI, SEPOLIA_CONTRACT, createRLN };