Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Nowak committed Nov 28, 2023
1 parent bcb4062 commit ecd9386
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ members = [
"noir_rs",
]

exclude = [
"examples"
]

resolver = "2"

[workspace.package]
Expand Down
1 change: 1 addition & 0 deletions examples/poly_check_circuit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
11 changes: 11 additions & 0 deletions examples/poly_check_circuit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions examples/poly_check_circuit/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "poly_check_circuit"
type = "bin"
authors = [""]
compiler_version = "0.10.5"

[dependencies]
29 changes: 29 additions & 0 deletions examples/poly_check_circuit/README.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions examples/poly_check_circuit/src/main.nr
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 18 additions & 0 deletions examples/poly_check_circuit/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit ecd9386

Please sign in to comment.