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,36 @@ 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 ( ) )
87- }
88-
89- pub fn const_zero ( ) -> Self {
9089 Self {
9190 magnitude : T :: zero ( ) ,
92- sgn : Sgn :: Pos ,
91+ sgn : CircuitVar :: Constant ( Sgn :: Pos ) ,
9392 value : Cell :: new ( None ) ,
9493 }
9594 }
9695
9796 pub fn negate ( self ) -> Self {
9897 Self {
9998 magnitude : self . magnitude ,
100- sgn : self . sgn . negate ( ) ,
99+ sgn : self . sgn . map ( |sgn| sgn . negate ( ) ) ,
101100 value : Cell :: new ( self . value . get ( ) . map ( |f| f. neg ( ) ) ) ,
102101 }
103102 }
104103
105104 pub fn is_neg ( & self ) -> Boolean {
106- match self . sgn {
105+ match self . sgn . value ( ) {
107106 Sgn :: Pos => Boolean :: False ,
108107 Sgn :: Neg => Boolean :: True ,
109108 }
110109 }
111110
112111 pub fn is_pos ( & self ) -> Boolean {
113- match self . sgn {
112+ match self . sgn . value ( ) {
114113 Sgn :: Pos => Boolean :: True ,
115114 Sgn :: Neg => Boolean :: False ,
116115 }
@@ -120,7 +119,7 @@ where
120119 match self . value . get ( ) {
121120 Some ( x) => x,
122121 None => {
123- let sgn: F = self . sgn . to_field ( ) ;
122+ let sgn: F = self . sgn . value ( ) . to_field ( ) ;
124123 let magnitude: F = self . magnitude . to_field ( ) ;
125124 let value = w. exists_no_check ( magnitude * sgn) ;
126125 self . value . replace ( Some ( value) ) ;
@@ -133,7 +132,7 @@ where
133132 match self . value . get ( ) {
134133 Some ( x) => x,
135134 None => {
136- let sgn: F = self . sgn . to_field ( ) ;
135+ let sgn: F = self . sgn . value ( ) . to_field ( ) ;
137136 let magnitude: F = self . magnitude . to_field ( ) ;
138137 magnitude * sgn
139138 }
@@ -144,7 +143,7 @@ where
144143 match self . value . get ( ) {
145144 Some ( _) => { }
146145 None => {
147- let sgn: F = self . sgn . to_field ( ) ;
146+ let sgn: F = self . sgn . value ( ) . to_field ( ) ;
148147 let magnitude: F = self . magnitude . to_field ( ) ;
149148 self . value . replace ( Some ( magnitude * sgn) ) ;
150149 }
@@ -158,7 +157,7 @@ where
158157 fn unchecked ( & self ) -> currency:: Signed < T :: Inner > {
159158 currency:: Signed {
160159 magnitude : self . magnitude . to_inner ( ) ,
161- sgn : self . sgn ,
160+ sgn : * self . sgn . value ( ) ,
162161 }
163162 }
164163
@@ -193,7 +192,7 @@ where
193192
194193 let res = Self {
195194 magnitude : res_magnitude,
196- sgn,
195+ sgn : CircuitVar :: Var ( sgn ) ,
197196 value : Cell :: new ( Some ( res_value) ) ,
198197 } ;
199198 ( res, overflow)
@@ -219,7 +218,11 @@ where
219218
220219 range_check :: < F , CURRENCY_NBITS > ( magnitude, w) ;
221220
222- Self :: create ( T :: from_field ( magnitude) , sgn, Some ( res_value) )
221+ Self :: create (
222+ T :: from_field ( magnitude) ,
223+ CircuitVar :: Var ( sgn) ,
224+ Some ( res_value) ,
225+ )
223226 }
224227
225228 pub fn equal ( & self , other : & Self , w : & mut Witness < F > ) -> Boolean {
@@ -504,7 +507,7 @@ macro_rules! impl_currency {
504507 pub fn to_checked<F : FieldWitness >( & self ) -> CheckedSigned <F , $name<F >> {
505508 CheckedSigned {
506509 magnitude: self . magnitude. to_checked( ) ,
507- sgn: self . sgn,
510+ sgn: CircuitVar :: Var ( self . sgn) ,
508511 value: Cell :: new( None ) ,
509512 }
510513 }
0 commit comments