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

Square implementation #138

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
104 changes: 103 additions & 1 deletion precompiles/Modexp.yul
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ object "ModExp" {

// HELPER FUNCTIONS

function overflowingAdd(augend, addend) -> sum, overflowed {
sum := add(augend, addend)
overflowed := lt(sum, augend)
}

/// @notice Retrieves the highest half of the multiplication result.
/// @param multiplicand The value to multiply.
/// @param multiplier The multiplier.
/// @return ret The highest half of the multiplication result.
function getHighestHalfOfMultiplication(multiplicand, multiplier) -> ret {
ret := verbatim_2i_1o("mul_high", multiplicand, multiplier)
}

/// @notice Checks whether a big number is zero.
/// @param start The pointer to the calldata where the big number starts.
/// @param len The number of bytes that the big number occupies.
Expand Down Expand Up @@ -67,6 +80,95 @@ object "ModExp" {
}
}

function bigUIntSqr(numberPtr, nLimbs, productPtr) {
let endOfProductPtr := add(productPtr, mul(mul(nLimbs, WORD_SIZE()), 2))
let hiPtr := productPtr
let loPtr := add(productPtr, mul(nLimbs, WORD_SIZE()))

let k, index, a_i, a_j, hi, lo, cs, overflow, c
for {let i := nLimbs} gt(i, 1) {i := sub(i, 1)} {
for {let j := i} gt(j, 0) {j := sub(j, 1)} {
k := add(i, j)
if or(gt(k, sub(i, 1)), eq(k, sub(i, 1))) {
index := sub(add(k, 1), nLimbs)
a_i := mload(add(numberPtr, mul(WORD_SIZE(), i)))
a_j := mload(add(numberPtr, mul(WORD_SIZE(), j)))
hi := getHighestHalfOfMultiplication(a_i, a_j)
lo := mul(a_i,a_j)
lo, overflow := overflowingAdd(lo, mload(add(loPtr, mul(index, WORD_SIZE()))))
hi := add(hi, overflow)
lo, overflow := overflowingAdd(lo, c)
hi := add(hi, overflow)
c := hi
mstore(add(loPtr, mul(index, WORD_SIZE())), lo)
}
if lt(k, sub(i, 1)) {
index := sub(add(k, 1), nLimbs)
a_i := mload(add(numberPtr, mul(WORD_SIZE(), i)))
a_j := mload(add(numberPtr, mul(WORD_SIZE(), j)))
hi := getHighestHalfOfMultiplication(a_i, a_j)
lo := mul(a_i,a_j)
lo, overflow := overflowingAdd(lo, mload(add(hiPtr, mul(index, WORD_SIZE()))))
hi := add(hi, overflow)
lo, overflow := overflowingAdd(lo, c)
hi := add(hi, overflow)
c := hi
mstore(add(hiPtr, mul(index, WORD_SIZE())), lo)
}
}
mstore(add(hiPtr, i), or(hi,lo))
}
let carry := shr(63, mload(loPtr))
// lo = shl(1, lo)
// hi = shl(1, hi)
// hi.limbs[NUM_LIMBS - 1] |= carry;

c := 0

for {let i := nLimbs} gt(i, 0) {i := sub(i, 1)} {
if or(lt(sub(nLimbs, 1), mul(i, 2)), eq(sub(nLimbs, 1), mul(i, 2))) {
index := sub(add(mul(2, i), 1), nLimbs)
a_i := mload(add(numberPtr, mul(WORD_SIZE(), i)))
hi := getHighestHalfOfMultiplication(a_i, a_i)
lo := mul(a_i,a_j)
lo, overflow := overflowingAdd(lo, mload(add(loPtr, mul(index, WORD_SIZE()))))
hi := add(hi, overflow)
lo, overflow := overflowingAdd(lo, c)
hi := add(hi, overflow)
c := hi
mstore(add(loPtr, mul(index, WORD_SIZE())), lo)
}
if gt(sub(nLimbs, 1), mul(i, 2)) {
index := add(mul(2, i), 1)
a_i := mload(add(numberPtr, mul(WORD_SIZE(), i)))
hi := getHighestHalfOfMultiplication(a_i, a_i)
lo := mul(a_i,a_j)
lo, overflow := overflowingAdd(lo, mload(add(hiPtr, mul(index, WORD_SIZE()))))
hi := add(hi, overflow)
lo, overflow := overflowingAdd(lo, c)
hi := add(hi, overflow)
c := hi
mstore(add(hiPtr, mul(index, WORD_SIZE())), lo)
}
if lt(sub(nLimbs, 1), mul(i, 2)) {
index := sub(mul(2, i), nLimbs)
hi := 0
lo, overflow := overflowingAdd(c, mload(add(hiPtr, mul(index, WORD_SIZE()))))
hi := add(hi, overflow)
c := hi
mstore(add(loPtr, mul(index, WORD_SIZE())), lo)
}
if or(gt(sub(nLimbs, 1), mul(i, 2)),eq(sub(nLimbs, 1), mul(i, 2))) {
index := mul(2, i)
hi := 0
lo, overflow := overflowingAdd(c, mload(add(hiPtr, mul(index, WORD_SIZE()))))
hi := add(hi, overflow)
c := hi
mstore(add(hiPtr, mul(index, WORD_SIZE())), lo)
}
}
}

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