🚧 This documentation is currently under development. Content may be incomplete or subject to change. 🚧
Skip to content

Arithmetic ​

Arithmetic operations on two encrypted values of the same type. All arithmetic uses wrapping semantics, matching Solidity's unchecked behavior: on overflow or underflow, values wrap around the type boundary instead of reverting.

For overflow-safe variants, see Safe Arithmetic.

Supported types: euint16, euint256, eint16, eint256

Usage ​

solidity
euint256 total = Nox.add(balance, deposit);
euint256 remaining = Nox.sub(total, fee);
euint256 reward = Nox.mul(remaining, rate);
euint256 share = Nox.div(reward, participantCount);
Nox.allowThis(share);

add ​

solidity
function add(euint256 a, euint256 b) internal returns (euint256)

Wrapping addition. On overflow, the result wraps around the type boundary.

  • Unsigned: (a + b) mod 2^N
  • Signed: (a + b) mod 2^N, interpreted in two's complement. For example on Int8, adding past 127 continues from -128
Example (Uint8)ResultReason
200 + 10044Overflows, wraps around
255 + 10Overflows to zero
100 + 50150No overflow
Example (Int8)ResultReason
100 + 100-56Overflows, wraps to negative
127 + 1-128Overflows to MIN
-50 + 30-20No overflow

sub ​

solidity
function sub(euint256 a, euint256 b) internal returns (euint256)

Wrapping subtraction. On underflow, the result wraps around the type boundary.

  • Unsigned: when the result would be negative, it wraps around through MAX. For example on Uint8, 0 - 1 gives 255
  • Signed: when the result goes below MIN, it wraps around through MAX. For example on Int8, -128 - 1 gives 127
Example (Uint8)ResultReason
0 - 1255Underflows, wraps to MAX
10 - 20066Underflows, wraps around
100 - 3070No underflow
Example (Int8)ResultReason
-128 - 1127Underflows, wraps to MAX
-100 - 10056Underflows, wraps to positive
50 - 3020No underflow

mul ​

solidity
function mul(euint256 a, euint256 b) internal returns (euint256)

Wrapping multiplication. On overflow, the result wraps around the type boundary.

  • Unsigned: (a * b) mod 2^N
  • Signed: (a * b) mod 2^N, interpreted in two's complement
Example (Uint8)ResultReason
3 * 10044Overflows, wraps around
16 * 160Overflows to zero
10 * 550No overflow
Example (Int8)ResultReason
-1 * -128-128Overflows, wraps back to MIN
10 * 20-56Overflows, wraps to negative
-5 * 3-15No overflow

div ​

solidity
function div(euint256 a, euint256 b) internal returns (euint256)

Integer division, truncated toward zero. Division by zero does not revert, it returns the maximum representable value of the type (saturates toward +infinity).

  • Unsigned: returns 2^N - 1 (MAX_U). For example on Uint8, 10 / 0 gives 255
  • Signed: returns 2^(N-1) - 1 (MAX_I). For example on Int8, 10 / 0 gives 127. Additionally, MIN / -1 wraps back to MIN because the true result (128) exceeds MAX_I (127)
Example (Uint8)ResultReason
10 / 0255Division by zero, returns MAX_U
7 / 23Truncated toward zero
1 / 20Truncated toward zero
0 / 50Zero numerator
Example (Int8)ResultReason
10 / 0127Division by zero, returns MAX_I
-128 / -1-128Overflow, wraps back to MIN
7 / 23Truncated toward zero
-7 / 2-3Truncated toward zero