Skip to content

Commit

Permalink
Merge pull request #11 from zkemail/feat/prover-api
Browse files Browse the repository at this point in the history
Feat/prover api
  • Loading branch information
jp4g authored Oct 3, 2024
2 parents ba34e7a + 166c84f commit 4324fb8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 69 deletions.
6 changes: 4 additions & 2 deletions examples/partial_hash/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ global MAX_PARTIAL_EMAIL_BODY_LENGTH: u32 = 192;
* @param body_hash_index - The index of the body hash in the partial hash array
* @param header - The email header, 0-padded at end to the MAX_EMAIL_HEADER_LENGTH
* @param header_length - The actual length of the email header
* @param body - The email body, 0-padded at end to the MAX_EMAIL_BODY_LENGTH
* @param body_length - The actual length of the email body
* @param body - The remaining email body, 0-padded at end to the MAX_PARTIAL_EMAIL_BODY_LENGTH
* @param body_length - The full length of the email body
* @param partial_body_hash - The SHA256 state of the partially hashed body
* @param partial_body_length - The remaining length of the email body to hash
* @param pubkey_modulus_limbs - The DKIM RSA Pubkey
* @param redc_params_limbs - Barrett Reduction Parameter for Pubkey for efficient signature verification
* @param signature - The DKIM RSA Signature
Expand Down
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mach-34/zkemail-nr",
"version": "1.0.5",
"version": "1.1.0",
"main": "dist",
"types": "dist",
"license": "MIT",
Expand Down
55 changes: 28 additions & 27 deletions js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,30 @@ export type InputGenerationArgs = {
};

// copied without modification, but not publicly exported in original
function removeSoftLineBreaks(body: string[]): string[] {
const result = [];
let i = 0;
while (i < body.length) {
if (
i + 2 < body.length &&
body[i] === "61" && // '=' character
body[i + 1] === "13" && // '\r' character
body[i + 2] === "10"
) {
// '\n' character
// Skip the soft line break sequence
i += 3; // Move past the soft line break
} else {
result.push(body[i]);
i++;
}
}
// Pad the result with zeros to make it the same length as the body
while (result.length < body.length) {
result.push("0");
}
return result;
}
// function removeSoftLineBreaks(body: string[]): string[] {
// const result = [];
// let i = 0;
// while (i < body.length) {
// if (
// i + 2 < body.length &&
// body[i] === "61" && // '=' character
// body[i + 1] === "13" && // '\r' character
// body[i + 2] === "10"
// ) {
// // '\n' character
// // Skip the soft line break sequence
// i += 3; // Move past the soft line break
// } else {
// result.push(body[i]);
// i++;
// }
// }
// // Pad the result with zeros to make it the same length as the body
// while (result.length < body.length) {
// result.push("0");
// }
// return result;
// }

// copied without modification, needed for different generateEmailVerifierInnputsFromDKIMResult
/**
Expand Down Expand Up @@ -130,15 +130,16 @@ export function generateEmailVerifierInputsFromDKIMResult(
Math.max(maxBodyLength, bodySHALength)
);

let { precomputedSha, bodyRemaining, bodyRemainingLength } = generatePartialSHA({
const { precomputedSha, bodyRemainingLength, ...rest } = generatePartialSHA({
body: bodyPadded,
bodyLength: bodyPaddedLen,
selectorString: params.shaPrecomputeSelector,
maxRemainingBodyLength: maxBodyLength,
});

// code smell but it passes the linter
let { bodyRemaining } = rest;
// idk why this gets out of sync, todo: fix
if (params.shaPrecomputeSelector && bodyRemaining.length != bodyRemainingLength) {
if (params.shaPrecomputeSelector && bodyRemaining.length !== bodyRemainingLength) {
bodyRemaining = bodyRemaining.slice(0, bodyRemainingLength);
}

Expand Down
67 changes: 29 additions & 38 deletions js/src/prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type ProvingBackend = "honk" | "plonk" | "all";

export class ZKEmailProver {
private plonk?: BarretenbergBackend;

private honk?: UltraHonkBackend;

private noir: Noir;

constructor(
Expand Down Expand Up @@ -41,7 +43,7 @@ export class ZKEmailProver {
async simulateWitness(
input: CircuitInput
): Promise<{ witness: Uint8Array; returnValue: InputValue }> {
return await this.noir.execute(input);
return this.noir.execute(input);
}

/**
Expand All @@ -57,27 +59,22 @@ export class ZKEmailProver {
): Promise<ProofData> {
// determine proving backend to use
let backend: BarretenbergBackend | UltraHonkBackend;
if (provingBackend) {
// check that the asserted backend is initialized
if (provingBackend === "plonk" && this.plonk) {
backend = this.plonk;
} else if (provingBackend === "honk" && this.honk) {
backend = this.honk;
} else {
throw new Error(`Proving scheme ${provingBackend} not initialized`);
}
if (
(provingBackend && this.plonk) ||
(this.provingBackend === "plonk" && this.plonk)
) {
backend = this.plonk;
} else if (
(provingBackend === "honk" && this.honk) ||
(this.provingBackend === "honk" && this.honk)
) {
backend = this.honk;
} else {
// default to the backend used to initialize the class
if (this.provingBackend === "honk" && this.honk) {
backend = this.honk;
} else if (this.provingBackend === "plonk" && this.plonk) {
backend = this.plonk;
} else {
throw new Error(`Proving scheme ${this.provingBackend} not initialized`);
}
throw new Error(`Proving scheme ${this.provingBackend} not initialized`);
}

// generate the proof
return await backend.generateProof(witness);
return backend.generateProof(witness);
}

/**
Expand All @@ -92,7 +89,7 @@ export class ZKEmailProver {
provingBackend?: ProvingBackend
): Promise<ProofData> {
const { witness } = await this.simulateWitness(input);
return await this.prove(witness, provingBackend);
return this.prove(witness, provingBackend);
}

/**
Expand All @@ -108,27 +105,21 @@ export class ZKEmailProver {
): Promise<boolean> {
// determine proving backend to use
let backend: BarretenbergBackend | UltraHonkBackend;
if (provingBackend) {
// check that the asserted backend is initialized
if (provingBackend === "plonk" && this.plonk) {
backend = this.plonk;
} else if (provingBackend === "honk" && this.honk) {
backend = this.honk;
} else {
throw new Error(`Proving scheme ${provingBackend} not initialized`);
}
if (
(provingBackend && this.plonk) ||
(this.provingBackend === "plonk" && this.plonk)
) {
backend = this.plonk;
} else if (
(provingBackend === "honk" && this.honk) ||
(this.provingBackend === "honk" && this.honk)
) {
backend = this.honk;
} else {
// default to the backend used to initialize the class
if (this.provingBackend === "honk" && this.honk) {
backend = this.honk;
} else if (this.provingBackend === "plonk" && this.plonk) {
backend = this.plonk;
} else {
throw new Error(`Proving scheme ${this.provingBackend} not initialized`);
}
throw new Error(`Proving scheme ${this.provingBackend} not initialized`);
}
// verify the proof
return await backend.verifyProof(proof);
return backend.verifyProof(proof);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Transforms a u32 array to a u8 array
* @dev sha-utils in zk-email-verify encodes partial hash as u8 array but noir expects u32
* transform back to keep upstream code but not have noir worry about transformation
*
*
* @param input - the input to convert to 32 bit array
* @returns - the input as a 32 bit array
*/
Expand Down

0 comments on commit 4324fb8

Please sign in to comment.