checkMinimalDataEncoding returns whether or not the passed byte array adheres to the minimal encoding requirements.
(v []byte)
| 60 | // checkMinimalDataEncoding returns whether or not the passed byte array adheres |
| 61 | // to the minimal encoding requirements. |
| 62 | func checkMinimalDataEncoding(v []byte) error { |
| 63 | if len(v) == 0 { |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // Check that the number is encoded with the minimum possible |
| 68 | // number of bytes. |
| 69 | // |
| 70 | // If the most-significant-byte - excluding the sign bit - is zero |
| 71 | // then we're not minimal. Note how this test also rejects the |
| 72 | // negative-zero encoding, [0x80]. |
| 73 | if v[len(v)-1]&0x7f == 0 { |
| 74 | // One exception: if there's more than one byte and the most |
| 75 | // significant bit of the second-most-significant-byte is set |
| 76 | // it would conflict with the sign bit. An example of this case |
| 77 | // is +-255, which encode to 0xff00 and 0xff80 respectively. |
| 78 | // (big-endian). |
| 79 | if len(v) == 1 || v[len(v)-2]&0x80 == 0 { |
| 80 | str := fmt.Sprintf("numeric value encoded as %x is "+ |
| 81 | "not minimally encoded", v) |
| 82 | return scriptError(ErrMinimalData, str) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | // Bytes returns the number serialized as a little endian with a sign bit. |
| 90 | // |
no test coverage detected
searching dependent graphs…