2

I'm trying to figure out how byte storage works for numbers. What size is a Nat vs an Int in bytes? Or, is there a way to encode a u8 vs a u64?

1 Answer 1

4

Integers have arbitrary precision, meaning that the only size limit is the gas_limit/storage_limit.

$ ./tezos-codec describe ground.Z binary schema

+-----------------+----------------------+----------+
| Name            | Size                 | Contents |
+=================+======================+==========+
| Unnamed field 0 | Determined from data | $Z.t     |
+-----------------+----------------------+----------+


Z.t
***

A variable length sequence of bytes, encoding a Zarith number. Each byte 
has a running unary size bit: the most significant bit of each byte 
tells is this is the last byte in the sequence (0) or if there is more 
to read (1). The second most significant bit of the first byte is 
reserved for the sign (positive if zero). Size and sign bits ignored, 
data is then the binary representation of the absolute value of the 
number in little endian order.

+------+----------------------+----------+
| Name | Size                 | Contents |
+======+======================+==========+
| Z.t  | Determined from data | bytes    |
+------+----------------------+----------+

In terms of bytes and gas usage between Int and Nat:

# Nat

$ tezos-client --endpoint https://granadanet.smartpy.io hash data '10000000' of type 'nat'

Raw packed data: 0x050080dac409
Gas remaining: 1039999.381 units remaining

# Int

$ tezos-client --endpoint https://granadanet.smartpy.io hash data '10000000' of type 'int'

Raw packed data: 0x050080dac409
Gas remaining: 1039999.381 units remaining

$ tezos-client --endpoint https://granadanet.smartpy.io hash data '-10000000' of type 'int'

Raw packed data: 0x0500c80dac409
Gas remaining: 1039999.381 units remaining
2
  • very cool, to clarify, is there a difference between 128 and 256 and 512 in terms of gas usage?
    – Rob
    Commented Sep 15, 2021 at 21:40
  • 1
    They cost the same amount of gas. Commented Sep 16, 2021 at 7:18

Your Answer

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

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