Skip to content

Commit

Permalink
Refactor - Remove montgomery functions (BitVM#188)
Browse files Browse the repository at this point in the history
* delete push montgomery functions and fq.mul,square,inv

* get rid of _not_montgomery from function names
  • Loading branch information
Hakkush-07 authored Jan 13, 2025
1 parent 901d1d7 commit 9932943
Show file tree
Hide file tree
Showing 30 changed files with 584 additions and 8,647 deletions.
950 changes: 12 additions & 938 deletions bitvm/src/bn254/fp254impl.rs

Large diffs are not rendered by default.

343 changes: 27 additions & 316 deletions bitvm/src/bn254/fq.rs

Large diffs are not rendered by default.

884 changes: 26 additions & 858 deletions bitvm/src/bn254/fq12.rs

Large diffs are not rendered by default.

304 changes: 23 additions & 281 deletions bitvm/src/bn254/fq2.rs

Large diffs are not rendered by default.

660 changes: 19 additions & 641 deletions bitvm/src/bn254/fq6.rs

Large diffs are not rendered by default.

169 changes: 3 additions & 166 deletions bitvm/src/bn254/fr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use num_bigint::BigUint;

use crate::bn254::fp254impl::Fp254Impl;
use crate::treepp::*;

Expand All @@ -9,24 +8,11 @@ impl Fp254Impl for Fr {
const MODULUS: &'static str =
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001";

// 2²⁶¹ mod p <=> 0xdc83629563d44755301fa84819caa8075bba827a494b01a2fd4e1568fffff57
const MONTGOMERY_ONE: &'static str =
"dc83629563d44755301fa84819caa8075bba827a494b01a2fd4e1568fffff57";

// montgomery_one^{-1} mod p <=> 0x18223d71645e71455ce0bffc0a6ec602ae5dab0851091e61fb9b65ed0584ee8b
const MONTGOMERY_ONE_INV: &'static str =
"18223d71645e71455ce0bffc0a6ec602ae5dab0851091e61fb9b65ed0584ee8b";

// p = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
const MODULUS_LIMBS: [u32; Self::N_LIMBS as usize] = [
0x10000001, 0x1f0fac9f, 0xe5c2450, 0x7d090f3, 0x1585d283, 0x2db40c0, 0xa6e141, 0xe5c2634,
0x30644e,
];
// inv₂₆₁ p <=> 0xd8c07d0e2f27cbe4d1c6567d766f9dc6e9a7979b4b396ee4c3d1e0a6c10000001
const MODULUS_INV_261: [u32; Self::N_LIMBS as usize] = [
0x10000001, 0x8f05360, 0x5bb930f, 0x12f36967, 0x1dc6e9a7, 0x13ebb37c, 0x19347195,
0x1c5e4f97, 0xd8c07d0,
];

const P_PLUS_ONE_DIV2: &'static str =
"183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000001";
Expand All @@ -46,13 +32,6 @@ impl Fr {
{ Fr::push_u32_le(&BigUint::from(a).to_u32_digits()) }
}
}

#[inline]
pub fn push_not_montgomery(a: ark_bn254::Fr) -> Script {
script! {
{ Fr::push_u32_le_not_montgomery(&BigUint::from(a).to_u32_digits()) }
}
}
}

#[cfg(test)]
Expand All @@ -61,30 +40,13 @@ mod test {
use crate::bn254::fr::Fr;
use crate::treepp::*;
use ark_ff::AdditiveGroup;
use ark_ff::{BigInteger, Field, PrimeField};
use ark_std::UniformRand;
use core::ops::{Add, Mul, Rem, Sub};
use core::ops::{Add, Rem, Sub};
use num_bigint::{BigUint, RandomBits};
use num_traits::Num;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;

#[test]
fn test_decode_montgomery() {
println!(
"Fr.decode_montgomery: {} bytes",
Fr::decode_montgomery().len()
);
let script = script! {
{ Fr::push_one() }
{ Fr::push_u32_le(&BigUint::from_str_radix(Fr::MONTGOMERY_ONE, 16).unwrap().to_u32_digits()) }
{ Fr::decode_montgomery() }
{ Fr::equalverify(1, 0) }
OP_TRUE
};
run(script);
}

#[test]
fn test_add() {
println!("Fr.add: {} bytes", Fr::add(0, 1).len());
Expand Down Expand Up @@ -163,54 +125,6 @@ mod test {
}
}

#[test]
fn test_mul() {
println!("Fr.mul: {} bytes", Fr::mul().len());
let m = BigUint::from_str_radix(Fr::MODULUS, 16).unwrap();
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..3 {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));

let a = a.rem(&m);
let b = b.rem(&m);
let c: BigUint = a.clone().mul(b.clone()).rem(&m);

let script = script! {
{ Fr::push_u32_le(&a.to_u32_digits()) }
{ Fr::push_u32_le(&b.to_u32_digits()) }
{ Fr::mul() }
{ Fr::push_u32_le(&c.to_u32_digits()) }
{ Fr::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}

#[test]
fn test_square() {
println!("Fr.square: {} bytes", Fr::square().len());
let m = BigUint::from_str_radix(Fr::MODULUS, 16).unwrap();

let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..10 {
let a: BigUint = prng.sample(RandomBits::new(254));

let a = a.rem(&m);
let c: BigUint = a.clone().mul(a.clone()).rem(&m);

let script = script! {
{ Fr::push_u32_le(&a.to_u32_digits()) }
{ Fr::square() }
{ Fr::push_u32_le(&c.to_u32_digits()) }
{ Fr::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}

#[test]
fn test_neg() {
println!("Fr.neg: {} bytes", Fr::neg(0).len());
Expand All @@ -232,26 +146,6 @@ mod test {
}
}

#[test]
fn test_inv() {
println!("Fr.inv: {} bytes", Fr::inv().len());
let mut prng = ChaCha20Rng::seed_from_u64(0);

for _ in 0..1 {
let a = ark_bn254::Fr::rand(&mut prng);
let c = a.inverse().unwrap();

let script = script! {
{ Fr::push_u32_le(&BigUint::from(a).to_u32_digits()) }
{ Fr::inv() }
{ Fr::push_u32_le(&BigUint::from(c).to_u32_digits()) }
{ Fr::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}

#[test]
fn test_div2() {
println!("Fr.div2: {} bytes", Fr::div2().len());
Expand Down Expand Up @@ -295,7 +189,7 @@ mod test {

#[test]
fn test_is_one() {
println!("Fr.is_one: {} bytes", Fr::is_one(0).len());
println!("Fr.is_one: {} bytes", Fr::is_one().len());
println!(
"Fr.is_one_keep_element: {} bytes",
Fr::is_one_keep_element(0).len()
Expand All @@ -304,7 +198,7 @@ mod test {
{ Fr::push_one() }
{ Fr::is_one_keep_element(0) }
OP_TOALTSTACK
{ Fr::is_one(0) }
{ Fr::is_one() }
OP_FROMALTSTACK
OP_BOOLAND
};
Expand Down Expand Up @@ -356,37 +250,6 @@ mod test {
}
}

#[test]
fn test_mul_by_constant() {
let m = BigUint::from_str_radix(Fr::MODULUS, 16).unwrap();
let mut prng = ChaCha20Rng::seed_from_u64(0);

for i in 0..3 {
let a: BigUint = prng.sample(RandomBits::new(254));
let a = a.rem(&m);

let b: BigUint = prng.sample(RandomBits::new(254));
let b = b.rem(&m);

let mul_by_constant = Fr::mul_by_constant(&ark_bn254::Fr::from(b.clone()));

if i == 0 {
println!("Fr.mul_by_constant: {} bytes", mul_by_constant.len());
}

let c: BigUint = a.clone().mul(b.clone()).rem(&m);

let script = script! {
{ Fr::push_u32_le(&a.to_u32_digits()) }
{ mul_by_constant.clone() }
{ Fr::push_u32_le(&c.to_u32_digits()) }
{ Fr::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}

#[test]
fn test_is_field() {
let m = BigUint::from_str_radix(Fr::MODULUS, 16).unwrap();
Expand Down Expand Up @@ -420,30 +283,4 @@ mod test {
};
run(script);
}

#[test]
fn test_convert_to_be_bytes() {
let mut prng = ChaCha20Rng::seed_from_u64(0);

let convert_to_be_bytes_script = Fr::convert_to_be_bytes();
println!(
"Fr.convert_to_be_bytes: {} bytes",
convert_to_be_bytes_script.len()
);

for _ in 0..10 {
let fr = ark_bn254::Fr::rand(&mut prng);
let bytes = fr.into_bigint().to_bytes_be();

let script = script! {
{ Fr::push_u32_le(&BigUint::from(fr).to_u32_digits()) }
{ convert_to_be_bytes_script.clone() }
for i in 0..32 {
{ bytes[i] } OP_EQUALVERIFY
}
OP_TRUE
};
run(script);
}
}
}
Loading

0 comments on commit 9932943

Please sign in to comment.