2

The mathematical structure of a set is an unordered collection. If a set does not have an order, how are they serialized by the PACK instruction?

According to this overview set is packable.

1 Answer 1

3

Sets are serialized by first ordering the elements, and then packing them as a sequence which is also how lists are represented when PACK is called. This means that these two expressions evaluate to the same, since they are first sorted, and then serialized.

ligo interpret -s pascaligo 'Bytes.pack(set [ 1; 2; 3; 4 ])'
ligo interpret -s pascaligo 'Bytes.pack(set [ 4; 2; 3; 1 ])'

Both output the same byte array

0x0502000000080001000200030004

Where

  • 05 is the prefix for any output from the PACK instruction
  • 02 is the tag for the sequence type
  • 00000008 is the four bytes indicating the size of the coming values, in bytes
  • 0001000200030004 are then numbers 1,2,3,4 encoded with first a 00 tag to indicate that this is a integer, and the numbers 01, ... 04 are the encoding of the actual numbers.
3
  • 2
    Note also, that in Michelson (may be different in Ligo), concrete sets must always be ordered. That is, PUSH (set nat) { 1 ; 3 ; 2 }; does not typecheck, where as PUSH (set nat) { 1 ; 2 ; 3 }; does.
    – arvidj
    Oct 15, 2020 at 14:48
  • For more details on how integers are encoded: tezos.stackexchange.com/a/2882/5135 Oct 15, 2020 at 20:21
  • Each datatype that is Comparable has an order. The ordering of integers is there numerical order, the ordering of strings and addresses is alphabetical ordering. Cf michelson.nomadic-labs.com/#instr-COMPARE Oct 16, 2020 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.