PopInt now guarantees that number will be 32 bit.

So remove later checks that need this.
This commit is contained in:
Owain G. Ainsworth 2014-03-14 17:51:17 +00:00
parent c7d5102954
commit f80c3255a3
3 changed files with 16 additions and 22 deletions

View file

@ -1258,10 +1258,7 @@ func opcodePick(op *parsedOpcode, s *Script) error {
return err return err
} }
if pidx.BitLen() > 32 { // PopInt promises that the int returned is 32 bit.
return StackErrNumberTooBig
}
val := int(pidx.Int64()) val := int(pidx.Int64())
return s.dstack.PickN(val) return s.dstack.PickN(val)
@ -1275,10 +1272,7 @@ func opcodeRoll(op *parsedOpcode, s *Script) error {
return err return err
} }
if ridx.BitLen() > 32 { // PopInt promises that the int returned is 32 bit.
return StackErrNumberTooBig
}
val := int(ridx.Int64()) val := int(ridx.Int64())
return s.dstack.RollN(val) return s.dstack.RollN(val)
@ -1823,10 +1817,7 @@ func opcodeCheckMultiSig(op *parsedOpcode, s *Script) error {
// XXX arbitrary limits // XXX arbitrary limits
// nore more than 20 pubkeyhs, or 201 operations // nore more than 20 pubkeyhs, or 201 operations
if numPubkeys.BitLen() > 32 { // PopInt promises that the int returned is 32 bit.
return StackErrNumberTooBig
}
npk := int(numPubkeys.Int64()) npk := int(numPubkeys.Int64())
if npk < 0 || npk > MaxPubKeysPerMultiSig { if npk < 0 || npk > MaxPubKeysPerMultiSig {
return StackErrTooManyPubkeys return StackErrTooManyPubkeys
@ -1848,10 +1839,8 @@ func opcodeCheckMultiSig(op *parsedOpcode, s *Script) error {
if err != nil { if err != nil {
return err return err
} }
if numSignatures.BitLen() > 32 {
return StackErrNumberTooBig
}
// PopInt promises that the int returned is 32 bit.
nsig := int(numSignatures.Int64()) nsig := int(numSignatures.Int64())
sigStrings := make([][]byte, nsig) sigStrings := make([][]byte, nsig)

View file

@ -872,12 +872,13 @@ var detailedTests = []detailedTest{
disassembly: "OP_IFDUP", disassembly: "OP_IFDUP",
}, },
{ {
name: "toaltastack", // alt stack is purged at the end of execution
name: "toaltstack",
before: [][]byte{{1}}, before: [][]byte{{1}},
altbefore: [][]byte{}, altbefore: [][]byte{},
script: []byte{btcscript.OP_TOALTSTACK}, script: []byte{btcscript.OP_TOALTSTACK},
after: [][]byte{}, after: [][]byte{},
altafter: [][]byte{{1}}, altafter: [][]byte{},
disassembly: "OP_TOALTSTACK", disassembly: "OP_TOALTSTACK",
}, },
{ {

View file

@ -10,9 +10,13 @@ import (
// asInt converts a byte array to a bignum by treating it as a little endian // asInt converts a byte array to a bignum by treating it as a little endian
// number with sign bit. // number with sign bit.
func asInt(v []byte) *big.Int { func asInt(v []byte) (*big.Int, error) {
// Only 32bit numbers allowed.
if len(v) > 4 {
return nil, StackErrNumberTooBig
}
if len(v) == 0 { if len(v) == 0 {
return big.NewInt(0) return big.NewInt(0), nil
} }
negative := false negative := false
origlen := len(v) origlen := len(v)
@ -44,7 +48,7 @@ func asInt(v []byte) *big.Int {
if negative { if negative {
num = num.Neg(num) num = num.Neg(num)
} }
return num return num, nil
} }
// fromInt provies a Big.Int in little endian format with the high bit of the // fromInt provies a Big.Int in little endian format with the high bit of the
@ -127,7 +131,7 @@ func (s *Stack) PopInt() (*big.Int, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return asInt(so), nil return asInt(so)
} }
// PopBool pops the value off the top of the stack, converts it into a bool and // PopBool pops the value off the top of the stack, converts it into a bool and
@ -155,7 +159,7 @@ func (s *Stack) PeekInt(idx int) (i *big.Int, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return asInt(so), nil return asInt(so)
} }
// PeekBool returns the nth item on the stack as a bool without removing it. // PeekBool returns the nth item on the stack as a bool without removing it.