COTI V2 Documentation
  • Welcome
  • Networks
    • Faucet
    • Contracts Addresses
    • Adding the COTI Network to Metamask
  • How COTI Works
    • Introduction
      • EVM Introduction
      • Conceptual Overview
      • Use Cases and Applications
      • COTI Architecture
    • Advanced Topics
      • Garbled Circuits
      • AES Keys
      • Precompiles
      • Whitepaper
      • COTI vs others
  • Build on COTI
    • Core Concepts
      • Account Onboarding Procedure
      • Private Data Types
      • Supported Operations on Private Data Types
    • Quickstart
    • Guides
      • Basic Private Smart Contract
      • Account Onboard
      • Sending a Transaction with Encrypted Inputs
      • Resolving a Transaction's Encrypted Outputs
      • Writing a Private Smart Contract
      • Dos and Don'ts
        • Proper Use of Types
        • No Constant/Immutable Secret Types
        • No Public Contract Variables
      • Best Practices
        • Careful Onboarding
        • Careful Decrypting
        • Don't loop over an array without an index
        • Check Overflow
    • Tools
      • TypeScript SDK
      • Ethers.js
      • Python SDK
      • Web3.py
      • Contracts Library
        • MPC Core
        • Data Privacy Framework
        • Tokens
          • Private ERC20
          • Private ERC721
        • Onboard
      • Hardhat
      • Remix Plugin
      • COTI MetaMask Snap
      • Developer Sandbox
  • Running a COTI Node
    • COTI Node Ecosystem Litepaper
  • COTI Bridge
    • Swap COTI V1 Funds to COTI V2
  • Support and Community
    • Glossary
    • Telegram
    • Discord
    • GitHub
    • X
    • YouTube
  • COTI Builders Program
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Build on COTI
  2. Guides
  3. Best Practices

Check Overflow

Extra caution should be exercised when performing addition, subtraction, or division operations. For instance, it's important to verify that an overflow hasn't occurred in cases where it could potentially happen.

Avoid:

contract AvoidContract {
    function addinGtWithoutChecking(gtUint16 lhs, gtUint16 rhs) public {
        gtUint16 addResult = MpcCore.add(lhs, rhs);
        return addResult;
    }
}

Do : Check that the result is greater than one of the operands, return zero for example on overflow

contract AvoidContract {
    function addinGtReturnZeroOnOverFlow(gtUint16 lhs, gtUint16 rhs) public {
        gtUint16 tempAddResult = MpcCore.add(lhs, rhs);
        gtBool isOverflowed = MpcCore.lt(tempAddResult , lhs);
        gtUint16 gtZero = MpcCore.setPublic16(0)
        addResult = MpcCore.mux(isOverflow, gtZero, tempAddResult);
        return addResult;
    }
}

You may also use the checkedAdd, checkedSub and checkedMul operands, which cause the transaction to revert on overflow/underflow.

PreviousDon't loop over an array without an indexNextTools

Last updated 5 months ago

Was this helpful?