1414
1515#![ allow( dead_code) ]
1616
17- use crate :: { RISTRETTO_POINT_LEN , SCALAR_LEN } ;
1817#[ cfg( not( target_os = "solana" ) ) ]
1918use {
2019 crate :: {
2524 inner_product:: InnerProductProof ,
2625 } ,
2726 transcript:: TranscriptProtocol ,
27+ UNIT_LEN ,
2828 } ,
2929 core:: iter,
3030 curve25519_dalek:: traits:: MultiscalarMul ,
@@ -35,44 +35,24 @@ use {
3535 } ,
3636 merlin:: Transcript ,
3737 rand:: rngs:: OsRng ,
38+ solana_zk_sdk_pod:: range_proof:: { PodRangeProofU128 , PodRangeProofU256 , PodRangeProofU64 } ,
3839 subtle:: { Choice , ConditionallySelectable } ,
3940 zeroize:: Zeroize ,
4041} ;
4142
4243pub mod errors;
43- pub mod pod;
44-
4544#[ cfg( not( target_os = "solana" ) ) ]
4645pub mod generators;
4746#[ cfg( not( target_os = "solana" ) ) ]
4847pub mod inner_product;
4948#[ cfg( not( target_os = "solana" ) ) ]
5049pub mod util;
5150
52- /// Byte length of a range proof excluding the inner-product proof component
53- pub const RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN : usize =
54- 5 * RISTRETTO_POINT_LEN + 2 * SCALAR_LEN ;
55-
56- /// Byte length of an inner-product proof for a vector of length 64
57- pub const INNER_PRODUCT_PROOF_U64_LEN : usize = 448 ;
58-
59- /// Byte length of a range proof for an unsigned 64-bit number
60- pub const RANGE_PROOF_U64_LEN : usize =
61- INNER_PRODUCT_PROOF_U64_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN ; // 672 bytes
62-
63- /// Byte length of an inner-product proof for a vector of length 128
64- pub const INNER_PRODUCT_PROOF_U128_LEN : usize = 512 ;
65-
66- /// Byte length of a range proof for an unsigned 128-bit number
67- pub const RANGE_PROOF_U128_LEN : usize =
68- INNER_PRODUCT_PROOF_U128_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN ; // 736 bytes
69-
70- /// Byte length of an inner-product proof for a vector of length 256
71- pub const INNER_PRODUCT_PROOF_U256_LEN : usize = 576 ;
72-
73- /// Byte length of a range proof for an unsigned 256-bit number
74- pub const RANGE_PROOF_U256_LEN : usize =
75- INNER_PRODUCT_PROOF_U256_LEN + RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN ; // 800 bytes
51+ pub use solana_zk_sdk_pod:: range_proof:: {
52+ INNER_PRODUCT_PROOF_U128_LEN , INNER_PRODUCT_PROOF_U256_LEN , INNER_PRODUCT_PROOF_U64_LEN ,
53+ RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN , RANGE_PROOF_U128_LEN , RANGE_PROOF_U256_LEN ,
54+ RANGE_PROOF_U64_LEN ,
55+ } ;
7656
7757/// A Bulletproofs range proof.
7858#[ allow( non_snake_case) ]
@@ -491,6 +471,102 @@ impl RangeProof {
491471 }
492472}
493473
474+ #[ cfg( not( target_os = "solana" ) ) ]
475+ impl TryFrom < PodRangeProofU64 > for RangeProof {
476+ type Error = RangeProofVerificationError ;
477+
478+ fn try_from ( pod_proof : PodRangeProofU64 ) -> Result < Self , Self :: Error > {
479+ Self :: from_bytes ( & pod_proof. 0 )
480+ }
481+ }
482+
483+ #[ cfg( not( target_os = "solana" ) ) ]
484+ impl TryFrom < RangeProof > for PodRangeProofU64 {
485+ type Error = RangeProofVerificationError ;
486+
487+ fn try_from ( decoded_proof : RangeProof ) -> Result < Self , Self :: Error > {
488+ if decoded_proof. ipp_proof . serialized_size ( ) != INNER_PRODUCT_PROOF_U64_LEN {
489+ return Err ( RangeProofVerificationError :: Deserialization ) ;
490+ }
491+
492+ let mut buf = [ 0_u8 ; RANGE_PROOF_U64_LEN ] ;
493+ copy_range_proof_modulo_inner_product_proof ( & decoded_proof, & mut buf) ;
494+ buf[ RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN ..RANGE_PROOF_U64_LEN ]
495+ . copy_from_slice ( & decoded_proof. ipp_proof . to_bytes ( ) ) ;
496+ Ok ( PodRangeProofU64 ( buf) )
497+ }
498+ }
499+
500+ #[ cfg( not( target_os = "solana" ) ) ]
501+ impl TryFrom < PodRangeProofU128 > for RangeProof {
502+ type Error = RangeProofVerificationError ;
503+
504+ fn try_from ( pod_proof : PodRangeProofU128 ) -> Result < Self , Self :: Error > {
505+ Self :: from_bytes ( & pod_proof. 0 )
506+ }
507+ }
508+
509+ #[ cfg( not( target_os = "solana" ) ) ]
510+ impl TryFrom < RangeProof > for PodRangeProofU128 {
511+ type Error = RangeProofVerificationError ;
512+
513+ fn try_from ( decoded_proof : RangeProof ) -> Result < Self , Self :: Error > {
514+ if decoded_proof. ipp_proof . serialized_size ( ) != INNER_PRODUCT_PROOF_U128_LEN {
515+ return Err ( RangeProofVerificationError :: Deserialization ) ;
516+ }
517+
518+ let mut buf = [ 0_u8 ; RANGE_PROOF_U128_LEN ] ;
519+ copy_range_proof_modulo_inner_product_proof ( & decoded_proof, & mut buf) ;
520+ buf[ RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN ..RANGE_PROOF_U128_LEN ]
521+ . copy_from_slice ( & decoded_proof. ipp_proof . to_bytes ( ) ) ;
522+ Ok ( PodRangeProofU128 ( buf) )
523+ }
524+ }
525+
526+ #[ cfg( not( target_os = "solana" ) ) ]
527+ impl TryFrom < PodRangeProofU256 > for RangeProof {
528+ type Error = RangeProofVerificationError ;
529+
530+ fn try_from ( pod_proof : PodRangeProofU256 ) -> Result < Self , Self :: Error > {
531+ Self :: from_bytes ( & pod_proof. 0 )
532+ }
533+ }
534+
535+ #[ cfg( not( target_os = "solana" ) ) ]
536+ impl TryFrom < RangeProof > for PodRangeProofU256 {
537+ type Error = RangeProofVerificationError ;
538+
539+ fn try_from ( decoded_proof : RangeProof ) -> Result < Self , Self :: Error > {
540+ if decoded_proof. ipp_proof . serialized_size ( ) != INNER_PRODUCT_PROOF_U256_LEN {
541+ return Err ( RangeProofVerificationError :: Deserialization ) ;
542+ }
543+
544+ let mut buf = [ 0_u8 ; RANGE_PROOF_U256_LEN ] ;
545+ copy_range_proof_modulo_inner_product_proof ( & decoded_proof, & mut buf) ;
546+ buf[ RANGE_PROOF_MODULO_INNER_PRODUCT_PROOF_LEN ..RANGE_PROOF_U256_LEN ]
547+ . copy_from_slice ( & decoded_proof. ipp_proof . to_bytes ( ) ) ;
548+ Ok ( PodRangeProofU256 ( buf) )
549+ }
550+ }
551+
552+ #[ cfg( not( target_os = "solana" ) ) ]
553+ fn copy_range_proof_modulo_inner_product_proof ( proof : & RangeProof , buf : & mut [ u8 ] ) {
554+ let mut chunks = buf. chunks_mut ( UNIT_LEN ) ;
555+ chunks. next ( ) . unwrap ( ) . copy_from_slice ( proof. A . as_bytes ( ) ) ;
556+ chunks. next ( ) . unwrap ( ) . copy_from_slice ( proof. S . as_bytes ( ) ) ;
557+ chunks. next ( ) . unwrap ( ) . copy_from_slice ( proof. T_1 . as_bytes ( ) ) ;
558+ chunks. next ( ) . unwrap ( ) . copy_from_slice ( proof. T_2 . as_bytes ( ) ) ;
559+ chunks. next ( ) . unwrap ( ) . copy_from_slice ( proof. t_x . as_bytes ( ) ) ;
560+ chunks
561+ . next ( )
562+ . unwrap ( )
563+ . copy_from_slice ( proof. t_x_blinding . as_bytes ( ) ) ;
564+ chunks
565+ . next ( )
566+ . unwrap ( )
567+ . copy_from_slice ( proof. e_blinding . as_bytes ( ) ) ;
568+ }
569+
494570/// Computes the `delta(y,z)` term for the verification equation.
495571///
496572/// This term is a function of the challenges `y` and `z` and the proof dimensions.
@@ -514,8 +590,7 @@ fn delta(bit_lengths: &[usize], y: &Scalar, z: &Scalar) -> Scalar {
514590#[ cfg( test) ]
515591mod tests {
516592 use {
517- super :: * , crate :: range_proof:: pod:: PodRangeProofU128 ,
518- solana_zk_sdk_pod:: encryption:: pedersen:: PodPedersenCommitment , std:: str:: FromStr ,
593+ super :: * , solana_zk_sdk_pod:: encryption:: pedersen:: PodPedersenCommitment , std:: str:: FromStr ,
519594 } ;
520595
521596 #[ test]
0 commit comments