How to define 2048-bit integers on smart contracts?

As we know solidity supports only 256-bit big integers. But for crypto primitives (sometimes) we need bigger integers, e.g. 2048-bit.

Question: how can we define 2048-bit integers on a smart contract and perform some modular operations on that size integers?

• Well, you can use a uint256 array with 8 entries. – goodvibration Nov 15 '18 at 15:51

I would try to recreate the operations with an array of uint128. To make it simpler, let's say we want to do it for 512-bit integers:

// With the large number = num + 2^128 * num + 2^256 * num + 2^384 * num
function add(uint128 num1, uint128 num2) public pure returns(uint128 sum) {
uint256 memory intermediaries;
intermediaries = (uint256) (num1) + (uint256) (num2);
intermediaries = (uint256) (num1) + (uint256) (num2);
intermediaries = (uint256) (num1) + (uint256) (num2);
intermediaries = (uint256) (num1) + (uint256) (num2);
uint256 shifter = 2**128;
uint256 lowerMask = shifter - 1;
sum = (uint128) (intermediaries & (lowerMask)); // To get the lower part
sum = (uint128) (intermediaries & (lowerMask) + intermediaries / shifter);
sum = (uint128) (intermediaries & (lowerMask) + intermediaries / shifter);
sum = (uint128) (intermediaries & (lowerMask) + intermediaries / shifter);
// Yes, intermediaries / 2^128 is lost
}