Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 9092084

Browse files
committed
tests against p256 branch
1 parent 0fd1512 commit 9092084

File tree

5 files changed

+256
-157
lines changed

5 files changed

+256
-157
lines changed

Cargo.lock

Lines changed: 37 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.
3737
halo2curves = { version = "0.1.0", features = [ "derive_serde" ] }
3838
poseidon-base = { package = "poseidon-base", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "main" }
3939
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "main" }
40-
halo2-base = { git = "https://github.com/scroll-tech/halo2-lib", branch = "develop", default-features=false, features=["halo2-pse","display"] }
41-
halo2-ecc = { git = "https://github.com/scroll-tech/halo2-lib", branch = "develop", default-features=false, features=["halo2-pse","display"] }
40+
halo2-base = { git = "https://github.com/scroll-tech/halo2-lib", branch = "ecc_double_p256", default-features=false, features=["halo2-pse","display"] }
41+
halo2-ecc = { git = "https://github.com/scroll-tech/halo2-lib", branch = "ecc_double_p256", default-features=false, features=["halo2-pse","display"] }
4242
hex = "0.4"
4343
itertools = "0.11"
4444
libsecp256k1 = "0.7"
@@ -57,8 +57,8 @@ serde = {version = "1.0", features = ["derive"] }
5757
serde_json = "1.0"
5858
serde_stacker = "0.1"
5959
sha3 = "0.10"
60-
snark-verifier = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop" }
61-
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] }
60+
snark-verifier = { git = "https://github.com/scroll-tech/snark-verifier", branch = "check_p256_branch" }
61+
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "check_p256_branch", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] }
6262
strum = "0.25"
6363
strum_macros = "0.25"
6464
subtle = "2.4"

eth-types/src/sign_types.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ pub fn sign<
5050
) -> (Fq, Fq, u8) {
5151
let randomness_inv = Option::<Fq>::from(randomness.invert()).expect("cannot invert randomness");
5252
let generator = Affine::generator();
53-
// generator is indeed for r1 if call with r1 type.
54-
5553
let sig_point = generator * randomness;
5654
let sig_v: bool = sig_point.to_affine().into_coordinates().1.is_odd().into();
5755

@@ -89,19 +87,34 @@ pub fn verify<
8987
r: Fq,
9088
s: Fq,
9189
msg_hash: Fq,
92-
// if pubkey is not recovered , v is not neccessary.
93-
v: Option<bool>,
90+
// if pubkey is provided rather than from recovered , v is not neccessary.
91+
v: Option<bool>,
9492
) -> bool {
93+
println!("r {:?}", r);
94+
println!("s {:?}", s);
95+
println!("pub_key {:?}", pub_key);
96+
println!("msg_hash {:?}", msg_hash);
9597
// Verify
9698
let s_inv = s.invert().unwrap();
9799
let u_1 = msg_hash * s_inv;
100+
println!("verify u_1: {:?}", u_1);
98101
let u_2 = r * s_inv;
102+
println!("verify u_2: {:?}", u_2);
99103

100104
let g = Affine::generator();
101-
let v_1 = g * u_1;
102-
let v_2 = pub_key * u_2;
105+
let u1_affine = g * u_1;
106+
println!(
107+
"verify u1_affine: {:?}",
108+
u1_affine.to_affine().coordinates().unwrap()
109+
);
110+
111+
let u2_affine = pub_key * u_2;
112+
println!(
113+
"verify u2_affine: {:?}",
114+
u2_affine.to_affine().coordinates().unwrap()
115+
);
103116

104-
let r_point = (v_1 + v_2).to_affine().coordinates().unwrap();
117+
let r_point = (u1_affine + u2_affine).to_affine().coordinates().unwrap();
105118
let x_candidate = r_point.x();
106119
let r_candidate = mod_n(*x_candidate);
107120

zkevm-circuits/src/sig_circuit/ecdsa.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ where
4343
modulus::<SF>(),
4444
);
4545
let n = scalar_chip.load_constant(ctx, scalar_chip.p.to_biguint().unwrap());
46+
println!("n of scalar_chip {:?}", n);
4647

4748
// check whether the pubkey is (0, 0), i.e. in the case of ecrecover, no pubkey could be
4849
// recovered.
@@ -72,12 +73,19 @@ where
7273
.gate()
7374
.or(ctx, Existing(r_is_zero), Existing(r_in_range));
7475
let s_is_zero = scalar_chip.is_soft_zero(ctx, s);
76+
7577
let s_in_range = scalar_chip.is_soft_nonzero(ctx, s);
7678
let s_is_valid = base_chip
7779
.range()
7880
.gate()
7981
.or(ctx, Existing(s_is_zero), Existing(s_in_range));
8082

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);
8189
// load required constants
8290
let zero = scalar_chip.load_constant(ctx, FpConfig::<F, SF>::fe_to_constant(SF::ZERO));
8391
let one = scalar_chip.load_constant(ctx, FpConfig::<F, SF>::fe_to_constant(SF::ONE));
@@ -95,9 +103,12 @@ where
95103
let u1 = scalar_chip.divide(ctx, msghash, &s_prime);
96104
let u1 = scalar_chip.select(ctx, &zero, &u1, &s_is_zero);
97105

106+
println!("u1 after: {:?}", u1);
107+
98108
// compute u2 = r * s^{-1} mod n
99109
let u2 = scalar_chip.divide(ctx, r, &s_prime);
100110
let u2 = scalar_chip.select(ctx, &zero, &u2, &s_is_zero);
111+
println!("u2 after: {:?}", u2);
101112

102113
// we want to compute u1*G + u2*PK, there are two edge cases
103114
// 1. either u1 or u2 is 0; we use binary selections to handle the this case
@@ -108,34 +119,44 @@ where
108119
// =================================
109120
let u1_is_zero = scalar_chip.is_zero(ctx, &u1);
110121
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>(
112123
base_chip,
113124
ctx,
114125
&GA::generator(),
115126
&u1_prime.truncation.limbs,
116127
base_chip.limb_bits,
117128
fixed_window_bits,
118129
);
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);
120134

121135
// compute u2 * pubkey
122136
let u2_prime = scalar_chip.select(ctx, &one, &u2, &s_is_zero);
123137
let pubkey_prime = ecc_chip.load_random_point::<GA>(ctx);
124138
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>(
126143
base_chip,
127144
ctx,
128145
&pubkey_prime,
129146
&u2_prime.truncation.limbs,
130147
base_chip.limb_bits,
131148
var_window_bits,
132149
);
150+
151+
println!("u2_mul_affine point {:?}", u2_mul_affine);
152+
133153
let u2_is_zero =
134154
base_chip
135155
.range()
136156
.gate()
137157
.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);
139160

140161
// =================================
141162
// case 2:
@@ -151,6 +172,8 @@ where
151172
.gate()
152173
.and(ctx, Existing(u1_is_zero), Existing(u2_is_zero));
153174
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);
154177
let u1_u2_y_neg = {
155178
let u2_y_neg = base_chip.negate(ctx, u2_mul.y());
156179
base_chip.is_equal(ctx, u1_mul.y(), &u2_y_neg)
@@ -161,17 +184,21 @@ where
161184
Existing(u1_u2_x_eq),
162185
Existing(u1_u2_y_neg),
163186
);
187+
188+
println!("sum_is_infinity {:?}", sum_is_infinity);
189+
164190
let sum_is_not_infinity = base_chip
165191
.gate()
166192
.not(ctx, QuantumCell::Existing(sum_is_infinity));
167193

194+
println!("sum_is_not_infinity {:?}", sum_is_not_infinity);
168195
// For a valid ECDSA signature, the x co-ordinate of u1.G + u2.Pk, i.e. x_3, MUST EQUAL r
169196
//
170197
// For ec_add:
171198
// P:(x_1, y_1) + Q:(x_2, y_2) == (x_3, y_3) we have:
172199
// - lambda == (y_2 - y_1) / (x_2 - x_1) (mod n)
173200
// - 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)
175202
let (x_3, y_3) = {
176203
// we implement divide_unsafe in a non-panicking way, lambda = dy/dx (mod n)
177204
let dx = base_chip.sub_no_carry(ctx, u2_mul.x(), u1_mul.x());
@@ -194,7 +221,9 @@ where
194221
let x_3 = base_chip.carry_mod(ctx, &x_3_no_carry);
195222
let dx_13 = base_chip.sub_no_carry(ctx, u1_mul.x(), &x_3);
196223
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+
198227
let y_3 = base_chip.carry_mod(ctx, &y_3_no_carry);
199228

200229
// edge cases
@@ -207,6 +236,10 @@ where
207236

208237
(x_3, y_3)
209238
};
239+
240+
scalar_chip.enforce_less_than(ctx, &x_3);
241+
println!("enforce_less_than x_3 {:?} ", x_3);
242+
210243
let equal_check = base_chip.is_equal(ctx, &x_3, r);
211244

212245
// TODO: maybe the big_less_than is optional?
@@ -246,8 +279,6 @@ where
246279
],
247280
);
248281

249-
println!("r {:?}", r);
250-
println!("s {:?}", s);
251282
println!("equal_check {:?}", equal_check);
252283
println!("x_3 {:?}", x_3);
253284
println!("y_3 {:?}", y_3);

0 commit comments

Comments
 (0)