@@ -438,63 +438,60 @@ func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
438438type blake2F struct {}
439439
440440func (c * blake2F ) RequiredGas (input []byte ) uint64 {
441+ // If the input is malformed, we can't calculate the gas, return 0 and let the
442+ // actual call choke and fault.
441443 if len (input ) != blake2FInputLength {
442- // Input is malformed, we can't read the number of rounds.
443- // Precompile can't be executed so we set its price to 0.
444444 return 0
445445 }
446-
447- rounds := binary .BigEndian .Uint32 (input [0 :4 ])
448- return uint64 (rounds )
446+ return uint64 (binary .BigEndian .Uint32 (input [0 :4 ]))
449447}
450448
451- const blake2FInputLength = 213
452- const blake2FFinalBlockBytes = byte (1 )
453- const blake2FNonFinalBlockBytes = byte (0 )
454-
455- var errBlake2FIncorrectInputLength = errors .New (
456- "input length for Blake2 F precompile should be exactly 213 bytes" ,
449+ const (
450+ blake2FInputLength = 213
451+ blake2FFinalBlockBytes = byte (1 )
452+ blake2FNonFinalBlockBytes = byte (0 )
457453)
458454
459- var errBlake2FIncorrectFinalBlockIndicator = errors .New (
460- "incorrect final block indicator flag" ,
455+ var (
456+ errBlake2FInvalidInputLength = errors .New ("invalid input length" )
457+ errBlake2FInvalidFinalFlag = errors .New ("invalid final flag" )
461458)
462459
463460func (c * blake2F ) Run (input []byte ) ([]byte , error ) {
461+ // Make sure the input is valid (correct lenth and final flag)
464462 if len (input ) != blake2FInputLength {
465- return nil , errBlake2FIncorrectInputLength
463+ return nil , errBlake2FInvalidInputLength
466464 }
467465 if input [212 ] != blake2FNonFinalBlockBytes && input [212 ] != blake2FFinalBlockBytes {
468- return nil , errBlake2FIncorrectFinalBlockIndicator
466+ return nil , errBlake2FInvalidFinalFlag
469467 }
468+ // Parse the input into the Blake2b call parameters
469+ var (
470+ rounds = binary .BigEndian .Uint32 (input [0 :4 ])
471+ final = (input [212 ] == blake2FFinalBlockBytes )
470472
471- rounds := binary .BigEndian .Uint32 (input [0 :4 ])
472-
473- var h [8 ]uint64
473+ h [8 ]uint64
474+ m [16 ]uint64
475+ t [2 ]uint64
476+ )
474477 for i := 0 ; i < 8 ; i ++ {
475478 offset := 4 + i * 8
476479 h [i ] = binary .LittleEndian .Uint64 (input [offset : offset + 8 ])
477480 }
478-
479- var m [16 ]uint64
480481 for i := 0 ; i < 16 ; i ++ {
481482 offset := 68 + i * 8
482483 m [i ] = binary .LittleEndian .Uint64 (input [offset : offset + 8 ])
483484 }
484-
485- var t [2 ]uint64
486485 t [0 ] = binary .LittleEndian .Uint64 (input [196 :204 ])
487486 t [1 ] = binary .LittleEndian .Uint64 (input [204 :212 ])
488487
489- f := (input [212 ] == blake2FFinalBlockBytes )
488+ // Execute the compression function, extract and return the result
489+ blake2b .F (& h , m , t , final , rounds )
490490
491- blake2b .F (& h , m , t , f , rounds )
492-
493- var output [64 ]byte
491+ output := make ([]byte , 64 )
494492 for i := 0 ; i < 8 ; i ++ {
495493 offset := i * 8
496494 binary .LittleEndian .PutUint64 (output [offset :offset + 8 ], h [i ])
497495 }
498-
499- return output [:], nil
496+ return output , nil
500497}
0 commit comments