Skip to content

Commit

Permalink
fix to work with 1024 bit dkim
Browse files Browse the repository at this point in the history
  • Loading branch information
jp4g committed Nov 28, 2024
1 parent 8902a58 commit 0fa8ce4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
33 changes: 29 additions & 4 deletions lib/src/dkim.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ impl<let KEY_LIMBS: u32> RSAPubkey<KEY_LIMBS> {
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<KEY_LIMBS_1024> {
Expand All @@ -36,6 +32,22 @@ impl RSAPubkey<KEY_LIMBS_1024> {
// 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<KEY_LIMBS_2048> {
Expand All @@ -55,4 +67,17 @@ impl RSAPubkey<KEY_LIMBS_2048> {
// 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)
}
}
30 changes: 0 additions & 30 deletions lib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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<let KEY_LIMBS: u32>(
pubkey: RSAPubkey<KEY_LIMBS>,
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
Expand Down

0 comments on commit 0fa8ce4

Please sign in to comment.