crypto-bigint icon indicating copy to clipboard operation
crypto-bigint copied to clipboard

Easier working with native numbers

Open dignifiedquire opened this issue 1 year ago • 6 comments

In my usage with BoxedUint I am really missing comparison and calculations with u32 and u64 at least, currently it is quite inefficient as I have to do the following

let x = make_boxed_uint();
let y = x - BoxedUint::from(4); // today
let y = x - 1u32; // should be easily possible

if x < 128u32 {
  return "small";
}

dignifiedquire avatar Mar 25 '24 10:03 dignifiedquire

That would require an impl with i32 as the argument. We don't support i32 for anything except shl/shr currently.

I guess specifically for the case of Add/Sub we could take the absolute value of the argument and if it's negative, switch from Add to Sub or Sub to Add accordingly, before converting the absolute value into a BoxedUint.

Do you want this just for i32 or other types of arguments?

tarcieri avatar Mar 25 '24 13:03 tarcieri

Sorry my example is bad, I am happy with just u32 and u64 support for this. (Updated the example)

dignifiedquire avatar Mar 25 '24 13:03 dignifiedquire

Would manually writing implementations be the best way to go about this? This is a lot of repetitive implementations.

I just did it locally for multiplying ConstMontyForm with u8, u16, u32, u64 and u128, and it is already 40 Mul implementations, if you want to make it work commutatively and with references (ie &{integer}).

If you do not make it work with &{integer}, the number of implementations is divided by 2.

TitouanReal avatar Jul 18 '24 11:07 TitouanReal

Here's a start which impls Add for BoxedUint: https://github.com/RustCrypto/crypto-bigint/pull/650

If that pattern seems acceptable I can do it for Sub and Mul as well.

tarcieri avatar Aug 15 '24 01:08 tarcieri

Would it be possible to have this for ConstMontyForm too? Right now this is even more cumbersome than for BoxedUint, because one has to go from native numbers to Uint and then to ConstMontyForm.

let a: ConstMontyForm<...> = ...;
let b = a * ConstMontyForm::new(&Uint::from(3u32)); // today
let b = a * 3u32; // should be easily possible

TitouanReal avatar Aug 21 '24 17:08 TitouanReal