Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/vm: metropolis precompiles #14959

Merged
merged 4 commits into from
Aug 15, 2017

Conversation

karalabe
Copy link
Member

@karalabe karalabe commented Aug 10, 2017

  • EIP 197: Precompiled contracts for pairing function check.
  • EIP 198: Precompiled contract for bigint modular exponentiation.
  • EIP 213 (196): Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128.

@karalabe karalabe mentioned this pull request Aug 10, 2017
8 tasks
@karalabe karalabe force-pushed the metropolis-precompiles branch 2 times, most recently from d659d42 to 7b66050 Compare August 11, 2017 11:07
@karalabe karalabe changed the title [WIP] Metropolis precompiles Metropolis precompiles Aug 11, 2017
@fjl fjl changed the title Metropolis precompiles core/vm: metropolis precompiles Aug 11, 2017
@karalabe
Copy link
Member Author

@fjl PTAL

@karalabe
Copy link
Member Author

@holiman PTAL

)
}
gas.Mul(gas, math.BigMax(adjExpLen, big.NewInt(1)))
gas.Div(gas, big.NewInt(100))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params.QuadCoeffDiv instead of 100

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is 512, the spec states 100.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does it state 100 ?

https://github.com/ethereum/EIPs/blob/4d4d8fb75eedeacc6456b804d9fb58c1778f1c76/EIPS/bigint_modexp.md#specification says

Consumes floor(mult_complexity(max(length_of_MODULUS, length_of_BASE)) * max(ADJUSTED_EXPONENT_LENGTH, 1) / GQUADDIVISOR) gas

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First line of the EIP

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see now. Then we should add GQUADDIVISOR as a new protocol param, it seems to me

gas.Mul(gas, math.BigMax(adjExpLen, big.NewInt(1)))
gas.Div(gas, big.NewInt(100))

if gas.Cmp(new(big.Int).SetUint64(math.MaxUint64)) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a more lightweight way to do the same check:
if x.BitLen() > 64

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is, will fix.

@karalabe
Copy link
Member Author

@holiman PTAL, last commit addresses your brought up issues.


package bn256

import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to not modify package bn256 even more. It's bad enough we have to change it at all. Please move these two functions to core/vm.

if err != nil {
return nil, err
}
// Add the two curve points and return the result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you like to comment things excessively - and usually I just skip over these - but this one triggered me. "add ... and return" followed by x.Add(...); return .... Please remove "XXX and return" comments.

} else {
offset := int(baseLen.Uint64())

input = common.RightPadBytes(input, offset+32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will copy the entire input. Is that really necessary?
Why not just use getData, as in either

expHead := new(big.Int).SetBytes(getData(input, baseLen, expLen))

or

expHead := new(big.Int).SetBytes(getData(input, baseLen, common.Big32))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not have a getData method. Arguably it could be a handy method. If desired, we can add a new method into common.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh, my bad, apparently we do have one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll thumb it it up if you avoid unnecessary copying. I'm wary of the gas calculation being an attack surface

@karalabe karalabe added this to the 1.7.0 milestone Aug 14, 2017
@@ -34,7 +34,21 @@ func calcMemSize(off, l *big.Int) *big.Int {

// getData returns a slice from the data based on the start and size and pads
// up to size with zero's. This function is overflow safe.
func getData(data []byte, start, size *big.Int) []byte {
func getData(data []byte, start uint64, size uint64) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment says this function is overflow safe. It's not..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overflow safe as in you can index outside of the bounds.

if start > length {
start = length
}
end := start + size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, I guess it would be overflow safe if you ensured that end >= start ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if start + size can overflow a uint64, the big version of this method should be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants