diff --git a/packages/apis/Cargo.toml b/packages/apis/Cargo.toml index ae06c97..2fffaf0 100644 --- a/packages/apis/Cargo.toml +++ b/packages/apis/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zk-regex-apis" -version = "2.3.0" +version = "2.3.1" license = "MIT" edition = "2018" authors = [ diff --git a/packages/apis/package.json b/packages/apis/package.json index bbd2f26..43a3315 100644 --- a/packages/apis/package.json +++ b/packages/apis/package.json @@ -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 ", diff --git a/packages/circom/circuits/common/body_hash_regex.circom b/packages/circom/circuits/common/body_hash_regex.circom index 13613cf..abe3b27 100644 --- a/packages/circom/circuits/common/body_hash_regex.circom +++ b/packages/circom/circuits/common/body_hash_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/email_addr_regex.circom b/packages/circom/circuits/common/email_addr_regex.circom index 096c287..4f4f0ba 100644 --- a/packages/circom/circuits/common/email_addr_regex.circom +++ b/packages/circom/circuits/common/email_addr_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/email_domain_regex.circom b/packages/circom/circuits/common/email_domain_regex.circom index f15da00..064b040 100644 --- a/packages/circom/circuits/common/email_domain_regex.circom +++ b/packages/circom/circuits/common/email_domain_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/from_all_regex.circom b/packages/circom/circuits/common/from_all_regex.circom index 91fbd70..14c3539 100644 --- a/packages/circom/circuits/common/from_all_regex.circom +++ b/packages/circom/circuits/common/from_all_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/message_id_regex.circom b/packages/circom/circuits/common/message_id_regex.circom index 637005a..705d777 100644 --- a/packages/circom/circuits/common/message_id_regex.circom +++ b/packages/circom/circuits/common/message_id_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/reversed_bracket_regex.circom b/packages/circom/circuits/common/reversed_bracket_regex.circom index 3ee1258..648a6e1 100644 --- a/packages/circom/circuits/common/reversed_bracket_regex.circom +++ b/packages/circom/circuits/common/reversed_bracket_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/subject_all_regex.circom b/packages/circom/circuits/common/subject_all_regex.circom index 11c39e8..a98c8dd 100644 --- a/packages/circom/circuits/common/subject_all_regex.circom +++ b/packages/circom/circuits/common/subject_all_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/timestamp_regex.circom b/packages/circom/circuits/common/timestamp_regex.circom index 31aa51d..c8817fa 100644 --- a/packages/circom/circuits/common/timestamp_regex.circom +++ b/packages/circom/circuits/common/timestamp_regex.circom @@ -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]; } diff --git a/packages/circom/circuits/common/to_all_regex.circom b/packages/circom/circuits/common/to_all_regex.circom index 873f00c..adf881a 100644 --- a/packages/circom/circuits/common/to_all_regex.circom +++ b/packages/circom/circuits/common/to_all_regex.circom @@ -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]; } diff --git a/packages/circom/package.json b/packages/circom/package.json index 86683c5..a8fc17f 100644 --- a/packages/circom/package.json +++ b/packages/circom/package.json @@ -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": [ diff --git a/packages/circom/tests/body_hash_regex.test.js b/packages/circom/tests/body_hash_regex.test.js index fd49da6..957076f 100644 --- a/packages/circom/tests/body_hash_regex.test.js +++ b/packages/circom/tests/body_hash_regex.test.js @@ -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 = { @@ -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(); + }); }); diff --git a/packages/circom/tests/circuits/asterisk1_regex.circom b/packages/circom/tests/circuits/asterisk1_regex.circom index 82dffb2..82da03e 100644 --- a/packages/circom/tests/circuits/asterisk1_regex.circom +++ b/packages/circom/tests/circuits/asterisk1_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/asterisk2_regex.circom b/packages/circom/tests/circuits/asterisk2_regex.circom index 3ff752f..30e09c2 100644 --- a/packages/circom/tests/circuits/asterisk2_regex.circom +++ b/packages/circom/tests/circuits/asterisk2_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/asterisk3_regex.circom b/packages/circom/tests/circuits/asterisk3_regex.circom index e0a4b02..68fbc82 100644 --- a/packages/circom/tests/circuits/asterisk3_regex.circom +++ b/packages/circom/tests/circuits/asterisk3_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/caret1_regex.circom b/packages/circom/tests/circuits/caret1_regex.circom index 073e941..adbc740 100644 --- a/packages/circom/tests/circuits/caret1_regex.circom +++ b/packages/circom/tests/circuits/caret1_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/caret2_regex.circom b/packages/circom/tests/circuits/caret2_regex.circom index a3655b4..a7f481f 100644 --- a/packages/circom/tests/circuits/caret2_regex.circom +++ b/packages/circom/tests/circuits/caret2_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/caret3_regex.circom b/packages/circom/tests/circuits/caret3_regex.circom index 3fbdd37..c901c5e 100644 --- a/packages/circom/tests/circuits/caret3_regex.circom +++ b/packages/circom/tests/circuits/caret3_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/caret4_regex.circom b/packages/circom/tests/circuits/caret4_regex.circom index f89098b..8bb1cbf 100644 --- a/packages/circom/tests/circuits/caret4_regex.circom +++ b/packages/circom/tests/circuits/caret4_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/caret5_regex.circom b/packages/circom/tests/circuits/caret5_regex.circom index 7a51160..e8d6a85 100644 --- a/packages/circom/tests/circuits/caret5_regex.circom +++ b/packages/circom/tests/circuits/caret5_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/dollar1_regex.circom b/packages/circom/tests/circuits/dollar1_regex.circom index a2ca8cd..a786191 100644 --- a/packages/circom/tests/circuits/dollar1_regex.circom +++ b/packages/circom/tests/circuits/dollar1_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/dollar2_regex.circom b/packages/circom/tests/circuits/dollar2_regex.circom index c0821df..f46c3f4 100644 --- a/packages/circom/tests/circuits/dollar2_regex.circom +++ b/packages/circom/tests/circuits/dollar2_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/dot1_regex.circom b/packages/circom/tests/circuits/dot1_regex.circom index af469f6..9e0f7ed 100644 --- a/packages/circom/tests/circuits/dot1_regex.circom +++ b/packages/circom/tests/circuits/dot1_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/dot2_regex.circom b/packages/circom/tests/circuits/dot2_regex.circom index de6e1c5..49ef1a1 100644 --- a/packages/circom/tests/circuits/dot2_regex.circom +++ b/packages/circom/tests/circuits/dot2_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/international_chars_decomposed.circom b/packages/circom/tests/circuits/international_chars_decomposed.circom index aa29c52..c1cfeb7 100644 --- a/packages/circom/tests/circuits/international_chars_decomposed.circom +++ b/packages/circom/tests/circuits/international_chars_decomposed.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/invitation_code_with_prefix_regex.circom b/packages/circom/tests/circuits/invitation_code_with_prefix_regex.circom index 2b96a36..a461f21 100644 --- a/packages/circom/tests/circuits/invitation_code_with_prefix_regex.circom +++ b/packages/circom/tests/circuits/invitation_code_with_prefix_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/negate1_regex.circom b/packages/circom/tests/circuits/negate1_regex.circom index c0df6ea..7837118 100644 --- a/packages/circom/tests/circuits/negate1_regex.circom +++ b/packages/circom/tests/circuits/negate1_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/negate2_regex.circom b/packages/circom/tests/circuits/negate2_regex.circom index 9dbf7c8..732eeab 100644 --- a/packages/circom/tests/circuits/negate2_regex.circom +++ b/packages/circom/tests/circuits/negate2_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/plus1_regex.circom b/packages/circom/tests/circuits/plus1_regex.circom index d8c9585..5538ee6 100644 --- a/packages/circom/tests/circuits/plus1_regex.circom +++ b/packages/circom/tests/circuits/plus1_regex.circom @@ -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]; } diff --git a/packages/circom/tests/circuits/plus2_regex.circom b/packages/circom/tests/circuits/plus2_regex.circom index 27663d2..0061f03 100644 --- a/packages/circom/tests/circuits/plus2_regex.circom +++ b/packages/circom/tests/circuits/plus2_regex.circom @@ -9,9 +9,11 @@ template Plus2Regex(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]; } diff --git a/packages/circom/tests/circuits/plus3_regex.circom b/packages/circom/tests/circuits/plus3_regex.circom index 6fb0c1a..70a7acc 100644 --- a/packages/circom/tests/circuits/plus3_regex.circom +++ b/packages/circom/tests/circuits/plus3_regex.circom @@ -9,9 +9,11 @@ template Plus3Regex(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]; } diff --git a/packages/circom/tests/circuits/plus4_regex.circom b/packages/circom/tests/circuits/plus4_regex.circom index 938b644..6977960 100644 --- a/packages/circom/tests/circuits/plus4_regex.circom +++ b/packages/circom/tests/circuits/plus4_regex.circom @@ -9,9 +9,11 @@ template Plus4Regex(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]; } diff --git a/packages/circom/tests/circuits/question1_regex.circom b/packages/circom/tests/circuits/question1_regex.circom index c9be7d3..1c822e4 100644 --- a/packages/circom/tests/circuits/question1_regex.circom +++ b/packages/circom/tests/circuits/question1_regex.circom @@ -9,9 +9,11 @@ template Question1Regex(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]; } diff --git a/packages/circom/tests/circuits/question2_regex.circom b/packages/circom/tests/circuits/question2_regex.circom index d954378..0cbf7b8 100644 --- a/packages/circom/tests/circuits/question2_regex.circom +++ b/packages/circom/tests/circuits/question2_regex.circom @@ -9,9 +9,11 @@ template Question2Regex(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]; } diff --git a/packages/circom/tests/circuits/question3_regex.circom b/packages/circom/tests/circuits/question3_regex.circom index d713b5d..ef9528c 100644 --- a/packages/circom/tests/circuits/question3_regex.circom +++ b/packages/circom/tests/circuits/question3_regex.circom @@ -9,9 +9,11 @@ template Question3Regex(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]; } diff --git a/packages/circom/tests/circuits/reveal_check1_regex.circom b/packages/circom/tests/circuits/reveal_check1_regex.circom index 51d0da3..d895628 100644 --- a/packages/circom/tests/circuits/reveal_check1_regex.circom +++ b/packages/circom/tests/circuits/reveal_check1_regex.circom @@ -9,9 +9,11 @@ template RevealCheck1Regex(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]; } diff --git a/packages/circom/tests/circuits/reveal_check2_regex.circom b/packages/circom/tests/circuits/reveal_check2_regex.circom index 62e8f96..15cc8b2 100644 --- a/packages/circom/tests/circuits/reveal_check2_regex.circom +++ b/packages/circom/tests/circuits/reveal_check2_regex.circom @@ -9,9 +9,11 @@ template RevealCheck2Regex(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]; } diff --git a/packages/circom/tests/circuits/simple_regex.circom b/packages/circom/tests/circuits/simple_regex.circom index 202099d..b4329ab 100644 --- a/packages/circom/tests/circuits/simple_regex.circom +++ b/packages/circom/tests/circuits/simple_regex.circom @@ -9,9 +9,11 @@ template SimpleRegex(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]; } diff --git a/packages/circom/tests/circuits/simple_regex_decomposed.circom b/packages/circom/tests/circuits/simple_regex_decomposed.circom index d26a005..35ad0b6 100644 --- a/packages/circom/tests/circuits/simple_regex_decomposed.circom +++ b/packages/circom/tests/circuits/simple_regex_decomposed.circom @@ -9,9 +9,11 @@ template SimpleRegexDecomposed(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]; } diff --git a/packages/circom/tests/from_addr.test.js b/packages/circom/tests/from_addr.test.js index bfc8baa..36c8117 100644 --- a/packages/circom/tests/from_addr.test.js +++ b/packages/circom/tests/from_addr.test.js @@ -216,7 +216,7 @@ describe("From Addr Regex", () => { } }); - it("invalid from field", async () => { + it("from field in the invalid field", async () => { const fromStr = "\r\nto:from:Sora Suegami \r\n"; const paddedStr = apis.padString(fromStr, 1024); const circuitInputs = { @@ -228,6 +228,21 @@ describe("From Addr Regex", () => { await expect(failFn).rejects.toThrow(); }); + it('invalid from field with 255', async () => { + const fromStr = `from:Sora Suegami \r\n`; + let paddedStr = apis.padString(fromStr, 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(); + }); + it("from field containing @ in the name part", async () => { const fromStr = "from:Sora Suegami \r\n"; const paddedStr = apis.padString(fromStr, 1024); @@ -353,5 +368,4 @@ describe("From Addr Regex", () => { } } }); - }); diff --git a/packages/circom/tests/subject_all.test.js b/packages/circom/tests/subject_all.test.js index d1d8d5c..0c82cd7 100644 --- a/packages/circom/tests/subject_all.test.js +++ b/packages/circom/tests/subject_all.test.js @@ -87,7 +87,7 @@ describe("Subject All Regex", () => { } }); - it("invalid subject", async () => { + it("subject in the invalid field", async () => { const subjectStr = "\r\nto:subject:This is a subject in To field.\r\n"; const paddedStr = apis.padString(subjectStr, 256); const circuitInputs = { @@ -100,4 +100,19 @@ describe("Subject All Regex", () => { expect(0n).toEqual(witness[2 + idx]); } }); + + it('invalid subject field with 255', async () => { + const subjectStr = `subject:This is a subject in To field.\r\n`; + let paddedStr = apis.padString(subjectStr, 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(); + }); }); diff --git a/packages/circom/tests/to_addr.test.js b/packages/circom/tests/to_addr.test.js index 266814a..c6b9cc6 100644 --- a/packages/circom/tests/to_addr.test.js +++ b/packages/circom/tests/to_addr.test.js @@ -215,7 +215,7 @@ describe("To Addr Regex", () => { } }); - it("invalid to field", async () => { + it("to field in the invalid field", async () => { const toStr = "subject:to:adityabisht@gmail.com\r\n"; const paddedStr = apis.padString(toStr, 1024); const circuitInputs = { @@ -227,6 +227,21 @@ describe("To Addr Regex", () => { await expect(failFn).rejects.toThrow(); }); + it('invalid to field with 255', async () => { + const toStr = `to:adityabisht@gmail.com\r\n`; + let paddedStr = apis.padString(toStr, 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(); + }); + it("to field containing @ in the name part", async () => { const toStr = "to:Aditya Bisht \r\n"; const paddedStr = apis.padString(toStr, 1024); diff --git a/packages/compiler/Cargo.toml b/packages/compiler/Cargo.toml index bc4b32b..72cce7d 100644 --- a/packages/compiler/Cargo.toml +++ b/packages/compiler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zk-regex-compiler" -version = "2.3.0" +version = "2.3.1" authors = [ "Javier Su ", "Kata Choi ", diff --git a/packages/compiler/package.json b/packages/compiler/package.json index c86d4c4..8b99558 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -1,6 +1,6 @@ { "name": "@zk-email/zk-regex-compiler", - "version": "2.3.0", + "version": "2.3.1", "description": "A compiler to generate a regex verification circuit in circom from a user-defined regex. Please check [zk-regex](https://github.com/zkemail/zk-regex/tree/main) for the detail.", "contributors": [ "Javier Su ", diff --git a/packages/compiler/src/circom.rs b/packages/compiler/src/circom.rs index 2180489..7c53b6c 100644 --- a/packages/compiler/src/circom.rs +++ b/packages/compiler/src/circom.rs @@ -582,9 +582,11 @@ fn generate_declarations( "".to_string(), "\tvar num_bytes = msg_bytes+1;".to_string(), "\tsignal in[num_bytes];".to_string(), + "\tsignal in_range_checks[msg_bytes];".to_string(), "\tin[0]<==255;".to_string(), "\tfor (var i = 0; i < msg_bytes; i++) {".to_string(), - "\t\t_ <== LessThan(8)([msg[i], 255]);".to_string(), + "\t\tin_range_checks[i] <== LessThan(8)([msg[i], 255]);".to_string(), + "\t\tin_range_checks[i] === 1;".to_string(), "\t\tin[i+1] <== msg[i];".to_string(), "\t}".to_string(), "".to_string(),