diff --git a/Cargo.toml b/Cargo.toml index b57ca9d..b21ad6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,10 @@ members = [ "noir_rs", ] +exclude = [ + "examples" +] + resolver = "2" [workspace.package] diff --git a/examples/poly_check_circuit/.gitignore b/examples/poly_check_circuit/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/examples/poly_check_circuit/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/poly_check_circuit/Cargo.toml b/examples/poly_check_circuit/Cargo.toml new file mode 100644 index 0000000..bd590da --- /dev/null +++ b/examples/poly_check_circuit/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "poly_check_circuit" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +noir_rs = { git = "https://github.com/visoftsolutions/noir_rs.git" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/examples/poly_check_circuit/Nargo.toml b/examples/poly_check_circuit/Nargo.toml new file mode 100644 index 0000000..c08ec92 --- /dev/null +++ b/examples/poly_check_circuit/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "poly_check_circuit" +type = "bin" +authors = [""] +compiler_version = "0.10.5" + +[dependencies] \ No newline at end of file diff --git a/examples/poly_check_circuit/README.md b/examples/poly_check_circuit/README.md new file mode 100644 index 0000000..253f7fb --- /dev/null +++ b/examples/poly_check_circuit/README.md @@ -0,0 +1,29 @@ +# `noir_rs`: Polynomial Value Check Circuit Example + +## Introduction + +Welcome to the `noir_rs` library, a Rust-based library designed for creating and working with zero-knowledge proofs. This example demonstrates the use of `noir_rs` in conjunction with `nargo`, showcasing a single project structure. + +The Polynomial Value Check Circuit example illustrates how to construct and verify a circuit that checks the value of a polynomial at a given point. + +## Running the Example + +To run the Polynomial Value Check Circuit example, follow these steps: + +1. **Compile the Circuit** + + Use `nargo` to compile the circuit: + + ```bash + nargo compile + ``` + +2. **Run the Circuit** + + Once compiled, you can run the circuit using Cargo: (compiles barretenberg backend will take a while) + + ```bash + cargo run + ``` + +This will execute the example, demonstrating the functionality of the `noir_rs` library in a practical scenario. \ No newline at end of file diff --git a/examples/poly_check_circuit/src/main.nr b/examples/poly_check_circuit/src/main.nr new file mode 100644 index 0000000..845b8d1 --- /dev/null +++ b/examples/poly_check_circuit/src/main.nr @@ -0,0 +1,13 @@ +fn main(y : Field, x : pub Field) { + // y <= 4x^3 + 7x + 1 + let x_3 = x * x * x; + assert(y as u32 <= (4 * x_3 + 7 * x + 1) as u32); +} + +#[test] +fn test_main() { + main(39, 2); + + // Uncomment to make test fail + // main(1, 1); +} diff --git a/examples/poly_check_circuit/src/main.rs b/examples/poly_check_circuit/src/main.rs new file mode 100644 index 0000000..cc23d34 --- /dev/null +++ b/examples/poly_check_circuit/src/main.rs @@ -0,0 +1,18 @@ +use std::fs; +use serde_json::Value; +use noir_rs::{native_types::{Witness, WitnessMap}, FieldElement}; + +static PACKAGE_NAME: &str = env!("CARGO_PKG_NAME"); + +fn main() { + let data = fs::read_to_string(format!("target/{}.json", PACKAGE_NAME)).expect("Unable to read file"); + let json: Value = serde_json::from_str(&data).expect("Unable to parse JSON"); + let bytecode: &str = json["bytecode"].as_str().expect("Unable to extract bytecode"); + + let mut initial_witness = WitnessMap::new(); + initial_witness.insert(Witness(1), FieldElement::from(47_i128)); + initial_witness.insert(Witness(2), FieldElement::from(2_i128)); + let (proof, vk) = noir_rs::prove(String::from(bytecode), initial_witness).unwrap(); + let verdict = noir_rs::verify(String::from(bytecode), proof, vk).unwrap(); + assert!(verdict); +}