Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Commit 749e873

Browse files
authored
Merge pull request #9 from pragma-org/jshy/num-bigint
2 parents e4c1a2a + 69f4927 commit 749e873

11 files changed

Lines changed: 171 additions & 200 deletions

File tree

Cargo.lock

Lines changed: 67 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uplc/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ bumpalo = { version = "3.16.0", features = ["collections"] }
1616
chumsky = { version = "=1.0.0-alpha.7", features = ["pratt"] }
1717
cryptoxide = "0.4.4"
1818
minicbor = { version = "0.25.1", features = ["std"] }
19+
num = "0.4.3"
20+
num-bigint = "0.4"
21+
num-integer = "0.1"
1922
once_cell = "1.20.2"
20-
rug = { version = "1.26.0", default-features = false, features = [
21-
"integer",
22-
"std",
23-
] }
2423
secp256k1 = "0.30.0"
2524
thiserror = "1.0.63"
2625

crates/uplc/src/bls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub static SCALAR_PERIOD: Lazy<Integer> = Lazy::new(|| {
1010
0x00, 0x01,
1111
];
1212

13-
Integer::from_digits(&bytes, rug::integer::Order::MsfBe)
13+
Integer::from_bytes_be(num_bigint::Sign::Plus, &bytes)
1414
});
1515

1616
pub const BLST_P1_COMPRESSED_SIZE: usize = 48;

crates/uplc/src/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub enum Constant<'a> {
2222
Bls12_381MlResult(&'a blst::blst_fp12),
2323
}
2424

25-
pub type Integer = rug::Integer;
25+
pub type Integer = num::BigInt;
2626

2727
pub fn integer(arena: &Bump) -> &mut Integer {
28-
arena.alloc(Integer::new())
28+
arena.alloc(Integer::default())
2929
}
3030

3131
pub fn integer_from(arena: &Bump, i: i128) -> &mut Integer {

crates/uplc/src/flat/data.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use bumpalo::collections::Vec as BumpVec;
22
use minicbor::data::{IanaTag, Tag};
3-
use rug::ops::NegAssign;
43

54
use crate::data::PlutusData;
65

@@ -73,13 +72,14 @@ impl<'a, 'b> minicbor::decode::Decode<'b, Ctx<'a>> for &'a PlutusData<'a> {
7372
bytes.extend_from_slice(chunk);
7473
}
7574

76-
let integer = ctx
77-
.arena
78-
.alloc(rug::Integer::from_digits(&bytes, rug::integer::Order::Msf));
79-
80-
if x == IanaTag::NegBignum {
81-
integer.neg_assign();
82-
}
75+
let integer = ctx.arena.alloc(num::BigInt::from_bytes_be(
76+
if x == IanaTag::PosBignum {
77+
num_bigint::Sign::Plus
78+
} else {
79+
num_bigint::Sign::Plus
80+
},
81+
&bytes,
82+
));
8383

8484
Ok(PlutusData::integer(ctx.arena, integer))
8585
}

crates/uplc/src/flat/encode/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,16 @@ impl Encoder {
153153

154154
loop {
155155
let temp: Integer = d.clone() % 128;
156-
let mut w = temp.to_u8().unwrap();
156+
let mut w: u8 = temp.try_into().unwrap();
157157

158158
d >>= 7;
159159

160-
if d != 0 {
160+
if d != Integer::ZERO {
161161
w |= 128;
162162
}
163163
self.bits(8, w);
164164

165-
if d == 0 {
165+
if d == Integer::ZERO {
166166
break;
167167
}
168168
}

crates/uplc/src/flat/zigzag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl ZigZag for &Integer {
1414
type Zag = Integer;
1515

1616
fn zigzag(self) -> Self::Zag {
17-
if *self >= 0 {
17+
if *self >= 0.into() {
1818
// For non-negative numbers, just multiply by 2 (left shift by 1)
1919
self.clone() << 1
2020
} else {
@@ -28,7 +28,7 @@ impl ZigZag for &Integer {
2828
}
2929

3030
fn unzigzag(self) -> Self::Zag {
31-
let temp: Integer = self.clone() & 1;
31+
let temp: Integer = self.clone() & Integer::from(1);
3232

3333
(self.clone() >> 1) ^ -(temp)
3434
}

crates/uplc/src/machine/cost_model/value.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rug::integer::BorrowInteger;
1+
use num::{BigUint, Zero};
22

33
use crate::{
44
binder::Eval,
@@ -11,27 +11,27 @@ pub const UNIT_EX_MEM: i64 = 1;
1111
pub const BOOL_EX_MEM: i64 = 1;
1212

1313
pub fn integer_ex_mem(i: &Integer) -> i64 {
14-
if *i == 0 {
14+
if i.is_zero() {
1515
1
1616
} else {
17-
(integer_log2(i.as_abs()) / 64) + 1
17+
(integer_log2(i.magnitude()) / 64) + 1
1818
}
1919
}
2020

21-
pub fn integer_log2(i: BorrowInteger<'_>) -> i64 {
21+
pub fn integer_log2(i: &BigUint) -> i64 {
2222
if i.is_zero() {
2323
return 0;
2424
}
2525

26-
(i.significant_bits() - 1) as i64
26+
(i.bits() - 1) as i64
2727
}
2828

2929
pub fn integer_log2_x(i: &Integer) -> i64 {
3030
if i.is_zero() {
3131
return 0;
3232
}
3333

34-
(i.significant_bits() - 1) as i64
34+
(i.bits() - 1) as i64
3535
}
3636

3737
pub fn byte_string_ex_mem(b: &[u8]) -> i64 {
@@ -129,76 +129,78 @@ pub fn ml_result_ex_mem() -> i64 {
129129
mod tests {
130130
use std::str::FromStr;
131131

132+
use crate::constant::Integer;
133+
132134
use super::integer_log2;
133135

134136
#[test]
135137
fn integer_log2_oracle() {
136138
// Values come from the Haskell implementation
137-
assert_eq!(integer_log2(rug::Integer::from(0).as_abs()), 0);
138-
assert_eq!(integer_log2(rug::Integer::from(1).as_abs()), 0);
139-
assert_eq!(integer_log2(rug::Integer::from(42).as_abs()), 5);
139+
assert_eq!(integer_log2(Integer::ZERO.magnitude()), 0);
140+
assert_eq!(integer_log2(Integer::from(1).magnitude()), 0);
141+
assert_eq!(integer_log2(Integer::from(42).magnitude()), 5);
140142

141143
assert_eq!(
142144
integer_log2(
143-
rug::Integer::from_str("18446744073709551615")
145+
Integer::from_str("18446744073709551615")
144146
.unwrap()
145-
.as_abs()
147+
.magnitude()
146148
),
147149
63
148150
);
149151
assert_eq!(
150152
integer_log2(
151-
rug::Integer::from_str("999999999999999999999999999999")
153+
Integer::from_str("999999999999999999999999999999")
152154
.unwrap()
153-
.as_abs()
155+
.magnitude()
154156
),
155157
99
156158
);
157159
assert_eq!(
158160
integer_log2(
159-
rug::Integer::from_str("170141183460469231731687303715884105726")
161+
Integer::from_str("170141183460469231731687303715884105726")
160162
.unwrap()
161-
.as_abs()
163+
.magnitude()
162164
),
163165
126
164166
);
165167
assert_eq!(
166168
integer_log2(
167-
rug::Integer::from_str("170141183460469231731687303715884105727")
169+
Integer::from_str("170141183460469231731687303715884105727")
168170
.unwrap()
169-
.as_abs()
171+
.magnitude()
170172
),
171173
126
172174
);
173175
assert_eq!(
174176
integer_log2(
175-
rug::Integer::from_str("170141183460469231731687303715884105728")
177+
Integer::from_str("170141183460469231731687303715884105728")
176178
.unwrap()
177-
.as_abs()
179+
.magnitude()
178180
),
179181
127
180182
);
181183
assert_eq!(
182184
integer_log2(
183-
rug::Integer::from_str("340282366920938463463374607431768211458")
185+
Integer::from_str("340282366920938463463374607431768211458")
184186
.unwrap()
185-
.as_abs()
187+
.magnitude()
186188
),
187189
128
188190
);
189191
assert_eq!(
190192
integer_log2(
191-
rug::Integer::from_str("999999999999999999999999999999999999999999")
193+
Integer::from_str("999999999999999999999999999999999999999999")
192194
.unwrap()
193-
.as_abs()
195+
.magnitude()
194196
),
195197
139
196198
);
197199
assert_eq!(
198200
integer_log2(
199-
rug::Integer::from_str("999999999999999999999999999999999999999999999999999999999999999999999999999999999999")
201+
Integer::from_str("999999999999999999999999999999999999999999999999999999999999999999999999999999999999")
200202
.unwrap()
201-
.as_abs()
203+
.magnitude()
202204
),
203205
279
204206
);

0 commit comments

Comments
 (0)