Skip to content

Commit

Permalink
Add a constraint for the input range check result.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Nov 7, 2024
1 parent 273b48b commit 7754156
Show file tree
Hide file tree
Showing 46 changed files with 180 additions and 47 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 = "2.3.0"
version = "2.3.1"
license = "MIT"
edition = "2018"
authors = [
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": "2.3.0",
"version": "2.3.1",
"description": "apis compatible with [zk-regex](https://github.com/zkemail/zk-regex/tree/main).",
"contributors": [
"Javier Su <[email protected]>",
Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/body_hash_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template BodyHashRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/email_addr_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template EmailAddrRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/email_domain_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template EmailDomainRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/from_all_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template FromAllRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/message_id_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template MessageIdRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template ReversedBracketRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/subject_all_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template SubjectAllRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/timestamp_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template TimestampRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/circuits/common/to_all_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template ToAllRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
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": "2.3.0",
"version": "2.3.1",
"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
17 changes: 16 additions & 1 deletion packages/circom/tests/body_hash_regex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Bodyhash Regex', () => {
}
});

it('invalid bodyhash', async () => {
it('bodyhash in the invalid field', async () => {
const signatureField = `\r\nto: dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`;
const paddedStr = apis.padString(signatureField, 1024);
const circuitInputs = {
Expand All @@ -96,4 +96,19 @@ describe('Bodyhash Regex', () => {
expect(0n).toEqual(witness[2 + idx]);
}
});

it('invalid bodyhash with 255', async () => {
const signatureField = `dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`;
let paddedStr = apis.padString(signatureField, 1022);
paddedStr.unshift(49);
paddedStr.unshift(255);
const circuitInputs = {
msg: paddedStr
};
async function failFn() {
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
}
await expect(failFn).rejects.toThrow();
});
});
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/asterisk1_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Asterisk1Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/asterisk2_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Asterisk2Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/asterisk3_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Asterisk3Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/caret1_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Caret1Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/caret2_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Caret2Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/caret3_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Caret3Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/caret4_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Caret4Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/caret5_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Caret5Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/dollar1_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Dollar1Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/dollar2_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Dollar2Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/dot1_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Dot1Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/dot2_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Dot2Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template InternationalCharsDecomposed(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template InvitationCodeWithPrefixRegex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/negate1_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Negate1Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/negate2_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Negate2Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
4 changes: 3 additions & 1 deletion packages/circom/tests/circuits/plus1_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ template Plus1Regex(msg_bytes) {

var num_bytes = msg_bytes+1;
signal in[num_bytes];
signal in_range_checks[msg_bytes];
in[0]<==255;
for (var i = 0; i < msg_bytes; i++) {
_ <== LessThan(8)([msg[i], 255]);
in_range_checks[i] <== LessThan(8)([msg[i], 255]);
in_range_checks[i] === 1;
in[i+1] <== msg[i];
}

Expand Down
Loading

0 comments on commit 7754156

Please sign in to comment.