MCPcopy Index your code
hub / github.com/btcsuite/btcd / checkMinimalDataEncoding

Function checkMinimalDataEncoding

txscript/scriptnum.go:62–87  ·  view source on GitHub ↗

checkMinimalDataEncoding returns whether or not the passed byte array adheres to the minimal encoding requirements.

(v []byte)

Source from the content-addressed store, hash-verified

60// checkMinimalDataEncoding returns whether or not the passed byte array adheres
61// to the minimal encoding requirements.
62func 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//

Callers 1

MakeScriptNumFunction · 0.85

Calls 1

scriptErrorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…