@@ -3,31 +3,22 @@ use crate::{
3
3
Address , Error , Precompile , PrecompileResult , PrecompileWithAddress ,
4
4
} ;
5
5
use bn:: { AffineG1 , AffineG2 , Fq , Fq2 , Group , Gt , G1 , G2 } ;
6
- use revm_primitives:: Bytes ;
7
6
8
7
pub mod add {
9
8
use super :: * ;
10
9
11
10
const ADDRESS : Address = crate :: u64_to_address ( 6 ) ;
12
11
12
+ pub const ISTANBUL_ADD_GAS_COST : u64 = 150 ;
13
13
pub const ISTANBUL : PrecompileWithAddress = PrecompileWithAddress (
14
14
ADDRESS ,
15
- Precompile :: Standard ( |input, gas_limit| {
16
- if 150 > gas_limit {
17
- return Err ( Error :: OutOfGas ) ;
18
- }
19
- Ok ( ( 150 , super :: run_add ( input) ?) )
20
- } ) ,
15
+ Precompile :: Standard ( |input, gas_limit| run_add ( input, ISTANBUL_ADD_GAS_COST , gas_limit) ) ,
21
16
) ;
22
17
18
+ pub const BYZANTIUM_ADD_GAS_COST : u64 = 500 ;
23
19
pub const BYZANTIUM : PrecompileWithAddress = PrecompileWithAddress (
24
20
ADDRESS ,
25
- Precompile :: Standard ( |input, gas_limit| {
26
- if 500 > gas_limit {
27
- return Err ( Error :: OutOfGas ) ;
28
- }
29
- Ok ( ( 500 , super :: run_add ( input) ?) )
30
- } ) ,
21
+ Precompile :: Standard ( |input, gas_limit| run_add ( input, BYZANTIUM_ADD_GAS_COST , gas_limit) ) ,
31
22
) ;
32
23
}
33
24
@@ -36,24 +27,16 @@ pub mod mul {
36
27
37
28
const ADDRESS : Address = crate :: u64_to_address ( 7 ) ;
38
29
30
+ pub const ISTANBUL_MUL_GAS_COST : u64 = 6_000 ;
39
31
pub const ISTANBUL : PrecompileWithAddress = PrecompileWithAddress (
40
32
ADDRESS ,
41
- Precompile :: Standard ( |input, gas_limit| {
42
- if 6_000 > gas_limit {
43
- return Err ( Error :: OutOfGas ) ;
44
- }
45
- Ok ( ( 6_000 , super :: run_mul ( input) ?) )
46
- } ) ,
33
+ Precompile :: Standard ( |input, gas_limit| run_mul ( input, ISTANBUL_MUL_GAS_COST , gas_limit) ) ,
47
34
) ;
48
35
36
+ pub const BYZANTIUM_MUL_GAS_COST : u64 = 40_000 ;
49
37
pub const BYZANTIUM : PrecompileWithAddress = PrecompileWithAddress (
50
38
ADDRESS ,
51
- Precompile :: Standard ( |input, gas_limit| {
52
- if 40_000 > gas_limit {
53
- return Err ( Error :: OutOfGas ) ;
54
- }
55
- Ok ( ( 40_000 , super :: run_mul ( input) ?) )
56
- } ) ,
39
+ Precompile :: Standard ( |input, gas_limit| run_mul ( input, BYZANTIUM_MUL_GAS_COST , gas_limit) ) ,
57
40
) ;
58
41
}
59
42
@@ -67,7 +50,7 @@ pub mod pair {
67
50
pub const ISTANBUL : PrecompileWithAddress = PrecompileWithAddress (
68
51
ADDRESS ,
69
52
Precompile :: Standard ( |input, gas_limit| {
70
- super :: run_pair (
53
+ run_pair (
71
54
input,
72
55
ISTANBUL_PAIR_PER_POINT ,
73
56
ISTANBUL_PAIR_BASE ,
@@ -81,7 +64,7 @@ pub mod pair {
81
64
pub const BYZANTIUM : PrecompileWithAddress = PrecompileWithAddress (
82
65
ADDRESS ,
83
66
Precompile :: Standard ( |input, gas_limit| {
84
- super :: run_pair (
67
+ run_pair (
85
68
input,
86
69
BYZANTIUM_PAIR_PER_POINT ,
87
70
BYZANTIUM_PAIR_BASE ,
@@ -137,7 +120,11 @@ pub fn new_g1_point(px: Fq, py: Fq) -> Result<G1, Error> {
137
120
}
138
121
}
139
122
140
- pub fn run_add ( input : & [ u8 ] ) -> Result < Bytes , Error > {
123
+ pub fn run_add ( input : & [ u8 ] , gas_cost : u64 , gas_limit : u64 ) -> PrecompileResult {
124
+ if gas_cost > gas_limit {
125
+ return Err ( Error :: OutOfGas ) ;
126
+ }
127
+
141
128
let input = right_pad :: < ADD_INPUT_LEN > ( input) ;
142
129
143
130
let p1 = read_point ( & input[ ..64 ] ) ?;
@@ -148,10 +135,14 @@ pub fn run_add(input: &[u8]) -> Result<Bytes, Error> {
148
135
sum. x ( ) . to_big_endian ( & mut output[ ..32 ] ) . unwrap ( ) ;
149
136
sum. y ( ) . to_big_endian ( & mut output[ 32 ..] ) . unwrap ( ) ;
150
137
}
151
- Ok ( output. into ( ) )
138
+ Ok ( ( gas_cost , output. into ( ) ) )
152
139
}
153
140
154
- pub fn run_mul ( input : & [ u8 ] ) -> Result < Bytes , Error > {
141
+ pub fn run_mul ( input : & [ u8 ] , gas_cost : u64 , gas_limit : u64 ) -> PrecompileResult {
142
+ if gas_cost > gas_limit {
143
+ return Err ( Error :: OutOfGas ) ;
144
+ }
145
+
155
146
let input = right_pad :: < MUL_INPUT_LEN > ( input) ;
156
147
157
148
let p = read_point ( & input[ ..64 ] ) ?;
@@ -164,7 +155,7 @@ pub fn run_mul(input: &[u8]) -> Result<Bytes, Error> {
164
155
mul. x ( ) . to_big_endian ( & mut output[ ..32 ] ) . unwrap ( ) ;
165
156
mul. y ( ) . to_big_endian ( & mut output[ 32 ..] ) . unwrap ( ) ;
166
157
}
167
- Ok ( output. into ( ) )
158
+ Ok ( ( gas_cost , output. into ( ) ) )
168
159
}
169
160
170
161
pub fn run_pair (
@@ -223,10 +214,12 @@ pub fn run_pair(
223
214
Ok ( ( gas_used, bool_to_bytes32 ( success) ) )
224
215
}
225
216
226
- /*
227
217
#[ cfg( test) ]
228
218
mod tests {
229
- use crate::test_utils::new_context;
219
+ use crate :: bn128:: add:: BYZANTIUM_ADD_GAS_COST ;
220
+ use crate :: bn128:: mul:: BYZANTIUM_MUL_GAS_COST ;
221
+ use crate :: bn128:: pair:: { BYZANTIUM_PAIR_BASE , BYZANTIUM_PAIR_PER_POINT } ;
222
+ use revm_primitives:: hex;
230
223
231
224
use super :: * ;
232
225
@@ -247,9 +240,7 @@ mod tests {
247
240
)
248
241
. unwrap ( ) ;
249
242
250
- let res = Bn128Add::<Byzantium>::run(&input, 500, &new_context(), false)
251
- .unwrap()
252
- .output;
243
+ let ( _, res) = run_add ( & input, BYZANTIUM_ADD_GAS_COST , 500 ) . unwrap ( ) ;
253
244
assert_eq ! ( res, expected) ;
254
245
255
246
// zero sum test
@@ -268,9 +259,7 @@ mod tests {
268
259
)
269
260
. unwrap ( ) ;
270
261
271
- let res = Bn128Add::<Byzantium>::run(&input, 500, &new_context(), false)
272
- .unwrap()
273
- .output;
262
+ let ( _, res) = run_add ( & input, BYZANTIUM_ADD_GAS_COST , 500 ) . unwrap ( ) ;
274
263
assert_eq ! ( res, expected) ;
275
264
276
265
// out of gas test
@@ -282,8 +271,10 @@ mod tests {
282
271
0000000000000000000000000000000000000000000000000000000000000000",
283
272
)
284
273
. unwrap ( ) ;
285
- let res = Bn128Add::<Byzantium>::run(&input, 499, &new_context(), false);
286
- assert!(matches!(res, Err(Return::OutOfGas)));
274
+
275
+ let res = run_add ( & input, BYZANTIUM_ADD_GAS_COST , 499 ) ;
276
+ println ! ( "{:?}" , res) ;
277
+ assert ! ( matches!( res, Err ( Error :: OutOfGas ) ) ) ;
287
278
288
279
// no input test
289
280
let input = [ 0u8 ; 0 ] ;
@@ -294,9 +285,7 @@ mod tests {
294
285
)
295
286
. unwrap ( ) ;
296
287
297
- let res = Bn128Add::<Byzantium>::run(&input, 500, &new_context(), false)
298
- .unwrap()
299
- .output;
288
+ let ( _, res) = run_add ( & input, BYZANTIUM_ADD_GAS_COST , 500 ) . unwrap ( ) ;
300
289
assert_eq ! ( res, expected) ;
301
290
302
291
// point not on curve fail
@@ -309,11 +298,8 @@ mod tests {
309
298
)
310
299
. unwrap ( ) ;
311
300
312
- let res = Bn128Add::<Byzantium>::run(&input, 500, &new_context(), false);
313
- assert!(matches!(
314
- res,
315
- Err(Return::Other(Cow::Borrowed("ERR_BN128_INVALID_POINT")))
316
- ));
301
+ let res = run_add ( & input, BYZANTIUM_ADD_GAS_COST , 500 ) ;
302
+ assert ! ( matches!( res, Err ( Error :: Bn128AffineGFailedToCreate ) ) ) ;
317
303
}
318
304
319
305
#[ test]
@@ -332,9 +318,7 @@ mod tests {
332
318
)
333
319
. unwrap ( ) ;
334
320
335
- let res = Bn128Mul::<Byzantium>::run(&input, 40_000, &new_context(), false)
336
- .unwrap()
337
- .output;
321
+ let ( _, res) = run_mul ( & input, BYZANTIUM_MUL_GAS_COST , 40_000 ) . unwrap ( ) ;
338
322
assert_eq ! ( res, expected) ;
339
323
340
324
// out of gas test
@@ -345,8 +329,9 @@ mod tests {
345
329
0200000000000000000000000000000000000000000000000000000000000000",
346
330
)
347
331
. unwrap ( ) ;
348
- let res = Bn128Mul::<Byzantium>::run(&input, 39_999, &new_context(), false);
349
- assert!(matches!(res, Err(Return::OutOfGas)));
332
+
333
+ let res = run_mul ( & input, BYZANTIUM_MUL_GAS_COST , 39_999 ) ;
334
+ assert ! ( matches!( res, Err ( Error :: OutOfGas ) ) ) ;
350
335
351
336
// zero multiplication test
352
337
let input = hex:: decode (
@@ -363,9 +348,7 @@ mod tests {
363
348
)
364
349
. unwrap ( ) ;
365
350
366
- let res = Bn128Mul::<Byzantium>::run(&input, 40_000, &new_context(), false)
367
- .unwrap()
368
- .output;
351
+ let ( _, res) = run_mul ( & input, BYZANTIUM_MUL_GAS_COST , 40_000 ) . unwrap ( ) ;
369
352
assert_eq ! ( res, expected) ;
370
353
371
354
// no input test
@@ -377,9 +360,7 @@ mod tests {
377
360
)
378
361
. unwrap ( ) ;
379
362
380
- let res = Bn128Mul::<Byzantium>::run(&input, 40_000, &new_context(), false)
381
- .unwrap()
382
- .output;
363
+ let ( _, res) = run_mul ( & input, BYZANTIUM_MUL_GAS_COST , 40_000 ) . unwrap ( ) ;
383
364
assert_eq ! ( res, expected) ;
384
365
385
366
// point not on curve fail
@@ -391,11 +372,8 @@ mod tests {
391
372
)
392
373
. unwrap ( ) ;
393
374
394
- let res = Bn128Mul::<Byzantium>::run(&input, 40_000, &new_context(), false);
395
- assert!(matches!(
396
- res,
397
- Err(Return::Other(Cow::Borrowed("ERR_BN128_INVALID_POINT")))
398
- ));
375
+ let res = run_mul ( & input, BYZANTIUM_MUL_GAS_COST , 40_000 ) ;
376
+ assert ! ( matches!( res, Err ( Error :: Bn128AffineGFailedToCreate ) ) ) ;
399
377
}
400
378
401
379
#[ test]
@@ -420,9 +398,13 @@ mod tests {
420
398
hex:: decode ( "0000000000000000000000000000000000000000000000000000000000000001" )
421
399
. unwrap ( ) ;
422
400
423
- let res = Bn128Pair::<Byzantium>::run(&input, 260_000, &new_context(), false)
424
- .unwrap()
425
- .output;
401
+ let ( _, res) = run_pair (
402
+ & input,
403
+ BYZANTIUM_PAIR_PER_POINT ,
404
+ BYZANTIUM_PAIR_BASE ,
405
+ 260_000 ,
406
+ )
407
+ . unwrap ( ) ;
426
408
assert_eq ! ( res, expected) ;
427
409
428
410
// out of gas test
@@ -442,18 +424,28 @@ mod tests {
442
424
12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
443
425
)
444
426
. unwrap ( ) ;
445
- let res = Bn128Pair::<Byzantium>::run(&input, 259_999, &new_context(), false);
446
- assert!(matches!(res, Err(Return::OutOfGas)));
427
+
428
+ let res = run_pair (
429
+ & input,
430
+ BYZANTIUM_PAIR_PER_POINT ,
431
+ BYZANTIUM_PAIR_BASE ,
432
+ 259_999 ,
433
+ ) ;
434
+ assert ! ( matches!( res, Err ( Error :: OutOfGas ) ) ) ;
447
435
448
436
// no input test
449
437
let input = [ 0u8 ; 0 ] ;
450
438
let expected =
451
439
hex:: decode ( "0000000000000000000000000000000000000000000000000000000000000001" )
452
440
. unwrap ( ) ;
453
441
454
- let res = Bn128Pair::<Byzantium>::run(&input, 260_000, &new_context(), false)
455
- .unwrap()
456
- .output;
442
+ let ( _, res) = run_pair (
443
+ & input,
444
+ BYZANTIUM_PAIR_PER_POINT ,
445
+ BYZANTIUM_PAIR_BASE ,
446
+ 260_000 ,
447
+ )
448
+ . unwrap ( ) ;
457
449
assert_eq ! ( res, expected) ;
458
450
459
451
// point not on curve fail
@@ -468,11 +460,13 @@ mod tests {
468
460
)
469
461
. unwrap ( ) ;
470
462
471
- let res = Bn128Pair::<Byzantium>::run(&input, 260_000, &new_context(), false);
472
- assert!(matches!(
473
- res,
474
- Err(Return::Other(Cow::Borrowed("ERR_BN128_INVALID_A")))
475
- ));
463
+ let res = run_pair (
464
+ & input,
465
+ BYZANTIUM_PAIR_PER_POINT ,
466
+ BYZANTIUM_PAIR_BASE ,
467
+ 260_000 ,
468
+ ) ;
469
+ assert ! ( matches!( res, Err ( Error :: Bn128AffineGFailedToCreate ) ) ) ;
476
470
477
471
// invalid input length
478
472
let input = hex:: decode (
@@ -484,11 +478,12 @@ mod tests {
484
478
)
485
479
. unwrap ( ) ;
486
480
487
- let res = Bn128Pair::<Byzantium>::run(&input, 260_000, &new_context(), false);
488
- assert!(matches!(
489
- res,
490
- Err(Return::Other(Cow::Borrowed("ERR_BN128_INVALID_LEN",)))
491
- ));
481
+ let res = run_pair (
482
+ & input,
483
+ BYZANTIUM_PAIR_PER_POINT ,
484
+ BYZANTIUM_PAIR_BASE ,
485
+ 260_000 ,
486
+ ) ;
487
+ assert ! ( matches!( res, Err ( Error :: Bn128PairLength ) ) ) ;
492
488
}
493
489
}
494
- */
0 commit comments