// AUTOGENERATED FILE. DO NOT EDIT. // This file was generated by the IncoLightning library generator. // The original template is located at Lib.template.sol /// SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8; import { IncoLightning } from "./IncoLightning.sol"; import { ebool, euint256, eaddress, ETypes, elist, IndexOutOfRange, InvalidRange, SliceOutOfRange, UnsupportedListType, HandleMismatch, InvalidTEEAttestation, UnexpectedDecryptedValue } from "./Types.sol"; import { asBool } from "./shared/TypeUtils.sol"; import { DecryptionAttestation, ElementAttestationWithProof } from "./lightning-parts/DecryptionAttester.types.sol"; IncoLightning constant inco = IncoLightning(payable(0xe9CB49A5b16C6D4a093E5900AA8b450FD40541B6)); address constant deployedBy = 0x9eb3483532302Ab76290BACf9D6DC4efD3fa0d1B; /// @notice Returns the ETypes enum value encoded in a handle /// @param handle The handle to decode /// @return The ETypes value function typeOf(bytes32 handle) pure returns (ETypes) { return ETypes(uint8(uint256(handle) >> 8)); } library e { error CallFailedAfterFeeRefresh(); /// @dev slot to store the fee for inco operations bytes32 private constant FEE_SLOT = keccak256("inco.fee"); /// @notice Returns a sanitized euint256, replacing zero with asEuint256(0) /// @param a The euint256 to sanitize /// @return The sanitized euint256 function sanitize(euint256 a) internal returns (euint256) { if (euint256.unwrap(a) == bytes32(0)) { return asEuint256(0); } return a; } /// @notice Returns a sanitized ebool, replacing zero with asEbool(false) /// @param a The ebool to sanitize /// @return The sanitized ebool function sanitize(ebool a) internal returns (ebool) { if (ebool.unwrap(a) == bytes32(0)) { return asEbool(false); } return a; } /// @notice Returns a sanitized eaddress, replacing zero with asEaddress(address(0)) /// @param a The eaddress to sanitize /// @return The sanitized eaddress function sanitize(eaddress a) internal returns (eaddress) { if (eaddress.unwrap(a) == bytes32(0)) { return asEaddress(address(0)); } return a; } /// @notice Alias for sanitize(euint256) /// @param a The euint256 to sanitize /// @return The sanitized euint256 function s(euint256 a) internal returns (euint256) { return sanitize(a); } /// @notice Alias for sanitize(ebool) /// @param a The ebool to sanitize /// @return The sanitized ebool function s(ebool a) internal returns (ebool) { return sanitize(a); } /// @notice Alias for sanitize(eaddress) /// @param a The eaddress to sanitize /// @return The sanitized eaddress function s(eaddress a) internal returns (eaddress) { return sanitize(a); } /// @notice Adds two encrypted uint256 values /// @param a First operand /// @param b Second operand /// @return The encrypted sum function add(euint256 a, euint256 b) internal returns (euint256) { return inco.eAdd(s(a), s(b)); } /// @notice Adds an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted sum function add(euint256 a, uint256 b) internal returns (euint256) { return inco.eAdd(s(a), asEuint256(b)); } /// @notice Adds a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted sum function add(uint256 a, euint256 b) internal returns (euint256) { return inco.eAdd(asEuint256(a), s(b)); } /// @notice Subtracts two encrypted uint256 values /// @param a Encrypted minuend /// @param b Encrypted subtrahend /// @return The encrypted difference function sub(euint256 a, euint256 b) internal returns (euint256) { return inco.eSub(s(a), s(b)); } /// @notice Subtracts a plaintext uint256 from an encrypted uint256 /// @param a Encrypted minuend /// @param b Plaintext subtrahend /// @return The encrypted difference function sub(euint256 a, uint256 b) internal returns (euint256) { return inco.eSub(s(a), asEuint256(b)); } /// @notice Subtracts an encrypted uint256 from a plaintext uint256 /// @param a Plaintext minuend /// @param b Encrypted subtrahend /// @return The encrypted difference function sub(uint256 a, euint256 b) internal returns (euint256) { return inco.eSub(asEuint256(a), s(b)); } /// @notice Multiplies two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted product function mul(euint256 a, euint256 b) internal returns (euint256) { return inco.eMul(s(a), s(b)); } /// @notice Multiplies an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted product function mul(euint256 a, uint256 b) internal returns (euint256) { return inco.eMul(s(a), asEuint256(b)); } /// @notice Multiplies a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted product function mul(uint256 a, euint256 b) internal returns (euint256) { return inco.eMul(asEuint256(a), s(b)); } /// @notice Divides two encrypted uint256 values /// @param a Encrypted Dividend /// @param b Encrypted Divisor /// @return The encrypted quotient function div(euint256 a, euint256 b) internal returns (euint256) { return inco.eDiv(s(a), s(b)); } /// @notice Divides an encrypted uint256 by a plaintext uint256 /// @param a Encrypted dividend /// @param b Plaintext divisor /// @return The encrypted quotient function div(euint256 a, uint256 b) internal returns (euint256) { return inco.eDiv(s(a), asEuint256(b)); } /// @notice Divides a plaintext uint256 by an encrypted uint256 /// @param a Plaintext dividend /// @param b Encrypted divisor /// @return The encrypted quotient function div(uint256 a, euint256 b) internal returns (euint256) { return inco.eDiv(asEuint256(a), s(b)); } /// @notice Remainder of two encrypted uint256 values /// @param a Encrypted dividend /// @param b Encrypted divisor /// @return The encrypted remainder function rem(euint256 a, euint256 b) internal returns (euint256) { return inco.eRem(s(a), s(b)); } /// @notice Remainder of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted dividend /// @param b Plaintext divisor /// @return The encrypted remainder function rem(euint256 a, uint256 b) internal returns (euint256) { return inco.eRem(s(a), asEuint256(b)); } /// @notice Remainder of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext dividend /// @param b Encrypted divisor /// @return The encrypted remainder function rem(uint256 a, euint256 b) internal returns (euint256) { return inco.eRem(asEuint256(a), s(b)); } /// @notice Bitwise AND of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted result function and(euint256 a, euint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(s(b)))); } /// @notice Bitwise AND of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted result function and(euint256 a, uint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)))); } /// @notice Bitwise AND of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted result function and(uint256 a, euint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitAnd(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)))); } /// @notice Bitwise AND of two encrypted bool values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted result function and(ebool a, ebool b) internal returns (ebool) { return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(s(b)))); } /// @notice Bitwise AND of an encrypted bool and a plaintext bool /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted result function and(ebool a, bool b) internal returns (ebool) { return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b)))); } /// @notice Bitwise AND of a plaintext bool and an encrypted bool /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted result function and(bool a, ebool b) internal returns (ebool) { return ebool.wrap(inco.eBitAnd(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b)))); } /// @notice Bitwise OR of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted result function or(euint256 a, euint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(s(b)))); } /// @notice Bitwise OR of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted result function or(euint256 a, uint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)))); } /// @notice Bitwise OR of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted result function or(uint256 a, euint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitOr(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)))); } /// @notice Bitwise OR of two encrypted bool values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted result function or(ebool a, ebool b) internal returns (ebool) { return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(s(b)))); } /// @notice Bitwise OR of an encrypted bool and a plaintext bool /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted result function or(ebool a, bool b) internal returns (ebool) { return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b)))); } /// @notice Bitwise OR of a plaintext bool and an encrypted bool /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted result function or(bool a, ebool b) internal returns (ebool) { return ebool.wrap(inco.eBitOr(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b)))); } /// @notice Bitwise XOR of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted result function xor(euint256 a, euint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(s(b)))); } /// @notice Bitwise XOR of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted result function xor(euint256 a, uint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)))); } /// @notice Bitwise XOR of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted result function xor(uint256 a, euint256 b) internal returns (euint256) { return euint256.wrap(inco.eBitXor(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)))); } /// @notice Bitwise XOR of two encrypted bool values /// @param a First operand /// @param b Second operand /// @return The encrypted result function xor(ebool a, ebool b) internal returns (ebool) { return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(s(b)))); } /// @notice Bitwise XOR of an encrypted bool and a plaintext bool /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted result function xor(ebool a, bool b) internal returns (ebool) { return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b)))); } /// @notice Bitwise XOR of a plaintext bool and an encrypted bool /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted result function xor(bool a, ebool b) internal returns (ebool) { return ebool.wrap(inco.eBitXor(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b)))); } /// @notice Left shift of two encrypted uint256 values /// @param a Encrypted value to shift /// @param b Encrypted shift amount /// @return The encrypted result function shl(euint256 a, euint256 b) internal returns (euint256) { return inco.eShl(s(a), s(b)); } /// @notice Left shift of an encrypted uint256 by a plaintext uint256 /// @param a Encrypted value to shift /// @param b Plaintext shift amount /// @return The encrypted result function shl(euint256 a, uint256 b) internal returns (euint256) { return inco.eShl(s(a), asEuint256(b)); } /// @notice Left shift of a plaintext uint256 by an encrypted uint256 /// @param a Plaintext value to shift /// @param b Encrypted shift amount /// @return The encrypted result function shl(uint256 a, euint256 b) internal returns (euint256) { return inco.eShl(asEuint256(a), s(b)); } /// @notice Right shift of two encrypted uint256 values /// @param a Encrypted value to shift /// @param b Encrypted shift amount /// @return The encrypted result function shr(euint256 a, euint256 b) internal returns (euint256) { return inco.eShr(s(a), s(b)); } /// @notice Right shift of an encrypted uint256 by a plaintext uint256 /// @param a Encrypted value to shift /// @param b Plaintext shift amount /// @return The encrypted result function shr(euint256 a, uint256 b) internal returns (euint256) { return inco.eShr(s(a), asEuint256(b)); } /// @notice Right shift of a plaintext uint256 by an encrypted uint256 /// @param a Plaintext value to shift /// @param b Encrypted shift amount /// @return The encrypted result function shr(uint256 a, euint256 b) internal returns (euint256) { return inco.eShr(asEuint256(a), s(b)); } /// @notice Left rotate of two encrypted uint256 values /// @param a Encrypted value to rotate /// @param b Encrypted rotate amount /// @return The encrypted result function rotl(euint256 a, euint256 b) internal returns (euint256) { return inco.eRotl(s(a), s(b)); } /// @notice Left rotate of an encrypted uint256 by a plaintext uint256 /// @param a Encrypted value to rotate /// @param b Plaintext rotate amount /// @return The encrypted result function rotl(euint256 a, uint256 b) internal returns (euint256) { return inco.eRotl(s(a), asEuint256(b)); } /// @notice Left rotate of a plaintext uint256 by an encrypted uint256 /// @param a Plaintext value to rotate /// @param b Encrypted rotate amount /// @return The encrypted result function rotl(uint256 a, euint256 b) internal returns (euint256) { return inco.eRotl(asEuint256(a), s(b)); } /// @notice Right rotate of two encrypted uint256 values /// @param a Encrypted value to rotate /// @param b Encrypted rotate amount /// @return The encrypted result function rotr(euint256 a, euint256 b) internal returns (euint256) { return inco.eRotr(s(a), s(b)); } /// @notice Right rotate of an encrypted uint256 by a plaintext uint256 /// @param a Encrypted value to rotate /// @param b Plaintext rotate amount /// @return The encrypted result function rotr(euint256 a, uint256 b) internal returns (euint256) { return inco.eRotr(s(a), asEuint256(b)); } /// @notice Right rotate of a plaintext uint256 by an encrypted uint256 /// @param a Plaintext value to rotate /// @param b Encrypted rotate amount /// @return The encrypted result function rotr(uint256 a, euint256 b) internal returns (euint256) { return inco.eRotr(asEuint256(a), s(b)); } /// @notice Checks equality of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function eq(euint256 a, euint256 b) internal returns (ebool) { return inco.eEq(euint256.unwrap(s(a)), euint256.unwrap(s(b))); } /// @notice Checks equality of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function eq(euint256 a, uint256 b) internal returns (ebool) { return inco.eEq(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))); } /// @notice Checks equality of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function eq(uint256 a, euint256 b) internal returns (ebool) { return inco.eEq(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))); } /// @notice Checks equality of an encrypted address and a plaintext address /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function eq(eaddress a, address b) internal returns (ebool) { return inco.eEq(eaddress.unwrap(s(a)), eaddress.unwrap(asEaddress(b))); } /// @notice Checks equality of two encrypted addresses /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function eq(eaddress a, eaddress b) internal returns (ebool) { return inco.eEq(eaddress.unwrap(s(a)), eaddress.unwrap(s(b))); } /// @notice Checks equality of a plaintext address and an encrypted address /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function eq(address a, eaddress b) internal returns (ebool) { return inco.eEq(eaddress.unwrap(asEaddress(a)), eaddress.unwrap(s(b))); } /// @notice Checks inequality of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function ne(euint256 a, euint256 b) internal returns (ebool) { return inco.eNe(euint256.unwrap(s(a)), euint256.unwrap(s(b))); } /// @notice Checks inequality of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function ne(euint256 a, uint256 b) internal returns (ebool) { return inco.eNe(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))); } /// @notice Checks inequality of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function ne(uint256 a, euint256 b) internal returns (ebool) { return inco.eNe(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))); } /// @notice Checks inequality of two encrypted addresses /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function ne(eaddress a, eaddress b) internal returns (ebool) { return inco.eNe(eaddress.unwrap(s(a)), eaddress.unwrap(s(b))); } /// @notice Checks inequality of an encrypted address and a plaintext address /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function ne(eaddress a, address b) internal returns (ebool) { return inco.eNe(eaddress.unwrap(s(a)), eaddress.unwrap(asEaddress(b))); } /// @notice Checks inequality of a plaintext address and an encrypted address /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function ne(address a, eaddress b) internal returns (ebool) { return inco.eNe(eaddress.unwrap(asEaddress(a)), eaddress.unwrap(s(b))); } /// @notice Checks if one encrypted uint256 is greater than or equal to another /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function ge(euint256 a, euint256 b) internal returns (ebool) { return inco.eGe(s(a), s(b)); } /// @notice Checks if an encrypted uint256 is greater than or equal to a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function ge(euint256 a, uint256 b) internal returns (ebool) { return inco.eGe(s(a), asEuint256(b)); } /// @notice Checks if a plaintext uint256 is greater than or equal to an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function ge(uint256 a, euint256 b) internal returns (ebool) { return inco.eGe(asEuint256(a), s(b)); } /// @notice Checks if one encrypted uint256 is greater than another /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function gt(euint256 a, euint256 b) internal returns (ebool) { return inco.eGt(s(a), s(b)); } /// @notice Checks if an encrypted uint256 is greater than a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function gt(euint256 a, uint256 b) internal returns (ebool) { return inco.eGt(s(a), asEuint256(b)); } /// @notice Checks if a plaintext uint256 is greater than an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function gt(uint256 a, euint256 b) internal returns (ebool) { return inco.eGt(asEuint256(a), s(b)); } /// @notice Checks if one encrypted uint256 is less than or equal to another /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function le(euint256 a, euint256 b) internal returns (ebool) { return inco.eLe(s(a), s(b)); } /// @notice Checks if an encrypted uint256 is less than or equal to a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function le(euint256 a, uint256 b) internal returns (ebool) { return inco.eLe(s(a), asEuint256(b)); } /// @notice Checks if a plaintext uint256 is less than or equal to an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function le(uint256 a, euint256 b) internal returns (ebool) { return inco.eLe(asEuint256(a), s(b)); } /// @notice Checks if one encrypted uint256 is less than another /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted bool result function lt(euint256 a, euint256 b) internal returns (ebool) { return inco.eLt(s(a), s(b)); } /// @notice Checks if an encrypted uint256 is less than a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted bool result function lt(euint256 a, uint256 b) internal returns (ebool) { return inco.eLt(s(a), asEuint256(b)); } /// @notice Checks if a plaintext uint256 is less than an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted bool result function lt(uint256 a, euint256 b) internal returns (ebool) { return inco.eLt(asEuint256(a), s(b)); } /// @notice Returns the minimum of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted minimum function min(euint256 a, euint256 b) internal returns (euint256) { return inco.eMin(s(a), s(b)); } /// @notice Returns the minimum of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted minimum function min(euint256 a, uint256 b) internal returns (euint256) { return inco.eMin(s(a), asEuint256(b)); } /// @notice Returns the minimum of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted minimum function min(uint256 a, euint256 b) internal returns (euint256) { return inco.eMin(asEuint256(a), s(b)); } /// @notice Returns the maximum of two encrypted uint256 values /// @param a Encrypted operand /// @param b Encrypted operand /// @return The encrypted maximum function max(euint256 a, euint256 b) internal returns (euint256) { return inco.eMax(s(a), s(b)); } /// @notice Returns the maximum of an encrypted uint256 and a plaintext uint256 /// @param a Encrypted operand /// @param b Plaintext operand /// @return The encrypted maximum function max(euint256 a, uint256 b) internal returns (euint256) { return inco.eMax(s(a), asEuint256(b)); } /// @notice Returns the maximum of a plaintext uint256 and an encrypted uint256 /// @param a Plaintext operand /// @param b Encrypted operand /// @return The encrypted maximum function max(uint256 a, euint256 b) internal returns (euint256) { return inco.eMax(asEuint256(a), s(b)); } /// @notice Bitwise NOT of an encrypted bool /// @param a Encrypted operand /// @return The encrypted result function not(ebool a) internal returns (ebool) { return inco.eNot(s(a)); } /// @notice Generates a random encrypted uint256 (costs the inco fee) /// @dev costs the inco fee /// @return The encrypted random value function rand() internal returns (euint256) { bytes32 boundHandle = euint256.unwrap(asEuint256(0)); bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256)); return euint256.wrap(result); } /// @notice Generates a random encrypted uint256 less than a plaintext upper bound (costs the inco fee) /// @dev costs the inco fee /// @param upperBound The plaintext upper bound (exclusive). If the upper bound is 0, the whole 256bit range is sampled, equivalent to providing type(uint256).max as upper bound. /// @return The encrypted random value function randBounded(uint256 upperBound) internal returns (euint256) { bytes32 boundHandle = euint256.unwrap(asEuint256(upperBound)); bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256)); return euint256.wrap(result); } /// @notice Generates a random encrypted uint256 less than an encrypted upper bound (costs the inco fee) /// @dev costs the inco fee /// @param upperBound The encrypted upper bound (exclusive). If the upper bound is e(0), the whole 256bit range is sampled, equivalent to providing e(type(uint256).max) as upper bound. /// @return The encrypted random value function randBounded(euint256 upperBound) internal returns (euint256) { bytes32 boundHandle = euint256.unwrap(s(upperBound)); bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256)); return euint256.wrap(result); } /// @notice Casts a plaintext uint256 to an encrypted uint256 /// @param a The plaintext value /// @return The encrypted value function asEuint256(uint256 a) internal returns (euint256) { return inco.asEuint256(a); } /// @notice Casts a plaintext bool to an encrypted bool /// @param a The plaintext value /// @return The encrypted value function asEbool(bool a) internal returns (ebool) { return inco.asEbool(a); } /// @notice Casts a plaintext address to an encrypted address /// @param a The plaintext value /// @return The encrypted value function asEaddress(address a) internal returns (eaddress) { return inco.asEaddress(a); } /// @notice Casts an encrypted bool to an encrypted uint256 /// @param a The encrypted bool value /// @return The encrypted uint256 value function asEuint256(ebool a) internal returns (euint256) { return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256)); } /// @notice Creates a new encrypted uint256 assuming msg.sender is the user /// @dev costs the inco fee function newEuint256(bytes memory ciphertext) internal returns (euint256) { return newEuint256(ciphertext, msg.sender); } /// @notice Creates a new encrypted uint256 for the given user. /// @dev costs the inco fee function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) { bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEuint256.selector, ciphertext, user)); return euint256.wrap(result); } /// @notice Creates a new encrypted bool assuming msg.sender is the user /// @dev costs the inco fee function newEbool(bytes memory ciphertext) internal returns (ebool) { return newEbool(ciphertext, msg.sender); } /// @notice Creates a new encrypted bool for the given user. /// @dev costs the inco fee function newEbool(bytes memory ciphertext, address user) internal returns (ebool) { bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEbool.selector, ciphertext, user)); return ebool.wrap(result); } /// @notice Creates a new encrypted address assuming msg.sender is the user /// @dev costs the inco fee function newEaddress(bytes memory ciphertext) internal returns (eaddress) { return newEaddress(ciphertext, msg.sender); } /// @notice Creates a new encrypted address for the given user. /// @dev costs the inco fee function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) { bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEaddress.selector, ciphertext, user)); return eaddress.wrap(result); } /// @notice Allows an address to access an encrypted uint256 /// @param a The encrypted uint256 /// @param to The address to allow function allow(euint256 a, address to) internal { inco.allow(euint256.unwrap(a), to); } /// @notice Allows an address to access an encrypted bool /// @param a The encrypted bool /// @param to The address to allow function allow(ebool a, address to) internal { inco.allow(ebool.unwrap(a), to); } /// @notice Allows an address to access an encrypted address /// @param a The encrypted address /// @param to The address to allow function allow(eaddress a, address to) internal { inco.allow(eaddress.unwrap(a), to); } /// @notice Allows an address to access an elist /// @param a The elist /// @param to The address to allow function allow(elist a, address to) internal { inco.allow(elist.unwrap(a), to); } /// @notice Reveals an encrypted uint256 /// @param a The encrypted uint256 function reveal(euint256 a) internal { inco.reveal(euint256.unwrap(a)); } /// @notice Reveals an encrypted bool /// @param a The encrypted bool function reveal(ebool a) internal { inco.reveal(ebool.unwrap(a)); } /// @notice Reveals an encrypted address /// @param a The encrypted address function reveal(eaddress a) internal { inco.reveal(eaddress.unwrap(a)); } /// @notice Reveals an elist (makes all elements publicly readable without wallet authentication) /// @param a The elist function reveal(elist a) internal { inco.reveal(elist.unwrap(a)); } /// @notice Verifies a decryption attestation for a euint256 /// @param handle The encrypted handle /// @param value The claimed decrypted value /// @param signatures The covalidator signatures /// @return True if the attestation is valid function verifyDecryption(euint256 handle, uint256 value, bytes[] memory signatures) internal view returns (bool) { DecryptionAttestation memory attestation = DecryptionAttestation({handle: euint256.unwrap(handle), value: bytes32(value)}); return inco.incoVerifier().isValidDecryptionAttestation(attestation, signatures); } /// @notice Verifies a decryption attestation for an ebool /// @param handle The encrypted handle /// @param value The claimed decrypted value /// @param signatures The covalidator signatures /// @return True if the attestation is valid function verifyDecryption(ebool handle, bool value, bytes[] memory signatures) internal view returns (bool) { DecryptionAttestation memory attestation = DecryptionAttestation({handle: ebool.unwrap(handle), value: value ? bytes32(uint256(1)) : bytes32(0)}); return inco.incoVerifier().isValidDecryptionAttestation(attestation, signatures); } /// @notice Allows this contract to access an encrypted uint256 /// @param a The encrypted uint256 function allowThis(euint256 a) internal { allow(a, address(this)); } /// @notice Allows this contract to access an encrypted bool /// @param a The encrypted bool function allowThis(ebool a) internal { allow(a, address(this)); } /// @notice Allows this contract to access an encrypted address /// @param a The encrypted address function allowThis(eaddress a) internal { allow(a, address(this)); } /// @notice Allows this contract to access an elist /// @param a The elist function allowThis(elist a) internal { allow(a, address(this)); } /// @notice Checks if a user is allowed to access an encrypted uint256 /// @param user The address to check /// @param a The encrypted uint256 /// @return True if allowed, false otherwise function isAllowed(address user, euint256 a) internal view returns (bool) { return inco.isAllowed(euint256.unwrap(a), user); } /// @notice Selects between two encrypted uint256 values based on an encrypted bool /// @param control The encrypted bool condition /// @param ifTrue Value if control is true /// @param ifFalse Value if control is false /// @return The selected encrypted uint256 function select(ebool control, euint256 ifTrue, euint256 ifFalse) internal returns (euint256) { return euint256.wrap(inco.eIfThenElse(s(control), euint256.unwrap(s(ifTrue)), euint256.unwrap(s(ifFalse)))); } /// @notice Selects between two encrypted bool values based on an encrypted bool /// @param control The encrypted bool condition /// @param ifTrue Value if control is true /// @param ifFalse Value if control is false /// @return The selected encrypted bool function select(ebool control, ebool ifTrue, ebool ifFalse) internal returns (ebool) { return ebool.wrap(inco.eIfThenElse(s(control), ebool.unwrap(s(ifTrue)), ebool.unwrap(s(ifFalse)))); } /// @notice Selects between two encrypted addresses based on an encrypted bool /// @param control The encrypted bool condition /// @param ifTrue Value if control is true /// @param ifFalse Value if control is false /// @return The selected encrypted address function select(ebool control, eaddress ifTrue, eaddress ifFalse) internal returns (eaddress) { return eaddress.wrap(inco.eIfThenElse(s(control), eaddress.unwrap(s(ifTrue)), eaddress.unwrap(s(ifFalse)))); } /// @dev Store fee in the custom slot /// @param _fee The fee to store function _setFee(uint256 _fee) private { bytes32 slot = FEE_SLOT; assembly { sstore(slot, _fee) } } /// @dev Retrieve fee from the custom slot /// @return fee The stored fee function _getFee() private view returns (uint256 fee) { bytes32 slot = FEE_SLOT; assembly { fee := sload(slot) } } /// @dev Get current fee with fallback to inco.getFee() if not cached function getCurrentFee() private returns (uint256) { uint256 cachedFee = _getFee(); if (cachedFee == 0) { cachedFee = inco.getFee(); _setFee(cachedFee); } return cachedFee; } /// @dev Execute a call to inco with fee, retrying with fresh fee if it fails /// @param callData The encoded function call (use abi.encodeWithSelector) /// @return result The bytes32 result from the call function _callWithFeeRetry(bytes memory callData) private returns (bytes32) { uint256 fee = getCurrentFee(); (bool success, bytes memory result) = address(inco).call{value: fee}(callData); if (!success) { fee = inco.getFee(); _setFee(fee); (success, result) = address(inco).call{value: fee}(callData); require(success, CallFailedAfterFeeRefresh()); } return abi.decode(result, (bytes32)); } /// @notice Returns the type of the elements contained within the list. /// @param handle EList handle to read container type from. /// @return ret Type of each element in the list. function listTypeOf(elist handle) internal pure returns (ETypes ret) { return ETypes(uint8(uint256(elist.unwrap(handle)) >> 16)); } /// @notice Creates a new empty list. Type must be specified ahead of time and can not be changed. /// @param listType Type of each element in the list. /// @return ret A new elist handle function newEList(ETypes listType) internal returns (elist ret) { return inco.newEList(new bytes32[](0), listType); } /// @notice Creates a new list from existing array of handles. /// @param handles An array of handles to create a new list from /// @param listType Type of each element in the list. /// @return ret A new elist handle function newEList(bytes32[] memory handles, ETypes listType) internal returns (elist ret) { return inco.newEList{value: inco.getEListFee(uint16(handles.length), listType)}(handles, listType); } /// @notice Takes an array of user encrypted ciphertexts and returns a new elist handle. /// @param ciphertexts An array of encrypted user inputs /// @param listType Expected type of each element in the list. /// @param user Address of the user encrypting the ciphertexts. /// @return ret A new elist handle /// @dev each ciphertext costs the inco fee function newEList(bytes[] memory ciphertexts, ETypes listType, address user) internal returns (elist ret) { return inco.newEList{value: inco.getEListFee(uint16(ciphertexts.length), listType)}(ciphertexts, listType, user); } /// @notice Appends an ebool element at the end of a list. /// @param a An elist handle to append to. /// @param b Element value to be appended. /// @return ret A new elist handle with length len(a)+1. function append(elist a, ebool b) internal returns (elist ret) { return inco.listAppend{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, ebool.unwrap(s(b))); } /// @notice Appends an euint256 element at the end of a list. /// @param a An elist handle to append to. /// @param b Element value to be appended. /// @return ret A new elist handle with length len(a)+1. function append(elist a, euint256 b) internal returns (elist ret) { return inco.listAppend{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(s(b))); } /// @notice Replaces an element at some hidden index "i" (ebool variant). /// @dev If the encrypted index is out of bounds, the operation is silently ignored /// and the returned list is identical to the input. This avoids leaking index validity. /// @param a EList handle to modify element in. /// @param i Hidden index position of an element to modify. /// @param b Element value to be changed. /// @return ret A new elist handle function set(elist a, euint256 i, ebool b) internal returns (elist ret) { return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(s(i)), ebool.unwrap(s(b))); } /// @notice Replaces an element at some hidden index "i" (euint256 variant). /// @dev If the encrypted index is out of bounds, the operation is silently ignored /// and the returned list is identical to the input. This avoids leaking index validity. /// @param a EList handle to modify element in. /// @param i Hidden index position of an element to modify. /// @param b Element value to be changed. /// @return ret A new elist handle function set(elist a, euint256 i, euint256 b) internal returns (elist ret) { return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(s(i)), euint256.unwrap(s(b))); } /// @notice Replaces an element at plaintext index "i" (ebool variant). Reverts if out of range. /// @param a EList handle to modify element in. /// @param i Index position of element to modify. /// @param b Element value to be changed. /// @return ret A new elist handle function set(elist a, uint16 i, ebool b) internal returns (elist ret) { require(i < length(a), IndexOutOfRange(i, length(a))); return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b))); } /// @notice Replaces an element at plaintext index "i" (euint256 variant). Reverts if out of range. /// @param a EList handle to modify element in. /// @param i Index position of element to modify. /// @param b Element value to be changed. /// @return ret A new elist handle function set(elist a, uint16 i, euint256 b) internal returns (elist ret) { require(i < length(a), IndexOutOfRange(i, length(a))); return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b))); } /// @notice Return hidden element at some hidden position (ebool variant). /// @param a EList handle to get element from. /// @param i Hidden index position to get element at. /// @param defaultValue A default element value returned if index is out of range. /// @return ret A new handle for the element or the defaultValue function getOr(elist a, euint256 i, ebool defaultValue) internal returns (ebool ret) { return ebool.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), ebool.unwrap(s(defaultValue)))); } /// @notice Return hidden element at some hidden position (euint256 variant). /// @param a EList handle to get element from. /// @param i Hidden index position to get element at. /// @param defaultValue A default element value returned if index is out of range. /// @return ret A new handle for the element or the defaultValue function getOr(elist a, euint256 i, euint256 defaultValue) internal returns (euint256 ret) { return euint256.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), euint256.unwrap(s(defaultValue)))); } /// @notice Return hidden element at a known position as ebool. /// @param a EList handle to get element from. /// @param i Index position to get element at. /// @return ret Returns a handle for the element. function getEbool(elist a, uint16 i) internal returns (ebool ret) { return ebool.wrap(inco.listGet(a, i)); } /// @notice Return hidden element at a known position as euint256. /// @param a EList handle to get element from. /// @param i Index position to get element at. /// @return ret Returns a handle for the element. function getEuint256(elist a, uint16 i) internal returns (euint256 ret) { return euint256.wrap(inco.listGet(a, i)); } /// @notice Inserts a hidden element at a desired hidden position (ebool, hidden index). /// @param a An elist handle to insert into. /// @param i Hidden index position to insert at. /// @param b Element value to be inserted. /// @return ret A new elist handle with length len(a)+1. function insert(elist a, euint256 i, ebool b) internal returns (elist ret) { return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(s(i)), ebool.unwrap(s(b))); } /// @notice Inserts an ebool element at a known position. Reverts if out of range. /// @param a An elist handle to insert into. /// @param i Index position to insert at. /// @param b Element value to be inserted. /// @return ret A new elist handle with length len(a)+1. function insert(elist a, uint16 i, ebool b) internal returns (elist ret) { require(i < length(a), IndexOutOfRange(i, length(a))); return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b))); } /// @notice Inserts a hidden element at a desired hidden position (euint256, hidden index). /// @param a An elist handle to insert into. /// @param i Hidden index position to insert at. /// @param b Element value to be inserted. /// @return ret A new elist handle with length len(a)+1. function insert(elist a, euint256 i, euint256 b) internal returns (elist ret) { return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(s(i)), euint256.unwrap(s(b))); } /// @notice Inserts a euint256 element at a known position. Reverts if out of range. /// @param a An elist handle to insert into. /// @param i Index position to insert at. /// @param b Element value to be inserted. /// @return ret A new elist handle with length len(a)+1. function insert(elist a, uint16 i, euint256 b) internal returns (elist ret) { require(i < length(a), IndexOutOfRange(i, length(a))); return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b))); } /// @notice Concatenates two elists into one. /// @param a EList handle to be prepended /// @param b EList handle to be appended /// @return ret A new elist handle with length len(a)+len(b). function concat(elist a, elist b) internal returns (elist ret) { return inco.listConcat{value: inco.getEListFee(length(a) + length(b), listTypeOf(a))}(a, b); } /// @notice Slices a list from plaintext start to plaintext end. /// @param a EList handle to be sliced /// @param start Starting index (inclusive). /// @param end End index (exclusive). Must be >= start and within bounds. /// @return ret A new sliced list with length "end-start". function slice(elist a, uint16 start, uint16 end) internal returns (elist ret) { require(end >= start, InvalidRange(start, end)); require(end <= length(a), SliceOutOfRange(start, end, length(a))); require(start < length(a), SliceOutOfRange(start, end, length(a))); bytes32 defaultValue; if (ETypes.Uint256 == listTypeOf(a)) { defaultValue = euint256.unwrap(asEuint256(0)); } else if (ETypes.Bool == listTypeOf(a)) { defaultValue = ebool.unwrap(asEbool(false)); } else { revert UnsupportedListType(listTypeOf(a)); } return inco.listSlice{value: inco.getEListFee(end - start, listTypeOf(a))}(a, euint256.unwrap(asEuint256(start)), end - start, defaultValue); } /// @notice Slices at a hidden index with a specified length (euint256 default). /// @param a EList handle to be sliced. /// @param start Hidden starting index. /// @param len Length of the desired slice. /// @param defaultValue Default value if out of range. /// @return ret A new sliced list with length "len". function sliceLen(elist a, euint256 start, uint16 len, euint256 defaultValue) internal returns (elist ret) { return inco.listSlice{value: inco.getEListFee(len, listTypeOf(a))}(a, euint256.unwrap(s(start)), len, euint256.unwrap(s(defaultValue))); } /// @notice Slices at a hidden index with a specified length (ebool default). /// @param a EList handle to be sliced. /// @param start Hidden starting index. /// @param len Length of the desired slice. /// @param defaultValue Default value if out of range. /// @return ret A new sliced list with length "len". function sliceLen(elist a, euint256 start, uint16 len, ebool defaultValue) internal returns (elist ret) { return inco.listSlice{value: inco.getEListFee(len, listTypeOf(a))}(a, euint256.unwrap(s(start)), len, ebool.unwrap(s(defaultValue))); } /// @notice Creates a new list populated with ordered values from within range. /// @param start Start value of the range, inclusive. /// @param end End of the range, exclusive. Must be >= start. /// @return ret A new elist handle with length "end-start" function range(uint16 start, uint16 end, ETypes listType) internal returns (elist ret) { return inco.listRange{value: inco.getEListFee(end - start, listType)}(start, end, listType); } /// @notice Deterministically shuffles elements within a list. /// @dev costs the inco fee /// @param a elist handle to be shuffled /// @return ret A new elist handle with elements shuffled function shuffle(elist a) internal returns (elist ret) { return inco.listShuffle{value: inco.getEListFee(length(a), listTypeOf(a))}(a); } /// @notice A convenience function equivalent to range() followed by shuffle(). /// @dev costs the inco fee /// @param start Start value of the range, inclusive. /// @param end End of the range, exclusive. Must be >= start. /// @return ret A new elist handle in random order with length "end-start". function shuffledRange(uint16 start, uint16 end, ETypes listType) internal returns (elist ret) { uint256 fee = inco.getEListFee(end - start, listType); elist rangeList = inco.listRange{value: fee}(start, end, listType); return inco.listShuffle{value: fee}(rangeList); } /// @notice Reverses the order of elements in a list. /// @param a Elist handle to be reversed /// @return ret A new elist handle with elements in reverse order function reverse(elist a) internal returns (elist ret) { return inco.listReverse{value: inco.getEListFee(length(a), listTypeOf(a))}(a); } /// @notice Returns the length of the list in plaintext. Pure function, no gas cost. /// @param a EList handle to read the length from /// @return len The length of the list function length(elist a) internal pure returns (uint16 len) { return inco.lengthOf(elist.unwrap(a)); } /// @notice verifyEListDecryption verifies elist decryption attestation and commitment proof. Each element can be either a pre-computed hash or a commitment-value pair. /// @param elistHandle The elist handle that was decrypted /// @param proofElements Array of proof elements. Each can provide either pairHash directly, or commitment+value to compute the pairHash /// @param proof The expected proof that should match keccak256 of all concatenated pair hashes /// @param signatures Array of signatures from attesters /// @return bool True if verification succeeds /// @dev For privacy, publish only pairHash. For transparency, publish commitment+value. Can mix both in the same array. function verifyEListDecryption(elist elistHandle, ElementAttestationWithProof[] memory proofElements, bytes32 proof, bytes[] memory signatures) internal view returns (bool) { return inco.incoVerifier().isValidEListDecryptionAttestation(elist.unwrap(elistHandle), proofElements, proof, signatures); } /// @notice Checks a decryption attestation for an encrypted bool against an expected plaintext value /// @param handle The encrypted handle /// @param expected The expected plaintext value /// @param decryption The decryption attestation to verify /// @param signatures The covalidator signatures for the attestation /// @dev Reverts if the handle doesn't match, the decrypted value doesn't match the expected value, or the attestation is invalid function requireEqual(ebool handle, bool expected, DecryptionAttestation memory decryption, bytes[] memory signatures) internal view { require(ebool.unwrap(handle) == decryption.handle, HandleMismatch()); require(asBool(decryption.value) == expected, UnexpectedDecryptedValue()); require(inco.incoVerifier().isValidDecryptionAttestation(decryption, signatures), InvalidTEEAttestation()); } /// @notice Checks a decryption attestation for an encrypted uint256 against an expected plaintext value /// @param handle The encrypted handle /// @param expected The expected plaintext value /// @param decryption The decryption attestation to verify /// @param signatures The covalidator signatures for the attestation /// @dev Reverts if the handle doesn't match, the decrypted value doesn't match the expected value, or the attestation is invalid function requireEqual(euint256 handle, uint256 expected, DecryptionAttestation memory decryption, bytes[] memory signatures) internal view { require(euint256.unwrap(handle) == decryption.handle, HandleMismatch()); require(uint256(decryption.value) == expected, UnexpectedDecryptedValue()); require(inco.incoVerifier().isValidDecryptionAttestation(decryption, signatures), InvalidTEEAttestation()); } }