@@ -42,6 +42,23 @@ fn add_padding(input: [u8; 96]) -> [u8; OUTPUT_LENGTH] {
42
42
output
43
43
}
44
44
45
+ // Wrapper around G1Affine::from_uncompressed to handle the case in which all
46
+ // the input bytes are zero, which should represent the infinity point in the
47
+ // curve.
48
+ fn g1_affine_from_uncompressed ( input : [ u8 ; 96 ] ) -> Result < G1Affine , PrecompileError > {
49
+ if input == [ 0 ; 96 ] {
50
+ Ok ( G1Affine :: identity ( ) )
51
+ } else {
52
+ let output = G1Affine :: from_uncompressed ( & input) ;
53
+ if ( !output. is_some ( ) ) . into ( ) {
54
+ return Err ( PrecompileError :: Other (
55
+ "The given input did not represent a valid elliptic curve point" . to_string ( ) ,
56
+ ) ) ;
57
+ }
58
+ Ok ( output. unwrap ( ) )
59
+ }
60
+ }
61
+
45
62
fn extract_input ( input : & [ u8 ] ) -> Result < [ u8 ; 96 ] , PrecompileError > {
46
63
if input. len ( ) != INPUT_LENGTH / 2 {
47
64
return Err ( PrecompileError :: Other ( format ! (
@@ -78,19 +95,13 @@ fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
78
95
}
79
96
80
97
let input_p0 = extract_input ( & input[ ..128 ] ) ?;
81
- let p0 = G1Affine :: from_uncompressed ( & input_p0) ;
82
- if ( !p0. is_some ( ) ) . into ( ) {
83
- return Err ( PrecompileError :: Other ( "p0 was not a valid elliptic curve point" . to_string ( ) ) ) ;
84
- }
98
+ let p0 = g1_affine_from_uncompressed ( input_p0) ?;
85
99
86
100
let input_p1 = extract_input ( & input[ 128 ..] ) ?;
87
- let p1 = G1Affine :: from_uncompressed ( & input_p1) ;
88
- if ( !p1. is_some ( ) ) . into ( ) {
89
- return Err ( PrecompileError :: Other ( "p1 was not a valid elliptic curve point" . to_string ( ) ) ) ;
90
- }
101
+ let p1 = g1_affine_from_uncompressed ( input_p1) ?;
91
102
92
- let p1_projective: G1Projective = p1. unwrap ( ) . into ( ) ;
93
- let out = p0. unwrap ( ) . add ( p1_projective) ;
103
+ let p1_projective: G1Projective = p1. into ( ) ;
104
+ let out = p0. add ( p1_projective) ;
94
105
let out: G1Affine = out. into ( ) ;
95
106
let out_bytes = add_padding ( out. to_uncompressed ( ) ) ;
96
107
@@ -226,6 +237,8 @@ mod test {
226
237
// test vectors from https://github.com/ethereum/go-ethereum/blob/master/core/vm/testdata/precompiles/blsG1Add.json and https://github.com/ethereum/go-ethereum/blob/master/core/vm/testdata/precompiles/fail-blsG1Add.json
227
238
#[ case:: g1_plus_g1_equals_two_times_g1( "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1" , "000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d28" , false , 500 ) ]
228
239
#[ case:: two_times_g1_plus_three_times_g1_equals_five_times_g1( "000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d280000000000000000000000000000000009ece308f9d1f0131765212deca99697b112d61f9be9a5f1f3780a51335b3ff981747a0b2ca2179b96d2c0c9024e522400000000000000000000000000000000032b80d3a6f5b09f8a84623389c5f80ca69a0cddabc3097f9d9c27310fd43be6e745256c634af45ca3473b0590ae30d1" , "0000000000000000000000000000000010e7791fb972fe014159aa33a98622da3cdc98ff707965e536d8636b5fcc5ac7a91a8c46e59a00dca575af0f18fb13dc0000000000000000000000000000000016ba437edcc6551e30c10512367494bfb6b01cc6681e8a4c3cd2501832ab5c4abc40b4578b85cbaffbf0bcd70d67c6e2" , false , 500 ) ]
240
+ #[ case:: inf_plus_g1_equals_g1( "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" , "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1" , false , 500 ) ]
241
+ #[ case:: matter_g1_add_47( "0000000000000000000000000000000001ea88d0f329135df49893406b4f9aee0abfd74b62e7eb5576d3ddb329fc4b1649b7c228ec39c6577a069c0811c952f100000000000000000000000000000000033f481fc62ab0a249561d180da39ff641a540c9c109cde41946a0e85d18c9d60b41dbcdec370c5c9f22a9ee9de00ccd0000000000000000000000000000000014b78c66c4acecdd913ba73cc4ab573c64b404a9494d29d4a2ba02393d9b8fdaba47bb7e76d32586df3a00e03ae2896700000000000000000000000000000000025c371cd8b72592a45dc521336a891202c5f96954812b1095ba2ea6bb11aad7b6941a44d68fe9b44e4e5fd06bd541d4" , "0000000000000000000000000000000015b164c854a2277658f5d08e04887d896a082c6c20895c8809ed4b349da8492d6fa0333ace6059a1f0d37e92ae9bad30000000000000000000000000000000001510d176ddba09ab60bb452188c2705ef154f449bed26abf0255897673a625637b5761355b17676748f67844a61d4e9f" , false , 500 ) ]
229
242
fn test_g1_add (
230
243
#[ case] input : & str ,
231
244
#[ case] expected_output : & str ,
0 commit comments