Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rsa and base64 version #20

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/email_mask/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "email_mask"
type = "bin"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
zkemail = { path = "../../lib"}
2 changes: 1 addition & 1 deletion examples/extract_addresses/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "extract_addresses"
type = "bin"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
zkemail = { path = "../../lib"}
2 changes: 1 addition & 1 deletion examples/partial_hash/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "partial_hash"
type = "bin"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
zkemail = { path = "../../lib"}
2 changes: 1 addition & 1 deletion examples/remove_soft_line_breaks/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "remove_soft_line_breaks"
type = "bin"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
zkemail = { path = "../../lib"}
2 changes: 1 addition & 1 deletion examples/verify_email_1024_bit_dkim/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "verify_email_1024_bit_dkim"
type = "bin"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
zkemail = { path = "../../lib"}
2 changes: 1 addition & 1 deletion examples/verify_email_2048_bit_dkim/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "verify_email_2048_bit_dkim"
type = "bin"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
zkemail = { path = "../../lib"}
12 changes: 6 additions & 6 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/zkemail-nr",
"version": "1.2.2",
"version": "1.2.3",
"main": "dist",
"types": "dist",
"license": "MIT",
Expand All @@ -11,10 +11,10 @@
"prepublishOnly": "yarn lint && yarn build"
},
"dependencies": {
"@mach-34/noir-bignum-paramgen": "^1.0.2",
"@noir-lang/backend_barretenberg": "=0.35.0",
"@noir-lang/noir_js": "=0.35.0",
"@noir-lang/noirc_abi": "^0.35.0",
"@mach-34/noir-bignum-paramgen": "^1.1.0",
"@noir-lang/backend_barretenberg": "=0.36.0",
"@noir-lang/noir_js": "=0.36.0",
"@noir-lang/noirc_abi": "^0.36.0",
"@zk-email/helpers": "=6.1.5"
},
"devDependencies": {
Expand Down Expand Up @@ -53,7 +53,7 @@
"/dist/",
"/node_modules/"
],
"testTimeout": 100000
"testTimeout": 10000000
},
"babel": {
"presets": [
Expand Down
68 changes: 34 additions & 34 deletions js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"@jridgewell/gen-mapping" "^0.3.5"
"@jridgewell/trace-mapping" "^0.3.24"

"@aztec/bb.js@0.56.0":
version "0.56.0"
resolved "https://registry.yarnpkg.com/@aztec/bb.js/-/bb.js-0.56.0.tgz#c1946f7fb23d6c3f2f2b8d6af1b1a3c1ea290122"
integrity sha512-NRuIH9kub99o7Ddx4wLIW1x0Ow0hemP036fL9vBhqTBeNGi6BusMlSVygrUPKAjQrVGihJJPqdndwblypZY2ew==
"@aztec/bb.js@0.58.0":
version "0.58.0"
resolved "https://registry.yarnpkg.com/@aztec/bb.js/-/bb.js-0.58.0.tgz#a9b2571c2121e45ee47b278567bfb2a0befe796b"
integrity sha512-ls9NNAkcTTOQ4TjqQ7oHb6MlM28KfXiXEN7e6YntPmmG4m1I0FqZ1uY0tFkpem1iXIORVyHVY5GqSadqhH1gvA==
dependencies:
comlink "^4.4.1"
commander "^10.0.1"
Expand Down Expand Up @@ -1638,10 +1638,10 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@mach-34/noir-bignum-paramgen@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@mach-34/noir-bignum-paramgen/-/noir-bignum-paramgen-1.0.2.tgz#9928bd1bc97ed67df654eda836a04ee9640a4761"
integrity sha512-80+8KcYR4RNxgYGBaJkZlmSDLUJcj0QIpKWUCUrShjqIQh4LGKi/dgf6JdziEzDVxKr4AkmCkA7pdwLcsD5xag==
"@mach-34/noir-bignum-paramgen@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mach-34/noir-bignum-paramgen/-/noir-bignum-paramgen-1.1.0.tgz#9601fde8cd42fff32d93cb94c935e6ddd23630c7"
integrity sha512-jiD5BzwZEeFPy5Bzro8Bu5JVixG0LT3DhlNwHH2ntuogc56jbejHSGZLhanhwjMMTvIvRKHc3D1sk/eCpWEs6Q==

"@nodelib/fs.scandir@2.1.5":
version "2.1.5"
Expand All @@ -1664,40 +1664,40 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@noir-lang/acvm_js@0.51.0":
version "0.51.0"
resolved "https://registry.yarnpkg.com/@noir-lang/acvm_js/-/acvm_js-0.51.0.tgz#0519b9581f78a37d9a12c85599c853c805716bca"
integrity sha512-yz57N4g27Vm4qdVpaMDIlTdBff4dAGrH7+GD5+QMr84CTeDgx0WQKfK0BYuZLnfesnFqLA/sFvR+eHJUp8n+hQ==
"@noir-lang/acvm_js@0.52.0":
version "0.52.0"
resolved "https://registry.yarnpkg.com/@noir-lang/acvm_js/-/acvm_js-0.52.0.tgz#7e028c1a1fdc01e37333e9e04bc2cf147db9448c"
integrity sha512-QEREOIbq+jK/Bqs9jk0+XAS/KYmQX7lBEUYhdFMxkNIQf3hDQ3hr9PcelOWQtoxiDn6IJ2sr7t7yeZFdRqCvhg==

"@noir-lang/backend_barretenberg@=0.35.0":
version "0.35.0"
resolved "https://registry.yarnpkg.com/@noir-lang/backend_barretenberg/-/backend_barretenberg-0.35.0.tgz#7845a61fb6c0fae988e4c584aed769ecd24188f1"
integrity sha512-cKTM2PJRc1RRtbHhza9ZRPVSbIh2G5kt3vXJn20qKoaROebE6rI0rLWB/6YmmWyMk03aZbVx7t7wI1IBY5kqUg==
"@noir-lang/backend_barretenberg@=0.36.0":
version "0.36.0"
resolved "https://registry.yarnpkg.com/@noir-lang/backend_barretenberg/-/backend_barretenberg-0.36.0.tgz#8b16bb161b60316922f5206b941fa9b3b36a033d"
integrity sha512-TShUJfMZskV8LOKY22jbofKnPAhMxBgUIi2FxcoNJ9y52i56GibrF0iuse3atEMWc8LnOE0ci69wsqZ2yYP8iw==
dependencies:
"@aztec/bb.js" "0.56.0"
"@noir-lang/types" "0.35.0"
"@aztec/bb.js" "0.58.0"
"@noir-lang/types" "0.36.0"
fflate "^0.8.0"

"@noir-lang/noir_js@=0.35.0":
version "0.35.0"
resolved "https://registry.yarnpkg.com/@noir-lang/noir_js/-/noir_js-0.35.0.tgz#60ff5402ba1eaa3ec617f32143bc65c7b02a5a9a"
integrity sha512-7yj0yURWCMX5ohZhaQup+qOyvzcwTsmOaUVuy4xYXnt4rGSYKKTR9HOFuAPAoNq1RiFdZcCGJxOAhQkG5I/9aw==
"@noir-lang/noir_js@=0.36.0":
version "0.36.0"
resolved "https://registry.yarnpkg.com/@noir-lang/noir_js/-/noir_js-0.36.0.tgz#63ef02cb8635a8edf6b32243aefcb7fdd4c3ce46"
integrity sha512-qMAfqkcWfERxS1IOeAjTssZGVolNLVIQhHI/Ers7iIx3tZYhejHRe30cMFdhgdJ9nfpqJea+TvlsKLsXbEBMfw==
dependencies:
"@noir-lang/acvm_js" "0.51.0"
"@noir-lang/noirc_abi" "0.35.0"
"@noir-lang/types" "0.35.0"
"@noir-lang/acvm_js" "0.52.0"
"@noir-lang/noirc_abi" "0.36.0"
"@noir-lang/types" "0.36.0"

"@noir-lang/noirc_abi@0.35.0", "@noir-lang/noirc_abi@^0.35.0":
version "0.35.0"
resolved "https://registry.yarnpkg.com/@noir-lang/noirc_abi/-/noirc_abi-0.35.0.tgz#6b56f356a056ef4f935503d70460afa2aae76018"
integrity sha512-iTlHWHdnxgwQveGNCaeQKj1UenFqtnWvh62mfY7m/GD9cs9LB5PFs57rmYMEGaQysEix3sM3BmSX1h9C93pHDg==
"@noir-lang/noirc_abi@0.36.0", "@noir-lang/noirc_abi@^0.36.0":
version "0.36.0"
resolved "https://registry.yarnpkg.com/@noir-lang/noirc_abi/-/noirc_abi-0.36.0.tgz#b4bb93897f2ce90e951000139810dc726206e234"
integrity sha512-xRs13RQArV+m4ehkWpbAB/67z7WBfB/EgFJTJtd2/QMdtJSDWJ+8zcf5oOjX+YpDt38c9qY9d/SRdIzws34m3w==
dependencies:
"@noir-lang/types" "0.35.0"
"@noir-lang/types" "0.36.0"

"@noir-lang/types@0.35.0":
version "0.35.0"
resolved "https://registry.yarnpkg.com/@noir-lang/types/-/types-0.35.0.tgz#47e73db1c545b0a68a7b1c4d551843ed71e47291"
integrity sha512-m4zzTuWVeYI/lgAKU4XwS61lfU/vr8laal8mLm2YEym8GTyVfezjjVrzQd0/mOZtpZ6hO7r3rbd3W2Vs9IkMrg==
"@noir-lang/types@0.36.0":
version "0.36.0"
resolved "https://registry.yarnpkg.com/@noir-lang/types/-/types-0.36.0.tgz#695f7ae552caf2d9033bdf6964d2d5ba6ac5db39"
integrity sha512-3A/yJtnbTsn0o3T/D3YPs5b0vPOxYwos6Y5Ko6NhZsE6V31rYPv2a2NYmbfJRYuriwwzFbdE8+ui3UiV5BmvWw==

"@rtsao/scc@^1.1.0":
version "1.1.0"
Expand Down
10 changes: 5 additions & 5 deletions lib/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name = "zkemail.nr"
type = "lib"
authors = ["Mach 34"]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
rsa = { tag = "v0.3.1", git = "https://github.com/noir-lang/noir_rsa", directory = "lib" }
base64 = { tag = "v0.2.2", git = "https://github.com/noir-lang/noir_base64" }
nodash = { tag = "v0.36.0", git = "https://github.com/olehmisar/nodash" }
# string_search = { tag = "v0.1", git = "https://github.com/noir-lang/noir_string_search" }
bignum = { tag = "v0.4.1", git = "https://github.com/noir-lang/noir-bignum" }
rsa = { tag = "v0.4.0", git = "https://github.com/noir-lang/noir_rsa", directory = "lib" }
base64 = { tag = "v0.3.0", git = "https://github.com/noir-lang/noir_base64" }
nodash = { tag = "v0.36.0", git = "https://github.com/olehmisar/nodash" }
35 changes: 14 additions & 21 deletions lib/src/dkim.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use dep::std::{collections::bounded_vec::BoundedVec, hash::{sha256_var, pedersen_hash}, panic::panic};
use dep::rsa::{bignum::{fields::{Params1024, Params2048}, runtime_bignum::BigNumInstance, BigNum}, types::RSA};
use crate::{KEY_LIMBS_1024, KEY_BYTES_1024, KEY_LIMBS_2048, KEY_BYTES_2048, RSA_EXPONENT};
use dep::bignum::{params::BigNumParams, RuntimeBigNum};
use dep::rsa::{rsa::verify_sha256_pkcs1v15, types::{RBN1024, RBN2048}};
use crate::{KEY_LIMBS_1024, KEY_LIMBS_2048, RSA_EXPONENT};

type BN1024 = BigNum<KEY_LIMBS_1024, Params1024>;
type RSA1024 = RSA<BN1024, BigNumInstance<KEY_LIMBS_1024, Params1024>, KEY_BYTES_1024>;
type BN2048 = BigNum<KEY_LIMBS_2048, Params2048>;
type RSA2048 = RSA<BN2048, BigNumInstance<KEY_LIMBS_2048, Params2048>, KEY_BYTES_2048>;

pub struct RSAPubkey<let KEY_LIMBS: u32> {
modulus: [Field; KEY_LIMBS],
Expand All @@ -26,36 +23,32 @@ impl RSAPubkey<KEY_LIMBS_1024> {
) {
// hash the header
let header_hash = sha256_var(header.storage, header.len() as u64);
// instantiate the BNInstance for the pubkey
// todo: make as BigNumInstance from the outset to reduce some work
let pubkey: BigNumInstance<KEY_LIMBS_1024, Params1024> = BigNumInstance::new(self.modulus, self.redc);

// instantiate BN for the signature
let signature: BN1024 = BigNum::from_array(signature);
let params: BigNumParams<KEY_LIMBS_1024, 1024> =
BigNumParams::new(false, self.modulus, self.redc);

let signature: RBN1024 = RuntimeBigNum::from_array(params, signature);

// verify the DKIM signature over the header
let rsa: RSA1024 = RSA {};
assert(rsa.verify_sha256_pkcs1v15(pubkey, header_hash, signature, RSA_EXPONENT));
assert(verify_sha256_pkcs1v15(header_hash, signature, RSA_EXPONENT));
}
}

impl RSAPubkey<KEY_LIMBS_2048> {
fn verify_dkim_signature<let MAX_HEADER_LENGTH: u32>(
self,
header: BoundedVec<u8, MAX_HEADER_LENGTH>,
signature: [Field; KEY_LIMBS_2048]
signature: [Field; KEY_LIMBS_2048],
) {
// hash the header
let header_hash = sha256_var(header.storage, header.len() as u64);
// instantiate the BNInstance for the pubkey
// todo: make as BigNumInstance from the outset to reduce some work
let pubkey: BigNumInstance<KEY_LIMBS_2048, Params2048> = BigNumInstance::new(self.modulus, self.redc);

// instantiate BN for the signature
let signature: BN2048 = BigNum::from_array(signature);
let params: BigNumParams<KEY_LIMBS_2048, 2048> =
BigNumParams::new(false, self.modulus, self.redc);

let signature: RBN2048 = RuntimeBigNum::from_array(params, signature);

// verify the DKIM signature over the header
let rsa: RSA2048 = RSA {};
assert(rsa.verify_sha256_pkcs1v15(pubkey, header_hash, signature, RSA_EXPONENT));
assert(verify_sha256_pkcs1v15(header_hash, signature, RSA_EXPONENT));
}
}
4 changes: 2 additions & 2 deletions lib/src/headers/body_hash.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::base64::base64_encode;
use dep::base64::{BASE64_DECODER};
use dep::std::collections::bounded_vec::BoundedVec;
use crate::{Sequence, BODY_HASH_BASE64_LENGTH, MAX_DKIM_HEADER_FIELD_LENGTH, headers::constrain_header_field};

Expand Down Expand Up @@ -55,5 +55,5 @@ pub fn get_body_hash_unsafe<let MAX_HEADER_LENGTH: u32>(
}
// return the decoded body hash
// idk why encode vs decode...
base64_encode::<BODY_HASH_BASE64_LENGTH, 32>(body_hash_encoded)
BASE64_DECODER.decode(body_hash_encoded)
}
2 changes: 1 addition & 1 deletion lib/src/tests/test_inputs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod EmailLarge {
};
global PUBKEY: RSAPubkey<KEY_LIMBS_2048> = RSAPubkey {
modulus: [0xe5cf995b5ef59ce9943d1f4209b6ab, 0xe0caf03235e91a2db27e9ed214bcc6, 0xafe1309f87414bd36ed296dacfade2, 0xbeff3f19046a43adce46c932514988, 0x324041af8736e87de4358860fff057, 0xadcc6669dfa346f322717851a8c22a, 0x8b2a193089e6bf951c553b5a6f71aa, 0x0a570fe582918c4f731a0002068df2, 0x39419a433d6bfdd1978356cbca4b60, 0x550d695a514d38b45c862320a00ea5, 0x1c56ac1dfbf1beea31e8a613c2a51f, 0x6a30c9f22d2e5cb6934263d0838809, 0x0a281f268a44b21a4f77a91a52f960, 0x5134dc3966c8e91402669a47cc8597, 0x71590781df114ec072e641cdc5d224, 0xa1bc0f0937489c806c1944fd029dc9, 0x911f6e47f84db3b64c3648ebb5a127, 0xd5],
redc: [0xa48a824e4ebc7e0f1059f3ecfa57c4, 0x05c1db23f3c7d47ad7e7d7cfda5189, 0x79bb6bbbd8facf011f022fa9051aec, 0x24faa4cef474bed639362ea71f7a21, 0x1503aa50b77e24b030841a7d061581, 0x5bbf4e62805e1860a904c0f66a5fad, 0x5cbd24b72442d2ce647dd7d0a44368, 0x074a8839a4460c169dce7138efdaef, 0x0f06e09e3191b995b08e5b45182f65, 0x51fad4a89f8369fe10e5d4b6e149a1, 0xdc778b15982d11ebf7fe23b4e15f10, 0xa09ff3a4567077510c474e4ac0a21a, 0xb37e69e5dbb77167b73065e4c5ad6a, 0xecf4774e22e7fe3a38642186f7ae74, 0x16e72b5eb4c813a3b37998083aab81, 0xa48e7050aa8abedce5a45c16985376, 0xdd3285e53b322b221f7bcf4f8f8ad8, 0x0132]
redc: [0x48a824e4ebc7e0f1059f3ecfa57c46, 0x5c1db23f3c7d47ad7e7d7cfda5189a, 0x9bb6bbbd8facf011f022fa9051aec0, 0x4faa4cef474bed639362ea71f7a217, 0x503aa50b77e24b030841a7d0615812, 0xbbf4e62805e1860a904c0f66a5fad1, 0xcbd24b72442d2ce647dd7d0a443685, 0x74a8839a4460c169dce7138efdaef5, 0xf06e09e3191b995b08e5b45182f650, 0x1fad4a89f8369fe10e5d4b6e149a10, 0xc778b15982d11ebf7fe23b4e15f105, 0x09ff3a4567077510c474e4ac0a21ad, 0x37e69e5dbb77167b73065e4c5ad6aa, 0xcf4774e22e7fe3a38642186f7ae74b, 0x6e72b5eb4c813a3b37998083aab81e, 0x48e7050aa8abedce5a45c169853761, 0xd3285e53b322b221f7bcf4f8f8ad8a, 0x132d]
};
global SIGNATURE: [Field; KEY_LIMBS_2048] = [0xf193c3300b7c9902e32861c38d0d2d, 0x9f6927fdb3df0b84092d8459654327, 0x8a0bea5e2fa82821e49c27b68d5a7b, 0xaa8c0acc1190f9fd845ef64f8e7ae9, 0xa7aeebb37f4395965543e6df69a5a7, 0x087ecef9921569cfba83331ca11c6b, 0x4589ed316ed20757e65ad221736011, 0x0835d8748f11dcc985700c3fea27b1, 0xe870d2493fb83b4a1d72350e5de926, 0x268b28eda0aac07625cfab32b60af1, 0xb41a164eae7ba1602eaec5b5a39fe6, 0x693cc5ec578422bee48eabe390fc37, 0xa29504dd504f14423f2ce65b2ac388, 0x6c3ac6310c084a0b126fcd5225c208, 0xab0903e48563e5f4a5365ac5cbd888, 0xf05bf2e5b6266c0ac88dfc733c414f, 0xf58f9e9669e0f4f3086cce1187fd44, 0xb9];
global BODY_HASH_INDEX: u32 = 361;
Expand Down