-
Notifications
You must be signed in to change notification settings - Fork 2
Add CLI Commands for Frida-poc #17
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
1ef5db0
feat: Add initial function stubs and main function structure
ca7064a
feat: Add generate_data
a70d1c2
feat: Add a test for generate_data
6d8fa26
feat: Add commit
0234c9a
feat: Add a test for commit
5e59f6e
chore: Refactor fri_options to fri_options_path
0d971ad
feat: Change default value location to command
96e6efc
Merge branch 'main' into feature/cli
520695c
feat: Add serialization for Commitment
dbee966
feat: Implement Serializable to Commitment
d1893cb
feat: Add open command
ed85773
feat: Initialize prover in the main
145f427
feat: Complete CLI implementation
9df2757
refactor: Refactor CLI codes
c73129a
Merge branch 'main' of github.com:NethermindEth/Frida-poc into featur…
56a3f5c
fix: Fix errors caused by change in commitment struct
83e22d4
refactor: Refactor handle_* functions
820e93a
refactor: Add CleanupFiles struct and use Path types
6edefee
refactor: Change deserialization of FriOptions with BP
97bfe52
fix: Use .display() instead of {:?}
bde35bb
refactor: Refactor error handling
b0adee7
refactor: Change to unwrap prover on an outer level
bc249c7
Fix conflicts with main branch
ali-rezai 85e6d63
fix
ali-rezai 41c015f
Fix main CLI
ali-rezai 4fd065a
Fix cli sub command issue
ali-rezai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 +1,6 @@ | ||
| target | ||
| data | ||
|
|
||
| # IDEs | ||
| .vscode | ||
| .idea | ||
|
|
||
| target | ||
| data |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,49 +1,84 @@ | ||
| use frida_poc::{ | ||
| frida_data::encoded_data_element_count, | ||
| frida_prover::{traits::BaseFriProver, FridaProver}, | ||
| use crate::{ | ||
| frida_prover::{Commitment, FridaProver}, | ||
| frida_prover_channel::FridaProverChannel, | ||
| frida_random::FridaRandom, | ||
| utils::{read_file_to_vec, write_to_file}, | ||
| }; | ||
| use std::path::Path; | ||
| use winter_crypto::hashers::Blake3_256; | ||
| use winter_fri::FriOptions; | ||
| use winter_math::fields::f128::BaseElement; | ||
| use winter_utils::{Deserializable, Serializable}; | ||
|
|
||
| type Blake3 = Blake3_256<BaseElement>; | ||
| type FridaChannel = | ||
| FridaProverChannel<BaseElement, Blake3, Blake3, FridaRandom<Blake3, Blake3, BaseElement>>; | ||
| type FridaProverType = FridaProver<BaseElement, BaseElement, FridaChannel, Blake3>; | ||
|
|
||
| pub fn run(data_path: &str, num_queries: usize, options: FriOptions) { | ||
| let data = std::fs::read(data_path).expect("Unable to read data file"); | ||
| let mut prover: FridaProverType = FridaProver::new(options.clone()); | ||
| /// Runs the commitment process, saving the commitment to a file. | ||
| pub fn run( | ||
| prover: &mut FridaProverType, | ||
| num_queries: usize, | ||
| data_path: &Path, | ||
| commitment_path: &Path, | ||
| ) -> Result<Commitment<Blake3>, Box<dyn std::error::Error>> { | ||
| // Read data from file | ||
| let data = read_file_to_vec(data_path)?; | ||
|
|
||
| let encoded_element_count = | ||
| encoded_data_element_count::<BaseElement>(data.len()).next_power_of_two(); | ||
| // Create commitment from data | ||
| let (commitment, _) = | ||
| prover | ||
| .commit(data, num_queries) | ||
| .map_err(|e| -> Box<dyn std::error::Error> { | ||
| format!("Prover commit error: {}", e).into() | ||
| })?; | ||
|
|
||
| let (commitment, _) = prover.commit(data.clone(), num_queries).unwrap(); | ||
| // TODO: Save commitment to file | ||
| // Write commitment to file | ||
| let commitment_bytes = commitment.to_bytes(); | ||
| write_to_file(commitment_path, &commitment_bytes)?; | ||
|
|
||
| println!( | ||
| "Data committed with commitment: {:?} and encoded element count: {}", | ||
| commitment, encoded_element_count | ||
| ); | ||
| println!("Commitment created and saved to {:?}", commitment_path); | ||
| Ok(commitment) | ||
| } | ||
|
|
||
| /// Reads the commitment from a file. | ||
| pub fn read_commitment_from_file( | ||
| file_path: &Path, | ||
| ) -> Result<Commitment<Blake3>, Box<dyn std::error::Error>> { | ||
| let commitment_bytes = read_file_to_vec(file_path)?; | ||
| let commitment = Commitment::<Blake3>::read_from_bytes(&commitment_bytes).map_err( | ||
| |e| -> Box<dyn std::error::Error> { format!("Deserialization error: {}", e).into() }, | ||
| )?; | ||
| Ok(commitment) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::{ | ||
| commands::generate_data, frida_prover::traits::BaseFriProver, utils::CleanupFiles, | ||
| }; | ||
| use winter_fri::FriOptions; | ||
|
|
||
| #[test] | ||
| fn test_commit() { | ||
| let data_path = "data/data.bin"; | ||
| assert!( | ||
| std::path::Path::new(data_path).exists(), | ||
| "Test data file does not exist" | ||
| ); | ||
| let data_path = Path::new("data/data.bin"); | ||
| let commitment_path = Path::new("data/commitment.bin"); | ||
|
|
||
| let _cleanup = CleanupFiles::new(vec![data_path, commitment_path]); | ||
|
|
||
| if !data_path.exists() { | ||
| generate_data::run(200, data_path).unwrap(); | ||
| } | ||
|
|
||
| let mut prover = FridaProverType::new(FriOptions::new(8, 2, 7)); | ||
|
|
||
| // Run the commitment process | ||
| let commitment = run(&mut prover, 31, data_path, commitment_path).unwrap(); | ||
|
|
||
| let options = FriOptions::new(8, 2, 7); | ||
| run(data_path, 31, options); | ||
| // Read the commitment from the file | ||
| let commitment_file = read_commitment_from_file(commitment_path).unwrap(); | ||
|
|
||
| // TODO: Check if the commitment file is correct | ||
| // Verify the commitment | ||
| assert_eq!(commitment, commitment_file, "Commitment does not match."); | ||
| } | ||
| } | ||
This file contains hidden or 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,26 +1,80 @@ | ||
| use std::fs::File; | ||
| use std::io::Write; | ||
| use crate::utils::write_to_file; | ||
| use std::{fs, io, path::Path}; | ||
| use winter_rand_utils::rand_vector; | ||
|
|
||
| pub fn run(size: usize, file_path: &str) { | ||
| pub fn run(size: usize, file_path: &Path) -> Result<Vec<u8>, GenerateDataError> { | ||
| // Generate random data | ||
| let data = rand_vector::<u8>(size); | ||
| let mut file = File::create(file_path).expect("Unable to create file"); | ||
| file.write_all(&data).expect("Unable to write data"); | ||
| println!("Generated data of size {} and saved to {}", size, file_path); | ||
|
|
||
| // Ensure directory exists | ||
| if let Some(parent) = std::path::Path::new(file_path).parent() { | ||
| fs::create_dir_all(parent).map_err(GenerateDataError::IoError)?; | ||
| } | ||
|
|
||
| // Write data to file | ||
| write_to_file(file_path, &data).map_err(GenerateDataError::IoError)?; | ||
|
|
||
| // Print success message | ||
| println!( | ||
| "Generated data of size {} and saved to {}", | ||
| size, | ||
| file_path.display() | ||
| ); | ||
|
|
||
| Ok(data) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::fs; | ||
| use crate::utils::{read_file_to_vec, CleanupFiles}; | ||
|
|
||
| #[test] | ||
| fn test_generate_data() { | ||
| fn test_generate_data() -> Result<(), GenerateDataError> { | ||
| let size = 200; | ||
| let file_path = "data/data.bin"; | ||
| run(size, file_path); | ||
| let metadata = fs::metadata(file_path).expect("Unable to read metadata"); | ||
| assert!(metadata.is_file()); | ||
| assert_eq!(metadata.len(), size as u64); | ||
| let file_path = Path::new("data/data.bin"); | ||
|
|
||
| let _cleanup = CleanupFiles::new(vec![file_path]); | ||
|
|
||
| // Generate data and write to file | ||
| let data = run(size, file_path)?; | ||
|
|
||
| // Read data from file | ||
| let file_data = read_file_to_vec(file_path).map_err(GenerateDataError::IoError)?; | ||
|
|
||
| // Verify data | ||
| assert_eq!(data, file_data); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum GenerateDataError { | ||
| IoError(io::Error), | ||
| CustomError(String), | ||
| } | ||
|
|
||
| impl std::fmt::Display for GenerateDataError { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match *self { | ||
| GenerateDataError::IoError(ref err) => write!(f, "IO error: {}", err), | ||
| GenerateDataError::CustomError(ref err) => write!(f, "Custom error: {}", err), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for GenerateDataError { | ||
| fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
| match *self { | ||
| GenerateDataError::IoError(ref err) => Some(err), | ||
| GenerateDataError::CustomError(_) => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<io::Error> for GenerateDataError { | ||
| fn from(err: io::Error) -> GenerateDataError { | ||
| GenerateDataError::IoError(err) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.