{"mappings":"AAYA;;;;;;;GAOG;AACH;IAII,QAAQ,CAAC,QAAQ,EAAE,SAAS,GAAG,OAAO;gBAFrB,QAAQ,EAAE,MAAM,EAChB,aAAa,EAAE,MAAM,EAC7B,QAAQ,GAAE,SAAS,GAAG,OAAmB;IAGpD;;;;OAIG;IACG,KAAK,CACT,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,EAAE,OAAO,GACjB,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;KAAC,CAAC;IAItG;;;;;;OAMG;IACG,WAAW,CACf,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;KAAC,CAAC;CAevG;AAED;;;;;;;GAOG;AACH,4BAA4B,KAAK,EAAE,OAAO,GAAG,MAAM,CAWlD;AAED;;;GAGG;AACH,2BAA2B,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMnD","sources":["src/src/index.ts","src/index.ts"],"sourcesContent":[null,"import {createHash} from 'crypto';\nimport {poseidon1} from 'poseidon-lite/poseidon1';\n\n// we need to import like this due to a bug\n// https://vivianblog.hashnode.dev/how-to-create-a-zero-knowledge-dapp-from-zero-to-production\n// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport {groth16, plonk} from 'snarkjs';\n\nconst bn254Prime = BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617');\nconst TooLargeError = new Error('Preimage is larger than the order of scalar field of BN254.');\n\n/** A zero-knowledge prover utility to be used with HollowDB.\n *\n * You will need to provide paths to a WASM circuit, and a prover key.\n * You can find these files [here](./circuits/) in the repository. it is up to you to decide where to place them for your application.\n * For example, in a web-app you may place under the `public` directory.\n *\n * You can also choose to provide the protocol, which defaults to Groth16.\n */\nexport class Prover {\n  constructor(\n    private readonly wasmPath: string,\n    private readonly proverKeyPath: string,\n    readonly protocol: 'groth16' | 'plonk' = 'groth16'\n  ) {}\n\n  /** Generate a zero-knowledge proof.\n   *\n   * Calls {@link hashToGroup} on inputs, and then generates\n   * a proof with {@link proveHashed}.\n   */\n  async prove(\n    preimage: bigint,\n    curValue: unknown,\n    nextValue: unknown\n  ): Promise<{proof: object; publicSignals: [curValueHash: string, nextValueHash: string, key: string]}> {\n    return await this.proveHashed(preimage, hashToGroup(curValue), hashToGroup(nextValue));\n  }\n\n  /** Generate a zero-knowledge proof.\n   *\n   * Value inputs are expected to be results of {@link hashToGroup}. The\n   * incentive of using this function instead of {@link prove} is that the\n   * hash may be stored somewhere, and there is no need to hash the entire value\n   * again at a later time instead of using the existing hash.\n   */\n  async proveHashed(\n    preimage: bigint,\n    curValueHash: bigint,\n    nextValueHash: bigint\n  ): Promise<{proof: object; publicSignals: [curValueHash: string, nextValueHash: string, key: string]}> {\n    if (preimage >= bn254Prime) {\n      throw TooLargeError;\n    }\n\n    return await (this.protocol === 'groth16' ? groth16 : plonk).fullProve(\n      {\n        preimage,\n        curValueHash,\n        nextValueHash,\n      },\n      this.wasmPath,\n      this.proverKeyPath\n    );\n  }\n}\n\n/** Given an input, stringifies and then hashes it and make sure the result is circuit-friendly for\n * [BN254](https://docs.circom.io/background/background/#signals-of-a-circuit).\n *\n * Uses Ripemd160 for the hash where 160-bit output is guaranteed to be\n * circuit-friendly (i.e. within the order of the curve's scalar field).\n *\n * If a given value is falsy, it will NOT be hashed but instead mapped to 0.\n */\nexport function hashToGroup(value: unknown): bigint {\n  if (value) {\n    return BigInt(\n      '0x' +\n        createHash('ripemd160')\n          .update(Buffer.from(JSON.stringify(value)))\n          .digest('hex')\n    );\n  } else {\n    return BigInt(0);\n  }\n}\n\n/** Compute the key that is the Poseidon hash of some preimage.\n *\n * The returned key is a string in hexadecimal format with 0x prefix.\n */\nexport function computeKey(preimage: bigint): string {\n  if (preimage >= bn254Prime) {\n    throw TooLargeError;\n  }\n\n  return '0x' + poseidon1([preimage]).toString(16);\n}\n"],"names":[],"version":3,"file":"index.d.ts.map","sourceRoot":"../"}