Skip to content

Commit

Permalink
feat: Add to_addr field in API (#48)
Browse files Browse the repository at this point in the history
* feat: add to_addr

* test: replace code test

* fix tests

change apis package.json

change apis package.json
  • Loading branch information
Bisht13 authored Jan 22, 2024
1 parent 29d589e commit e88b2e9
Show file tree
Hide file tree
Showing 15 changed files with 556 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
paths = ["./"]

[target.x86_64-unknown-linux-gnu]
rustflags = ["-Clink-arg=-Wl,--allow-multiple-definition"]
9 changes: 5 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 18
- name: "Setup Rust"
uses: ATiltedTree/setup-rust@v1
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
rust-version: nightly
components: clippy
toolchain: stable
override: true
components: rustfmt, clippy
- name: Download circom (Linux)
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 -O /usr/local/bin/circom && chmod +x /usr/local/bin/circom
- name: Install yarn
Expand Down
1 change: 1 addition & 0 deletions packages/apis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ itertools = "0.10.3"
thiserror = "1.0.40"
serde_json = "1.0.95"


[dependencies.neon]
version = "0.10"
default-features = false
Expand Down
5 changes: 3 additions & 2 deletions packages/apis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
"build-debug": "npm run build --",
"build-release": "npm run build -- --release",
"install": "node-pre-gyp install --update-binary --fallback-to-build=false || npm run build-release",
"install": "npm run build-debug",
"install-release": "node-pre-gyp install --update-binary --fallback-to-build=false || npm run build-release",
"test": "cargo test",
"package": "node-pre-gyp package",
"upload-binary": "npm run package && node-pre-gyp-github publish"
Expand All @@ -35,4 +36,4 @@
"package_name": "apis-{node_abi}-{platform}-{arch}.tar.gz",
"module_path": "./"
}
}
}
20 changes: 20 additions & 0 deletions packages/apis/src/decomposed_defs/to_addr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"parts": [
{
"is_public": false,
"regex_def": "((\r\n)|^)to:"
},
{
"is_public": false,
"regex_def": "([^\r\n]+<)?"
},
{
"is_public": true,
"regex_def": "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|!|#|$|%|&|'|\\*|\\+|-|/|=|\\?|^|_|`|{|\\||}|~|\\.)+@(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|_|\\.|-)+"
},
{
"is_public": false,
"regex_def": ">?\r\n"
}
]
}
16 changes: 16 additions & 0 deletions packages/apis/src/decomposed_defs/to_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parts": [
{
"is_public": false,
"regex_def": "((\r\n)|^)to:"
},
{
"is_public": true,
"regex_def": "[^\r\n]+"
},
{
"is_public": false,
"regex_def": "\r\n"
}
]
}
65 changes: 59 additions & 6 deletions packages/apis/src/extract_substrs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use zk_regex_compiler::DecomposedRegexConfig;
use fancy_regex::Regex;

use neon::prelude::*;
use serde_json;
use thiserror::Error;
use zk_regex_compiler::DecomposedRegexConfig;

/// Error definitions of the compiler.
#[derive(Error, Debug)]
Expand Down Expand Up @@ -102,6 +101,17 @@ pub fn extract_from_addr_idxes(
extract_substr_idxes(input_str, &regex_config)
}

pub fn extract_to_all_idxes(input_str: &str) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
let regex_config = serde_json::from_str(include_str!("./decomposed_defs/to_all.json")).unwrap();
extract_substr_idxes(input_str, &regex_config)
}

pub fn extract_to_addr_idxes(input_str: &str) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
let regex_config =
serde_json::from_str(include_str!("./decomposed_defs/to_addr.json")).unwrap();
extract_substr_idxes(input_str, &regex_config)
}

pub fn extract_subject_all_idxes(
input_str: &str,
) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
Expand Down Expand Up @@ -247,6 +257,49 @@ pub fn extract_from_addr_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray
Ok(js_array)
}

pub fn extract_to_all_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray> {
let input_str = cx.argument::<JsString>(0)?.value(&mut cx);

let substr_idxes = match extract_to_all_idxes(&input_str) {
Ok(substr_idxes) => substr_idxes,
Err(e) => return cx.throw_error(e.to_string()),
};

let js_array = JsArray::new(&mut cx, substr_idxes.len() as u32);

for (i, (start_idx, end_idx)) in substr_idxes.iter().enumerate() {
let start_end_array = JsArray::new(&mut cx, 2u32);

let start_idx = cx.number(*start_idx as f64);
start_end_array.set(&mut cx, 0, start_idx)?;

let end_idx = cx.number(*end_idx as f64);
start_end_array.set(&mut cx, 1, end_idx)?;

js_array.set(&mut cx, i as u32, start_end_array)?;
}

Ok(js_array)
}

pub fn extract_to_addr_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray> {
let input_str = cx.argument::<JsString>(0)?.value(&mut cx);
let substr_idxes = match extract_to_addr_idxes(&input_str) {
Ok(substr_idxes) => substr_idxes,
Err(e) => return cx.throw_error(e.to_string()),
};
let js_array = JsArray::new(&mut cx, substr_idxes.len() as u32);
for (i, (start_idx, end_idx)) in substr_idxes.iter().enumerate() {
let start_end_array = JsArray::new(&mut cx, 2u32);
let start_idx = cx.number(*start_idx as f64);
start_end_array.set(&mut cx, 0, start_idx)?;
let end_idx = cx.number(*end_idx as f64);
start_end_array.set(&mut cx, 1, end_idx)?;
js_array.set(&mut cx, i as u32, start_end_array)?;
}
Ok(js_array)
}

pub fn extract_subject_all_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray> {
let input_str = cx.argument::<JsString>(0)?.value(&mut cx);
let substr_idxes = match extract_subject_all_idxes(&input_str) {
Expand Down Expand Up @@ -369,13 +422,13 @@ mod test {
}

#[test]
fn test_code_in_subject_valid() {
fn test_code_in_email_address_valid() {
let code_regex = DecomposedRegexConfig {
// max_byte_size: 1024,
parts: vec![
RegexPartConfig {
is_public: false,
regex_def: "CODE:0x".to_string(),
regex_def: "ACCOUNTKEY.0x".to_string(),
// max_size: 7,
// solidity: None
},
Expand All @@ -387,9 +440,9 @@ mod test {
},
],
};
let input_str = "subject:Email Wallet CODE:0x123abc";
let input_str = "[email protected]";
let idxes = extract_substr_idxes(input_str, &code_regex).unwrap();
assert_eq!(idxes, vec![(28, 34)]);
assert_eq!(idxes, vec![(21, 27)]);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions packages/apis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
)?;
cx.export_function("extractFromAllIdxes", extract_from_all_idxes_node)?;
cx.export_function("extractFromAddrIdxes", extract_from_addr_idxes_node)?;
cx.export_function("extractToAllIdxes", extract_to_all_idxes_node)?;
cx.export_function("extractToAddrIdxes", extract_to_addr_idxes_node)?;
cx.export_function("extractSubjectAllIdxes", extract_subject_all_idxes_node)?;
cx.export_function("extractBodyHashIdxes", extract_body_hash_idxes_node)?;
cx.export_function("extractTimestampIdxes", extract_timestamp_idxes_node)?;
Expand Down
20 changes: 20 additions & 0 deletions packages/circom/circuits/common/to_addr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"parts": [
{
"is_public": false,
"regex_def": "((\r\n)|^)to:"
},
{
"is_public": false,
"regex_def": "([^\r\n]+<)?"
},
{
"is_public": true,
"regex_def": "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|!|#|$|%|&|'|\\*|\\+|-|/|=|\\?|^|_|`|{|\\||}|~|\\.)+@(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|_|\\.|-)+"
},
{
"is_public": false,
"regex_def": ">?\r\n"
}
]
}
31 changes: 31 additions & 0 deletions packages/circom/circuits/common/to_addr_regex.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pragma circom 2.1.5;

include "@zk-email/zk-regex-circom/circuits/regex_helpers.circom";
include "@zk-email/zk-regex-circom/circuits/common/to_all_regex.circom";
include "@zk-email/zk-regex-circom/circuits/common/email_addr_regex.circom";
include "@zk-email/zk-regex-circom/circuits/common/email_addr_with_name_regex.circom";


template ToAddrRegex(msg_bytes) {
signal input msg[msg_bytes];
signal output out;
signal output reveal0[msg_bytes];

signal toOut;
signal toReveal[msg_bytes];
(toOut, toReveal) <== ToAllRegex(msg_bytes)(msg);
toOut === 1;

signal emailNameOut;
signal emailNameReveal[msg_bytes];
(emailNameOut, emailNameReveal) <== EmailAddrWithNameRegex(msg_bytes)(toReveal);

signal emailAddrOut;
signal emailAddrReveal[msg_bytes];
(emailAddrOut, emailAddrReveal) <== EmailAddrRegex(msg_bytes)(toReveal);

out <== MultiOR(2)([emailNameOut, emailAddrOut]);
for(var i=0; i<msg_bytes; i++) {
reveal0[i] <== emailNameOut * (emailNameReveal[i] - emailAddrReveal[i]) + emailAddrReveal[i];
}
}
16 changes: 16 additions & 0 deletions packages/circom/circuits/common/to_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parts": [
{
"is_public": false,
"regex_def": "((\r\n)|^)to:"
},
{
"is_public": true,
"regex_def": "[^\r\n]+"
},
{
"is_public": false,
"regex_def": "\r\n"
}
]
}
Loading

0 comments on commit e88b2e9

Please sign in to comment.