Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Big UInt Bitwise Or for modexp #135

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion precompiles/Modexp.yul
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ object "ModExp" {
}
}

/// @notice Performs the big unsigned integer bit or operation.
/// @dev The result is stored from `resPtr` to `resPtr + (LIMB_SIZE * nLimbs)`.
/// @param lhsPtr The pointer to the MSB of the left operand.
/// @param rhsPtr The pointer to the MSB of the right operand.
/// @param nLimbs The number of limbs needed to represent the operands.
/// @param resPtr The pointer to where you want the result to be stored
function bigUIntBitOr(lhsPtr, rhsPtr, nLimbs, resPtr) {
// +------------+-----------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+-----------------+--------------------------------------+
// | Iteration | offset_i | ptr_lhs_i | ptr_rhs_i | ptr_res_i | value_lhs_i | value_rhs_i | value_res_i |
// +------------+-----------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+-----------------+--------------------------------------+
// | 0 | +0x00 | lhsPtr + 0x00 | rhsPtr + 0x00 | resPtr + 0x00 | lhs[0] | rhs[0] | or(lhs[0], rhs[0]) |
// | 1 | +0x20 | lhsPtr + 0x20 | rhsPtr + 0x20 | resPtr + 0x20 | lhs[1] | rhs[1] | or(lhs[1], rhs[1]) |
// | 2 | +0x40 | lhsPtr + 0x40 | rhsPtr + 0x40 | resPtr + 0x40 | lhs[2] | rhs[2] | or(lhs[2], rhs[2]) |
// | | | | | | | | |
// | ... | ... | ... | ... | ... | ... | ... | ... |
// | | | | | | | | |
// | nLimbs - 1 | +(0x20 * (nLimbs - 1) | lhsPtr + (0x20 * (nLimbs - 1) | rhsPtr + (0x20 * (nLimbs - 1) | resPtr + (0x20 * (nLimbs - 1) | lhs[nLimbs - 1] | rhs[nLimbs - 1] | or(lhs[nLimbs - 1], rhs[nLimbs - 1]) |
// +------------+-----------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+-----------------+--------------------------------------+

let finalOffset := shl(5, nLimbs) // == ( LIMB_SIZE * nLimbs ) == (32 * nLimbs)
for { let offset_i := 0 } lt(offset_i, finalOffset) { offset_i := add(offset_i, 0x20) }
{
let ptr_lhs_i := add(lhsPtr, offset_i)
let ptr_lhs_i := add(rhsPtr, offset_i)
let ptr_res_i := add(resPtr, offset_i)
let value_lhs_i := mload(ptr_lhs_i)
let value_rhs_i := mload(ptr_lhs_i)
let value_res_i := or(value_lhs_i, value_rhs_i)
jpcenteno marked this conversation as resolved.
Show resolved Hide resolved
mstore(ptr_res_i, value_res_i)
}

////////////////////////////////////////////////////////////////
// FALLBACK
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -126,4 +157,4 @@ object "ModExp" {
// TODO: big arithmetics
}
}
}
}