Skip to content

Commit

Permalink
Add more zkProgram variations for emailVerify
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed Jul 7, 2024
1 parent c3c2343 commit 9910d1d
Showing 1 changed file with 98 additions and 31 deletions.
129 changes: 98 additions & 31 deletions src/zkProgram.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
//! Note: Compiling and generating proofs for the existing `emailVerify` circuits is not possible
//! because the total number of rows far exceeds the 2^16 constraint limit of o1js circuits.

import { Field, ZkProgram, Bytes } from 'o1js';
import { Bigint2048 } from 'o1js-rsa';
import { generateInputs } from './generate-inputs.js';
import { emailVerify } from './email-verify.js';
import fs from 'fs';

const filePath = './eml/email.eml';
const rawEmail = fs.readFileSync(filePath, 'utf8');

const inputs = await generateInputs(rawEmail);
class Bytes32 extends Bytes(32) {}

class HeadersBytes extends Bytes(1024) {}
class BodyBytes extends Bytes(1536) {}
class Bytes32 extends Bytes(32) {}
class BodyBytes1536 extends Bytes(1536) {}
class BodyBytes1024 extends Bytes(1024) {}

let verifyEmailZkProgram = ZkProgram({
name: 'verify-email',
methods: {
verifyEmail: {
verifyEmailNoBodyCheck: {
privateInputs: [
HeadersBytes.provable,
Field,
Bigint2048,
Bigint2048,
BodyBytes.provable,
BodyBytes1536.provable,
Bytes32.provable,
Field,
Field,
],

async method(
paddedHeader: Bytes,
headerHashIndex: Field,
signature: Bigint2048,
publicKey: Bigint2048,
paddedBodyRemainingBytes: Bytes,
precomputedHash: Bytes,
bodyHashIndex: Field,
headerBodyHashIndex: Field
) {
emailVerify(
paddedHeader,
headerHashIndex,
signature,
publicKey,
1024,
false,
paddedBodyRemainingBytes,
precomputedHash,
bodyHashIndex,
headerBodyHashIndex
);
},
},

verifyEmailBodyCheck1024: {
privateInputs: [
HeadersBytes.provable,
Field,
Bigint2048,
Bigint2048,
BodyBytes1024.provable,
Bytes32.provable,
Field,
Field,
Expand Down Expand Up @@ -52,31 +87,63 @@ let verifyEmailZkProgram = ZkProgram({
);
},
},
},
});

let { verifyEmail } = await verifyEmailZkProgram.analyzeMethods();
verifyEmailBodyCheck1536: {
privateInputs: [
HeadersBytes.provable,
Field,
Bigint2048,
Bigint2048,
BodyBytes1536.provable,
Bytes32.provable,
Field,
Field,
],

console.log(verifyEmail.summary());
async method(
paddedHeader: Bytes,
headerHashIndex: Field,
signature: Bigint2048,
publicKey: Bigint2048,
paddedBodyRemainingBytes: Bytes,
precomputedHash: Bytes,
bodyHashIndex: Field,
headerBodyHashIndex: Field
) {
emailVerify(
paddedHeader,
headerHashIndex,
signature,
publicKey,
1024,
true,
paddedBodyRemainingBytes,
precomputedHash,
bodyHashIndex,
headerBodyHashIndex
);
},
},
},
});

console.time('compile');
await verifyEmailZkProgram.compile();
console.timeEnd('compile');
const {
verifyEmailNoBodyCheck,
verifyEmailBodyCheck1536,
verifyEmailBodyCheck1024,
} = await verifyEmailZkProgram.analyzeMethods();

console.time('prove');
console.log(
'verifyEmailNoBodyCheck summary: ',
verifyEmailNoBodyCheck.summary()
);

let proof = await verifyEmailZkProgram.verifyEmail(
inputs.paddedHeader,
inputs.headerHashIndex,
inputs.signature,
inputs.publicKey,
inputs.paddedBodyRemainingBytes,
inputs.precomputedHash,
inputs.bodyHashIndex,
inputs.headerBodyHashIndex
console.log(
'\nverifyEmailBodyCheck1024 summary: ',
verifyEmailBodyCheck1024.summary()
);
console.timeEnd('prove');

console.time('verify');
await verifyEmailZkProgram.verify(proof);
console.timeEnd('verify');
console.log(
'\nverifyEmailBodyCheck1536 summary: ',
verifyEmailBodyCheck1536.summary()
);

0 comments on commit 9910d1d

Please sign in to comment.