Skip to content
On this page

Integers โ€‹

There are two main types of integers in SmartPy:

The corresponding types in Michelson are

Michelson int

and

Michelson nat.

SmartPy also uses a third definition sp.TIntOrNat which stands for integers that are not yet determined as sp.TInt or sp.TNat.

Literals โ€‹

1,ย 2,ย 0,ย -5
Literal of type sp.TIntOrNat when non negative and sp.TInt otherwise.

sp.int(i)
A literal of type sp.TInt when i is a Python integer literal.

sp.nat(n)
A literal of type sp.TNat when n is a non negative Python integer literal.

Example โ€‹

python
value  = 1          # sp.TIntOrNat
value1 = -1         # sp.TInt
value2 = sp.int(10) # sp.TInt
value3 = sp.nat(20) # sp.TNat

Arithmetic Operations โ€‹

The usual arithmetic operators +, -, *, %, //, <<, >> behave just like in Python.

In SmartPy, type inference of arithmetic operators imposes that both sides have the same type. This constraint can be relaxed by explicitly using sp.to_int.

Addition โ€‹

expr1 + expr2 sp.add(<expr1>,ย <expr2>) (Overloaded version) Michelson ADD
Add two numerical values, expr1 and expr2.

Example โ€‹

python
sp.int(1) + sp.int(2) # 3 of type sp.TInt
sp.nat(1) + sp.nat(2) # 3 of type sp.TNat
1 + sp.int(2)         # 3 of type sp.TInt
1 + sp.nat(2)         # 3 of type sp.TNat
1 + 2                 # 3 of type sp.TIntOrNat
sp.add(sp.int(1) + sp.nat(2)) # 3 of type sp.TInt

Subtraction โ€‹

expr1 - expr2 Michelson SUB
Subtract two numerical values, expr1 and expr2.

Example โ€‹

python
sp.int(1) - sp.int(2) # -1 of type sp.TInt
sp.nat(1) - sp.nat(2) # -1 of type sp.TInt
sp.int(2) - sp.int(1) #  1 of type sp.TInt
sp.nat(2) - sp.nat(1) #  1 of type sp.TInt

Negation โ€‹

- expr Michelson NEG
Negate a numerical value, expr.

Example โ€‹

python
value = - sp.nat(2) # -2 of type sp.TInt

Multiplication โ€‹

expr1 * expr2, sp.mul(<expr1>,ย <expr2>) (Overloaded version) Michelson MUL
Multiply two numerical values, expr1 and expr2.

Example โ€‹

python
value1 = sp.int(2) * sp.int(2) # 4 of type sp.TInt
value2 = sp.nat(2) * sp.nat(2) # 4 of type sp.TNat
value3 = sp.mul(sp.nat(2), sp.int(2)) # 4 of type sp.TInt

Division โ€‹

expr1 % expr2 Michelson EDIV
Perform euclidean division and get the remainder, where expr1 is the dividend, and expr2 is the divisor.

Example โ€‹

python
remainder = 11 % 2 # 1

Which is an alias of:

python
remainder = sp.snd(sp.ediv(11, 2).open_some()) # 1

expr1 // expr2 Michelson EDIV
Perform euclidean division and get the quotient, where expr1 is the dividend, and expr2 is the divisor.

Example โ€‹

python
quotient = 11 // 2 # 5

Which is an alias of:

python
quotient = sp.fst(sp.ediv(11, 2).open_some()) # 5

sp.ediv(expr1, expr2) Michelson EDIV
Perform euclidean division, where expr1 is the dividend, and expr2 is the divisor.

When expr1 and expr2 are both of type sp.TNat, the returned value is of type sp.TOption(sp.TPair(sp.TNat, sp.TNat)).

In the remaining cases, the returned value is of type sp.TOption(sp.TPair(sp.TInt, sp.TNat)).

The first element of sp.TPair(sp.TInt, sp.TNat) is the quotient, whereas the second element is the remainder.

Example โ€‹

python
(quotient, remainder) = sp.match_pair(sp.ediv(11, 2).open_some())

Logical Operations โ€‹

Shift โ€‹

expr1 << expr2 Michelson LSL
Logically left shift a natural number.

The operation expects expr1 and expr2 to be of type sp.TNat and to be less than or equal to 256. It produces the first number logically left-shifted by second number, which is of type sp.TNat.

expr1 >> expr2 Michelson LSR
Logically right shift a natural number.

The operation expects expr1 and expr2 to be of type sp.TNat and to be less than or equal to 256. It produces the first number logically right-shifted by second number, which is of type sp.TNat.

Or โ€‹

expr1 | expr2 Michelson OR
Compute bitwise expr1 or expr2 for expr1 and expr2 of type sp.TNat.
Result is also of type sp.TNat.

And โ€‹

expr1 & expr2 Michelson AND
Compute bitwise expr1 and expr2 for expr1 and expr2 of type sp.TNat.
Result is also of type sp.TNat.

Exclusive or โ€‹

expr1 ^ expr2 Michelson XOR
Compute bitwise expr1 xor expr2 for expr1 and expr2 of type sp.TNat.
Result is also of type sp.TNat.

Int / Nat Operations โ€‹

Cast nat to int โ€‹

sp.to_int(n)
Convert a sp.TNat into an sp.TInt. This operation doesn't fail.

Example โ€‹

python
intValue = sp.to_int(sp.nat(10)) # 10 of type sp.TInt
Michelson INT

Extract nat from int โ€‹

sp.is_nat(i)
Convert a sp.TInt into an sp.TOption(sp.TNat). This operation doesn't fail.

sp.is_nat(i)ย == sp.some(n) when i is a non negative sp.TInt and sp.none otherwise.

Example โ€‹

python
intValue = sp.is_nat(i)

sp.as_nat(i)
Convert an sp.TInt into a sp.TNat. This operation fails if not possible with the optional message, i.e., when i is negative.

python
natValue = sp.as_nat(i, message = None)

It is an alias of sp.is_nat(i).open_some(messageย = message).

Michelson ISNAT

Absolute value โ€‹

abs(i)
Return the absolute value of i.
abs takes an sp.TInt and returns a sp.TNat.

Example โ€‹

python
self.data.x = -10
natValue = abs(self.data.x) # equivalent to 10 of type sp.TNat
Michelson ABS