Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimidumo/zk 849 add external input to proof creation in sdk #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ target
**/.DS_Store
npm-debug.log*
pkg

tests/outputs
65 changes: 65 additions & 0 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,69 @@ mod tests {

Ok(())
}

#[tokio::test]
async fn test_generate_regex_inputs_with_external_inputs() -> Result<()> {
// Get the test file path relative to the project root
let test_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.join("test.eml");

let email = std::fs::read_to_string(test_file)?;

let mut decomposed_regexes = Vec::new();
let part_1 = RegexPartConfig {
is_public: false,
regex_def: "Hi".to_string(),
};
let part_2 = RegexPartConfig {
is_public: true,
regex_def: "!".to_string(),
};

decomposed_regexes.push(DecomposedRegex {
parts: vec![part_1, part_2],
name: "hi".to_string(),
max_length: 64,
location: "body".to_string(),
});

let external_inputs = vec![ExternalInput {
name: "address".to_string(),
value: Some("[email protected]".to_string()),
max_length: 64,
}];

let input = generate_circuit_inputs_with_decomposed_regexes_and_external_inputs(
&email,
decomposed_regexes,
external_inputs,
CircuitInputWithDecomposedRegexesAndExternalInputsParams {
max_body_length: 2816,
max_header_length: 1024,
ignore_body_hash_check: false,
remove_soft_lines_breaks: true,
sha_precompute_selector: None,
},
)
.await?;

// Save the input to a file in the test output directory
let output_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("outputs")
.join("input.json");

// Create the output directory if it doesn't exist
if let Some(parent) = output_file.parent() {
std::fs::create_dir_all(parent)?;
}

// Save the input to a file
let input_str = serde_json::to_string_pretty(&input)?;
std::fs::write(output_file, input_str)?;

Ok(())
}
}
6 changes: 3 additions & 3 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ pub async fn generateCircuitInputsWithDecomposedRegexesAndExternalInputs(
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| async move {
// Deserialize decomposed_regexes
let decomposed_regexes: Vec<DecomposedRegex> = from_value(decomposed_regexes)
.map_err(|_| String::from("Invalid decomposed_regexes input"))?;
.map_err(|e| format!("Invalid decomposed_regexes input: {}", e))?;

// Deserialize external_inputs
let external_inputs: Vec<ExternalInput> = from_value(external_inputs)
.map_err(|_| String::from("Invalid external_inputs input"))?;
.map_err(|e| format!("Invalid external_inputs input: {}", e))?;

// Deserialize params
let params: CircuitInputWithDecomposedRegexesAndExternalInputsParams =
from_value(params).map_err(|_| String::from("Invalid params input"))?;
from_value(params).map_err(|e| format!("Invalid params input: {}", e))?;

// Call the async function and await the result
let circuit_inputs = generate_circuit_inputs_with_decomposed_regexes_and_external_inputs(
Expand Down
50 changes: 46 additions & 4 deletions ts_tests/circuit_intput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { readFile } from "fs/promises";
describe("generateCircuitInputsWithDecomposedRegexesAndExternalInputs test suite", async () => {
await init();
const helloEml = await readFile("tests/fixtures/test.eml", "utf-8");
console.log("got eml: ", helloEml);

test("Should parse valid email", async () => {
test("Should create circuit inputs", async () => {
const decomposedRegexes = [
{
parts: [
Expand Down Expand Up @@ -42,7 +41,50 @@ describe("generateCircuitInputsWithDecomposedRegexesAndExternalInputs test suite
[],
params
);
console.log("inputs: ", inputs);
// expect(parsedEmail).not.toBeUndefined();
expect(inputs).toBeDefined();
});

test("Should create circuit inputs with external inputs", async () => {
const decomposedRegexes = [
{
parts: [
{
is_public: true,
regex_def: "Hi",
},
{
is_public: true,
regex_def: "!",
},
],
name: "hi",
maxLength: 64,
location: "body",
},
];

const externalInputs = [
{
name: "address",
maxLength: 64,
value: "[email protected]"
}
]

const params = {
maxHeaderLength: 2816,
maxBodyLength: 1024,
ignoreBodyHashCheck: false,
removeSoftLinesBreaks: true,
// sha_precompute_selector
};

const inputs = await generateCircuitInputsWithDecomposedRegexesAndExternalInputs(
helloEml,
decomposedRegexes,
externalInputs,
params
);
expect(inputs).toBeDefined();
});
});