Skip to content

Commit

Permalink
refactor(boojum): modmul for UInt256 - better conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Oct 11, 2024
1 parent 66823d2 commit 8b62c63
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions crates/boojum/src/gadgets/u256/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,10 @@ impl<F: SmallField> UInt256<F> {
});

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);
Expand All @@ -410,19 +409,6 @@ impl<F: SmallField> UInt256<F> {
}
}

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;
Expand Down

0 comments on commit 8b62c63

Please sign in to comment.