@@ -438,63 +438,60 @@ func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
438
438
type blake2F struct {}
439
439
440
440
func (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.
441
443
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.
444
444
return 0
445
445
}
446
-
447
- rounds := binary .BigEndian .Uint32 (input [0 :4 ])
448
- return uint64 (rounds )
446
+ return uint64 (binary .BigEndian .Uint32 (input [0 :4 ]))
449
447
}
450
448
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 )
457
453
)
458
454
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" )
461
458
)
462
459
463
460
func (c * blake2F ) Run (input []byte ) ([]byte , error ) {
461
+ // Make sure the input is valid (correct lenth and final flag)
464
462
if len (input ) != blake2FInputLength {
465
- return nil , errBlake2FIncorrectInputLength
463
+ return nil , errBlake2FInvalidInputLength
466
464
}
467
465
if input [212 ] != blake2FNonFinalBlockBytes && input [212 ] != blake2FFinalBlockBytes {
468
- return nil , errBlake2FIncorrectFinalBlockIndicator
466
+ return nil , errBlake2FInvalidFinalFlag
469
467
}
468
+ // Parse the input into the Blake2b call parameters
469
+ var (
470
+ rounds = binary .BigEndian .Uint32 (input [0 :4 ])
471
+ final = (input [212 ] == blake2FFinalBlockBytes )
470
472
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
+ )
474
477
for i := 0 ; i < 8 ; i ++ {
475
478
offset := 4 + i * 8
476
479
h [i ] = binary .LittleEndian .Uint64 (input [offset : offset + 8 ])
477
480
}
478
-
479
- var m [16 ]uint64
480
481
for i := 0 ; i < 16 ; i ++ {
481
482
offset := 68 + i * 8
482
483
m [i ] = binary .LittleEndian .Uint64 (input [offset : offset + 8 ])
483
484
}
484
-
485
- var t [2 ]uint64
486
485
t [0 ] = binary .LittleEndian .Uint64 (input [196 :204 ])
487
486
t [1 ] = binary .LittleEndian .Uint64 (input [204 :212 ])
488
487
489
- f := (input [212 ] == blake2FFinalBlockBytes )
488
+ // Execute the compression function, extract and return the result
489
+ blake2b .F (& h , m , t , final , rounds )
490
490
491
- blake2b .F (& h , m , t , f , rounds )
492
-
493
- var output [64 ]byte
491
+ output := make ([]byte , 64 )
494
492
for i := 0 ; i < 8 ; i ++ {
495
493
offset := i * 8
496
494
binary .LittleEndian .PutUint64 (output [offset :offset + 8 ], h [i ])
497
495
}
498
-
499
- return output [:], nil
496
+ return output , nil
500
497
}
0 commit comments