Skip to content

Commit 3d8e467

Browse files
authored
fix incorrect wrappers for tmul (#385)
1 parent 794c593 commit 3d8e467

3 files changed

Lines changed: 53 additions & 25 deletions

File tree

‎bitvm/src/bn254/fp254impl.rs‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,10 @@ pub trait Fp254Impl {
556556
let y = BigInt::from_str(&b.to_string()).unwrap();
557557
let modulus = &Fq::modulus_as_bigint();
558558
let q = (x * y) / modulus;
559+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_1().2;
559560

560561
let script = script! {
561-
for _ in 0..Self::N_LIMBS {
562+
for _ in 0..T_N_LIMBS {
562563
OP_DEPTH OP_1SUB OP_ROLL // hints
563564
}
564565
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -583,9 +584,10 @@ pub trait Fp254Impl {
583584
let y = BigInt::from_str(&constant.to_string()).unwrap();
584585
let modulus = &Fq::modulus_as_bigint();
585586
let q = (x * y) / modulus;
587+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_1().2;
586588

587589
let script = script! {
588-
for _ in 0..Self::N_LIMBS {
590+
for _ in 0..T_N_LIMBS {
589591
OP_DEPTH OP_1SUB OP_ROLL // hints
590592
}
591593
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -615,9 +617,10 @@ pub trait Fp254Impl {
615617
let y = BigInt::from_str(&b.to_string()).unwrap();
616618
let modulus = &Fq::modulus_as_bigint();
617619
let q = (x * y) / modulus;
620+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_1().2;
618621

619622
let script = script! {
620-
for _ in 0..Self::N_LIMBS {
623+
for _ in 0..T_N_LIMBS {
621624
OP_DEPTH OP_1SUB OP_ROLL // hints
622625
}
623626
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -653,9 +656,10 @@ pub trait Fp254Impl {
653656
let w = BigInt::from_str(&d.to_string()).unwrap();
654657

655658
let q = (x * z + y * w) / modulus;
659+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_2().2;
656660

657661
let script = script! {
658-
for _ in 0..Self::N_LIMBS {
662+
for _ in 0..T_N_LIMBS {
659663
OP_DEPTH OP_1SUB OP_ROLL // hints
660664
}
661665
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -696,9 +700,10 @@ pub trait Fp254Impl {
696700
let w = BigInt::from_str(&d.to_string()).unwrap();
697701

698702
let q = (x * z + y * w) / modulus;
703+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_2_w4().2;
699704

700705
let script = script! {
701-
for _ in 0..Self::N_LIMBS {
706+
for _ in 0..T_N_LIMBS {
702707
OP_DEPTH OP_1SUB OP_ROLL // hints
703708
}
704709
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -708,7 +713,7 @@ pub trait Fp254Impl {
708713
{ Fq::roll(d_depth + 4) }
709714
{ Fq::tmul_lc2_w4() }
710715
};
711-
hints.push(Hint::BigIntegerTmulLC2(q));
716+
hints.push(Hint::BigIntegerTmulLC2W4(q));
712717

713718
(script, hints)
714719
}
@@ -758,9 +763,10 @@ pub trait Fp254Impl {
758763
let w2 = BigInt::from_str(&h.to_string()).unwrap();
759764

760765
let q = (x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2) / modulus;
766+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_4().2;
761767

762768
let script = script! {
763-
for _ in 0..Self::N_LIMBS {
769+
for _ in 0..T_N_LIMBS {
764770
OP_DEPTH OP_1SUB OP_ROLL // hints
765771
}
766772
// { fq_push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -802,9 +808,10 @@ pub trait Fp254Impl {
802808
let w = BigInt::from_str(&d.to_string()).unwrap();
803809

804810
let q = (x * z + y * w) / modulus;
811+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_2().2;
805812

806813
let script = script! {
807-
for _ in 0..Self::N_LIMBS {
814+
for _ in 0..T_N_LIMBS {
808815
OP_DEPTH OP_1SUB OP_ROLL // hints
809816
}
810817
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -843,9 +850,10 @@ pub trait Fp254Impl {
843850
let w = BigInt::from_str(&d.to_string()).unwrap();
844851

845852
let q = (x * z + y * w) / modulus;
853+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_2_w4().2;
846854

847855
let script = script! {
848-
for _ in 0..Self::N_LIMBS {
856+
for _ in 0..T_N_LIMBS {
849857
OP_DEPTH OP_1SUB OP_ROLL // hints
850858
}
851859
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -855,7 +863,7 @@ pub trait Fp254Impl {
855863
{ Fq::copy(d_depth + 4) }
856864
{ Fq::tmul_lc2_w4() }
857865
};
858-
hints.push(Hint::BigIntegerTmulLC2(q));
866+
hints.push(Hint::BigIntegerTmulLC2W4(q));
859867

860868
(script, hints)
861869
}
@@ -866,8 +874,10 @@ pub trait Fp254Impl {
866874
let x = &BigInt::from_str(&a.to_string()).unwrap();
867875
let modulus = &Fq::modulus_as_bigint();
868876
let q = (x * x) / modulus;
877+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_1().2;
878+
869879
let script = script! {
870-
for _ in 0..Self::N_LIMBS {
880+
for _ in 0..T_N_LIMBS {
871881
OP_DEPTH OP_1SUB OP_ROLL // hints
872882
}
873883
// { Fq::push(ark_bn254::Fq::from_str(&q.to_string()).unwrap()) }
@@ -886,11 +896,13 @@ pub trait Fp254Impl {
886896
let modulus = &Fq::modulus_as_bigint();
887897
let y = &x.modinv(modulus).unwrap();
888898
let q = (x * y) / modulus;
899+
const T_N_LIMBS: u32 = Fq::bigint_tmul_lc_1().2;
900+
889901
let script = script! {
890902
for _ in 0..Self::N_LIMBS {
891903
OP_DEPTH OP_1SUB OP_ROLL // hints
892904
}
893-
for _ in 0..Self::N_LIMBS {
905+
for _ in 0..T_N_LIMBS {
894906
OP_DEPTH OP_1SUB OP_ROLL // hints
895907
}
896908
// { Fq::push(ark_bn254::Fq::from_str(&y.to_string()).unwrap()) }

‎bitvm/src/bn254/fq.rs‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,32 @@ impl Fq {
6363
}
6464
}
6565

66-
pub const fn bigint_tmul_lc_1() -> (u32, u32) {
66+
pub const fn bigint_tmul_lc_1() -> (u32, u32, u32) {
6767
const X: u32 = <Fq as Fp254Mul>::T::N_BITS;
68-
const Y: u32 = <Fq as Fp254Mul>::LIMB_SIZE;
69-
(X, Y)
68+
const Y: u32 = <Fq as Fp254Mul>::T::LIMB_SIZE;
69+
const Z: u32 = <Fq as Fp254Mul>::T::N_LIMBS;
70+
(X, Y, Z)
7071
}
7172

72-
pub const fn bigint_tmul_lc_2() -> (u32, u32) {
73+
pub const fn bigint_tmul_lc_2() -> (u32, u32, u32) {
7374
const X: u32 = <Fq as Fp254Mul2LC>::T::N_BITS;
74-
const Y: u32 = <Fq as Fp254Mul2LC>::LIMB_SIZE;
75-
(X, Y)
75+
const Y: u32 = <Fq as Fp254Mul2LC>::T::LIMB_SIZE;
76+
const Z: u32 = <Fq as Fp254Mul2LC>::T::N_LIMBS;
77+
(X, Y, Z)
7678
}
7779

78-
pub const fn bigint_tmul_lc_4() -> (u32, u32) {
80+
pub const fn bigint_tmul_lc_2_w4() -> (u32, u32, u32) {
81+
const X: u32 = <Fq as Fp254Mul2LCW4>::T::N_BITS;
82+
const Y: u32 = <Fq as Fp254Mul2LCW4>::T::LIMB_SIZE;
83+
const Z: u32 = <Fq as Fp254Mul2LCW4>::T::N_LIMBS;
84+
(X, Y, Z)
85+
}
86+
87+
pub const fn bigint_tmul_lc_4() -> (u32, u32, u32) {
7988
const X: u32 = <Fq as Fp254Mul4LC>::T::N_BITS;
80-
const Y: u32 = <Fq as Fp254Mul4LC>::LIMB_SIZE;
81-
(X, Y)
89+
const Y: u32 = <Fq as Fp254Mul4LC>::T::LIMB_SIZE;
90+
const Z: u32 = <Fq as Fp254Mul4LC>::T::N_LIMBS;
91+
(X, Y, Z)
8292
}
8393

8494
#[inline]

‎bitvm/src/bn254/utils.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ pub enum Hint {
1414
U256(num_bigint::BigInt),
1515
BigIntegerTmulLC1(num_bigint::BigInt),
1616
BigIntegerTmulLC2(num_bigint::BigInt),
17+
BigIntegerTmulLC2W4(num_bigint::BigInt),
1718
BigIntegerTmulLC4(num_bigint::BigInt),
1819
}
1920

2021
impl Hint {
2122
pub fn push(&self) -> Script {
22-
const K1: (u32, u32) = Fq::bigint_tmul_lc_1();
23-
const K2: (u32, u32) = Fq::bigint_tmul_lc_2();
24-
const K4: (u32, u32) = Fq::bigint_tmul_lc_4();
23+
const K1: (u32, u32, u32) = Fq::bigint_tmul_lc_1();
24+
const K2: (u32, u32, u32) = Fq::bigint_tmul_lc_2();
25+
const K3: (u32, u32, u32) = Fq::bigint_tmul_lc_2_w4();
26+
const K4: (u32, u32, u32) = Fq::bigint_tmul_lc_4();
2527
pub type T1 = BigIntImpl<{ K1.0 }, { K1.1 }>;
2628
pub type T2 = BigIntImpl<{ K2.0 }, { K2.1 }>;
29+
pub type T3 = BigIntImpl<{ K3.0 }, { K3.1 }>;
2730
pub type T4 = BigIntImpl<{ K4.0 }, { K4.1 }>;
2831
match self {
2932
Hint::Fq(fq) => script! {
@@ -46,8 +49,11 @@ impl Hint {
4649
Hint::BigIntegerTmulLC2(a) => script! {
4750
{ T2::push_u32_le(&bigint_to_u32_limbs(a.clone(), T2::N_BITS)) }
4851
},
52+
Hint::BigIntegerTmulLC2W4(a) => script! {
53+
{ T3::push_u32_le(&bigint_to_u32_limbs(a.clone(), T3::N_BITS)) }
54+
},
4955
Hint::BigIntegerTmulLC4(a) => script! {
50-
{ T2::push_u32_le(&bigint_to_u32_limbs(a.clone(), T4::N_BITS)) }
56+
{ T4::push_u32_le(&bigint_to_u32_limbs(a.clone(), T4::N_BITS)) }
5157
},
5258
}
5359
}

0 commit comments

Comments
 (0)