From 8902a58308eaf8926e1480ebea1dd7030e81c8e1 Mon Sep 17 00:00:00 2001 From: Jack Gilcrest Date: Thu, 28 Nov 2024 20:48:18 +0700 Subject: [PATCH 1/5] hash redc with dkim, compose two u120 limbs into one felt to offset doubled hash cost --- lib/src/lib.nr | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/src/lib.nr b/lib/src/lib.nr index 7f96813..81b5c68 100644 --- a/lib/src/lib.nr +++ b/lib/src/lib.nr @@ -43,18 +43,26 @@ global EMAIL_ADDRESS_CHAR_TABLE: [u8; 123] = [ * Standard outputs that essentially every email circuit will need to export (alongside app-specific outputs) * @notice if you only need the pubkey hash just import pedersen and hash away * - * @param pubkey - the BN limbs of the DKIM RSA pubkey + * @param pubkey - the pubkey redc and modulus limbs * @param signature - the BN limbs of the DKIM RSA signature * @returns * 0: Pedersen hash of DKIM public key (root of trust) * 1: Pedersen hash of DKIM signature (email nullifier) */ -pub fn standard_outputs( - pubkey: [Field; KEY_BYTE_LENGTH], - signature: [Field; KEY_BYTE_LENGTH], +pub fn standard_outputs( + pubkey: RSAPubkey, + signature: [Field; KEY_LIMBS], ) -> [Field; 2] { // create pedersen hash of DKIM signing key to minimize public outputs - let pubkey_hash = pedersen_hash(pubkey); + let mut dkim_preimage: [Field; 18] = [0; 18]; + + for i in 0..9 { + let modulus_hi = pubkey.modulus[i * 2] * 2.pow_32(120); + let redc_hi = pubkey.redc[i * 2] * 2.pow_32(120); + dkim_preimage[i] = modulus_hi + pubkey.modulus[i * 2 + 1]; + dkim_preimage[i + 9] = redc_hi + pubkey.redc[i * 2 + 1]; + } + let pubkey_hash = pedersen_hash(dkim_preimage); // create email nullifier for email let email_nullifier = pedersen_hash(signature); // output the root of trust and email nullifier From 0fa8ce4c36bef7a4479a305ad8906751edd4444b Mon Sep 17 00:00:00 2001 From: Jack Gilcrest Date: Thu, 28 Nov 2024 21:16:19 +0700 Subject: [PATCH 2/5] fix to work with 1024 bit dkim --- lib/src/dkim.nr | 33 +++++++++++++++++++++++++++++---- lib/src/lib.nr | 30 ------------------------------ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/lib/src/dkim.nr b/lib/src/dkim.nr index 38071dc..31d70e5 100644 --- a/lib/src/dkim.nr +++ b/lib/src/dkim.nr @@ -13,10 +13,6 @@ impl RSAPubkey { pub fn new(modulus: [Field; KEY_LIMBS], redc: [Field; KEY_LIMBS]) -> Self { Self { modulus, redc } } - - pub fn hash(self) -> Field { - pedersen_hash(self.modulus) - } } impl RSAPubkey { @@ -36,6 +32,22 @@ impl RSAPubkey { // verify the DKIM signature over the header assert(verify_sha256_pkcs1v15(header_hash, signature, RSA_EXPONENT)); } + + pub fn hash(self) -> Field { + let mut dkim_preimage = [0; 9]; + // compose first 4 limbs of modulus and redc + for i in 0..4 { + let modulus_hi = self.modulus[i * 2] * 2.pow_32(120); + let redc_hi = self.redc[i * 2] * 2.pow_32(120); + dkim_preimage[i] = modulus_hi + self.modulus[i * 2 + 1]; + dkim_preimage[i + 4] = redc_hi + self.redc[i * 2 + 1]; + } + // compose last two elements of redc and modulus together + let modulus_hi = self.modulus[8] * 2.pow_32(120); + dkim_preimage[8] = modulus_hi + self.redc[8]; + // hash the pubkey + pedersen_hash(dkim_preimage) + } } impl RSAPubkey { @@ -55,4 +67,17 @@ impl RSAPubkey { // verify the DKIM signature over the header assert(verify_sha256_pkcs1v15(header_hash, signature, RSA_EXPONENT)); } + + pub fn hash(self) -> Field { + let mut dkim_preimage = [0; 18]; + // compose limbs + for i in 0..18 { + let modulus_hi = self.modulus[i * 2] * 2.pow_32(120); + let redc_hi = self.redc[i * 2] * 2.pow_32(120); + dkim_preimage[i] = modulus_hi + self.modulus[i * 2 + 1]; + dkim_preimage[i + 9] = redc_hi + self.redc[i * 2 + 1]; + } + // hash the pubkey + pedersen_hash(dkim_preimage) + } } diff --git a/lib/src/lib.nr b/lib/src/lib.nr index 81b5c68..550454a 100644 --- a/lib/src/lib.nr +++ b/lib/src/lib.nr @@ -39,36 +39,6 @@ global EMAIL_ADDRESS_CHAR_TABLE: [u8; 123] = [ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]; -/** - * Standard outputs that essentially every email circuit will need to export (alongside app-specific outputs) - * @notice if you only need the pubkey hash just import pedersen and hash away - * - * @param pubkey - the pubkey redc and modulus limbs - * @param signature - the BN limbs of the DKIM RSA signature - * @returns - * 0: Pedersen hash of DKIM public key (root of trust) - * 1: Pedersen hash of DKIM signature (email nullifier) - */ -pub fn standard_outputs( - pubkey: RSAPubkey, - signature: [Field; KEY_LIMBS], -) -> [Field; 2] { - // create pedersen hash of DKIM signing key to minimize public outputs - let mut dkim_preimage: [Field; 18] = [0; 18]; - - for i in 0..9 { - let modulus_hi = pubkey.modulus[i * 2] * 2.pow_32(120); - let redc_hi = pubkey.redc[i * 2] * 2.pow_32(120); - dkim_preimage[i] = modulus_hi + pubkey.modulus[i * 2 + 1]; - dkim_preimage[i + 9] = redc_hi + pubkey.redc[i * 2 + 1]; - } - let pubkey_hash = pedersen_hash(dkim_preimage); - // create email nullifier for email - let email_nullifier = pedersen_hash(signature); - // output the root of trust and email nullifier - [pubkey_hash, email_nullifier] -} - /** * Default email verification function * @dev use #[zkemail] attribute macro to apply other functionality From ae8357de17522911789a76395581a938ff4d23c5 Mon Sep 17 00:00:00 2001 From: Jack Gilcrest Date: Thu, 28 Nov 2024 21:20:31 +0700 Subject: [PATCH 3/5] update examples --- examples/email_mask/src/main.nr | 7 +++--- examples/extract_addresses/src/main.nr | 23 ++++++++----------- examples/partial_hash/src/main.nr | 6 +++-- examples/remove_soft_line_breaks/src/main.nr | 16 +++++++------ .../verify_email_1024_bit_dkim/src/main.nr | 7 +++--- .../verify_email_2048_bit_dkim/src/main.nr | 7 +++--- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/examples/email_mask/src/main.nr b/examples/email_mask/src/main.nr index f02bbae..1f82190 100644 --- a/examples/email_mask/src/main.nr +++ b/examples/email_mask/src/main.nr @@ -1,8 +1,8 @@ use dep::zkemail::{ KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash, - standard_outputs, Sequence, masking::mask_text + Sequence, masking::mask_text }; -use dep::std::{collections::bounded_vec::BoundedVec, hash::sha256_var}; +use dep::std::{collections::bounded_vec::BoundedVec, hash::{pedersen_hash, sha256_var}}; global MAX_EMAIL_HEADER_LENGTH: u32 = 512; global MAX_EMAIL_BODY_LENGTH: u32 = 1024; @@ -55,6 +55,7 @@ fn main( let masked_body = mask_text(body, body_mask); // hash the pubkey and signature for the standard outputs - let standard_out = standard_outputs(pubkey.modulus, signature); + let email_nullifier = pedersen_hash(signature); + let standard_out = [pubkey.hash(), email_nullifier]; (standard_out, masked_header, masked_body) } diff --git a/examples/extract_addresses/src/main.nr b/examples/extract_addresses/src/main.nr index e15ed3d..6de8825 100644 --- a/examples/extract_addresses/src/main.nr +++ b/examples/extract_addresses/src/main.nr @@ -1,12 +1,10 @@ use dep::zkemail::{ - KEY_LIMBS_2048, dkim::RSAPubkey, - headers::{body_hash::get_body_hash, email_address::get_email_address}, standard_outputs, Sequence, - MAX_EMAIL_ADDRESS_LENGTH + KEY_LIMBS_2048, dkim::RSAPubkey, headers::email_address::get_email_address, Sequence, + MAX_EMAIL_ADDRESS_LENGTH, }; -use dep::std::{collections::bounded_vec::BoundedVec, hash::sha256_var}; +use dep::std::{collections::bounded_vec::BoundedVec, hash::pedersen_hash}; global MAX_EMAIL_HEADER_LENGTH: u32 = 512; -global MAX_EMAIL_BODY_LENGTH: u32 = 1024; /** * Verify an arbitrary email signed by a 2048-bit RSA DKIM signature and extract sender and recipient addresses @@ -30,8 +28,8 @@ fn main( from_header_sequence: Sequence, from_address_sequence: Sequence, to_header_sequence: Sequence, - to_address_sequence: Sequence -) -> pub ([Field; 2], BoundedVec, BoundedVec) { + to_address_sequence: Sequence, + ) -> pub ([Field; 2], BoundedVec, BoundedVec) { // check the body and header lengths are within bounds assert(header.len() <= MAX_EMAIL_HEADER_LENGTH); @@ -39,17 +37,14 @@ fn main( pubkey.verify_dkim_signature(header, signature); // extract to and from email addresses - let from = comptime { - "from".as_bytes() - }; - let to = comptime { - "to".as_bytes() - }; + let from = comptime { "from".as_bytes() }; + let to = comptime { "to".as_bytes() }; // 16k gate cost? has to be able to be brought down let from_address = get_email_address(header, from_header_sequence, from_address_sequence, from); let to_address = get_email_address(header, to_header_sequence, to_address_sequence, to); // hash the pubkey and signature for the standard outputs - let standard_out = standard_outputs(pubkey.modulus, signature); + let email_nullifier = pedersen_hash(signature); + let standard_out = [pubkey.hash(), email_nullifier]; (standard_out, from_address, to_address) } diff --git a/examples/partial_hash/src/main.nr b/examples/partial_hash/src/main.nr index aa4c3aa..28caf9c 100644 --- a/examples/partial_hash/src/main.nr +++ b/examples/partial_hash/src/main.nr @@ -1,7 +1,8 @@ use dep::zkemail::{ KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash, - partial_hash::partial_sha256_var_end, standard_outputs, Sequence + partial_hash::partial_sha256_var_end, Sequence }; +use std::hash::pedersen_hash; global MAX_EMAIL_HEADER_LENGTH: u32 = 512; global MAX_PARTIAL_EMAIL_BODY_LENGTH: u32 = 192; @@ -52,5 +53,6 @@ fn main( ); // hash the pubkey and signature for the standard outputs - standard_outputs(pubkey.modulus, signature) + let email_nullifier = pedersen_hash(signature); + [pubkey.hash(), email_nullifier] } diff --git a/examples/remove_soft_line_breaks/src/main.nr b/examples/remove_soft_line_breaks/src/main.nr index db255c1..3262214 100644 --- a/examples/remove_soft_line_breaks/src/main.nr +++ b/examples/remove_soft_line_breaks/src/main.nr @@ -1,8 +1,8 @@ use zkemail::{ - KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash, - standard_outputs, Sequence, remove_soft_line_breaks::remove_soft_line_breaks + KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash, Sequence, + remove_soft_line_breaks::remove_soft_line_breaks, }; -use std::hash::sha256_var; +use std::hash::{pedersen_hash, sha256_var}; global MAX_EMAIL_HEADER_LENGTH: u32 = 512; global MAX_EMAIL_BODY_LENGTH: u32 = 1024; @@ -28,7 +28,7 @@ fn main( pubkey: RSAPubkey, signature: [Field; KEY_LIMBS_2048], body_hash_index: u32, - dkim_header_sequence: Sequence + dkim_header_sequence: Sequence, ) -> pub [Field; 2] { // check the body and header lengths are within bounds assert(header.len() <= MAX_EMAIL_HEADER_LENGTH); @@ -48,17 +48,19 @@ fn main( // compare the body hashes assert( - signed_body_hash == computed_body_hash, "SHA256 hash computed over body does not match body hash found in DKIM-signed header" + signed_body_hash == computed_body_hash, + "SHA256 hash computed over body does not match body hash found in DKIM-signed header", ); // ~ 37,982 constraints // ensure the decoded body is the same as the original body assert( remove_soft_line_breaks(body.storage(), decoded_body.storage()), - "Decoded body does not properly remove soft line breaks" + "Decoded body does not properly remove soft line breaks", ); // ~ 10,255 constraints // hash the pubkey and signature for the standard outputs - standard_outputs(pubkey.modulus, signature) + let email_nullifier = pedersen_hash(signature); + [pubkey.hash(), email_nullifier] } diff --git a/examples/verify_email_1024_bit_dkim/src/main.nr b/examples/verify_email_1024_bit_dkim/src/main.nr index a759fab..d9534e3 100644 --- a/examples/verify_email_1024_bit_dkim/src/main.nr +++ b/examples/verify_email_1024_bit_dkim/src/main.nr @@ -1,8 +1,8 @@ use dep::zkemail::{ KEY_LIMBS_1024, dkim::RSAPubkey, headers::body_hash::get_body_hash, - standard_outputs, Sequence + Sequence }; -use dep::std::{collections::bounded_vec::BoundedVec, hash::sha256_var}; +use dep::std::{collections::bounded_vec::BoundedVec, hash::{sha256_var, pedersen_hash}}; global MAX_EMAIL_HEADER_LENGTH: u32 = 512; global MAX_EMAIL_BODY_LENGTH: u32 = 1024; @@ -48,5 +48,6 @@ fn main( ); // hash the pubkey and signature for the standard outputs - standard_outputs(pubkey.modulus, signature) + let email_nullifier = pedersen_hash(signature); + [pubkey.hash(), email_nullifier] } diff --git a/examples/verify_email_2048_bit_dkim/src/main.nr b/examples/verify_email_2048_bit_dkim/src/main.nr index 76291c8..b498313 100644 --- a/examples/verify_email_2048_bit_dkim/src/main.nr +++ b/examples/verify_email_2048_bit_dkim/src/main.nr @@ -1,8 +1,8 @@ use dep::zkemail::{ KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash, - standard_outputs, Sequence + Sequence }; -use dep::std::{collections::bounded_vec::BoundedVec, hash::sha256_var}; +use dep::std::{collections::bounded_vec::BoundedVec, hash::{sha256_var, pedersen_hash}}; global MAX_EMAIL_HEADER_LENGTH: u32 = 512; global MAX_EMAIL_BODY_LENGTH: u32 = 1024; @@ -52,5 +52,6 @@ fn main( // ~ 10,255 constraints // hash the pubkey and signature for the standard outputs - standard_outputs(pubkey.modulus, signature) + let email_nullifier = pedersen_hash(signature); + [pubkey.hash(), email_nullifier] } From e6be1acba6d8e1ab3ca4c1c2524c92df1acb6b68 Mon Sep 17 00:00:00 2001 From: Jack Gilcrest Date: Thu, 28 Nov 2024 21:21:19 +0700 Subject: [PATCH 4/5] fix packing error --- lib/src/dkim.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/dkim.nr b/lib/src/dkim.nr index 31d70e5..4d76470 100644 --- a/lib/src/dkim.nr +++ b/lib/src/dkim.nr @@ -71,7 +71,7 @@ impl RSAPubkey { pub fn hash(self) -> Field { let mut dkim_preimage = [0; 18]; // compose limbs - for i in 0..18 { + for i in 0..9 { let modulus_hi = self.modulus[i * 2] * 2.pow_32(120); let redc_hi = self.redc[i * 2] * 2.pow_32(120); dkim_preimage[i] = modulus_hi + self.modulus[i * 2 + 1]; From edb81e6be77f758f0129f87e8554cda4a19ab221 Mon Sep 17 00:00:00 2001 From: Jack Gilcrest Date: Thu, 28 Nov 2024 21:21:58 +0700 Subject: [PATCH 5/5] slight tweak to readme --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 87353e1..996bdb0 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ The library exports the following functions: - `headers::constrain_header_field` - constrain an index/ length in the header to be the correct name, full, and uninterrupted - `partial_hash::partial_sha256_var_end` - finish a precomputed sha256 hash over the body - `masking::mask_text` - apply a byte mask to the header or body to selectively reveal parts of the entire email -- `standard_outputs` - returns the hash of the DKIM pubkey and a nullifier for the email (`hash(signature)`) Additionally, the `@zk-email/zkemail-nr` JS library exports an ergonomic API for easily deriving circuit inputs needed to utilize the Noir library. @@ -28,9 +27,9 @@ A basic email verifier will often look like this: ```rust use dep::zkemail::{ KEY_LIMBS_1024, dkim::RSAPubkey, get_body_hash_by_index, - base64::body_hash_base64_decode, standard_outputs + base64::body_hash_base64_decode }; -use dep::std::hash::sha256_var; +use dep::std::hash::{sha256_var, pedersen_hash}; // Somewhere in your function ...