@@ -111,11 +111,26 @@ impl From<u64> for PrecompileCalls {
111111 0x08 => Self :: Bn128Pairing ,
112112 0x09 => Self :: Blake2F ,
113113 0x100 => Self :: P256Verify ,
114- _ => unreachable ! ( "precompile contracts only from 0x01 to 0x09 " ) ,
114+ _ => unreachable ! ( "precompile contracts only from 0x01 to 0x100 " ) ,
115115 }
116116 }
117117}
118118
119+ /// size limit of modexp
120+ pub const MODEXP_SIZE_LIMIT : usize = 32 ;
121+ /// size of input limit
122+ pub const MODEXP_INPUT_LIMIT : usize = 192 ;
123+
124+ /// size of Bn128_Mul input limit
125+ pub const BN128MUL_INPUT_LIMIT : usize = 96 ;
126+ /// size of Bn128Add input limit
127+ pub const BN128ADD_INPUT_LIMIT : usize = 128 ;
128+
129+ /// size of p256Verify input limit
130+ pub const P256VERIFY_INPUT_LIMIT : usize = 160 ;
131+ /// size of Ecrecover input limit
132+ pub const ECRECOVER_INPUT_LIMIT : usize = 128 ;
133+
119134impl PrecompileCalls {
120135 /// Get the base gas cost for the precompile call.
121136 pub fn base_gas_cost ( & self ) -> GasCost {
@@ -141,9 +156,11 @@ impl PrecompileCalls {
141156 /// Maximum length of input bytes considered for the precompile call.
142157 pub fn input_len ( & self ) -> Option < usize > {
143158 match self {
144- Self :: Ecrecover | Self :: Bn128Add => Some ( 128 ) ,
145- Self :: Bn128Mul => Some ( 96 ) ,
159+ Self :: Ecrecover => Some ( ECRECOVER_INPUT_LIMIT ) ,
160+ Self :: Bn128Add => Some ( BN128ADD_INPUT_LIMIT ) ,
161+ Self :: Bn128Mul => Some ( BN128MUL_INPUT_LIMIT ) ,
146162 Self :: Modexp => Some ( MODEXP_INPUT_LIMIT ) ,
163+ Self :: P256Verify => Some ( P256VERIFY_INPUT_LIMIT ) ,
147164 _ => None ,
148165 }
149166 }
@@ -174,7 +191,7 @@ impl EcrecoverAuxData {
174191 /// Create a new instance of ecrecover auxiliary data.
175192 pub fn new ( input : & [ u8 ] , output : & [ u8 ] , return_bytes : & [ u8 ] ) -> Self {
176193 let mut resized_input = input. to_vec ( ) ;
177- resized_input. resize ( 128 , 0u8 ) ;
194+ resized_input. resize ( ECRECOVER_INPUT_LIMIT , 0u8 ) ;
178195 let mut resized_output = output. to_vec ( ) ;
179196 resized_output. resize ( 32 , 0u8 ) ;
180197
@@ -206,10 +223,52 @@ impl EcrecoverAuxData {
206223 }
207224}
208225
209- /// size limit of modexp
210- pub const MODEXP_SIZE_LIMIT : usize = 32 ;
211- /// size of input limit
212- pub const MODEXP_INPUT_LIMIT : usize = 192 ;
226+ /// Auxiliary data for P256Verify
227+ #[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
228+ pub struct P256VerifyAuxData {
229+ /// mas hash of the message being signed, can be any hash algorithm result.
230+ pub msg_hash : Word ,
231+ /// r-component of signature.
232+ pub sig_r : Word ,
233+ /// s-component of signature.
234+ pub sig_s : Word ,
235+ /// x-component of public key.
236+ pub pubkey_x : Word ,
237+ /// y-component of public key.
238+ pub pubkey_y : Word ,
239+ /// Input bytes to the P256Verify call.
240+ pub input_bytes : Vec < u8 > ,
241+ /// Output bytes from the P256Verify call.
242+ pub output_bytes : Vec < u8 > ,
243+ /// Bytes returned to the caller from the P256Verify call.
244+ pub return_bytes : Vec < u8 > ,
245+ }
246+
247+ impl P256VerifyAuxData {
248+ /// Create a new instance of p256Verify auxiliary data.
249+ pub fn new ( input : & [ u8 ] , output : & [ u8 ] , return_bytes : & [ u8 ] ) -> Self {
250+ let mut resized_input = input. to_vec ( ) ;
251+ resized_input. resize ( P256VERIFY_INPUT_LIMIT , 0u8 ) ;
252+ let mut resized_output = output. to_vec ( ) ;
253+ resized_output. resize ( 32 , 0u8 ) ;
254+
255+ // assert that output bytes is 32 bytes.
256+ assert ! ( resized_output[ 0x01 ..0x20 ] . iter( ) . all( |& b| b == 0 ) ) ;
257+ // assert first byte is bool
258+ assert ! ( resized_output[ 0x01 ] == 1u8 || resized_output[ 0x01 ] == 0 ) ;
259+
260+ Self {
261+ msg_hash : Word :: from_big_endian ( & resized_input[ 0x00 ..0x20 ] ) ,
262+ sig_r : Word :: from_big_endian ( & resized_input[ 0x20 ..0x40 ] ) ,
263+ sig_s : Word :: from_big_endian ( & resized_input[ 0x40 ..0x60 ] ) ,
264+ pubkey_x : Word :: from_big_endian ( & resized_input[ 0x60 ..0x80 ] ) ,
265+ pubkey_y : Word :: from_big_endian ( & resized_input[ 0x80 ..0xa0 ] ) ,
266+ input_bytes : input. to_vec ( ) ,
267+ output_bytes : output. to_vec ( ) ,
268+ return_bytes : return_bytes. to_vec ( ) ,
269+ }
270+ }
271+ }
213272
214273/// Auxiliary data for Modexp
215274#[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
@@ -336,10 +395,10 @@ pub struct EcAddAuxData {
336395}
337396
338397impl EcAddAuxData {
339- /// Create a new instance of ecrecover auxiliary data.
398+ /// Create a new instance of EcAddAux auxiliary data.
340399 pub fn new ( input : & [ u8 ] , output : & [ u8 ] , return_bytes : & [ u8 ] ) -> Self {
341400 let mut resized_input = input. to_vec ( ) ;
342- resized_input. resize ( 128 , 0u8 ) ;
401+ resized_input. resize ( BN128ADD_INPUT_LIMIT , 0u8 ) ;
343402 let mut resized_output = output. to_vec ( ) ;
344403 resized_output. resize ( 64 , 0u8 ) ;
345404
@@ -384,7 +443,7 @@ impl EcMulAuxData {
384443 /// Create a new instance of EcMul auxiliary data.
385444 pub fn new ( input : & [ u8 ] , output : & [ u8 ] , return_bytes : & [ u8 ] ) -> Self {
386445 let mut resized_input = input. to_vec ( ) ;
387- resized_input. resize ( 96 , 0u8 ) ;
446+ resized_input. resize ( BN128MUL_INPUT_LIMIT , 0u8 ) ;
388447 let mut resized_output = output. to_vec ( ) ;
389448 resized_output. resize ( 64 , 0u8 ) ;
390449
@@ -457,6 +516,8 @@ pub enum PrecompileAuxData {
457516 EcMul ( EcMulAuxData ) ,
458517 /// EcPairing.
459518 EcPairing ( Box < Result < EcPairingAuxData , EcPairingError > > ) ,
519+ /// p256Verify
520+ P256Verify ( P256VerifyAuxData ) ,
460521}
461522
462523impl Default for PrecompileAuxData {
0 commit comments