uint icon indicating copy to clipboard operation
uint copied to clipboard

Add split method for uint

Open tcoratger opened this issue 10 months ago • 1 comments

Is your feature request related to a problem? Please describe.

In situations like:

  • https://github.com/kkrt-labs/kakarot-rpc/blob/e09b452b258d6ef1f6f12c44d856f178cfa870a3/src/eth_provider/utils.rs#L45-L51
  • https://github.com/dojoengine/dojo/blob/f0637f928470217fccd60fe15b1b9c6fefd5c984/crates/katana/primitives/src/utils/mod.rs#L11-L15

it is useful to have a method that splits a uint but this method is not yet available in the implementation.

Describe the solution you'd like

It would therefore be appropriate to implement a method that splits a uint into two equal parts with high and low, based on the model of what is done here

https://github.com/recmo/uint/blob/41c45f89e6dab00bdb2878bf7495c5df864b8784/src/algorithms/mod.rs#L37-L39

This would allow for example to split a U256 into a high part and a low part.

tcoratger avatar Apr 08 '24 16:04 tcoratger

a few problems in implementing:

  • How would the implementation handle odd numbers of bits like Uint<7, 1> ?
  • How would the implementation handle the edge cases Uint<0, 0> and Uint<1,1>
  • Generic params can't be used in const operations, so we can't declare a function with the appropriate signature
    • fn split(self) -> (Uint<{ BITS.ceil_div(2) }, { LIMBS.ceil_div(2) }>, Uint<{ BITS.floor_div(2) }, { LIMBS.ceil_div(2) }>)
  • We could do it the way to_*e_bytes works and force the user to specify the correct output size, panicking otherwise
    • that would force the user to specify 4 generic params. Seems bad.
    • fn split<const B1: usize, const L1: usize, const B2: usize, const L2: usize>(self) -> (Uint<B1, L1>, Uint<B2, L2>)
  • Why not just use a similar DoubleWord type to the linked code?

prestwich avatar Apr 08 '24 17:04 prestwich