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

feat: proof generation using local srs transcript file #5

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .github/workflows/build&test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Build
working-directory: noir/tooling/noir_rs
run: cargo build -vv
- name: Download SRS transcript
working-directory: noir/tooling/noir_rs
run: curl -LO https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat
- name: Run tests
working-directory: noir/tooling/noir_rs
run: cargo test -vv
38 changes: 38 additions & 0 deletions noir/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion noir/tooling/noir_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ bincode.workspace = true
flate2.workspace = true
hex.workspace = true
serde.workspace = true
thiserror.workspace = true
thiserror.workspace = true

[dev-dependencies]
serial_test = "3.0.0"
6 changes: 5 additions & 1 deletion noir/tooling/noir_rs/barretenberg/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::env;
use cmake::Config;

fn main() {
// Notify Cargo to rerun this build script if `build.rs` changes.
println!("cargo:rerun-if-changed=build.rs");

// Retrieve CMAKE_BUILD_TYPE from environment or use default
let cmake_build_type = env::var("CMAKE_BUILD_TYPE").unwrap_or_else(|_| "RelWithAssert".to_string());

// Build the C++ code using CMake and get the build directory path.
let dst = Config::new("../../../../barretenberg/cpp")
.configure_arg("-DCMAKE_BUILD_TYPE=RelWithAssert")
.define("CMAKE_BUILD_TYPE", &cmake_build_type)
.define("TARGET_ARCH", "skylake")
.build();

Expand Down
92 changes: 92 additions & 0 deletions noir/tooling/noir_rs/barretenberg/src/srs/localsrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};

use super::Srs;

#[derive(Debug)]
pub struct LocalSrs {
pub g1_data: Option<Vec<u8>>,
pub g2_data: Option<Vec<u8>>,
pub num_points: Option<u32>,
pub srs_path: String,
}

impl LocalSrs {
/// Creates a new LocalSrs instance for loading the required SRS data from a local transcript file.
///
/// # Arguments
/// * `srs_path` - Local file path of SRS transcript.
pub fn new(srs_path: &str) -> Self {
LocalSrs {
num_points: None,
g1_data: None,
g2_data: None,
srs_path: srs_path.to_string(),
}
}

/// Returns the G1 data from a local SRS transcript file based on the specified number of points.
///
/// # Arguments
/// * `num_points` - Number of points required for G1 data.
///
/// # Returns
/// * `Vec<u8>` - A byte vector containing the G1 data.
fn get_g1_data(&self, num_points: u32) -> Vec<u8> {
const G1_START: u64 = 28;
let g1_end: u64 = G1_START + num_points as u64 * 64 - 1;

let mut file = File::open(self.srs_path.clone()).unwrap();
file.seek(SeekFrom::Start(G1_START)).unwrap();

let mut buffer = Vec::new();
let read_length = (g1_end - G1_START + 1) as usize;
buffer.resize(read_length, 0);
file.read_exact(&mut buffer).unwrap();

buffer[..].to_vec()
}

/// Returns the G2 data from a local SRS transcript file.
///
/// # Returns
/// * `Vec<u8>` - A byte vector containing the G2 data.
fn get_g2_data(&self) -> Vec<u8> {
const G2_START: u64 = 28 + 5040001 * 64;
const G2_END: u64 = G2_START + 128 - 1;

let mut file = File::open(self.srs_path.clone()).unwrap();
file.seek(SeekFrom::Start(G2_START)).unwrap();

let mut buffer = Vec::new();
let read_length = (G2_END - G2_START + 1) as usize;
buffer.resize(read_length, 0);
file.read_exact(&mut buffer).unwrap();

buffer[..].to_vec()
}
}

impl Srs for LocalSrs {
/// Loads the required SRS data into memory.
///
/// # Arguments
/// * `num_points` - Number of points required for G1 data.
fn load_data(&mut self, num_points: u32) {
self.num_points = Some(num_points);
self.g1_data = Some(self.get_g1_data(num_points));
self.g2_data = Some(self.get_g2_data());
}

fn g1_data(&self) -> &Vec<u8> {
&self.g1_data.as_ref().unwrap()
}

fn g2_data(&self) -> &Vec<u8> {
&self.g2_data.as_ref().unwrap()
}

fn num_points(&self) -> u32 {
self.num_points.unwrap()
}
}
8 changes: 8 additions & 0 deletions noir/tooling/noir_rs/barretenberg/src/srs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ use crate::rust_srs_init_srs;

use super::{parse_c_str, BackendError};

pub mod localsrs;
pub mod netsrs;

pub trait Srs {
fn load_data(&mut self, num_points: u32);
fn g1_data(&self) -> &Vec<u8>;
fn g2_data(&self) -> &Vec<u8>;
fn num_points(&self) -> u32;
}

/// Initializes the SRS inside the C++ backend.
///
/// Uses the trusted setup data downloaded by the `NetSrs` struct and provides it to a C++ backend function to set up the SRS.
Expand Down
64 changes: 43 additions & 21 deletions noir/tooling/noir_rs/barretenberg/src/srs/netsrs.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,92 @@
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, RANGE};

use super::Srs;

#[derive(Debug)]
pub struct NetSrs {
pub data: Vec<u8>,
pub g2_data: Vec<u8>,
pub num_points: u32,
pub g1_data: Option<Vec<u8>>,
pub g2_data: Option<Vec<u8>>,
pub num_points: Option<u32>,
pub srs_url: String,
}

impl NetSrs {
/// Creates a new NetSrs instance by downloading the required SRS data from Noir Cloud.
/// Creates a new NetSrs instance for remotely downloading the required SRS data from a URL.
///
/// # Arguments
/// * `num_points` - Number of points required for G1 data.
pub fn new(num_points: u32) -> Self {
/// * `srs_url` - URL to SRS transcript file.
pub fn new(srs_url: &str) -> Self {
NetSrs {
num_points,
data: Self::download_g1_data(num_points),
g2_data: Self::download_g2_data(),
num_points: None,
g1_data: None,
g2_data: None,
srs_url: srs_url.to_string(),
}
}

/// Downloads the G1 data from Noir Cloud based on the specified number of points.
/// Downloads the G1 data from a URL based on the specified number of points.
///
/// # Arguments
/// * `num_points` - Number of points required for G1 data.
///
/// # Returns
/// * `Vec<u8>` - A byte vector containing the G1 data.
fn download_g1_data(num_points: u32) -> Vec<u8> {
fn get_g1_data(&self, num_points: u32) -> Vec<u8> {
const G1_START: u32 = 28;
let g1_end: u32 = G1_START + num_points * 64 - 1;

let mut headers = HeaderMap::new();
headers.insert(RANGE, format!("bytes={}-{}", G1_START, g1_end).parse().unwrap());

let response = Client::new()
.get(
"https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat",
)
.get(self.srs_url.clone())
.headers(headers)
.send()
.unwrap();

response.bytes().unwrap().to_vec()
}

/// Downloads the G2 data from Noir Cloud.
/// Downloads the G2 data from a URL.
///
/// # Returns
/// * `Vec<u8>` - A byte vector containing the G2 data.
fn download_g2_data() -> Vec<u8> {
fn get_g2_data(&self) -> Vec<u8> {
const G2_START: usize = 28 + 5040001 * 64;
const G2_END: usize = G2_START + 128 - 1;

let mut headers = HeaderMap::new();
headers.insert(RANGE, format!("bytes={}-{}", G2_START, G2_END).parse().unwrap());

let response = Client::new()
.get(
"https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat",
)
.get(self.srs_url.clone())
.headers(headers)
.send()
.unwrap();

response.bytes().unwrap().to_vec()
}
}

impl Srs for NetSrs {
/// Downloads and loads the required SRS data into memory.
///
/// # Arguments
/// * `num_points` - Number of points required for G1 data.
fn load_data(&mut self, num_points: u32) {
self.num_points = Some(num_points);
self.g1_data = Some(self.get_g1_data(num_points));
self.g2_data = Some(self.get_g2_data());
}

fn g1_data(&self) -> &Vec<u8> {
&self.g1_data.as_ref().unwrap()
}

fn g2_data(&self) -> &Vec<u8> {
&self.g2_data.as_ref().unwrap()
}

fn num_points(&self) -> u32 {
self.num_points.unwrap()
}
}
Loading