This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe("ZK", function () { | |
| it("should process", async () => { | |
| const secret = 42; | |
| const input = { secret }; | |
| const output = secret * secret + 6; | |
| const publicSignals = [BigInt(output)]; | |
| let { proof } = await groth16.fullProve(input, wasmPath, zkeyPath); | |
| const calldata = await groth16.exportSolidityCallData(unstringifyBigInts(proof), publicSignals); | |
| const args = JSON.parse("[" + calldata + "]"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface IVerifier { | |
| function verifyProof( | |
| uint256[2] memory a, | |
| uint256[2][2] memory b, | |
| uint256[2] memory c, | |
| uint256[1] memory input // publicSignals | |
| ) external view returns (bool r); | |
| } | |
| contract ZK { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template Square() { | |
| signal input in; | |
| signal output out; | |
| out <== in * in; | |
| } | |
| template Add() { | |
| signal input in; | |
| signal output out; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| contract WithoutZK { | |
| string greeting = "hello world"; | |
| uint256 answer = 1770; | |
| function greet() public view returns (string memory) { | |
| return greeting; | |
| } | |
| function _setGreeting(string memory _greeting) internal { | |
| greeting = _greeting; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| proof(indexOfLeaf: number) { | |
| let pathElements: string[] = []; | |
| let pathIndices: number[] = []; | |
| const leaf = this.storage.get(MerkleTree.indexToKey(0, indexOfLeaf)); | |
| if (!leaf) throw new Error("leaf not found"); | |
| // store sibling into pathElements and target's indices into pathIndices | |
| const handleIndex = (level: number, currentIndex: number, siblingIndex: number) => { | |
| const siblingValue = this.storage.get(MerkleTree.indexToKey(level, siblingIndex)) || this.zeros[level]; | |
| pathElements.push(siblingValue); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const circomlib = require("circomlib"); | |
| const mimcsponge = circomlib.mimcsponge; | |
| export function MiMCSponge(left: string, right: string): string { | |
| return mimcsponge.multiHash([BigInt(left), BigInt(right)]).toString(); | |
| } | |
| export interface IMerkleTree { | |
| root: () => string; | |
| proof: (index: number) => { | |
| root: string; | |
| pathElements: string[]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| proof = { | |
| root: "117086588437903260255921638146322982", | |
| pathElements: [ | |
| '40', | |
| '1978699832413026196628606142134435304247' | |
| ], | |
| pathIndices: [ 0, 1 ], | |
| leaf: '30' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // prover | |
| const levels = 2; | |
| const leaves = [10, 20 ,30 ,40]; | |
| const tree = new MerkleTree(levels, leaves); | |
| const index = 2; | |
| const proof = tree.proof(index); | |
| // verifier | |
| const root = "117086588437903260255921638146322982" | |
| console.log(verify(proof, root)); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const circomlib = require("circomlib"); | |
| const mimcsponge = circomlib.mimcsponge; | |
| export function MiMCSponge(left: string, right: string): string { | |
| return mimcsponge.multiHash([BigInt(left), BigInt(right)]).toString(); | |
| } | |
| export interface IMerkleTree { | |
| root: () => string; | |
| proof: (index: number) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity ^0.7.0; | |
| // 這是一個叫做 Token 的合約 | |
| contract Token { | |
| // 貨幣的名稱叫做... | |
| string public name = "My Awesome Token"; | |
| // 貨幣的代號是... | |
| string public symbol = "MAT"; |