DER
The Distinguished Encoding Rules (DER) format is used to encode ECDSA signatures in Bitcoin. An ECDSA signature is generated using a private key and a hash of the signed message. It consists of two 32-byte numbers (r,s). As described by Pieter here the DER signature format has the following components:
0x30byte: header byte to indicate compound structure- one byte to encode the length of the following data
0x02: header byte indicating an integer- one byte to encode the length of the following
rvalue - the
rvalue as a big-endian integer 0x02: header byte indicating an integer- one byte to encode the length of the following
rsvalue - the
svalue as a big-endian integer
Note that the r and s value must be prepended with 0x00 if their first byte is greater than 0x7F. This causes variable signature lengths in the case that the r value is in the upper half of the range (referred to as "high r"). Signatures with high s values are non-standard and usually don't appear in the wild. Also note that in rare cases r or s can be shorter than 32 bytes which is legal and leads to shorter signatures. Note that in bitcoin transactions a byte is added at the end of a DER signature denoting the SigHash type used.
SEC
The Standards of Efficient Cryptography (SEC) encoding is used to serialize ECDSA public keys. Public keys in Bitcoin are ECDSA points consisting of two coordinates (x,y). x and y may be smaller than 32 bytes in which case they must be padded with zeros to 32 bytes (H/T Coding Enthusiast). Bitcoin uses two formats, uncompressed and compressed:
Uncompressed:
0x04byte: header byte to indicate ECDSA point- the
xcoordinate as a 32-byte big-endian integer - the
ycoordinate as a 32-byte big-endian integer
Compressed:
As the coordinates (x,y) must satisfy the secp256k1 curve equation y² = x³ + 7, the two possible values for y can be calculated from the x value. Thus, we can express a public key just as the x coordinate in combination with an indicator which of the two y values to use.
0x02/0x03byte: header byte to indicate compressed ECDSA point,0x02for eveny,0x03for oddy- the
xcoordinate as a 32-byte big-endian integer
The above is more comprehensively explained e.g. in Jimmy Song's Programming Bitcoin: Ch4. Serialization.