Skip to content

Commit

Permalink
Refactor and reorder (BitVM#206)
Browse files Browse the repository at this point in the history
* reorder fq12 functions

* reorder fq6 functions

* reorder fq2 functions

* reorder fp254impl functions

* remove unused in fq12 and use run instead of execute_script in tests

* remove unused in fq6 and use run instead of execute_script in tests

* remove unused in fq2 and use run instead of execute_script in tests

* delete u29x9

* remove unused in fp254impl and use run instead of execute_script in tests
  • Loading branch information
Hakkush-07 authored Jan 27, 2025
1 parent 47a4f4c commit 438f3a1
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 3,948 deletions.
1 change: 0 additions & 1 deletion bitvm/src/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod inv;
pub mod mul;
pub mod std;
pub mod sub;
pub mod u29x9;

pub struct BigIntImpl<const N_BITS: u32, const LIMB_SIZE: u32> {}

Expand Down
2,697 changes: 0 additions & 2,697 deletions bitvm/src/bigint/u29x9.rs

This file was deleted.

307 changes: 96 additions & 211 deletions bitvm/src/bn254/fp254impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::bigint::add::limb_add_carry;
use crate::bigint::bits::limb_to_be_bits;
use crate::bigint::sub::limb_sub_borrow;
use crate::bigint::U254;
use crate::bn254::fq::Fq;
Expand Down Expand Up @@ -44,31 +43,46 @@ pub trait Fp254Impl {
U254::drop()
}

#[inline]
fn toaltstack() -> Script {
U254::toaltstack()
}

#[inline]
fn fromaltstack() -> Script {
U254::fromaltstack()
}

#[inline]
fn zip(a: u32, b: u32) -> Script {
U254::zip(a, b)
}

#[inline]
fn push_u32_le(v: &[u32]) -> Script {
script! {
{ U254::push_u32_le(&BigUint::from_slice(v).to_u32_digits()) }
}
fn push_modulus() -> Script {
U254::push_hex(Self::MODULUS)
}

#[inline]
fn read_u32_le(witness: Vec<Vec<u8>>) -> Vec<u32> {
U254::read_u32_le(witness)
fn push_zero() -> Script {
U254::push_zero()
}

#[inline]
fn equal(a: u32, b: u32) -> Script {
U254::equal(a, b)
fn push_one() -> Script {
U254::push_one()
}

#[inline]
fn equalverify(a: u32, b: u32) -> Script {
U254::equalverify(a, b)
fn push_u32_le(v: &[u32]) -> Script {
script! {
{ U254::push_u32_le(&BigUint::from_slice(v).to_u32_digits()) }
}
}

#[inline]
fn read_u32_le(witness: Vec<Vec<u8>>) -> Vec<u32> {
U254::read_u32_le(witness)
}

#[inline]
Expand All @@ -88,38 +102,61 @@ pub trait Fp254Impl {
}

#[inline]
fn convert_to_be_bits() -> Script {
U254::convert_to_be_bits()
fn equal(a: u32, b: u32) -> Script {
U254::equal(a, b)
}

#[inline]
fn convert_to_be_bits_toaltstack() -> Script {
U254::convert_to_be_bits_toaltstack()
fn equalverify(a: u32, b: u32) -> Script {
U254::equalverify(a, b)
}

#[inline]
fn convert_to_le_bits() -> Script {
U254::convert_to_le_bits()
fn is_zero(a: u32) -> Script {
U254::is_zero(a)
}

#[inline]
fn convert_to_le_bits_toaltstack() -> Script {
U254::convert_to_le_bits_toaltstack()
fn is_zero_keep_element(a: u32) -> Script {
U254::is_zero_keep_element(a)
}

#[inline]
fn push_modulus() -> Script {
U254::push_hex(Self::MODULUS)
fn is_one() -> Script {
script! {
{ Self::push_one() }
{ Self::equal(1, 0) }
}
}

#[inline]
fn push_zero() -> Script {
U254::push_zero()
fn is_one_keep_element(a: u32) -> Script {
script! {
{ Self::copy(a) }
{ Self::is_one() }
}
}

fn is_field() -> Script {
script! {
// Each limb must not be negative
for i in 0..Self::N_LIMBS - 1 {
{ i } OP_PICK
0 OP_GREATERTHANOREQUAL OP_TOALTSTACK
}
{ Self::N_LIMBS - 1 } OP_PICK
0 OP_GREATERTHANOREQUAL
for _ in 0..Self::N_LIMBS - 1 {
OP_FROMALTSTACK OP_BOOLAND
}
OP_TOALTSTACK

{ Self::push_modulus() }
{ U254::lessthan(1, 0) }

OP_FROMALTSTACK OP_BOOLAND
}
}

#[inline]
fn push_one() -> Script {
U254::push_one()
fn convert_to_le_bits_toaltstack() -> Script {
U254::convert_to_le_bits_toaltstack()
}

// A + B mod M
Expand Down Expand Up @@ -493,6 +530,36 @@ pub trait Fp254Impl {
}
}

fn div2() -> Script {
script! {
{ U254::div2rem() }
OP_IF
{ U254::push_hex(Self::P_PLUS_ONE_DIV2) }
{ Self::add(1, 0) }
OP_ENDIF
}
}

fn div3() -> Script {
script! {
{ U254::div3rem() }
OP_DUP
0 OP_GREATERTHAN
OP_IF
OP_1SUB
OP_IF
{ U254::push_hex(Self::P_PLUS_TWO_DIV3) }
{ Self::add(1, 0) }
OP_ELSE
{ U254::push_hex(Self::TWO_P_PLUS_ONE_DIV3) }
{ Self::add(1, 0) }
OP_ENDIF
OP_ELSE
OP_DROP
OP_ENDIF
}
}

fn hinted_mul(
mut a_depth: u32,
mut a: ark_bn254::Fq,
Expand Down Expand Up @@ -659,49 +726,6 @@ pub trait Fp254Impl {
(script, hints)
}

fn is_zero(a: u32) -> Script {
U254::is_zero(a)
}

fn is_zero_keep_element(a: u32) -> Script {
U254::is_zero_keep_element(a)
}

fn is_one() -> Script {
script! {
{ Self::push_one() }
{ Self::equal(1, 0) }
}
}

fn is_one_keep_element(a: u32) -> Script {
script! {
{ Self::copy(a) }
{ Self::is_one() }
}
}

fn is_field() -> Script {
script! {
// Each limb must not be negative
for i in 0..Self::N_LIMBS - 1 {
{ i } OP_PICK
0 OP_GREATERTHANOREQUAL OP_TOALTSTACK
}
{ Self::N_LIMBS - 1 } OP_PICK
0 OP_GREATERTHANOREQUAL
for _ in 0..Self::N_LIMBS - 1 {
OP_FROMALTSTACK OP_BOOLAND
}
OP_TOALTSTACK

{ Self::push_modulus() }
{ U254::lessthan(1, 0) }

OP_FROMALTSTACK OP_BOOLAND
}
}

// TODO: Optimize using the sqaure feature
fn hinted_square(a: ark_bn254::Fq) -> (Script, Vec<Hint>) {
let mut hints = Vec::new();
Expand Down Expand Up @@ -751,143 +775,4 @@ pub trait Fp254Impl {

(script, hints)
}

fn div2() -> Script {
script! {
{ U254::div2rem() }
OP_IF
{ U254::push_hex(Self::P_PLUS_ONE_DIV2) }
{ Self::add(1, 0) }
OP_ENDIF
}
}

fn div3() -> Script {
script! {
{ U254::div3rem() }
OP_DUP
0 OP_GREATERTHAN
OP_IF
OP_1SUB
OP_IF
{ U254::push_hex(Self::P_PLUS_TWO_DIV3) }
{ Self::add(1, 0) }
OP_ELSE
{ U254::push_hex(Self::TWO_P_PLUS_ONE_DIV3) }
{ Self::add(1, 0) }
OP_ENDIF
OP_ELSE
OP_DROP
OP_ENDIF
}
}

fn convert_to_be_u4() -> Script {
let build_u8_from_be_bits = |i| {
script! {
for _ in 0..(i - 1) {
OP_DUP OP_ADD OP_ADD
}
}
};

script! {
// { Self::decode_montgomery() }
// start with the top limb
// 22 bits => 2 + 5 u4
{ Self::N_LIMBS - 1 } OP_ROLL
{ limb_to_be_bits(22) }
{ build_u8_from_be_bits(2) } OP_TOALTSTACK
for _ in 0..5 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}

// second limb, 29 bits => 7 u4 + 1 leftover bits
{ Self::N_LIMBS - 2 } OP_ROLL
{ limb_to_be_bits(29) }
for _ in 0..7 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(1) } OP_TOALTSTACK

// third limb, 29 bits = 3 bits borrow + 6 u4 + 2 leftover bits
{ Self::N_LIMBS - 3 } OP_ROLL
{ limb_to_be_bits(29) }
OP_FROMALTSTACK
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
for _ in 0..6 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(2) } OP_TOALTSTACK

// fourth limb, 29 bits = 2 bits borrow + 6 u4 + 3 leftover bits
{ Self::N_LIMBS - 4 } OP_ROLL
{ limb_to_be_bits(29) }
OP_FROMALTSTACK
{ build_u8_from_be_bits(3) } OP_TOALTSTACK
for _ in 0..6 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(3) } OP_TOALTSTACK

// fifth limb, 30 bits = 1 bits borrow + 7 u4
{ Self::N_LIMBS - 5 } OP_ROLL
{ limb_to_be_bits(29) }
OP_FROMALTSTACK
{ build_u8_from_be_bits(2) } OP_TOALTSTACK
for _ in 0..7 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}

// sixth limb, 30 bits => 7 u4 + 1 leftover bits
{ Self::N_LIMBS - 6 } OP_ROLL
{ limb_to_be_bits(29) }
for _ in 0..7 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(1) } OP_TOALTSTACK

// seventh limb, 30 bits = 3 bits borrow + 6 u4 + 2 leftover bits
{ Self::N_LIMBS - 7 } OP_ROLL
{ limb_to_be_bits(29) }
OP_FROMALTSTACK
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
for _ in 0..6 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(2) } OP_TOALTSTACK

// eighth limb, 30 bits = 2 bits borrow + 6 u4 + 3 leftover bits
{ Self::N_LIMBS - 8 } OP_ROLL
{ limb_to_be_bits(29) }
OP_FROMALTSTACK
{ build_u8_from_be_bits(3) } OP_TOALTSTACK
for _ in 0..6 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(3) } OP_TOALTSTACK

// ninth limb, 29 bits = 1 bits borrow + 7 u4
{ Self::N_LIMBS - 9 } OP_ROLL
{ limb_to_be_bits(29) }
OP_FROMALTSTACK
{ build_u8_from_be_bits(2) } OP_TOALTSTACK
for _ in 0..6 {
{ build_u8_from_be_bits(4) } OP_TOALTSTACK
}
{ build_u8_from_be_bits(4) }

for _ in 0..63 {
OP_FROMALTSTACK
}
}
}

fn toaltstack() -> Script {
U254::toaltstack()
}

fn fromaltstack() -> Script {
U254::fromaltstack()
}
}
Loading

0 comments on commit 438f3a1

Please sign in to comment.