-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70a1277
commit 0cba201
Showing
8 changed files
with
274 additions
and
916 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"max_byte_size": 64, | ||
"parts": [ | ||
{ | ||
"is_public": false, | ||
"regex_def": "a:", | ||
"max_size": 2 | ||
}, | ||
{ | ||
"is_public": true, | ||
"regex_def": "[^abcdefghijklmnopqrstuvwxyz]+", | ||
"max_size": 128 | ||
}, | ||
{ | ||
"is_public": false, | ||
"regex_def": ".", | ||
"max_size": 1 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "zk-regex-circom/circuits/regex_helpers.circom"; | ||
|
||
template Negate1Regex(msg_bytes) { | ||
signal input msg[msg_bytes]; | ||
signal output out; | ||
|
||
var num_bytes = msg_bytes+1; | ||
signal in[num_bytes]; | ||
in[0]<==128; | ||
for (var i = 0; i < msg_bytes; i++) { | ||
in[i+1] <== msg[i]; | ||
} | ||
|
||
component eq[4][num_bytes]; | ||
component lt[4][num_bytes]; | ||
component and[7][num_bytes]; | ||
component multi_or[2][num_bytes]; | ||
signal states[num_bytes+1][5]; | ||
component state_changed[num_bytes]; | ||
|
||
states[0][0] <== 1; | ||
for (var i = 1; i < 5; i++) { | ||
states[0][i] <== 0; | ||
} | ||
|
||
for (var i = 0; i < num_bytes; i++) { | ||
state_changed[i] = MultiOR(4); | ||
lt[0][i] = LessThan(8); | ||
lt[0][i].in[0] <== 96; | ||
lt[0][i].in[1] <== in[i]; | ||
lt[1][i] = LessThan(8); | ||
lt[1][i].in[0] <== in[i]; | ||
lt[1][i].in[1] <== 123; | ||
and[0][i] = AND(); | ||
and[0][i].a <== lt[0][i].out; | ||
and[0][i].b <== lt[1][i].out; | ||
eq[0][i] = IsEqual(); | ||
eq[0][i].in[0] <== in[i]; | ||
eq[0][i].in[1] <== 46; | ||
and[1][i] = AND(); | ||
and[1][i].a <== states[i][1]; | ||
multi_or[0][i] = MultiOR(2); | ||
multi_or[0][i].in[0] <== and[0][i].out; | ||
multi_or[0][i].in[1] <== eq[0][i].out; | ||
and[1][i].b <== 1 - multi_or[0][i].out; | ||
lt[2][i] = LessThan(8); | ||
lt[2][i].in[0] <== 96; | ||
lt[2][i].in[1] <== in[i]; | ||
lt[3][i] = LessThan(8); | ||
lt[3][i].in[0] <== in[i]; | ||
lt[3][i].in[1] <== 123; | ||
and[2][i] = AND(); | ||
and[2][i].a <== lt[2][i].out; | ||
and[2][i].b <== lt[3][i].out; | ||
and[3][i] = AND(); | ||
and[3][i].a <== states[i][4]; | ||
and[3][i].b <== 1 - and[2][i].out; | ||
multi_or[1][i] = MultiOR(2); | ||
multi_or[1][i].in[0] <== and[1][i].out; | ||
multi_or[1][i].in[1] <== and[3][i].out; | ||
states[i+1][1] <== multi_or[1][i].out; | ||
state_changed[i].in[0] <== states[i+1][1]; | ||
eq[1][i] = IsEqual(); | ||
eq[1][i].in[0] <== in[i]; | ||
eq[1][i].in[1] <== 46; | ||
and[4][i] = AND(); | ||
and[4][i].a <== states[i][1]; | ||
and[4][i].b <== eq[1][i].out; | ||
states[i+1][2] <== and[4][i].out; | ||
state_changed[i].in[1] <== states[i+1][2]; | ||
eq[2][i] = IsEqual(); | ||
eq[2][i].in[0] <== in[i]; | ||
eq[2][i].in[1] <== 97; | ||
and[5][i] = AND(); | ||
and[5][i].a <== states[i][0]; | ||
and[5][i].b <== eq[2][i].out; | ||
states[i+1][3] <== and[5][i].out; | ||
state_changed[i].in[2] <== states[i+1][3]; | ||
eq[3][i] = IsEqual(); | ||
eq[3][i].in[0] <== in[i]; | ||
eq[3][i].in[1] <== 58; | ||
and[6][i] = AND(); | ||
and[6][i].a <== states[i][3]; | ||
and[6][i].b <== eq[3][i].out; | ||
states[i+1][4] <== and[6][i].out; | ||
state_changed[i].in[3] <== states[i+1][4]; | ||
states[i+1][0] <== 1 - state_changed[i].out; | ||
} | ||
|
||
component final_state_result = MultiOR(num_bytes+1); | ||
for (var i = 0; i <= num_bytes; i++) { | ||
final_state_result.in[i] <== states[i][2]; | ||
} | ||
out <== final_state_result.out; | ||
|
||
signal is_consecutive[msg_bytes+1][2]; | ||
is_consecutive[msg_bytes][1] <== 1; | ||
for (var i = 0; i < msg_bytes; i++) { | ||
is_consecutive[msg_bytes-1-i][0] <== states[num_bytes-i][2] * (1 - is_consecutive[msg_bytes-i][1]) + is_consecutive[msg_bytes-i][1]; | ||
is_consecutive[msg_bytes-1-i][1] <== state_changed[msg_bytes-i].out * is_consecutive[msg_bytes-1-i][0]; | ||
} | ||
signal is_substr0[msg_bytes][3]; | ||
signal is_reveal0[msg_bytes]; | ||
signal output reveal0[msg_bytes]; | ||
for (var i = 0; i < msg_bytes; i++) { | ||
is_substr0[i][0] <== 0; | ||
is_substr0[i][1] <== is_substr0[i][0] + states[i+1][4] * states[i+2][1]; | ||
is_substr0[i][2] <== is_substr0[i][1] + states[i+1][1] * states[i+2][1]; | ||
is_reveal0[i] <== is_substr0[i][2] * is_consecutive[i][1]; | ||
reveal0[i] <== in[i+1] * is_reveal0[i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include "./negate1_regex.circom"; | ||
component main = Negate1Regex(64); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const ff = require('ffjavascript'); | ||
const stringifyBigInts = ff.utils.stringifyBigInts; | ||
const circom_tester = require("circom_tester"); | ||
const wasm_tester = circom_tester.wasm; | ||
import * as path from "path"; | ||
const p = "21888242871839275222246405745257275088548364400416034343698204186575808495617"; | ||
const field = new ff.F1Field(p); | ||
const apis = require("../../apis"); | ||
const option = { | ||
include: path.join(__dirname, "../../../node_modules") | ||
}; | ||
|
||
jest.setTimeout(120000); | ||
describe("Negate Regex", () => { | ||
it("case 1 with regex 1", async () => { | ||
const input = "a: ABCDEFG XYZ."; | ||
const paddedStr = apis.padString(input, 64); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
// console.log(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]]; | ||
for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { | ||
for (let idx = 0; idx < 64; ++idx) { | ||
if (revealedIdx[substr_idx].includes(idx)) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
it("case 2 with regex 1", async () => { | ||
// Spanish character "í" has 2 bytes. | ||
const input = "a: CRIPTOGRAFíA."; | ||
const paddedStr = apis.padString(input, 64); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
// console.log(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]; | ||
for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { | ||
for (let idx = 0; idx < 64; ++idx) { | ||
if (revealedIdx[substr_idx].includes(idx)) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
it("case 3 with regex 1", async () => { | ||
/// Each Japanese character has 3 bytes. | ||
const input = "a: あいう."; | ||
const paddedStr = apis.padString(input, 64); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
// console.log(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]; | ||
for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { | ||
for (let idx = 0; idx < 64; ++idx) { | ||
if (revealedIdx[substr_idx].includes(idx)) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
it("case 4 with regex 1", async () => { | ||
/// Arabian character "التشفير" has 14 bytes. | ||
const input = "a: التشفير."; | ||
const paddedStr = apis.padString(input, 64); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
console.log(witness); | ||
console.log(paddedStr); | ||
expect(1n).toEqual(witness[1]); | ||
const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]; | ||
for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { | ||
for (let idx = 0; idx < 64; ++idx) { | ||
if (revealedIdx[substr_idx].includes(idx)) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); | ||
} | ||
} | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters