From f80c3255a34cea396f9bc6bec273c71505aa0616 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Fri, 14 Mar 2014 17:51:17 +0000 Subject: [PATCH] PopInt now guarantees that number will be 32 bit. So remove later checks that need this. --- opcode.go | 19 ++++--------------- opcode_test.go | 5 +++-- stack.go | 14 +++++++++----- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/opcode.go b/opcode.go index 58ec7367..52b930b9 100644 --- a/opcode.go +++ b/opcode.go @@ -1258,10 +1258,7 @@ func opcodePick(op *parsedOpcode, s *Script) error { return err } - if pidx.BitLen() > 32 { - return StackErrNumberTooBig - } - + // PopInt promises that the int returned is 32 bit. val := int(pidx.Int64()) return s.dstack.PickN(val) @@ -1275,10 +1272,7 @@ func opcodeRoll(op *parsedOpcode, s *Script) error { return err } - if ridx.BitLen() > 32 { - return StackErrNumberTooBig - } - + // PopInt promises that the int returned is 32 bit. val := int(ridx.Int64()) return s.dstack.RollN(val) @@ -1823,10 +1817,7 @@ func opcodeCheckMultiSig(op *parsedOpcode, s *Script) error { // XXX arbitrary limits // nore more than 20 pubkeyhs, or 201 operations - if numPubkeys.BitLen() > 32 { - return StackErrNumberTooBig - } - + // PopInt promises that the int returned is 32 bit. npk := int(numPubkeys.Int64()) if npk < 0 || npk > MaxPubKeysPerMultiSig { return StackErrTooManyPubkeys @@ -1848,10 +1839,8 @@ func opcodeCheckMultiSig(op *parsedOpcode, s *Script) error { if err != nil { return err } - if numSignatures.BitLen() > 32 { - return StackErrNumberTooBig - } + // PopInt promises that the int returned is 32 bit. nsig := int(numSignatures.Int64()) sigStrings := make([][]byte, nsig) diff --git a/opcode_test.go b/opcode_test.go index d5591df1..2549973c 100644 --- a/opcode_test.go +++ b/opcode_test.go @@ -872,12 +872,13 @@ var detailedTests = []detailedTest{ disassembly: "OP_IFDUP", }, { - name: "toaltastack", + // alt stack is purged at the end of execution + name: "toaltstack", before: [][]byte{{1}}, altbefore: [][]byte{}, script: []byte{btcscript.OP_TOALTSTACK}, after: [][]byte{}, - altafter: [][]byte{{1}}, + altafter: [][]byte{}, disassembly: "OP_TOALTSTACK", }, { diff --git a/stack.go b/stack.go index ffc30429..f2c96c5f 100644 --- a/stack.go +++ b/stack.go @@ -10,9 +10,13 @@ import ( // asInt converts a byte array to a bignum by treating it as a little endian // 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 { - return big.NewInt(0) + return big.NewInt(0), nil } negative := false origlen := len(v) @@ -44,7 +48,7 @@ func asInt(v []byte) *big.Int { if negative { num = num.Neg(num) } - return num + return num, nil } // 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 { 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 @@ -155,7 +159,7 @@ func (s *Stack) PeekInt(idx int) (i *big.Int, err error) { if err != nil { return nil, err } - return asInt(so), nil + return asInt(so) } // PeekBool returns the nth item on the stack as a bool without removing it.