Skip to content

Commit

Permalink
Merge pull request #33 from zkemail/saleel/update-deps
Browse files Browse the repository at this point in the history
feat: update to noir v1 beta
  • Loading branch information
jp4g authored Jan 13, 2025
2 parents f0c3652 + 9728acf commit 10cbc47
Show file tree
Hide file tree
Showing 24 changed files with 1,460 additions and 1,335 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
toolchain: [0.36.0]
toolchain: [1.0.0-beta.1]
steps:
- name: Checkout sources
uses: actions/checkout@v4
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Install Nargo
uses: noir-lang/[email protected]
with:
toolchain: 0.36.0
toolchain: 1.0.0-beta.1

- name: Run formatter
working-directory: ./lib
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
.tsbuildinfo
zk-email-verify
!*/email-*.eml
.DS_Store
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ In your Nargo.toml file, add the version of this library you would like to insta

```toml
[dependencies]
zkemail = { tag = "v0.3.6", git = "https://github.com/zkemail/zkemail.nr", directory = "lib" }
zkemail = { tag = "v0.4.0", git = "https://github.com/zkemail/zkemail.nr", directory = "lib" }
```

The library exports the following functions:
Expand All @@ -25,11 +25,11 @@ For demonstrations of all functionality, see the [examples](./examples).
### Basic Email Verification
A basic email verifier will often look like this:
```rust
use dep::zkemail::{
use zkemail::{
KEY_LIMBS_1024, dkim::RSAPubkey, get_body_hash_by_index,
base64::body_hash_base64_decode
};
use dep::std::hash::{sha256_var, pedersen_hash};
use std::hash::{sha256_var, pedersen_hash};

// Somewhere in your function
...
Expand Down Expand Up @@ -57,7 +57,7 @@ You can use partial hashing technique for email with large body when the part yo
Since SHA works in chunks of 64 bytes, we can hash the body up to the chunk from where we want to extract outside of the circuit and do the remaining hash in the circuit. This will save a lot of constraints as SHA is very expensive in circuit (~100 constraints/ byte).

```rust
use dep::zkemail::{
use zkemail::{
KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash,
partial_hash::partial_sha256_var_end
};
Expand All @@ -81,7 +81,7 @@ use dep::zkemail::{

To and from email addresses can be extracted from the header with `get_email_address`
```rust
use dep::zkemail::get_email_address;
use zkemail::get_email_address;
...
// define the header field to access (set "to" or "from")
let to = comptime { "to".as_bytes() };
Expand Down
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.36.0"
compiler_version = ">=1.0.0"

[dependencies]
zkemail = { path = "../../lib"}
13 changes: 7 additions & 6 deletions examples/email_mask/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dep::zkemail::{
KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash,
Sequence, masking::mask_text
use std::{collections::bounded_vec::BoundedVec, hash::{pedersen_hash, sha256_var}};
use zkemail::{
dkim::RSAPubkey, headers::body_hash::get_body_hash, KEY_LIMBS_2048, masking::mask_text,
Sequence,
};
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;
Expand Down Expand Up @@ -30,7 +30,7 @@ fn main(
body_hash_index: u32,
dkim_header_sequence: Sequence,
header_mask: [bool; MAX_EMAIL_HEADER_LENGTH],
body_mask: [bool; MAX_EMAIL_BODY_LENGTH]
body_mask: [bool; MAX_EMAIL_BODY_LENGTH],
) -> pub ([Field; 2], [u8; MAX_EMAIL_HEADER_LENGTH], [u8; MAX_EMAIL_BODY_LENGTH]) {
// check the body and header lengths are within bounds
assert(header.len() <= MAX_EMAIL_HEADER_LENGTH);
Expand All @@ -47,7 +47,8 @@ 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",
);

// mask the header and body
Expand Down
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.36.0"
compiler_version = ">=1.0.0"

[dependencies]
zkemail = { path = "../../lib"}
8 changes: 4 additions & 4 deletions examples/extract_addresses/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dep::zkemail::{
KEY_LIMBS_2048, dkim::RSAPubkey, headers::email_address::get_email_address, Sequence,
MAX_EMAIL_ADDRESS_LENGTH,
use std::{collections::bounded_vec::BoundedVec, hash::pedersen_hash};
use zkemail::{
dkim::RSAPubkey, headers::email_address::get_email_address, KEY_LIMBS_2048,
MAX_EMAIL_ADDRESS_LENGTH, Sequence,
};
use dep::std::{collections::bounded_vec::BoundedVec, hash::pedersen_hash};

global MAX_EMAIL_HEADER_LENGTH: u32 = 512;

Expand Down
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.36.0"
compiler_version = ">=1.0.0"

[dependencies]
zkemail = { path = "../../lib"}
27 changes: 18 additions & 9 deletions examples/partial_hash/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dep::zkemail::{
KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash,
partial_hash::partial_sha256_var_end, Sequence
};
use std::hash::pedersen_hash;
use zkemail::{
dkim::RSAPubkey, headers::body_hash::get_body_hash, KEY_LIMBS_2048,
partial_hash::partial_sha256_var_end, Sequence,
};

global MAX_EMAIL_HEADER_LENGTH: u32 = 512;
global MAX_PARTIAL_EMAIL_BODY_LENGTH: u32 = 192;
Expand Down Expand Up @@ -32,24 +32,33 @@ fn main(
body_hash_index: u32,
dkim_header_sequence: Sequence,
partial_body_hash: [u32; 8],
partial_body_real_length: u64
partial_body_real_length: u64,
) -> pub [Field; 2] {
// check the body and header lengths are within bounds
assert(header.len() <= MAX_EMAIL_HEADER_LENGTH, "Email header length exceeds maximum length");
assert(body.len() <= MAX_PARTIAL_EMAIL_BODY_LENGTH, "Partial email body length exceeds maximum length");

assert(
body.len() <= MAX_PARTIAL_EMAIL_BODY_LENGTH,
"Partial email body length exceeds maximum length",
);

// verify the dkim signature over the header
pubkey.verify_dkim_signature(header, signature);

// manually extract the body hash from the header
let signed_body_hash = get_body_hash(header, dkim_header_sequence, body_hash_index);

// finish the partial hash
let computed_body_hash = partial_sha256_var_end(partial_body_hash, body.storage(), body.len() as u64, partial_body_real_length);
let computed_body_hash = partial_sha256_var_end(
partial_body_hash,
body.storage(),
body.len() as u64,
partial_body_real_length,
);

// check the body hashes match
assert(
signed_body_hash == computed_body_hash, "Sha256 hash computed over body does not match DKIM-signed header"
signed_body_hash == computed_body_hash,
"Sha256 hash computed over body does not match DKIM-signed header",
);

// hash the pubkey and signature for the standard outputs
Expand Down
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.36.0"
compiler_version = ">=1.0.0"

[dependencies]
zkemail = { path = "../../lib"}
6 changes: 3 additions & 3 deletions examples/remove_soft_line_breaks/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::hash::{pedersen_hash, sha256_var};
use zkemail::{
KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash, Sequence,
remove_soft_line_breaks::remove_soft_line_breaks,
dkim::RSAPubkey, headers::body_hash::get_body_hash, KEY_LIMBS_2048,
remove_soft_line_breaks::remove_soft_line_breaks, Sequence,
};
use std::hash::{pedersen_hash, sha256_var};

global MAX_EMAIL_HEADER_LENGTH: u32 = 512;
global MAX_EMAIL_BODY_LENGTH: u32 = 1024;
Expand Down
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.36.0"
compiler_version = ">=1.0.0"

[dependencies]
zkemail = { path = "../../lib"}
14 changes: 6 additions & 8 deletions examples/verify_email_1024_bit_dkim/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dep::zkemail::{
KEY_LIMBS_1024, dkim::RSAPubkey, headers::body_hash::get_body_hash,
Sequence
};
use dep::std::{collections::bounded_vec::BoundedVec, hash::{sha256_var, pedersen_hash}};
use std::{collections::bounded_vec::BoundedVec, hash::{pedersen_hash, sha256_var}};
use zkemail::{dkim::RSAPubkey, headers::body_hash::get_body_hash, KEY_LIMBS_1024, Sequence};

global MAX_EMAIL_HEADER_LENGTH: u32 = 512;
global MAX_EMAIL_BODY_LENGTH: u32 = 1024;
Expand All @@ -27,7 +24,7 @@ fn main(
pubkey: RSAPubkey<KEY_LIMBS_1024>,
signature: [Field; KEY_LIMBS_1024],
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);
Expand All @@ -37,14 +34,15 @@ fn main(
pubkey.verify_dkim_signature(header, signature);

// extract the body hash from the header
let signed_body_hash = get_body_hash(header, dkim_header_sequence,body_hash_index);
let signed_body_hash = get_body_hash(header, dkim_header_sequence, body_hash_index);

// hash the asserted body
let computed_body_hash: [u8; 32] = sha256_var(body.storage(), body.len() as u64);

// 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",
);

// hash the pubkey and signature for the standard outputs
Expand Down
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.36.0"
compiler_version = ">=1.0.0"

[dependencies]
zkemail = { path = "../../lib"}
12 changes: 5 additions & 7 deletions examples/verify_email_2048_bit_dkim/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dep::zkemail::{
KEY_LIMBS_2048, dkim::RSAPubkey, headers::body_hash::get_body_hash,
Sequence
};
use dep::std::{collections::bounded_vec::BoundedVec, hash::{sha256_var, pedersen_hash}};
use std::{collections::bounded_vec::BoundedVec, hash::{pedersen_hash, sha256_var}};
use zkemail::{dkim::RSAPubkey, headers::body_hash::get_body_hash, KEY_LIMBS_2048, Sequence};

global MAX_EMAIL_HEADER_LENGTH: u32 = 512;
global MAX_EMAIL_BODY_LENGTH: u32 = 1024;
Expand All @@ -27,7 +24,7 @@ fn main(
pubkey: RSAPubkey<KEY_LIMBS_2048>,
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);
Expand All @@ -47,7 +44,8 @@ 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",
);

// ~ 10,255 constraints
Expand Down
10 changes: 5 additions & 5 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.3",
"version": "1.3.0",
"main": "dist",
"types": "dist",
"license": "MIT",
Expand All @@ -11,11 +11,11 @@
"prepublishOnly": "yarn lint && yarn build"
},
"dependencies": {
"@aztec/bb.js": "0.66.0",
"@mach-34/noir-bignum-paramgen": "^1.1.0",
"@noir-lang/backend_barretenberg": "=0.36.0",
"@noir-lang/noir_js": "=0.38.0",
"@noir-lang/noirc_abi": "=0.36.0",
"@zk-email/helpers": "=6.1.5"
"@noir-lang/noir_js": "1.0.0-beta.1",
"@noir-lang/noirc_abi": "^1.0.0-beta.1",
"@zk-email/helpers": "^6.3.2"
},
"devDependencies": {
"@babel/core": "^7.25.2",
Expand Down
21 changes: 8 additions & 13 deletions js/src/prover.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import {
BarretenbergBackend,
CompiledCircuit,
ProofData,
UltraHonkBackend,
} from "@noir-lang/backend_barretenberg";
import { Noir } from "@noir-lang/noir_js";
import { InputValue, InputMap } from "@noir-lang/noirc_abi";
import { UltraPlonkBackend, UltraHonkBackend, ProofData } from "@aztec/bb.js";
import { Noir, InputMap, CompiledCircuit } from "@noir-lang/noir_js";
import { InputValue } from "@noir-lang/noirc_abi";

type ProvingBackend = "honk" | "plonk" | "all";

export class ZKEmailProver {
private plonk?: BarretenbergBackend;
private plonk?: UltraPlonkBackend;

private honk?: UltraHonkBackend;

Expand All @@ -24,10 +19,10 @@ export class ZKEmailProver {
) {
// initialize the backends
if (provingBackend === "plonk" || provingBackend === "all") {
this.plonk = new BarretenbergBackend(circuit);
this.plonk = new UltraPlonkBackend(circuit.bytecode);
}
if (provingBackend === "honk" || provingBackend === "all") {
this.honk = new UltraHonkBackend(circuit);
this.honk = new UltraHonkBackend(circuit.bytecode);
}
// initialize the Noir instance
this.noir = new Noir(circuit);
Expand Down Expand Up @@ -57,7 +52,7 @@ export class ZKEmailProver {
provingBackend?: ProvingBackend
): Promise<ProofData> {
// determine proving backend to use
let backend: BarretenbergBackend | UltraHonkBackend;
let backend: UltraPlonkBackend | UltraHonkBackend;
if (
(provingBackend && this.plonk) ||
(this.provingBackend === "plonk" && this.plonk)
Expand Down Expand Up @@ -103,7 +98,7 @@ export class ZKEmailProver {
provingBackend?: ProvingBackend
): Promise<boolean> {
// determine proving backend to use
let backend: BarretenbergBackend | UltraHonkBackend;
let backend: UltraHonkBackend | UltraPlonkBackend;
if (
(provingBackend && this.plonk) ||
(this.provingBackend === "plonk" && this.plonk)
Expand Down
Loading

0 comments on commit 10cbc47

Please sign in to comment.