11//! Currency types with ledger-specific implementations.
22//!
33//! Re-exports types from [`mina_tx_type::currency`] and adds implementations
4- //! for ledger-specific traits like [`ToFieldElements`], [`Check`], and [`ToInputs `].
5-
6- use rand :: Rng ;
4+ //! for ledger-specific traits like [`ToFieldElements`] and [`Check `].
5+ //!
6+ //! Note: [`ToInputs`] implementations are provided by mina-tx-type.
77
88use crate :: proofs:: {
99 field:: FieldWitness , to_field_elements:: ToFieldElements , transaction:: Check , witness:: Witness ,
@@ -12,163 +12,14 @@ use crate::proofs::{
1212// Re-export all types from mina-tx-type
1313pub use mina_tx_type:: currency:: {
1414 Amount , Balance , BlockTime , BlockTimeSpan , Epoch , Fee , FieldLike , Index , Length , Magnitude ,
15- MinMax , Nonce , Sgn , Signed , Slot , SlotSpan , TxnVersion ,
15+ MinMax , Nonce , Sgn , Signed , SignedRandExt , Slot , SlotRandExt , SlotSpan , ToChecked , TxnVersion ,
16+ N ,
1617} ;
1718
1819// ============================================================================
19- // Extension traits for ledger-specific functionality
20- // ============================================================================
21-
22- /// Extension trait for random generation of Signed values.
23- pub trait SignedRandExt < T : Magnitude > {
24- fn gen ( ) -> Signed < T > ;
25- }
26-
27- impl < T > SignedRandExt < T > for Signed < T >
28- where
29- T : Magnitude + PartialOrd + Ord + Clone ,
30- rand:: distributions:: Standard : rand:: distributions:: Distribution < T > ,
31- {
32- fn gen ( ) -> Signed < T > {
33- let mut rng = rand:: thread_rng ( ) ;
34-
35- let magnitude: T = rng. gen ( ) ;
36- let sgn = if rng. gen :: < bool > ( ) {
37- Sgn :: Pos
38- } else {
39- Sgn :: Neg
40- } ;
41-
42- Signed :: create ( magnitude, sgn)
43- }
44- }
45-
46- /// Extension trait for Slot random generation.
47- pub trait SlotRandExt {
48- fn gen_small ( ) -> Slot ;
49- }
50-
51- impl SlotRandExt for Slot {
52- fn gen_small ( ) -> Slot {
53- let mut rng = rand:: thread_rng ( ) ;
54- Slot :: from_u32 ( rng. gen :: < u32 > ( ) % 10_000 )
55- }
56- }
57-
58- // ============================================================================
59- // Ledger-specific type: N (generic number)
20+ // Ledger-specific trait implementations for N
6021// ============================================================================
6122
62- /// A generic 64-bit number type for proof computations.
63- #[ derive(
64- Copy , Clone , Default , PartialEq , Eq , PartialOrd , Ord , Hash , serde:: Deserialize , serde:: Serialize ,
65- ) ]
66- pub struct N ( pub ( super ) u64 ) ;
67-
68- impl std:: fmt:: Debug for N {
69- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
70- f. write_fmt ( format_args ! ( "N({:?})" , self . 0 ) )
71- }
72- }
73-
74- impl Magnitude for N {
75- const NBITS : usize = 64 ;
76-
77- fn zero ( ) -> Self {
78- Self ( 0 )
79- }
80-
81- fn is_zero ( & self ) -> bool {
82- self . 0 == 0
83- }
84-
85- fn wrapping_add ( & self , rhs : & Self ) -> Self {
86- Self ( self . 0 . wrapping_add ( rhs. 0 ) )
87- }
88-
89- fn wrapping_mul ( & self , rhs : & Self ) -> Self {
90- Self ( self . 0 . wrapping_mul ( rhs. 0 ) )
91- }
92-
93- fn wrapping_sub ( & self , rhs : & Self ) -> Self {
94- Self ( self . 0 . wrapping_sub ( rhs. 0 ) )
95- }
96-
97- fn checked_add ( & self , rhs : & Self ) -> Option < Self > {
98- self . 0 . checked_add ( rhs. 0 ) . map ( Self )
99- }
100-
101- fn checked_mul ( & self , rhs : & Self ) -> Option < Self > {
102- self . 0 . checked_mul ( rhs. 0 ) . map ( Self )
103- }
104-
105- fn checked_sub ( & self , rhs : & Self ) -> Option < Self > {
106- self . 0 . checked_sub ( rhs. 0 ) . map ( Self )
107- }
108-
109- fn checked_div ( & self , rhs : & Self ) -> Option < Self > {
110- self . 0 . checked_div ( rhs. 0 ) . map ( Self )
111- }
112-
113- fn checked_rem ( & self , rhs : & Self ) -> Option < Self > {
114- self . 0 . checked_rem ( rhs. 0 ) . map ( Self )
115- }
116-
117- fn abs_diff ( & self , rhs : & Self ) -> Self {
118- Self ( self . 0 . abs_diff ( rhs. 0 ) )
119- }
120-
121- fn to_field < F : FieldLike > ( & self ) -> F {
122- F :: from ( self . 0 )
123- }
124-
125- fn of_field < F : FieldLike > ( field : F ) -> Self {
126- use ark_ff:: BigInteger256 ;
127- let bigint: BigInteger256 = field. into ( ) ;
128- Self ( bigint. 0 [ 0 ] )
129- }
130- }
131-
132- impl MinMax for N {
133- fn min ( ) -> Self {
134- Self ( 0 )
135- }
136- fn max ( ) -> Self {
137- Self ( u64:: MAX )
138- }
139- }
140-
141- impl N {
142- pub const NBITS : usize = 64 ;
143-
144- pub fn as_u64 ( & self ) -> u64 {
145- self . 0
146- }
147-
148- pub const fn from_u64 ( value : u64 ) -> Self {
149- Self ( value)
150- }
151-
152- pub const fn scale ( & self , n : u64 ) -> Option < Self > {
153- match self . 0 . checked_mul ( n) {
154- Some ( n) => Some ( Self ( n) ) ,
155- None => None ,
156- }
157- }
158- }
159-
160- impl rand:: distributions:: Distribution < N > for rand:: distributions:: Standard {
161- fn sample < R : rand:: Rng + ?Sized > ( & self , rng : & mut R ) -> N {
162- N ( rng. next_u64 ( ) )
163- }
164- }
165-
166- impl crate :: ToInputs for N {
167- fn to_inputs ( & self , inputs : & mut poseidon:: hash:: Inputs ) {
168- inputs. append_u64 ( self . 0 ) ;
169- }
170- }
171-
17223impl < F : FieldWitness > ToFieldElements < F > for N {
17324 fn to_field_elements ( & self , fields : & mut Vec < F > ) {
17425 fields. push ( self . to_field ( ) ) ;
@@ -186,21 +37,17 @@ impl<F: FieldWitness> Check<F> for N {
18637}
18738
18839// ============================================================================
189- // Macro for implementing ledger traits on currency types
40+ // Macro for implementing ledger-specific traits on currency types
19041// ============================================================================
19142
43+ /// Macro to implement ToFieldElements and Check for currency types.
44+ /// ToInputs implementations are provided by mina-tx-type.
19245macro_rules! impl_ledger_traits {
19346 ( 32 : { $( $name32: ident, ) * } , 64 : { $( $name64: ident, ) * } , ) => {
194- $( impl_ledger_traits!( { $name32, u32 , as_u32, append_u32 } , ) ; ) *
195- $( impl_ledger_traits!( { $name64, u64 , as_u64, append_u64 } , ) ; ) *
47+ $( impl_ledger_traits!( { $name32, u32 , as_u32} , ) ; ) *
48+ $( impl_ledger_traits!( { $name64, u64 , as_u64} , ) ; ) *
19649 } ;
197- ( $( { $name: ident, $inner: ty, $as_name: ident, $append_name: ident } , ) * ) => ( $(
198- impl crate :: ToInputs for $name {
199- fn to_inputs( & self , inputs: & mut poseidon:: hash:: Inputs ) {
200- inputs. $append_name( self . $as_name( ) ) ;
201- }
202- }
203-
50+ ( $( { $name: ident, $inner: ty, $as_name: ident } , ) * ) => ( $(
20451 impl <F : FieldWitness > ToFieldElements <F > for $name {
20552 fn to_field_elements( & self , fields: & mut Vec <F >) {
20653 fields. push( self . to_field( ) ) ;
0 commit comments