Skip to content

[#319] Handle natural numbers

Alyona Antonova requested to merge alyoanton9/#319-handle-natural-numbers into master

Description

Handling natural numbers raises Underflow exception. Namely there are two cases:

  1. Underflow while performing AND operation of int and nat.

    We can do AND with a negative Integer, so we must apply bitwise-and (.&. from Data.Bits) first and then convert the result to Natural (because the type of "return value" is Natural). However, there is converting Integer to Natural first, so fromIntegral raises Underflow. To solve this, we just fixed the order of the operations.

  2. Underflow while unpacking packed int as nat.

    In Michelson we are able to pack a number as int and then to unpack it as nat. When a number is negative, there must be NONE. However, there is an attempt to decode a negative number as Natural in decodeInt function, so Underflow is raised by fromIntegral again. To solve this, we changed fromIntegral to toIntegralSized and made decodeInt return Maybe with Nothing in case of negative number.

Related issue(s)

Resolves #319 (closed)

Checklist for your Merge Request

Related changes (conditional)

  • Tests (see short guidelines)

    • If I added new functionality, I added tests covering it.
    • If I fixed a bug, I added a regression test to prevent the bug from silently reappearing again.
  • Documentation

    • I checked whether I should update the docs and did so if necessary:
    • I updated changelog files of all affected packages released to Hackage if my changes are externally visible.

Stylistic guide (mandatory)

Edited by Alyona Antonova

Merge request reports