NEWTON
Asked
2 months ago
195
views
1
This question has a bounty of $0.00 in testnet ETH. Answers to this question are eligible to win that bounty.
These are testnet contracts. Not real ETH.
$0.00
Can someone explain the difference between unsigned_div_rem
and uint256_unsigned_div_rem
?
Why unit version process so many extra steps, but overall number of steps for computing are different for these methods?
vargastartup
asked
2 months ago
0
Accepted answer
Both function compute the quotient and remainder(or modulo) for unsigned integers.
unsigned_div_rem
takes felt
arguments
uint256_unsigned_div_rem
takes Uint256
arguments.
Recall that Uint256 integers are encoded as two felts representing the 256 bits as two 128 bits numbers low
and high
.
struct Uint256 {
low: felt \\ 128 low bits
high: felt \\ 128 high bits
}
Thus, uint256_unsigned_div_rem
needs to do more thorough checks than the felt version to make sure the encoding is safe and sound.
Namely, it calls uint256_check
on return values and needs to verify the result (gotten via a hint).
Those checks can be seen here in the source, while the felt counterpart is more straightforward.
In general, functions using the Uint256 struct will be less efficient than their felt counterpart.
0xDE8D
answered
2 months ago
0
In Solidity, the uint256_unsigned_div_rem function is an implementation of an unsigned integer division and remainder operation for 256-bit integers. This function takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.
The unsigned_div_rem function is similar, but it is not specific to 256-bit integers and can be used with any unsigned integer type. It also takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.
Here is an example of how these functions can be used:
Copy code pragma solidity ^0.7.0;
contract Example { function example() public pure { uint256 a = 100; uint256 b = 3; uint256 c = 7;
// Use uint256_unsigned_div_rem to perform division and remainder with 256-bit integers
(uint256 q, uint256 r) = uint256_unsigned_div_rem(a, b);
assert(q == 33);
assert(r == 1);
// Use unsigned_div_rem to perform division and remainder with any unsigned integer type
(uint q2, uint r2) = unsigned_div_rem(c, b);
assert(q2 == 2);
assert(r2 == 1);
}
} In this example, the uint256_unsigned_div_rem function is used to perform division and remainder with 256-bit integers, while the unsigned_div_rem function is used to perform the same operation with smaller unsigned integers.
0x2932...Bc3E69
answered
29 days ago
0
In Solidity, the uint256_unsigned_div_rem function is an implementation of an unsigned integer division and remainder operation for 256-bit integers. This function takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.
The unsigned_div_rem function is similar, but it is not specific to 256-bit integers and can be used with any unsigned integer type. It also takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.
Here is an example of how these functions can be used:
Copy code pragma solidity ^0.7.0;
contract Example { function example() public pure { uint256 a = 100; uint256 b = 3; uint256 c = 7;
// Use uint256_unsigned_div_rem to perform division and remainder with 256-bit integers
(uint256 q, uint256 r) = uint256_unsigned_div_rem(a, b);
assert(q == 33);
assert(r == 1);
// Use unsigned_div_rem to perform division and remainder with any unsigned integer type
(uint q2, uint r2) = unsigned_div_rem(c, b);
assert(q2 == 2);
assert(r2 == 1);
}
} In this example, the uint256_unsigned_div_rem function is used to perform division and remainder with 256-bit integers, while the unsigned_div_rem function is used to perform the same operation with smaller unsigned integers.
0x2932...Bc3E69
answered
29 days ago
0
We use Cairo’s common math library, specifically unsigned_div_rem (unsigned division with remainder) to calculate the amount of tokens to be received.
// Calculate swap amount. let (local amount_to, _) = unsigned_div_rem( amm_to_balance * amount_from, amm_from_balance + amount_from, ); uint256_unsigned_div_rem takes Uint256 arguments.
Recall that Uint256 integers are encoded as two felts representing the 256 bits as two 128 bits numbers low and high.
Thus, uint256_unsigned_div_rem needs to do more thorough checks than the felt version to make sure the encoding is safe and sound. Namely, it calls uint256_check on return values and needs to verify the result (gotten via a hint). Those checks can be seen here in the source, while the felt counterpart is more straightforward.
In general, functions using the Uint256 struct will be less efficient than their felt counterpart.
Additional resources can be found here and here.
0xeF57...46cee1
answered
2 months ago
Cairo: What is the difference between let and tempvar?
How do you optimize gas in Cairo with Uint256/felt?
Cairo: Difference between amount and gas_fee
Cairo: let vs tempvar what is the difference?
How to divide felt in Cairo Lang using unsigned_div_rem?
Is uint256 math operators like uint256_le safe? Why do I need to use uint256_check?
What is the maximum value an uint and felt can store in Cairo Language?
What resources (Github repos, projects) do you follow about Cairo Language? Why?
Does Account Abstraction (StarkNet) support bundling multiple operations at once?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
Cairo v. Solidity
How to divide felt in Cairo Lang using unsigned_div_rem?
How to check if a contract has been deployed at a certain address in cairo?
When will Kakarot zkEVM launch on mainnet?