Soda Labs
Soda Labs Docs
Developer GuidesTypes and operations

Operations on encrypted types

Arithmetic, logical, and comparison operations available on encrypted types through the MpcCore Solidity library.

Our solidity library provides methods for manipulating secret data. It's essential to utilize these functions for any operations involving sensitive information. Additionally, it's worth noting that you can also call these functions with some of the inputs being scalars.

A developer wishing to invoke operations on private data should import the library in MpcCore.sol .

Bubble supports arithmetic, logical, and comparison operations, all performed securely off-chain through MPC while remaining encrypted on-chain.


Arithmetic

Operate on encrypted integers:

  • MPCCore.add(a, b), MPCCore.addWithOverflowBit(a, b), addition
  • MPCCore.sub(a, b), MPCCore.subWithOverflowBit(a, b), subtraction
  • MPCCore.mul(a, b), MPCCore.mulWithOverflowBit(a, b), multiplication
  • MPCCore.div(a, b), division
  • MPCCore.rem(a, b), remainder

Comparison

Return encrypted booleans (gtBool):

  • MPCCore.eq(a, b), MPCCore.ne(a, b)
  • MPCCore.gt(a, b), MPCCore.ge(a, b)
  • MPCCore.lt(a, b), MPCCore.le(a, b)

Bitwise & Logical

For encrypted integers and booleans:

  • MPCCore.and(a, b)
  • MPCCore.or(a, b)
  • MPCCore.xor(a, b)
  • MPCCore.not(a)- supported only for booleans

Conditional

Select between encrypted values without revealing conditions:

  • MPCCore.mux(condition, a, b)

Random

  • MPCCore.rand(), MPCCore.rand8(), MPCCore.rand16(), MPCCore.rand32(), MPCCore.rand64(), MPCCore.rand128(), MPCCore.rand256() - random generation
  • MPCCore.randBoundedBits(numBits), MPCCore.randBoundedBits8(numBits), MPCCore.randBoundedBits16(numBits), MPCCore.randBoundedBits32(numBits), MPCCore.randBoundedBits64(numBits), MPCCore.randBoundedBits128(numBits), MPCCore.randBoundedBits256(numBits) - random generation up to the requested num bits

Utility

  • MPCCore.setPublic(value), MPCCore.setPublic8(value), MPCCore.setPublic16(value), MPCCore.setPublic32(value), MPCCore.setPublic128(value), MPCCore.setPublic256(value), encrypt plaintext
  • MPCCore.validateCiphertext(inputtexttm)- onboard new encrypted input into the system.
  • MPCCore.requestDecryption(cipher), request decryption
  • MPCCore.max(a, b) / MPCCore.min(a, b), secure comparisons

Transfer

Securely move encrypted balances or values between users or contracts without revealing amounts:

  • MPCCore.transfer(senderBalance, receiverBalance, amount)
  • MPCCore.transferWithAllowance(senderBalance, receiverBalance, amount)

Below are the function signatures for the available operations. Note that different type sizes can be mixed during calls.

The following list pertains to a single type size, as the function-signatures remain consistent across all type sizes and for mixed-size calls.

Secret Boolean type and various operations

function validateCiphertext(itBool memory input) internal returns (gtBool);
function setPublic(bool pt) internal returns (gtBool);
function rand() internal returns (gtBool);
function and(gtBool a, gtBool b) internal returns (gtBool);
function or(gtBool a, gtBool b) internal returns (gtBool);
function xor(gtBool a, gtBool b) internal returns (gtBool);
function eq(gtBool a, gtBool b) internal returns (gtBool);
function ne(gtBool a, gtBool b) internal returns (gtBool);
function mux(gtBool bit, gtBool a, gtBool b) internal returns (gtBool);
function not(gtBool a) internal returns (gtBool);

Secret type of 64 bits and various operations

function validateCiphertext(itUint64 memory input) internal returns (gtUint64);
function setPublic64(uint64 pt) internal returns (gtUint64);
function rand64() internal returns (gtUint64);
function randBoundedBits64(uint8 numBits) internal returns (gtUint64);
function add(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function checkedAddWithOverflowBit(gtUint64 a, gtUint64 b) internal returns (gtBool, gtUint64);
function sub(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function checkedSubWithOverflowBit(gtUint64 a, gtUint64 b) internal returns (gtBool, gtUint64);
function mul(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function checkedMulWithOverflowBit(gtUint64 a, gtUint64 b) internal returns (gtBool, gtUint64);
function div(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function rem(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function and(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function or(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function xor(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function eq(gtUint64 a, gtUint64 b) internal returns (gtBool);
function ne(gtUint64 a, gtUint64 b) internal returns (gtBool);
function ge(gtUint64 a, gtUint64 b) internal returns (gtBool);
function gt(gtUint64 a, gtUint64 b) internal returns (gtBool);
function le(gtUint64 a, gtUint64 b) internal returns (gtBool);
function lt(gtUint64 a, gtUint64 b) internal returns (gtBool);
function min(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function max(gtUint64 a, gtUint64 b) internal returns (gtUint64);
function mux(gtBool bit, gtUint64 a, gtUint64 b) internal returns (gtUint64);
function transfer(gtUint64 a, gtUint64 b, gtUint64 amount) internal returns (gtUint64, gtUint64, gtBool);
function transferWithAllowance(gtUint64 a, gtUint64 b, gtUint64 amount, gtUint64 allowance) internal returns (gtUint64, gtUint64, gtBool, gtUint64);

// 64 bit add example with scalars
function add(gtUint64 a, uint64 b) internal returns (gtUint64);
function add(uint64 a, gtUint64 b) internal returns (gtUint64);

// 64 bit add example with different sized types
function add(gtUint8 a, gtUint64 b) internal returns (gtUint64);
function add(gtUint64 a, gtUint8 b) internal returns (gtUint64);
function add(gtUint16 a, gtUint64 b) internal returns (gtUint64);
function add(gtUint64 a, gtUint16 b) internal returns (gtUint64);
function add(gtUint32 a, gtUint64 b) internal returns (gtUint64);
function add(gtUint64 a, gtUint32 b) internal returns (gtUint64);
function add(gtUint64 a, gtUint128 b) internal returns (gtUint128);
function add(gtUint128 a, gtUint64 b) internal returns (gtUint128);
function add(gtUint64 a, gtUint256 b) internal returns (gtUint256);
function add(gtUint256 a, gtUint64 b) internal returns (gtUint256);

The library file MPCCore.sol can be found here.

On this page