Slightly cleaner fix.

This commit is contained in:
Dale Rahn 2013-10-09 19:24:40 -04:00 committed by Owain G. Ainsworth
parent 53ca29efad
commit 003a41f66f

View file

@ -15,15 +15,15 @@ func asInt(v []byte) *big.Int {
return big.NewInt(0) return big.NewInt(0)
} }
negative := false negative := false
origlen := len(v)
msb := v[len(v)-1] msb := v[len(v)-1]
if msb&0x80 == 0x80 { if msb&0x80 == 0x80 {
negative = true negative = true
// remove sign bit // remove sign bit
msb &= 0x7f msb &= 0x7f
} }
start := 0
// trim leading 0 bytes // trim leading 0 bytes
for ; msb == 0; msb, start = v[len(v)-1], start+1 { for ; msb == 0; msb = v[len(v)-1] {
v = v[:len(v)-1] v = v[:len(v)-1]
if len(v) == 0 { if len(v) == 0 {
break break
@ -32,12 +32,12 @@ func asInt(v []byte) *big.Int {
// reverse bytes with a copy since stack is immutable. // reverse bytes with a copy since stack is immutable.
intArray := make([]byte, len(v)) intArray := make([]byte, len(v))
for i := range v { for i := range v {
// Mask off the sign bit without changing original array. intArray[len(v)-i-1] = v[i]
if i == len(v) -1 && start == 0 && negative { }
intArray[len(v)-i -1] = v[i] & 0x7f // IFF the value is negative and no 0 bytes were trimmed,
} else { // the leading byte needs to be sign corrected
intArray[len(v)-i-1] = v[i] if negative && len(intArray) == origlen {
} intArray[0] &= 0x7f
} }
num := new(big.Int).SetBytes(intArray) num := new(big.Int).SetBytes(intArray)