Skip to content

Commit

Permalink
Add more apis to extract substrs.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Oct 6, 2023
1 parent f5fcfea commit 6d6cd03
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 126 deletions.
2 changes: 1 addition & 1 deletion packages/apis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zk-regex-apis"
version = "1.0.1"
version = "1.1.0"
license = "MIT"
edition = "2018"
exclude = ["index.node"]
Expand Down
2 changes: 1 addition & 1 deletion packages/apis/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/zk-regex-apis",
"version": "1.0.1",
"version": "1.1.0",
"description": "apis compatible with [zk-regex](https://github.com/zkemail/zk-regex/tree/main).",
"contributors": [
"Javier Su <[email protected]>",
Expand Down
414 changes: 310 additions & 104 deletions packages/apis/src/extract_substrs.rs

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion packages/apis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
use neon::prelude::*;
pub mod extract_substrs;
pub mod padding;
use extract_substrs::extract_substr_idxes_node;
use extract_substrs::*;
use padding::pad_string_node;

#[cfg(feature = "export_neon_main")]
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("extractSubstrIdxes", extract_substr_idxes_node)?;
cx.export_function("padString", pad_string_node)?;
cx.export_function("extractEmailAddrIdxes", extract_email_addr_idxes_node)?;
cx.export_function("extractEmailDomainIdxes", extract_email_domain_idxes_node)?;
cx.export_function(
"extractEmailAddrWithNameIdxes",
extract_email_addr_with_name_idxes_node,
)?;
cx.export_function("extractFromAllIdxes", extract_from_all_idxes_node)?;
cx.export_function("extractFromAddrIdxes", extract_from_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)?;
cx.export_function("extractMessageIdIdxes", extract_message_id_idxes_node)?;
Ok(())
}
2 changes: 1 addition & 1 deletion packages/circom/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/zk-regex-circom",
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",
"description": "regex verification circuits in circom for common regexes, generated with the compiler in [zk-regex](https://github.com/zkemail/zk-regex/tree/main).",
"contributors": [
Expand Down
4 changes: 2 additions & 2 deletions packages/circom/tests/email_addr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("Email Address Regex", () => {
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(emailAddr, readFileSync(path.join(__dirname, "../circuits/common/email_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractEmailAddrIdxes(emailAddr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -57,7 +57,7 @@ describe("Email Address Regex", () => {
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(string, readFileSync(path.join(__dirname, "../circuits/common/email_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractEmailAddrIdxes(string)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand Down
2 changes: 1 addition & 1 deletion packages/circom/tests/email_domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Email Domain Regex", () => {
for (let idx = 0; idx < 12; ++idx) {
expect(0n).toEqual(witness[2 + idx]);
}
const prefixIdxes = apis.extractSubstrIdxes(emailAddr, readFileSync(path.join(__dirname, "../circuits/common/email_domain.json"), "utf8"))[0];
const prefixIdxes = apis.extractEmailDomainIdxes(emailAddr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand Down
16 changes: 8 additions & 8 deletions packages/circom/tests/from_addr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("From Addr Regex", () => {
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
for (let idx = 0; idx < 1024; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -70,7 +70,7 @@ describe("From Addr Regex", () => {
// console.log(witness);
// console.log(paddedStr);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand All @@ -97,7 +97,7 @@ describe("From Addr Regex", () => {
// console.log(witness);
// console.log(paddedStr);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand All @@ -124,7 +124,7 @@ describe("From Addr Regex", () => {
// console.log(witness);
// console.log(paddedStr);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand All @@ -150,7 +150,7 @@ describe("From Addr Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand All @@ -176,7 +176,7 @@ describe("From Addr Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand All @@ -203,7 +203,7 @@ describe("From Addr Regex", () => {
// console.log(witness);
// console.log(paddedStr);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand All @@ -230,7 +230,7 @@ describe("From Addr Regex", () => {
// console.log(witness);
// console.log(paddedStr);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(fromStr, readFileSync(path.join(__dirname, "../circuits/common/from_addr.json"), "utf8"))[0];
const prefixIdxes = apis.extractFromAddrIdxes(fromStr)[0];
// for (let idx = 0; idx < revealed.length; ++idx) {
// expect(BigInt(paddedStr[prefixIdx + idx])).toEqual(witness[2 + prefixIdx + idx]);
// }
Expand Down
4 changes: 2 additions & 2 deletions packages/circom/tests/message_id_regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Message Id Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(messageIdStr, readFileSync(path.join(__dirname, "../circuits/common/message_id.json"), "utf8"))[0];
const prefixIdxes = apis.extractMessageIdIdxes(messageIdStr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -61,7 +61,7 @@ describe("Message Id Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(messageIdStr, readFileSync(path.join(__dirname, "../circuits/common/message_id.json"), "utf8"))[0];
const prefixIdxes = apis.extractMessageIdIdxes(messageIdStr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand Down
6 changes: 3 additions & 3 deletions packages/circom/tests/subject_all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Subject All Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(subjectStr, readFileSync(path.join(__dirname, "../circuits/common/subject_all.json"), "utf8"))[0];
const prefixIdxes = apis.extractSubjectAllIdxes(subjectStr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -61,7 +61,7 @@ describe("Subject All Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(subjectStr, readFileSync(path.join(__dirname, "../circuits/common/subject_all.json"), "utf8"))[0];
const prefixIdxes = apis.extractSubjectAllIdxes(subjectStr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -84,7 +84,7 @@ describe("Subject All Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(subjectStr, readFileSync(path.join(__dirname, "../circuits/common/subject_all.json"), "utf8"))[0];
const prefixIdxes = apis.extractSubjectAllIdxes(subjectStr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand Down
4 changes: 2 additions & 2 deletions packages/circom/tests/timestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("Timestamp Regex", () => {
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(signatureField, readFileSync(path.join(__dirname, "../circuits/common/timestamp.json"), "utf8"))[0];
const prefixIdxes = apis.extractTimestampIdxes(signatureField)[0];
for (let idx = 0; idx < 1024; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -57,7 +57,7 @@ describe("Timestamp Regex", () => {
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = apis.extractSubstrIdxes(signatureField, readFileSync(path.join(__dirname, "../circuits/common/timestamp.json"), "utf8"))[0];
const prefixIdxes = apis.extractTimestampIdxes(signatureField)[0];
for (let idx = 0; idx < 1024; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand Down

0 comments on commit 6d6cd03

Please sign in to comment.