11use anyhow:: Result ;
22use ark_poly:: { EvaluationDomain , Radix2EvaluationDomain } ;
33use clap:: Parser ;
4- use mina_curves:: pasta:: { Fp , Vesta } ;
5- use poly_commitment:: { ipa:: SRS , SRS as _} ;
6- use saffron:: { blob:: FieldBlob , cli, commitment, env, utils} ;
4+ use kimchi:: groupmap:: GroupMap ;
5+ use mina_curves:: pasta:: { Fp , Vesta , VestaParameters } ;
6+ use mina_poseidon:: { constants:: PlonkSpongeConstantsKimchi , sponge:: DefaultFqSponge } ;
7+ use poly_commitment:: { commitment:: CommitmentCurve , ipa:: SRS , SRS as _} ;
8+ use rand:: rngs:: OsRng ;
9+ use saffron:: {
10+ blob:: FieldBlob ,
11+ cli:: { self , HexString } ,
12+ commitment, env, proof, utils,
13+ } ;
714use sha3:: { Digest , Sha3_256 } ;
815use std:: {
916 fs:: File ,
1017 io:: { Read , Write } ,
1118} ;
12- use tracing:: debug;
19+ use tracing:: { debug, debug_span } ;
1320
1421pub const DEFAULT_SRS_SIZE : usize = 1 << 16 ;
1522
1623fn get_srs ( cache : Option < String > ) -> ( SRS < Vesta > , Radix2EvaluationDomain < Fp > ) {
17- match cache {
24+ let res = match cache {
1825 Some ( cache) => {
1926 let srs = env:: get_srs_from_cache ( cache) ;
2027 let domain_fp = Radix2EvaluationDomain :: new ( srs. size ( ) ) . unwrap ( ) ;
@@ -28,11 +35,16 @@ fn get_srs(cache: Option<String>) -> (SRS<Vesta>, Radix2EvaluationDomain<Fp>) {
2835 let domain_size = DEFAULT_SRS_SIZE ;
2936 let srs = SRS :: create ( domain_size) ;
3037 let domain_fp = Radix2EvaluationDomain :: new ( srs. size ( ) ) . unwrap ( ) ;
31- srs. get_lagrange_basis ( domain_fp) ;
3238 debug ! ( "SRS created successfully" ) ;
3339 ( srs, domain_fp)
3440 }
35- }
41+ } ;
42+
43+ debug_span ! ( "get_lagrange_basis" , basis_size = res. 0 . size( ) ) . in_scope ( || {
44+ res. 0 . get_lagrange_basis ( res. 1 ) ;
45+ } ) ;
46+
47+ res
3648}
3749
3850fn decode_file ( args : cli:: DecodeFileArgs ) -> Result < ( ) > {
@@ -66,12 +78,12 @@ fn encode_file(args: cli::EncodeFileArgs) -> Result<()> {
6678 . into_iter ( )
6779 . for_each ( |asserted_commitment| {
6880 let bytes = rmp_serde:: to_vec ( & blob. commitments ) . unwrap ( ) ;
69- let hash = Sha3_256 :: new ( ) . chain_update ( bytes) . finalize ( ) ;
70- let computed_commitment = hex:: encode ( hash) ;
71- if asserted_commitment != computed_commitment {
81+ let hash = Sha3_256 :: new ( ) . chain_update ( bytes) . finalize ( ) . to_vec ( ) ;
82+ if asserted_commitment. 0 != hash {
7283 panic ! (
7384 "commitment hash mismatch: asserted {}, computed {}" ,
74- asserted_commitment, computed_commitment
85+ asserted_commitment,
86+ HexString ( hash)
7587 ) ;
7688 }
7789 } ) ;
@@ -81,16 +93,34 @@ fn encode_file(args: cli::EncodeFileArgs) -> Result<()> {
8193 Ok ( ( ) )
8294}
8395
84- pub fn compute_commitment ( args : cli:: ComputeCommitmentArgs ) -> Result < String > {
96+ pub fn compute_commitment ( args : cli:: ComputeCommitmentArgs ) -> Result < HexString > {
8597 let ( srs, domain_fp) = get_srs ( args. srs_cache ) ;
8698 let mut file = File :: open ( args. input ) ?;
8799 let mut buf = Vec :: new ( ) ;
88100 file. read_to_end ( & mut buf) ?;
89101 let field_elems = utils:: encode_for_domain ( & domain_fp, & buf) ;
90102 let commitments = commitment:: commit_to_field_elems ( & srs, domain_fp, field_elems) ;
91103 let bytes = rmp_serde:: to_vec ( & commitments) . unwrap ( ) ;
92- let hash = Sha3_256 :: new ( ) . chain_update ( bytes) . finalize ( ) ;
93- Ok ( hex:: encode ( hash) )
104+ let hash = Sha3_256 :: new ( ) . chain_update ( bytes) . finalize ( ) . to_vec ( ) ;
105+ Ok ( HexString ( hash) )
106+ }
107+
108+ pub fn storage_proof ( args : cli:: StorageProofArgs ) -> Result < HexString > {
109+ let file = File :: open ( args. input ) ?;
110+ let blob: FieldBlob < Vesta > = rmp_serde:: decode:: from_read ( file) ?;
111+ let proof =
112+ {
113+ let ( srs, _) = get_srs ( args. srs_cache ) ;
114+ let group_map = <Vesta as CommitmentCurve >:: Map :: setup ( ) ;
115+ let mut rng = OsRng ;
116+ let evaluation_point = utils:: encode ( & args. challenge . 0 ) ;
117+ proof:: storage_proof :: <
118+ Vesta ,
119+ DefaultFqSponge < VestaParameters , PlonkSpongeConstantsKimchi > ,
120+ > ( & srs, & group_map, blob, evaluation_point, & mut rng)
121+ } ;
122+ let bytes = rmp_serde:: to_vec ( & proof) . unwrap ( ) ;
123+ Ok ( HexString ( bytes) )
94124}
95125
96126pub fn main ( ) -> Result < ( ) > {
@@ -99,15 +129,15 @@ pub fn main() -> Result<()> {
99129 match args {
100130 cli:: Commands :: Encode ( args) => encode_file ( args) ,
101131 cli:: Commands :: Decode ( args) => decode_file ( args) ,
102- cli:: Commands :: ComputeCommitment ( args) => match compute_commitment ( args ) {
103- Ok ( c ) => {
104- println ! ( "{}" , c ) ;
105- Ok ( ( ) )
106- }
107- Err ( e ) => {
108- eprintln ! ( "{}" , e ) ;
109- Err ( e )
110- }
111- } ,
132+ cli:: Commands :: ComputeCommitment ( args) => {
133+ let commitment = compute_commitment ( args ) ? ;
134+ println ! ( "{}" , commitment ) ;
135+ Ok ( ( ) )
136+ }
137+ cli :: Commands :: StorageProof ( args ) => {
138+ let proof = storage_proof ( args ) ? ;
139+ println ! ( "{}" , proof ) ;
140+ Ok ( ( ) )
141+ }
112142 }
113143}
0 commit comments