core/vm: fix incorrect computation of BLS discount (#21253)

* core/vm: fix incorrect computation of discount

During testing on Yolov1 we found that the way geth calculates the discount
is not in line with the specification. Basically what we did is calculate
128 * Bls12381GXMulGas * discount / 1000 whenever we received more than 128 pairs
of values. Correct would be to calculate k * Bls12381... for k > 128.

* core/vm: better logic for discount calculation

* core/vm: better calculation logic, added worstcase benchmarks

* core/vm: better benchmarking logic
This commit is contained in:
Marius van der Wijden
2020-06-24 19:58:28 +00:00
committed by GitHub
parent b482423e61
commit 0c82928981
2 changed files with 57 additions and 8 deletions

View File

@ -620,11 +620,12 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
return 0
}
// Lookup discount value for G1 point, scalar value pair length
maxDiscountLen := len(params.Bls12381MultiExpDiscountTable)
if k > maxDiscountLen {
k = maxDiscountLen
var discount uint64
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
discount = params.Bls12381MultiExpDiscountTable[k-1]
} else {
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
}
discount := params.Bls12381MultiExpDiscountTable[k-1]
// Calculate gas and return the result
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
}
@ -750,11 +751,12 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
return 0
}
// Lookup discount value for G2 point, scalar value pair length
maxDiscountLen := len(params.Bls12381MultiExpDiscountTable)
if k > maxDiscountLen {
k = maxDiscountLen
var discount uint64
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
discount = params.Bls12381MultiExpDiscountTable[k-1]
} else {
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
}
discount := params.Bls12381MultiExpDiscountTable[k-1]
// Calculate gas and return the result
return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000
}