1- use crate :: scan_state:: currency:: { self , Amount , Balance , Fee , Magnitude , MinMax , Sgn , Signed } ;
1+ use crate :: {
2+ proofs:: field:: CircuitVar ,
3+ scan_state:: currency:: { self , Amount , Balance , Fee , Magnitude , MinMax , Sgn , Signed } ,
4+ } ;
25use std:: { cell:: Cell , cmp:: Ordering :: Less } ;
36
47use crate :: proofs:: {
4245 T : CheckedCurrency < F > ,
4346{
4447 pub magnitude : T ,
45- pub sgn : Sgn ,
48+ pub sgn : CircuitVar < Sgn > ,
4649 pub value : Cell < Option < F > > ,
4750}
4851
6568 F : FieldWitness + std:: fmt:: Debug ,
6669 T : CheckedCurrency < F > + std:: fmt:: Debug ,
6770{
68- pub fn create ( magnitude : T , sgn : Sgn , value : Option < F > ) -> Self {
71+ pub fn create ( magnitude : T , sgn : CircuitVar < Sgn > , value : Option < F > ) -> Self {
6972 Self {
7073 magnitude,
7174 sgn,
@@ -77,40 +80,46 @@ where
7780 let value = magnitude. to_field ( ) ;
7881 Self {
7982 magnitude,
80- sgn : Sgn :: Pos ,
83+ sgn : CircuitVar :: Constant ( Sgn :: Pos ) ,
8184 value : Cell :: new ( Some ( value) ) ,
8285 }
8386 }
8487
8588 pub fn zero ( ) -> Self {
86- Self :: of_unsigned ( T :: zero ( ) )
89+ Self {
90+ magnitude : T :: zero ( ) ,
91+ sgn : CircuitVar :: Constant ( Sgn :: Pos ) ,
92+ value : Cell :: new ( None ) ,
93+ }
8794 }
8895
89- pub fn const_zero ( ) -> Self {
96+ // https://github.com/MinaProtocol/mina/blob/ca9c8c86aa21d3c346d28ea0be7ad4cb0c22bf7f/src/lib/transaction_snark/transaction_snark.ml#L1891-L1892
97+ // https://github.com/MinaProtocol/mina/blob/ca9c8c86aa21d3c346d28ea0be7ad4cb0c22bf7f/src/lib/currency/currency.ml#L579
98+ pub fn constant_zero ( ) -> Self {
9099 Self {
91100 magnitude : T :: zero ( ) ,
92- sgn : Sgn :: Pos ,
93- value : Cell :: new ( None ) ,
101+ sgn : CircuitVar :: Constant ( Sgn :: Pos ) ,
102+ value : Cell :: new ( Some ( T :: zero ( ) . to_field ( ) ) ) ,
94103 }
95104 }
96105
97106 pub fn negate ( self ) -> Self {
98107 Self {
99108 magnitude : self . magnitude ,
100- sgn : self . sgn . negate ( ) ,
109+ sgn : self . sgn . map ( |sgn| sgn . negate ( ) ) ,
101110 value : Cell :: new ( self . value . get ( ) . map ( |f| f. neg ( ) ) ) ,
102111 }
103112 }
104113
105114 pub fn is_neg ( & self ) -> Boolean {
106- match self . sgn {
115+ match self . sgn . value ( ) {
107116 Sgn :: Pos => Boolean :: False ,
108117 Sgn :: Neg => Boolean :: True ,
109118 }
110119 }
111120
112121 pub fn is_pos ( & self ) -> Boolean {
113- match self . sgn {
122+ match self . sgn . value ( ) {
114123 Sgn :: Pos => Boolean :: True ,
115124 Sgn :: Neg => Boolean :: False ,
116125 }
@@ -120,7 +129,7 @@ where
120129 match self . value . get ( ) {
121130 Some ( x) => x,
122131 None => {
123- let sgn: F = self . sgn . to_field ( ) ;
132+ let sgn: F = self . sgn . value ( ) . to_field ( ) ;
124133 let magnitude: F = self . magnitude . to_field ( ) ;
125134 let value = w. exists_no_check ( magnitude * sgn) ;
126135 self . value . replace ( Some ( value) ) ;
@@ -133,7 +142,7 @@ where
133142 match self . value . get ( ) {
134143 Some ( x) => x,
135144 None => {
136- let sgn: F = self . sgn . to_field ( ) ;
145+ let sgn: F = self . sgn . value ( ) . to_field ( ) ;
137146 let magnitude: F = self . magnitude . to_field ( ) ;
138147 magnitude * sgn
139148 }
@@ -144,7 +153,7 @@ where
144153 match self . value . get ( ) {
145154 Some ( _) => { }
146155 None => {
147- let sgn: F = self . sgn . to_field ( ) ;
156+ let sgn: F = self . sgn . value ( ) . to_field ( ) ;
148157 let magnitude: F = self . magnitude . to_field ( ) ;
149158 self . value . replace ( Some ( magnitude * sgn) ) ;
150159 }
@@ -158,7 +167,7 @@ where
158167 fn unchecked ( & self ) -> currency:: Signed < T :: Inner > {
159168 currency:: Signed {
160169 magnitude : self . magnitude . to_inner ( ) ,
161- sgn : self . sgn ,
170+ sgn : * self . sgn . value ( ) ,
162171 }
163172 }
164173
@@ -193,7 +202,7 @@ where
193202
194203 let res = Self {
195204 magnitude : res_magnitude,
196- sgn,
205+ sgn : CircuitVar :: Var ( sgn ) ,
197206 value : Cell :: new ( Some ( res_value) ) ,
198207 } ;
199208 ( res, overflow)
@@ -219,7 +228,11 @@ where
219228
220229 range_check :: < F , CURRENCY_NBITS > ( magnitude, w) ;
221230
222- Self :: create ( T :: from_field ( magnitude) , sgn, Some ( res_value) )
231+ Self :: create (
232+ T :: from_field ( magnitude) ,
233+ CircuitVar :: Var ( sgn) ,
234+ Some ( res_value) ,
235+ )
223236 }
224237
225238 pub fn equal ( & self , other : & Self , w : & mut Witness < F > ) -> Boolean {
@@ -504,7 +517,7 @@ macro_rules! impl_currency {
504517 pub fn to_checked<F : FieldWitness >( & self ) -> CheckedSigned <F , $name<F >> {
505518 CheckedSigned {
506519 magnitude: self . magnitude. to_checked( ) ,
507- sgn: self . sgn,
520+ sgn: CircuitVar :: Var ( self . sgn) ,
508521 value: Cell :: new( None ) ,
509522 }
510523 }
0 commit comments