Skip to content

Instantly share code, notes, and snippets.

View johnson86tw's full-sized avatar
🥝

Johnson johnson86tw

🥝
View GitHub Profile
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 + "]");
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 {
template Square() {
signal input in;
signal output out;
out <== in * in;
}
template Add() {
signal input in;
signal output out;
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;
...
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);
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[];
proof = {
root: "117086588437903260255921638146322982",
pathElements: [
'40',
'1978699832413026196628606142134435304247'
],
pathIndices: [ 0, 1 ],
leaf: '30'
}
// 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
@johnson86tw
johnson86tw / MerkleTree.ts
Last active January 10, 2023 05:02
A TypeScript implementation of Merkle Tree rewrite from tornado-core: https://github.com/tornadocash/tornado-core/blob/master/lib/MerkleTree.js
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) => {
pragma solidity ^0.7.0;
// 這是一個叫做 Token 的合約
contract Token {
// 貨幣的名稱叫做...
string public name = "My Awesome Token";
// 貨幣的代號是...
string public symbol = "MAT";