MCPcopy Create free account
hub / github.com/BitVM/BitVM / test_windowed_mul_2lc

Function test_windowed_mul_2lc

bitvm/src/bn254/fq.rs:1305–1383  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1303
1304 #[test]
1305 fn test_windowed_mul_2lc() {
1306 type U = <Fq as Fp254Mul2LC>::U;
1307 type T = <Fq as Fp254Mul2LC>::T;
1308
1309 let lcs = <Fq as Fp254Mul2LC>::LCS;
1310
1311 let zero = &BigInt::ZERO;
1312 let modulus = &Fq::modulus_as_bigint();
1313
1314 let mut prng: ChaCha20Rng = ChaCha20Rng::seed_from_u64(0);
1315
1316 let mut max_stack = 0;
1317
1318 for _ in 0..100 {
1319 let xs = lcs.map(|_| prng.gen_bigint_range(zero, modulus));
1320 let ys = lcs.map(|_| prng.gen_bigint_range(zero, modulus));
1321 let mut qs = vec![];
1322 let mut rs = vec![];
1323
1324 let mut c = zero.clone();
1325 for i in 0..lcs.len() {
1326 let xy = &xs[i] * &ys[i];
1327 qs.push(&xy / modulus);
1328 rs.push(&xy % modulus);
1329 c += if lcs[i] { xy } else { -xy };
1330 }
1331 let r = &c % modulus;
1332 let r = &(if r.is_negative() { modulus + r } else { r });
1333
1334 // correct quotient
1335 let q = &(&c / modulus);
1336 let script = script! {
1337 { T::push_u32_le(&bigint_to_u32_limbs(q.clone(), T::N_BITS)) }
1338 for i in 0..lcs.len() {
1339 { U::push_u32_le(&xs[i].to_u32_digits().1) }
1340 }
1341 for i in 0..lcs.len() {
1342 { U::push_u32_le(&ys[i].to_u32_digits().1) }
1343 }
1344 { <Fq as Fp254Mul2LC>::tmul() }
1345 { U::push_u32_le(&r.to_u32_digits().1) }
1346 { U::equal(0, 1) }
1347 };
1348 let res = execute_script(script);
1349 assert!(res.success);
1350
1351 max_stack = max_stack.max(res.stats.max_nb_stack_items);
1352
1353 // incorrect quotient
1354 let q = loop {
1355 let rnd = prng.gen_bigint_range(zero, modulus);
1356 if rnd != *q {
1357 break rnd;
1358 }
1359 };
1360 let script = script! {
1361 { T::push_u32_le(&bigint_to_u32_limbs(q.clone(), T::N_BITS)) }
1362 for i in 0..lcs.len() {

Callers

nothing calls this directly

Calls 5

execute_scriptFunction · 0.85
cloneMethod · 0.80
lenMethod · 0.80
is_negativeMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected