hollowdb-prover
Version:
Zero-knowledge proof generator utiity for HollowDB
1 lines • 4.46 kB
Source Map (JSON)
{"mappings":";;;;;;;;;;;;;;ACSA,MAAM,mCAAa,OAAO;AAC1B,MAAM,sCAAgB,IAAI,MAAM;AAU1B,MAAO;IAOX;;;;KAIG,GACH,MAAM,MACJ,QAAgB,EAChB,QAAiB,EACjB,SAAkB,EAHpB;QAKE,OAAO,MAAM,IAAI,CAAC,YAAY,UAAU,0CAAY,WAAW,0CAAY;IAC7E;IAEA;;;;;;KAMG,GACH,MAAM,YACJ,QAAgB,EAChB,YAAoB,EACpB,aAAqB,EAHvB;QAKE,IAAI,YAAY,kCACd,MAAM;QAGR,OAAO,MAAM,AAAC,CAAA,IAAI,CAAC,aAAa,YAAY,CAAA,GAAA,sBAAA,IAAU,CAAA,GAAA,oBAAA,CAAA,EAAO,UAC3D;sBACE;0BACA;2BACA;QACD,GACD,IAAI,CAAC,UACL,IAAI,CAAC;IAET;IA5CA,YACmB,QAAgB,EAChB,aAAqB,EAC7B,WAAgC,SAAS,CAHpD;QACmB,IAAA,CAAA,WAAA;QACA,IAAA,CAAA,gBAAA;QACR,IAAA,CAAA,WAAA;IACR;AAyCJ;AAUK,SAAU,0CAAY,KAAc;IACxC,IAAI,OACF,OAAO,OACL,OACE,CAAA,GAAA,wBAAA,EAAW,aACR,OAAO,OAAO,KAAK,KAAK,UAAU,SAClC,OAAO;SAGd,OAAO,OAAO;AAElB;AAMM,SAAU,0CAAW,QAAgB;IACzC,IAAI,YAAY,kCACd,MAAM;IAGR,OAAO,OAAO,CAAA,GAAA,sCAAA,EAAU;QAAC;KAAS,EAAE,SAAS;AAC/C","sources":["src/index.ts","index.ts"],"sourcesContent":["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",null],"names":[],"version":3,"file":"index.cjs.map","sourceRoot":"../"}