Skip to content

Commit ea68a94

Browse files
committed
Clarifications. Requirements for simple values.
1 parent 401ea1e commit ea68a94

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ This is the working area for the individual Internet-Draft, "draft-mcnally-deter
99

1010
## Change History
1111

12+
### August 6, 2023 - 04
13+
14+
* Made rules for encoders and decoders much more explicit.
15+
* Added a section on requirements for simple values.
16+
1217
### August 5, 2023 - 03
1318

1419
* Clarifications and minor corrections.

draft-mcnally-deterministic-cbor.md

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ This application profile is intended to be used in conjunction with an applicati
8080

8181
## Base Requirements
8282

83-
dCBOR encoders MUST only emit CBOR conforming to the requirements "Core Deterministic Encoding Requirements" of {{-CBOR}} §4.2.1.
83+
dCBOR encoders MUST only emit CBOR conforming to the requirements "Core Deterministic Encoding Requirements" of {{-CBOR}} §4.2.1. To summarize,
8484

85-
To summarize, dCBOR codecs:
85+
dCBOR encoders:
8686

8787
1. MUST encode variable-length integers using the shortest form possible.
8888
2. MUST encode floating-point values using the shortest form that preserves the value.
@@ -91,26 +91,48 @@ To summarize, dCBOR codecs:
9191

9292
In addition, dCBOR decoders:
9393

94-
 5\. MUST validate and return errors for any encoded CBOR that is not conformant to any part of this specification.
94+
5. MUST reject any variable length integers that are not encoded in the shortest form possible.
95+
{:start="5"}
96+
6. MUST reject any floating-point values that are not encoded in the shortest form that preserves the value.
97+
{:start="6"}
98+
7. MUST reject any indefinite-length arrays or maps.
99+
{:start="7"}
100+
8. MUST reject any maps whose keys are not sorted in bytewise lexicographic order of their deterministic encodings.
101+
{:start="8"}
95102

96103
## Duplicate Map Keys
97104

98105
Standard CBOR {{-CBOR}} defines maps with duplicate keys as invalid, but leaves how to handle such cases to the implementor (§2.2, §3.1, §5.4, §5.6).
99106

100-
dCBOR codecs:
107+
dCBOR encoders:
101108

102109
1. MUST NOT emit CBOR that contains duplicate map keys.
110+
111+
dCBOR decoders:
112+
103113
2. MUST reject encoded maps with duplicate keys.
114+
{:start="2"}
104115

105116
## Numeric Reduction
106117

107118
dCBOR codecs that support floating point numbers (CBOR major type 7):
108119

109-
1. MUST reduce floating point values with no fractional part to the shortest integer encoding that can accurately represent it.
110-
2. MUST reduce floating point values with a non-zero fractional part to the shortest floating point encoding that can accurately represent it.
111-
3. MUST support floating point {{IEEE754}} binary16 as the most-preferred encoding for floating point values, followed by binary32, then binary64.
120+
1. MUST support floating point {{IEEE754}} binary16 as the most-preferred encoding for floating point values, followed by binary32, then binary64.
121+
122+
dCBOR encoders that support floating point numbers:
123+
124+
2. MUST reduce floating point values with no fractional part to the shortest integer encoding that can accurately represent them.
125+
{:start="2"}
126+
127+
3. MUST reduce floating point values with a non-zero fractional part to the shortest floating point encoding that can accurately represent them.
128+
{:start="3"}
129+
130+
dCBOR decoders that support floating point numbers:
112131

113-
This practice still produces well-formed CBOR according to the standard, and all existing generic decoders will be able to read it. It does exclude a map such as the following from being validated as dCBOR, even though it would be allowed in standard CBOR because:
132+
4. MUST reject any encoded floating point values that are not encoded as the shortest encoding that can accurately represent them.
133+
{:start="4"}
134+
135+
The above rules still produce well-formed CBOR according to the standard, and all existing generic decoders will be able to read it. It does exclude a map such as the following from being validated as dCBOR, even though it would be allowed in standard CBOR because:
114136

115137
* `10.0` is an invalid numeric value in dCBOR, and
116138
* using the unsigned integer value `10` more than once as a map key is not allowed.
@@ -124,10 +146,16 @@ This practice still produces well-formed CBOR according to the standard, and all
124146

125147
### Reduction of Negative Zero
126148

127-
{{IEEE754}} defines a negative zero value `-0.0`. dCBOR codecs that support floating point:
149+
{{IEEE754}} defines a negative zero value `-0.0`.
150+
151+
dCBOR encoders that support floating point:
128152

129153
1. MUST reduce all negative zero values to the integer value `0`.
154+
155+
dCBOR decoders that support floating point:
156+
130157
2. MUST reject any encoded negative zero values.
158+
{:start="2"}
131159

132160
Therefore with dCBOR, `0.0`, `-0.0`, and `0` all encode to the same canonical single-byte value `0x00`.
133161

@@ -138,22 +166,49 @@ Therefore with dCBOR, `0.0`, `-0.0`, and `0` all encode to the same canonical si
138166
dCBOR encoders that support floating point:
139167

140168
1. MUST reduce all `NaN` values to the binary16 quiet `NaN` value having the canonical bit pattern `0x7e00`.
141-
2. MUST reject any other encoded `NaN` values.
142-
3. MUST reduce all `+INF` values to the binary16 `+INF` having the canonical bit pattern `0x7c00` and likewise with `-INF` to `0xfc00`.
143-
4. MUST reject any encoded `INF` or `-INF` values other than these.
169+
2. MUST reduce all `+INF` values to the binary16 `+INF` having the canonical bit pattern `0x7c00`.
170+
3. MUST reduce all `-INF` values to the binary16 `-INF` having the canonical bit pattern `0xfc00`.
171+
172+
dCBOR decoders that support floating point:
173+
174+
4. MUST reject any encoded `NaN` values not having the canonical bit pattern `0x7e00`.
175+
{:start="4"}
176+
5. MUST reject any encoded `+INF` values not having the canonical bit pattern `0x7c00`.
177+
{:start="5"}
178+
6. MUST reject any encoded `-INF` values not having the canonical bit pattern `0xfc00`.
179+
{:start="6"}
144180

145181
## 65-bit Negative Integers
146182

147-
The largest negative integer that can be represented in 64-bit two's complement (STANDARD_NEGATIVE_INT_MAX) is -2^63 (0x8000000000000000).
183+
The largest negative integer that can be represented in 64-bit two's complement (`STANDARD_NEGATIVE_INT_MAX`) is -2<sup>63</sup> (`0x8000000000000000`).
184+
185+
However, standard CBOR major type 1 can encode negative integers as low as `CBOR_NEGATIVE_INT_MAX`, which is -2<sup>64</sup> (two's complement: `0x10000000000000000`, CBOR: `0x3BFFFFFFFFFFFFFFFF`).
148186

149-
However, standard CBOR major type 1 can encode negative integers as low as CBOR_NEGATIVE_INT_MAX, which is -2^64 (two's complement: 0x10000000000000000, CBOR: 0x3BFFFFFFFFFFFFFFFF).
187+
Negative integers in the range \[`CBOR_NEGATIVE_INT_MAX` ... `STANDARD_NEGATIVE_INT_MAX` - 1\] require 65 bits of precision, and are thus not representable in typical machine-sized integers.
150188

151-
Negative integers in the range \[CBOR_NEGATIVE_INT_MAX ... STANDARD_NEGATIVE_INT_MAX - 1\] require 65 bits of precision, and are thus not representable in typical machine-sized integers.
189+
Because of this incompatibility between standard CBOR and typical machine-size representations, dCBOR disallows encoding negative integer values in the range \[`CBOR_NEGATIVE_INT_MAX` ... `STANDARD_NEGATIVE_INT_MAX` - 1\].
152190

153-
Because of this incompatibility between standard CBOR and typical machine-size representations, dCBOR disallows encoding negative integer values in the range \[CBOR_NEGATIVE_INT_MAX ... STANDARD_NEGATIVE_INT_MAX - 1\]. dCBOR codecs:
191+
dCBOR encoders:
154192

155193
1. MUST NOT encode these values as CBOR major type 1.
194+
195+
dCBOR decoders:
196+
156197
2. MUST reject these encoded major type 1 CBOR values.
198+
{:start="2"}
199+
200+
## Simple Values
201+
202+
CBOR Major Type 7 includes the floating point values (`0xf7`, `0xfa`, `0xfb`) and also the "simple values" `false` (`0xf4`), `true` (`0xf5`), and `null` (`0xf6`).
203+
204+
dCBOR encoders:
205+
206+
1. MUST NOT encode major type 7 values other than `false`, `true`, `null`, and the floating point values.
207+
208+
dCBOR decoders:
209+
210+
2. MUST reject any encoded major type 7 values other than `false`, `true`, `null`, and the floating point values.
211+
{:start="2"}
157212

158213
# Reference Implementations
159214

0 commit comments

Comments
 (0)