Overflow/underflow of []byte integer operations

i did read about byte operation here Opcodes - Algorand Developer Portal. so i think if need require large result number, i should use byte instead of uint64 (due to the value).
But i want to know what if the +,* failed on overflow or - operation failed on underflow for bytes (i see in uint operation it did)? what happended when they does not failed (the result will be unexpected)?

The byte integer operations b+ and b* do not overflow. The result will just be larger than 64 bytes.
b- fails on underflow.

See Transaction Execution Approval Language (TEAL) - Algorand Developer Portal for details.

1 Like

I still have some confuse when reading the doc.
Sorry if i’m not getting it but i really want to deeply understand about this.

  • with your answer The byte integer operations `b+` and `b*` do not overflow so what happend with the return value (if the result more than 64 bytes)?
  • The docs have this line there is an implicit length limit of 128 bytes on outputs, so does that mean the result of all operation (include b+,b*) must be less than 128 bytes, otherwise it will failed? or what happended with the return value?

Thanks again for your help.

The result is larger than 64 bytes.

It can never happen. When you add or multiply two 64-byte numbers, the result always fit in 128 bytes.

thanks for your answer. it help me a lot

just for clear, for example with this pyteal operation BytesDiv(BytesMul(a,b),c)
If a*b exceed 64 bytes, then the (a*b)/c will be failed (since left argument (which is a*b)) exceed 64 bytes)? the same this happend with b*,b+,b-?

and speaking to operation, i want to ask what exactly mulw , addw Opcodes - Algorand Developer Portal do?

can you give me some example, sorry for my bad understanding

Yes, if a*b exceeds 64 bytes, the above operation will fail.
Note that it is “bytes” not “bits”. 64 bytes = 512 bits, which is very very large.

(Remember uint64 = 64 bits = 8 bytes.)

BytesMul in PyTeal compiles to b*.
BytesDiv in PyTeal compiles to b/.
And so on.

1 Like

These are used if you don’t want to use the b... operations.
Nowadays, I think b... are much easier.

1 Like

Thanks for your answer. i have another question that need to ask here for byte calculation here.
back to the scenario where i have BytesMul(a,b)
if a*b exceeds 64 bytes and i store the result in scratch slot or global/local slot (which defined as bytes), will it work?
If not, what does the result of a*b that exceeds 64 bytes can work? why doesnt it failed on overflow?

inputs of the b+, b*, … operations are limited to 64 bytes.
But outputs are not.
So b* output may be 128 bytes. There is never an overflow.

However, then you cannot use the 128-byte output in any of the b+, b*, … operations.

See The Algorand Virtual Machine (AVM) and TEAL. - Algorand Developer Portal

1 Like

thanks for your answer.
my question is can the 128-byte output be used to store in scratch slot or global variable in application (which type defined as bytes)?
If not, what does this 128-byte output can be used?

A 128-byte string can be stored in scratchpad or in the global/local storage with key "", the empty key (since you have 128 bytes for both the key and the value together).
You can also hash it, extract part of it, … or do more complex algorithms.

1 Like