Skip to content

Commit 1bccafe

Browse files
committed
core/vm, crypto/blake2b: add SSE, AVX and AVX2 code
1 parent 2890f06 commit 1bccafe

14 files changed

+2560
-164
lines changed

core/vm/contracts.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -438,63 +438,60 @@ func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
438438
type blake2F struct{}
439439

440440
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.
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

463460
func (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
}

core/vm/contracts_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,22 +349,22 @@ var bn256PairingTests = []precompiledTest{
349349
var blake2FMalformedInputTests = []precompiledFailureTest{
350350
{
351351
input: "",
352-
expectedError: errBlake2FIncorrectInputLength,
352+
expectedError: errBlake2FInvalidInputLength,
353353
name: "vector 0: empty input",
354354
},
355355
{
356356
input: "00000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
357-
expectedError: errBlake2FIncorrectInputLength,
357+
expectedError: errBlake2FInvalidInputLength,
358358
name: "vector 1: less than 213 bytes input",
359359
},
360360
{
361361
input: "000000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
362-
expectedError: errBlake2FIncorrectInputLength,
362+
expectedError: errBlake2FInvalidInputLength,
363363
name: "vector 2: more than 213 bytes input",
364364
},
365365
{
366366
input: "0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000002",
367-
expectedError: errBlake2FIncorrectFinalBlockIndicator,
367+
expectedError: errBlake2FInvalidFinalFlag,
368368
name: "vector 3: malformed final block indicator flag",
369369
},
370370
}
@@ -391,6 +391,11 @@ var blake2FTests = []precompiledTest{
391391
expected: "b63a380cb2897d521994a85234ee2c181b5f844d2c624c002677e9703449d2fba551b3a8333bcdf5f2f7e08993d53923de3d64fcc68c034e717b9293fed7a421",
392392
name: "vector 7",
393393
},
394+
{
395+
input: "007A120048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
396+
expected: "6d2ce9e534d50e18ff866ae92d70cceba79bbcd14c63819fe48752c8aca87a4bb7dcc230d22a4047f0486cfcfb50a17b24b2899eb8fca370f22240adb5170189",
397+
name: "vector 8",
398+
},
394399
}
395400

396401
func testPrecompiled(addr string, test precompiledTest, t *testing.T) {

0 commit comments

Comments
 (0)