-
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.
- Loading branch information
1 parent
66cd35b
commit 27b8107
Showing
12 changed files
with
582 additions
and
79 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,76 @@ | ||
mod formats; | ||
|
||
use anyhow::{Context, Result}; | ||
use clap::{Parser, Subcommand}; | ||
use plonky2_verifier::Plonky2Config; | ||
use std::fs::File; | ||
use std::io::{self, Write}; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command( | ||
name = "plonky2-converter", | ||
about = "Converts plonky2 formats", | ||
version | ||
)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Commands { | ||
/// Serialize VerifierCircuitData into zkVerify format. | ||
Vk(VkArgs), | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
struct VkArgs { | ||
#[arg(short, long, value_enum, default_value_t = formats::VerifierCircuitDataFormat::default())] | ||
in_fmt: formats::VerifierCircuitDataFormat, | ||
|
||
#[arg(short, long, value_enum, default_value_t = formats::Format::default())] | ||
out_fmt: formats::Format, | ||
|
||
input: PathBuf, | ||
output: Option<PathBuf>, | ||
|
||
#[arg(short, long, value_enum, default_value_t = Plonky2Config::default())] | ||
config: Plonky2Config, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
env_logger::init(); | ||
let cli = Cli::parse(); | ||
match cli.command { | ||
Commands::Vk(args) => handle_vk(args), | ||
} | ||
} | ||
|
||
fn handle_vk(args: VkArgs) -> Result<()> { | ||
log::info!("Processing VK command with args: {:?}", args); | ||
|
||
let vk_bytes = std::fs::read(&args.input).with_context(|| { | ||
format!( | ||
"Could not read file {:?}. Ensure the file exists and is accessible.", | ||
&args.input | ||
) | ||
})?; | ||
let vk = args.in_fmt.decode(vk_bytes, args.config)?; | ||
let mut out = out_file(args.output.as_ref())?; | ||
args.out_fmt.write_vk(&vk, &mut out)?; | ||
|
||
log::info!("Successfully wrote output"); | ||
Ok(()) | ||
} | ||
|
||
fn out_file(output: Option<&PathBuf>) -> Result<Box<dyn Write>> { | ||
match output { | ||
Some(path) => { | ||
Ok(Box::new(File::create(path).with_context(|| { | ||
format!("Failed to create output file {:?}", path) | ||
})?)) | ||
} | ||
None => Ok(Box::new(io::stdout())), | ||
} | ||
} |
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,70 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::ValueEnum; | ||
use plonky2_verifier::Plonky2Config; | ||
use plonky2_verifier::Vk; | ||
use std::io; | ||
|
||
/// Supported formats for input verifier circuit data. | ||
#[derive(Copy, Default, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Hash)] | ||
pub enum VerifierCircuitDataFormat { | ||
/// Raw binary bytes. | ||
#[default] | ||
Bytes, | ||
/// Hex-encoded string (with optional "0x" prefix). | ||
Hex, | ||
} | ||
|
||
impl VerifierCircuitDataFormat { | ||
/// Decodes the verifier circuit data from the specified format. | ||
pub fn decode(&self, vk_bytes: Vec<u8>, config: Plonky2Config) -> Result<Vk> { | ||
let vk_bytes = match self { | ||
VerifierCircuitDataFormat::Bytes => vk_bytes, | ||
VerifierCircuitDataFormat::Hex => { | ||
let hex_str = vk_bytes.strip_prefix(b"0x").unwrap_or(&vk_bytes); | ||
hex::decode(hex_str).with_context(|| { | ||
format!( | ||
"Failed to decode hex string: {:?}", | ||
String::from_utf8_lossy(hex_str) | ||
) | ||
})? | ||
} | ||
}; | ||
|
||
Ok(Vk { | ||
vk_bytes: bytes, | ||
config, | ||
}) | ||
} | ||
} | ||
|
||
/// Formats for outputting serialized verifier circuit data. | ||
#[derive(Copy, Default, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Hash)] | ||
pub enum Format { | ||
/// JSON format (pretty-printed). | ||
#[default] | ||
Json, | ||
/// Raw binary bytes. | ||
Bytes, | ||
/// Hex-encoded string. | ||
Hex, | ||
} | ||
|
||
impl Format { | ||
/// Writes the verifier circuit data (`Vk`) to the specified output in the selected format. | ||
pub fn write_vk(&self, vk: &Vk, out: &mut dyn io::Write) -> Result<()> { | ||
match self { | ||
Format::Json => { | ||
serde_json::to_writer_pretty(out, vk).context("Failed to serialize Vk as JSON")?; | ||
} | ||
Format::Bytes => { | ||
out.write_all(&vk.as_bytes()) | ||
.context("Failed to write Vk as raw bytes")?; | ||
} | ||
Format::Hex => { | ||
out.write_all(vk.as_hex().as_bytes()) | ||
.context("Failed to write Vk as a hex string")?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
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,16 @@ | ||
//! Configuration for `plonky2` verifier. | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg(feature = "converter")] | ||
use clap::ValueEnum; | ||
|
||
/// Config for `Plonky2` proving system with options, acceptable by `zkVerify`. | ||
#[derive(Copy, Default, Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
#[cfg_attr(feature = "converter", derive(ValueEnum))] | ||
pub enum Plonky2Config { | ||
/// Preset Keccak over Goldilocks config available in `plonky2` | ||
Keccak, | ||
/// Preset Poseidon over Goldilocks config available in `plonky2` | ||
#[default] | ||
Poseidon, | ||
} |
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
Oops, something went wrong.