UNPKG

o1js

Version:

TypeScript framework for zk-SNARKs and zkApps

75 lines (53 loc) 2.97 kB
import { Secp256k1, Ecdsa, keccakAndEcdsa, ecdsa, ecdsaEthers, Bytes32 } from './ecdsa.js'; import assert from 'assert'; // create an example ecdsa signature let privateKey = Secp256k1.Scalar.random(); let publicKey = Secp256k1.generator.scale(privateKey); let message = Bytes32.fromString("what's up"); let signature = Ecdsa.sign(message.toBytes(), privateKey.toBigInt()); // investigate the constraint system generated by ECDSA verify console.time('ecdsa verify only (build constraint system)'); let csEcdsa = await ecdsa.analyzeMethods(); console.timeEnd('ecdsa verify only (build constraint system)'); console.log(csEcdsa.verifySignedHash.summary()); console.time('keccak + ecdsa verify (build constraint system)'); let cs = await keccakAndEcdsa.analyzeMethods(); console.timeEnd('keccak + ecdsa verify (build constraint system)'); console.log(cs.verifyEcdsa.summary()); // compile and prove console.time('keccak + ecdsa verify (compile)'); await keccakAndEcdsa.compile(); console.timeEnd('keccak + ecdsa verify (compile)'); console.time('keccak + ecdsa verify (prove)'); let { proof } = await keccakAndEcdsa.verifyEcdsa(message, signature, publicKey); console.timeEnd('keccak + ecdsa verify (prove)'); proof.publicOutput.assertTrue('signature verifies'); assert(await keccakAndEcdsa.verify(proof), 'proof verifies'); // Hardcoded ethers.js signature and inputs for verification in o1js // message signed using ethers.js const msg = 'Secrets hidden, truth in ZKPs ;)'; // uncompressed public key generated by ethers.js const uncompressedPublicKey = '0x040957928494c38660d254dc03ba78f091a4aea0270afb447f193c4daf6648f02b720071af9b5bda4936998ec186e632f4be82886914851d7c753747b0a949d1a4'; // compressed public key generated by ethers.js const compressedPublicKey = '0x020957928494c38660d254dc03ba78f091a4aea0270afb447f193c4daf6648f02b'; // ECDSA signature generated by ethers.js const rawSignature = '0x6fada464c3bc2ae127f8c907c0c4bccbd05ba83a584156edb808b7400346b4c9558598d9c7869f5fd75d81128711f6621e4cb5ba2f52a2a51c46c859f49a833a1b'; const publicKeyE = Secp256k1.fromEthers(compressedPublicKey); const signatureE = Ecdsa.fromHex(rawSignature); const msgBytes = Bytes32.fromString(msg); // investigate the constraint system generated by ECDSA verifyEthers console.time('ethers verify only (build constraint system)'); let csEcdsaEthers = await ecdsaEthers.analyzeMethods(); console.timeEnd('ethers verify only (build constraint system)'); console.log(csEcdsaEthers.verifyEthers.summary()); // compile and prove console.time('ecdsa / ethers verify (compile)'); await ecdsaEthers.compile(); console.timeEnd('ecdsa / ethers verify (compile)'); console.time('ecdsa / ethers verify (prove)'); let { proof: proofE } = await ecdsaEthers.verifyEthers(msgBytes, signatureE, publicKeyE); console.timeEnd('ecdsa / ethers verify (prove)'); proofE.publicOutput.assertTrue('signature verifies'); assert(await ecdsaEthers.verify(proofE), 'proof verifies');