Skip to content

Commit

Permalink
add zkprogram example (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x471 committed Sep 10, 2024
1 parent b90b4e3 commit eb8d912
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"testw": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
"lint": "npx eslint src/* --fix",
"example": "npm run build && node build/src/ecdh-secp256k1.js"
"example": "npm run build && node build/src/run.js"
},
"devDependencies": {
"@babel/preset-env": "^7.16.4",
Expand Down
2 changes: 1 addition & 1 deletion src/ecdh-secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ECDHSecp256k1 {
* The shared secret is derived by performing scalar multiplication of the peer's public key with the local private key.
* This operation relies on the elliptic curve's property where both parties arrive at the same shared secret.
* Mathematically, the shared secret is computed as S = dP, where d is the local private key and P is the peer's public key.
*
*
* @param {CanonicalForeignField} privateKey - The local private key.
* @param {ForeignCurveV2} peersPublicKey - The public key of the peer.
* @returns {ForeignCurveV2} - The computed shared secret.
Expand Down
44 changes: 44 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { CanonicalForeignField, ForeignCurveV2, Provable, ZkProgram } from 'o1js';
import { ECDHSecp256k1, Secp256k1Curve } from './ecdh-secp256k1.js';

// Create a zkSNARK program for verifying the ECDH operation on Secp256k1
let ecdhVerificationProgram = ZkProgram({
name: 'ecdh-secp256k1-verification',
publicOutput: ForeignCurveV2,
methods: {
verifyECDHSecp256k1: {
privateInputs: [Secp256k1Curve.Scalar.Canonical, ForeignCurveV2],
async method(
userPrivateKey: CanonicalForeignField,
peersPublicKey: ForeignCurveV2,
) {
return ECDHSecp256k1.computeSharedSecret(userPrivateKey, peersPublicKey);
},
},
},
});

let { verifyECDHSecp256k1 } = await ecdhVerificationProgram.analyzeMethods();

console.log(verifyECDHSecp256k1.summary());

console.time('compile');
const forceRecompile = false;
await ecdhVerificationProgram.compile({ forceRecompile });
console.timeEnd('compile');

console.time('generate ECDH keys');
const ecdhInstance = new ECDHSecp256k1();
const { privateKey: alicePrivateKey, publicKey: alicePublicKey } = ecdhInstance.generateKey();
const { privateKey: bobPrivateKey, publicKey: bobPublicKey } = ecdhInstance.generateKey();
console.timeEnd('generate ECDH keys');

console.time('prove');
let proof = await ecdhVerificationProgram.verifyECDHSecp256k1(alicePrivateKey, bobPublicKey);
console.timeEnd('prove');

console.time('verify');
let isVerified = await ecdhVerificationProgram.verify(proof);
console.timeEnd('verify');

console.log(`Proof verified: ${isVerified}`);

0 comments on commit eb8d912

Please sign in to comment.