From 8b62c63155cf8097a3466bcb1d510514fee983fb Mon Sep 17 00:00:00 2001 From: Nikita Masych Date: Fri, 11 Oct 2024 15:40:58 +0300 Subject: [PATCH] refactor(boojum): modmul for UInt256 - better conversions --- crates/boojum/src/gadgets/u256/mod.rs | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/crates/boojum/src/gadgets/u256/mod.rs b/crates/boojum/src/gadgets/u256/mod.rs index 0ff0d11..7326ecb 100644 --- a/crates/boojum/src/gadgets/u256/mod.rs +++ b/crates/boojum/src/gadgets/u256/mod.rs @@ -382,11 +382,10 @@ impl UInt256 { }); let product = a.full_mul(b); - let m = convert_u256_to_u512(m); - let (q, r) = product.div_mod(m); - let q = convert_u512_to_u256(q); - let r = convert_u512_to_u256(r); + let (q, r) = product.div_mod(m.into()); + let q: U256 = q.try_into().unwrap(); + let r: U256 = r.try_into().unwrap(); let q = UInt256::allocate(cs, q); let r = UInt256::allocate(cs, r); @@ -410,19 +409,6 @@ impl UInt256 { } } -fn convert_u256_to_u512(v: U256) -> U512 { - let mut bytes = [0; 32]; - v.to_little_endian(&mut bytes); - U512::from_little_endian(&bytes) -} - -fn convert_u512_to_u256(v: U512) -> U256 { - let mut bytes = [0; 64]; - v.to_little_endian(&mut bytes); - let bytes = &bytes[..32]; - U256::from_little_endian(&bytes) -} - use crate::cs::Variable; use crate::gadgets::traits::castable::Convertor; use crate::gadgets::traits::castable::WitnessCastable;