Skip to content

Commit

Permalink
feat: proof generation using local srs transcript file
Browse files Browse the repository at this point in the history
Implemented additional functions for proof generation leveraging either local or remote SRS data, offering the choice between using a local SRS transcript file or fetching from a default or specified remote URL.

Refactored existing `prove` function to use a new shared utility function `prove_with_srs`, which accepts an SRS strategy object for SRS data handling.

Implemented support for loading local SRS data in the introduced `LocalSrs` trait, which reads the necessary SRS data from a given file path, and updated `NetSrs` trait to optionally accept a custom URL. This makes the proof generation process more portable and less reliant on an internet connection.

Added serial test annotations to prevent state pollution between tests that access shared resources.
  • Loading branch information
michaelelliot committed Jan 24, 2024
1 parent 9e99af0 commit 75dd85e
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 58 deletions.
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"
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

0 comments on commit 75dd85e

Please sign in to comment.