-
Notifications
You must be signed in to change notification settings - Fork 21
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(example): cli #275
Merged
Merged
feat(example): cli #275
Changes from all commits
Commits
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 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,3 +1,4 @@ | ||
[alias] | ||
poseidon-example = ["run", "--example", "poseidon", "--release", "--", "--json"] | ||
trivial-example = ["run", "--example", "trivial", "--release", "--", "--json"] | ||
cli-example = ["run", "--example", "cli", "--release", "--"] |
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,207 @@ | ||
use std::{array, num::NonZeroUsize}; | ||
|
||
use clap::{Parser, ValueEnum}; | ||
|
||
#[allow(dead_code)] | ||
mod poseidon; | ||
|
||
use ff::Field; | ||
use poseidon::poseidon_step_circuit::TestPoseidonCircuit; | ||
use sirius::{ | ||
ivc::{step_circuit::trivial, CircuitPublicParamsInput, PublicParams, StepCircuit, IVC}, | ||
poseidon::ROPair, | ||
}; | ||
use tracing::*; | ||
use tracing_subscriber::{filter::LevelFilter, fmt::format::FmtSpan, EnvFilter}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version, about, long_about = None)] | ||
struct Args { | ||
#[arg(value_enum, default_value_t = Circuits::Poseidon) ] | ||
primary_circuit: Circuits, | ||
#[arg(long, default_value_t = 17)] | ||
primary_circuit_k_table_size: u32, | ||
#[arg(long, default_value_t = 21)] | ||
primary_commitment_key_size: usize, | ||
#[arg(long, default_value_t = 1)] | ||
primary_repeat_count: usize, | ||
#[arg(long, default_value_t = 10)] | ||
primary_r_f: usize, | ||
#[arg(long, default_value_t = 10)] | ||
primary_r_p: usize, | ||
#[arg(value_enum, default_value_t = Circuits::Trivial) ] | ||
secondary_circuit: Circuits, | ||
#[arg(long, default_value_t = 17)] | ||
secondary_circuit_k_table_size: u32, | ||
#[arg(long, default_value_t = 21)] | ||
secondary_commitment_key_size: usize, | ||
#[arg(long, default_value_t = 1)] | ||
secondary_repeat_count: usize, | ||
#[arg(long, default_value_t = 10)] | ||
secondary_r_f: usize, | ||
#[arg(long, default_value_t = 10)] | ||
secondary_r_p: usize, | ||
#[arg(long, default_value_t = NonZeroUsize::new(32).unwrap()) ] | ||
limb_width: NonZeroUsize, | ||
#[arg(long, default_value_t = NonZeroUsize::new(10).unwrap()) ] | ||
limbs_count: NonZeroUsize, | ||
#[arg(long, default_value_t = false)] | ||
debug_mode: bool, | ||
#[arg(long, default_value_t = NonZeroUsize::new(1).unwrap()) ] | ||
fold_step_count: NonZeroUsize, | ||
#[arg(long, default_value_t = false)] | ||
json_logs: bool, | ||
} | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)] | ||
enum Circuits { | ||
Poseidon, | ||
Trivial, | ||
} | ||
|
||
use halo2curves::{bn256, grumpkin}; | ||
|
||
use bn256::G1 as C1; | ||
use grumpkin::G1 as C2; | ||
|
||
const MAIN_GATE_SIZE: usize = 5; | ||
const RATE: usize = 4; | ||
|
||
type RandomOracle = sirius::poseidon::PoseidonRO<MAIN_GATE_SIZE, RATE>; | ||
type RandomOracleConstant<F> = <RandomOracle as ROPair<F>>::Args; | ||
|
||
type C1Affine = <C1 as halo2curves::group::prime::PrimeCurve>::Affine; | ||
type C1Scalar = <C1 as halo2curves::group::Group>::Scalar; | ||
|
||
type C2Affine = <C2 as halo2curves::group::prime::PrimeCurve>::Affine; | ||
type C2Scalar = <C2 as halo2curves::group::Group>::Scalar; | ||
|
||
fn fold( | ||
args: &Args, | ||
primary: impl StepCircuit<1, C1Scalar>, | ||
secondary: impl StepCircuit<1, C2Scalar>, | ||
) { | ||
let primary_commitment_key = poseidon::get_or_create_commitment_key::<C1Affine>( | ||
args.primary_commitment_key_size, | ||
"bn256", | ||
) | ||
.expect("Failed to get primary key"); | ||
let secondary_commitment_key = poseidon::get_or_create_commitment_key::<C2Affine>( | ||
args.secondary_commitment_key_size, | ||
"grumpkin", | ||
) | ||
.expect("Failed to get secondary key"); | ||
|
||
// Specifications for random oracle used as part of the IVC algorithm | ||
let primary_spec = RandomOracleConstant::<C1Scalar>::new(args.primary_r_f, args.primary_r_p); | ||
let secondary_spec = | ||
RandomOracleConstant::<C2Scalar>::new(args.secondary_r_f, args.secondary_r_p); | ||
|
||
let pp = PublicParams::< | ||
'_, | ||
1, | ||
1, | ||
MAIN_GATE_SIZE, | ||
C1Affine, | ||
C2Affine, | ||
_, | ||
_, | ||
RandomOracle, | ||
RandomOracle, | ||
>::new( | ||
CircuitPublicParamsInput::new( | ||
args.primary_circuit_k_table_size, | ||
&primary_commitment_key, | ||
primary_spec, | ||
&primary, | ||
), | ||
CircuitPublicParamsInput::new( | ||
args.secondary_circuit_k_table_size, | ||
&secondary_commitment_key, | ||
secondary_spec, | ||
&secondary, | ||
), | ||
args.limb_width, | ||
args.limbs_count, | ||
) | ||
.unwrap(); | ||
|
||
let mut rnd = rand::thread_rng(); | ||
let primary_input = array::from_fn(|_| C1Scalar::random(&mut rnd)); | ||
let secondary_input = array::from_fn(|_| C2Scalar::random(&mut rnd)); | ||
|
||
if args.debug_mode { | ||
IVC::fold_with_debug_mode( | ||
&pp, | ||
primary, | ||
primary_input, | ||
secondary, | ||
secondary_input, | ||
args.fold_step_count, | ||
) | ||
.unwrap(); | ||
} else { | ||
IVC::fold( | ||
&pp, | ||
primary, | ||
primary_input, | ||
secondary, | ||
secondary_input, | ||
args.fold_step_count, | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
fn main() { | ||
let args = Args::parse(); | ||
|
||
let builder = tracing_subscriber::fmt() | ||
// Adds events to track the entry and exit of the span, which are used to build | ||
// time-profiling | ||
.with_span_events(FmtSpan::ENTER | FmtSpan::CLOSE) | ||
// Changes the default level to INFO | ||
.with_env_filter( | ||
EnvFilter::builder() | ||
.with_default_directive(LevelFilter::INFO.into()) | ||
.from_env_lossy(), | ||
); | ||
|
||
// Structured logs are needed for time-profiling, while for simple run regular logs are | ||
// more convenient. | ||
// | ||
// So this expr keeps track of the --json argument for turn-on json-logs | ||
if args.json_logs { | ||
builder.json().init(); | ||
} else { | ||
builder.init(); | ||
} | ||
|
||
// To osterize the total execution time of the example | ||
let _span = info_span!("poseidon_example").entered(); | ||
|
||
// Such a redundant call design due to the fact that they are different function types for the | ||
// compiler due to generics | ||
match (args.primary_circuit, args.secondary_circuit) { | ||
(Circuits::Poseidon, Circuits::Trivial) => fold( | ||
&args, | ||
TestPoseidonCircuit::new(args.primary_repeat_count), | ||
trivial::Circuit::default(), | ||
), | ||
(Circuits::Poseidon, Circuits::Poseidon) => fold( | ||
&args, | ||
TestPoseidonCircuit::new(args.primary_repeat_count), | ||
TestPoseidonCircuit::new(args.secondary_repeat_count), | ||
), | ||
(Circuits::Trivial, Circuits::Poseidon) => fold( | ||
&args, | ||
trivial::Circuit::default(), | ||
TestPoseidonCircuit::new(args.secondary_repeat_count), | ||
), | ||
(Circuits::Trivial, Circuits::Trivial) => fold( | ||
&args, | ||
trivial::Circuit::default(), | ||
trivial::Circuit::default(), | ||
), | ||
} | ||
} |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remind me where is halo2_gadgets used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't add it as a dependency now, it's just that the add dependency utility lexicographically sorted out all the crates.
And so it is probably not needed, because only
BLOCK_SIZE
is imported from it. We should add a cargo-udeps utility to our pipeline to check dependencies for necessity automaticallyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I saw it is reordered, but I didn't find where it used. Probably just outdated.