forked from visoftsolutions/noir_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: proof generation using local srs transcript file
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
1 parent
9e99af0
commit 75dd85e
Showing
6 changed files
with
374 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.