NEWTON
Asked
5 months ago
215
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
5 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
5 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
3 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
3 months 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
5 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?
Is there no way for me to have a struct as a `@storage_var` if it contains an array fo felts inside ?
Where can I learn more about starknet?
How to get the block hash in Cairo lang?
signing a message using account.signMessage(data) | starknet.js
Is there a Cairo equivalent to Keccak256(abi.encode(parameters))?
Cairo Automatic Events
`HttpError: Bad Gateway ` from `node_modules/starknet/dist/ error in StarkNet