🔍Check Overflow
Extra caution should be exercised when performing addition, subtraction, or multiplication operations to ensure no overflow occurs. If there's potential for an overflow, use the operators that return a gtBool value indicating whether an overflow has taken place.
Avoid:
contract AvoidContract {
function addingGtWithoutChecking(gtUint16 lhs, gtUint16 rhs) public {
gtUint16 addResult = MpcCore.add(lhs, rhs);
return addResult;
}
}
Do : Check the overflow bit with mux, return zero if an on overflow accrued.
contract goodContract {
function addingGtReturnZeroOnOverFlow(gtUint16 lhs, gtUint16 rhs) public {
(gtBool isOverflowed, gtUint16 tempAddResult) = MpcCore.checkedAddWithOverflowBit(lhs, rhs);
gtUint16 gtZero = MpcCore.setPublic16(0)
addResult = MpcCore.mux(isOverflow, gtZero, tempAddResult);
return addResult;
}
}
Last updated