Question

Single value constraints in PER encoding

My understanding is that the following statement in X.691 says that INTEGER types with single value constraints will not encode anything.

13.2.1 If PER-visible constraints restrict the integer value to a single value, then there shall be no addition to the field-list, completing these procedures.

I also understand that nothing is encoded in the ASN.1 like below. and I tested it with asn1tools in python.

Num1 ::= INTEGER (40)

Input: Num1 = 40
Output: b'' (Empty bit string)

When using union for single value constraints, there is no guarantee that the value encoded will always be the same. However, with asn1tools in python, nothing is ever encoded.

Num2 ::= INTEGER (40 | 50 | 60)

Encoding:
  Input: Num2 = 40
  Output: b'' (Empty bit string)
  Input: Num2 = 50
  Output: b'' (Empty bit string)
  Input: Num2 = 60
  Output: b'' (Empty bit string)

Decoding:
  Input: b'' (Empty bit string)
  Output: 40

I had assumed that it would follow the ENUMERATED type encoding, but I didn't find a basis for that in X.691.

I'm wondering if it's correct that nothing is encoded in the above case, and if it's no longer a single value constraints when using a union.

Any help is appreciated.

 3  60  3
1 Jan 1970

Solution

 2

This is a limitation of the tool you are using ...

If you try on https://asn1.io/ you'll find following results

Num2 ::= INTEGER (40 | 50 | 60)

Encoding:
  Input: Num2 = 40
  Output: b'00000000' (0x00 - No bits set)
  Input: Num2 = 50
  Output: b'01010000' (0x50)
  Input: Num2 = 60
  Output: b'10100000' (0xA0)

Thanks Paul for the explanation in the comments on how you get that from the X691 document ...

The range of the INTEGER is 60-40=20

"range" is less than or equal to 255 (the bit-field case), we need 5 bits

It requires the generation of a bit-field with a 5 bits containing the value ("n" – "lower bound") as a non-negative-binary-integer encoding in a bit-field as specified in 11.3.

The bit field will contain 0 for the value 40, 10 for the value 50 and 20 for the value 60

So, you get the encoding of the 3 possible values for Num2

  • 40: we encode 0 on 5 bits: 00000
  • 50: we encode 10 on 5 bits: 01010
  • 60: we encode 20 on 5 bits: 10100

You find the hex value above padding 3 bits set to 0

2024-07-08
YaFred