43
43
modulus :: < SF > ( ) ,
44
44
) ;
45
45
let n = scalar_chip. load_constant ( ctx, scalar_chip. p . to_biguint ( ) . unwrap ( ) ) ;
46
+ println ! ( "n of scalar_chip {:?}" , n) ;
46
47
47
48
// check whether the pubkey is (0, 0), i.e. in the case of ecrecover, no pubkey could be
48
49
// recovered.
@@ -72,12 +73,19 @@ where
72
73
. gate ( )
73
74
. or ( ctx, Existing ( r_is_zero) , Existing ( r_in_range) ) ;
74
75
let s_is_zero = scalar_chip. is_soft_zero ( ctx, s) ;
76
+
75
77
let s_in_range = scalar_chip. is_soft_nonzero ( ctx, s) ;
76
78
let s_is_valid = base_chip
77
79
. range ( )
78
80
. gate ( )
79
81
. or ( ctx, Existing ( s_is_zero) , Existing ( s_in_range) ) ;
80
82
83
+ println ! ( "r {:?}" , r) ;
84
+ println ! ( "s {:?}" , s) ;
85
+ println ! ( "pub_key {:?}" , pubkey) ;
86
+ println ! ( "msg_hash {:?}" , msghash) ;
87
+ println ! ( "r_is_valid {:?}" , r_is_valid) ;
88
+ println ! ( "s_is_valid {:?}" , s_is_valid) ;
81
89
// load required constants
82
90
let zero = scalar_chip. load_constant ( ctx, FpConfig :: < F , SF > :: fe_to_constant ( SF :: ZERO ) ) ;
83
91
let one = scalar_chip. load_constant ( ctx, FpConfig :: < F , SF > :: fe_to_constant ( SF :: ONE ) ) ;
@@ -95,9 +103,12 @@ where
95
103
let u1 = scalar_chip. divide ( ctx, msghash, & s_prime) ;
96
104
let u1 = scalar_chip. select ( ctx, & zero, & u1, & s_is_zero) ;
97
105
106
+ println ! ( "u1 after: {:?}" , u1) ;
107
+
98
108
// compute u2 = r * s^{-1} mod n
99
109
let u2 = scalar_chip. divide ( ctx, r, & s_prime) ;
100
110
let u2 = scalar_chip. select ( ctx, & zero, & u2, & s_is_zero) ;
111
+ println ! ( "u2 after: {:?}" , u2) ;
101
112
102
113
// we want to compute u1*G + u2*PK, there are two edge cases
103
114
// 1. either u1 or u2 is 0; we use binary selections to handle the this case
@@ -108,34 +119,44 @@ where
108
119
// =================================
109
120
let u1_is_zero = scalar_chip. is_zero ( ctx, & u1) ;
110
121
let u1_prime = scalar_chip. select ( ctx, & one, & u1, & u1_is_zero) ;
111
- let u1_mul = fixed_base:: scalar_multiply :: < F , _ , _ > (
122
+ let u1_mul_affine = fixed_base:: scalar_multiply :: < F , _ , GA > (
112
123
base_chip,
113
124
ctx,
114
125
& GA :: generator ( ) ,
115
126
& u1_prime. truncation . limbs ,
116
127
base_chip. limb_bits ,
117
128
fixed_window_bits,
118
129
) ;
119
- let u1_mul = ecc_chip. select ( ctx, & point_at_infinity, & u1_mul, & u1_is_zero) ;
130
+ println ! ( "u1_mul point {:?}" , u1_mul_affine) ;
131
+ println ! ( "u1_is_zero {:?}" , u1_is_zero) ;
132
+
133
+ let u1_mul = ecc_chip. select ( ctx, & point_at_infinity, & u1_mul_affine, & u1_is_zero) ;
120
134
121
135
// compute u2 * pubkey
122
136
let u2_prime = scalar_chip. select ( ctx, & one, & u2, & s_is_zero) ;
123
137
let pubkey_prime = ecc_chip. load_random_point :: < GA > ( ctx) ;
124
138
let pubkey_prime = ecc_chip. select ( ctx, & pubkey_prime, pubkey, & is_pubkey_zero) ;
125
- let u2_mul = scalar_multiply :: < F , _ > (
139
+ println ! ( "u2_prime {:?}" , u2_prime) ;
140
+ println ! ( "pubkey_prime {:?}" , pubkey_prime) ;
141
+
142
+ let u2_mul_affine = scalar_multiply :: < F , _ , GA > (
126
143
base_chip,
127
144
ctx,
128
145
& pubkey_prime,
129
146
& u2_prime. truncation . limbs ,
130
147
base_chip. limb_bits ,
131
148
var_window_bits,
132
149
) ;
150
+
151
+ println ! ( "u2_mul_affine point {:?}" , u2_mul_affine) ;
152
+
133
153
let u2_is_zero =
134
154
base_chip
135
155
. range ( )
136
156
. gate ( )
137
157
. or ( ctx, Existing ( s_is_zero) , Existing ( is_pubkey_zero) ) ;
138
- let u2_mul = ecc_chip. select ( ctx, & point_at_infinity, & u2_mul, & u2_is_zero) ;
158
+ let u2_mul = ecc_chip. select ( ctx, & point_at_infinity, & u2_mul_affine, & u2_is_zero) ;
159
+ println ! ( "u2_is_zero {:?}" , u2_is_zero) ;
139
160
140
161
// =================================
141
162
// case 2:
@@ -151,6 +172,8 @@ where
151
172
. gate ( )
152
173
. and ( ctx, Existing ( u1_is_zero) , Existing ( u2_is_zero) ) ;
153
174
let u1_u2_x_eq = base_chip. is_equal ( ctx, u1_mul. x ( ) , u2_mul. x ( ) ) ;
175
+
176
+ println ! ( "u1_u2_x_eq {:?}" , u1_u2_x_eq) ;
154
177
let u1_u2_y_neg = {
155
178
let u2_y_neg = base_chip. negate ( ctx, u2_mul. y ( ) ) ;
156
179
base_chip. is_equal ( ctx, u1_mul. y ( ) , & u2_y_neg)
@@ -161,17 +184,21 @@ where
161
184
Existing ( u1_u2_x_eq) ,
162
185
Existing ( u1_u2_y_neg) ,
163
186
) ;
187
+
188
+ println ! ( "sum_is_infinity {:?}" , sum_is_infinity) ;
189
+
164
190
let sum_is_not_infinity = base_chip
165
191
. gate ( )
166
192
. not ( ctx, QuantumCell :: Existing ( sum_is_infinity) ) ;
167
193
194
+ println ! ( "sum_is_not_infinity {:?}" , sum_is_not_infinity) ;
168
195
// For a valid ECDSA signature, the x co-ordinate of u1.G + u2.Pk, i.e. x_3, MUST EQUAL r
169
196
//
170
197
// For ec_add:
171
198
// P:(x_1, y_1) + Q:(x_2, y_2) == (x_3, y_3) we have:
172
199
// - lambda == (y_2 - y_1) / (x_2 - x_1) (mod n)
173
200
// - x_3 == (lambda * lambda) - x_1 - x_2 (mod n)
174
- // - y_3 == lambda * (x_1 - x_3) - y_1 (mod n)
201
+ // - y_3 == lambda * (x_1 - x_3) + y_1 (mod n)
175
202
let ( x_3, y_3) = {
176
203
// we implement divide_unsafe in a non-panicking way, lambda = dy/dx (mod n)
177
204
let dx = base_chip. sub_no_carry ( ctx, u2_mul. x ( ) , u1_mul. x ( ) ) ;
@@ -194,7 +221,9 @@ where
194
221
let x_3 = base_chip. carry_mod ( ctx, & x_3_no_carry) ;
195
222
let dx_13 = base_chip. sub_no_carry ( ctx, u1_mul. x ( ) , & x_3) ;
196
223
let lambda_dx_13 = base_chip. mul_no_carry ( ctx, & lambda, & dx_13) ;
197
- let y_3_no_carry = base_chip. sub_no_carry ( ctx, & lambda_dx_13, u1_mul. y ( ) ) ;
224
+ //let y_3_no_carry = base_chip.sub_no_carry(ctx, &lambda_dx_13, u1_mul.y());
225
+ let y_3_no_carry = base_chip. add_no_carry ( ctx, & lambda_dx_13, u1_mul. y ( ) ) ;
226
+
198
227
let y_3 = base_chip. carry_mod ( ctx, & y_3_no_carry) ;
199
228
200
229
// edge cases
@@ -207,6 +236,10 @@ where
207
236
208
237
( x_3, y_3)
209
238
} ;
239
+
240
+ scalar_chip. enforce_less_than ( ctx, & x_3) ;
241
+ println ! ( "enforce_less_than x_3 {:?} " , x_3) ;
242
+
210
243
let equal_check = base_chip. is_equal ( ctx, & x_3, r) ;
211
244
212
245
// TODO: maybe the big_less_than is optional?
@@ -246,8 +279,6 @@ where
246
279
] ,
247
280
) ;
248
281
249
- println ! ( "r {:?}" , r) ;
250
- println ! ( "s {:?}" , s) ;
251
282
println ! ( "equal_check {:?}" , equal_check) ;
252
283
println ! ( "x_3 {:?}" , x_3) ;
253
284
println ! ( "y_3 {:?}" , y_3) ;
0 commit comments