Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement felt to bigint functions #23

Merged
merged 11 commits into from
Dec 20, 2023
3 changes: 3 additions & 0 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ arbitrary = { version = "1.3.0", optional = true, default-features = false }
num-traits = { version = "0.2.16", default-features = false }
num-bigint = {version = "0.4.4", default-features = false}
num-integer = {version = "0.1.45", default-features = false}
lazy_static = { version = "1.4.0", default-features = false, features = [
"spin_no_std",
] }

[features]
default = ["std", "serde", "curve"]
Expand Down
100 changes: 99 additions & 1 deletion crates/starknet-types-core/src/felt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use core::ops::{Add, Mul, Neg};

use bitvec::array::BitArray;
use num_bigint::BigInt;
use lazy_static::lazy_static;
use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
use num_integer::Integer;
use num_traits::Num;
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};

lazy_static! {
pub static ref CAIRO_PRIME_BIGINT: BigInt = BigInt::from_str_radix(
"800000000000011000000000000000000000000000000000000000000000001",
16
)
.unwrap();
}

#[cfg(target_pointer_width = "64")]
pub type BitArrayStore = [u64; 4];

Expand Down Expand Up @@ -796,6 +806,33 @@ mod arithmetic {
}
}

pub fn felt_to_biguint(felt: Felt) -> BigUint {
0xLucqs marked this conversation as resolved.
Show resolved Hide resolved
let big_digits = felt
.to_le_digits()
.into_iter()
.flat_map(|limb| [limb as u32, (limb >> 32) as u32])
.collect();
BigUint::new(big_digits)
}

pub fn felt_to_bigint(felt: Felt) -> BigInt {
0xLucqs marked this conversation as resolved.
Show resolved Hide resolved
felt_to_biguint(felt).to_bigint().unwrap()
}

pub fn biguint_to_felt(biguint: &BigUint) -> Felt {
0xLucqs marked this conversation as resolved.
Show resolved Hide resolved
Felt::from_bytes_le_slice(&biguint.to_bytes_le())
}

pub fn bigint_to_felt(bigint: &BigInt) -> Felt {
let (sign, bytes) = bigint.mod_floor(&CAIRO_PRIME_BIGINT).to_bytes_le();
let felt = Felt::from_bytes_le_slice(&bytes);
if sign == Sign::Minus {
felt.neg()
} else {
felt
}
}

#[cfg(feature = "serde")]
mod serde {
use ::serde::{de, ser::SerializeSeq, Deserialize, Serialize};
Expand Down Expand Up @@ -1600,4 +1637,65 @@ mod test {

assert_eq!(x.mul_mod(&y, &p), expected_result);
}

#[test]
fn bigints_to_felt() {
let one = &*CAIRO_PRIME_BIGINT + BigInt::from(1_u32);
assert_eq!(biguint_to_felt(&one.to_biguint().unwrap()), Felt::from(1));
assert_eq!(bigint_to_felt(&one), Felt::from(1));

let zero = &*CAIRO_PRIME_BIGINT * 99_u32;
assert_eq!(biguint_to_felt(&zero.to_biguint().unwrap()), Felt::from(0));
assert_eq!(bigint_to_felt(&zero), Felt::from(0));

assert_eq!(
bigint_to_felt(&BigInt::from(-1)),
Felt::from_hex("0x800000000000011000000000000000000000000000000000000000000000000")
.unwrap()
);

let numbers_str = [
"0x0",
"0x1",
"0x10",
"0x8000000000000110000000000",
"0xffffffffffffff",
"0xffffffffefff12380777abcd",
];

for number_str in numbers_str {
assert_eq!(
bigint_to_felt(&BigInt::from_str_radix(&number_str[2..], 16).unwrap()),
Felt::from_hex(number_str).unwrap()
);
assert_eq!(
biguint_to_felt(&BigUint::from_str_radix(&number_str[2..], 16).unwrap()),
Felt::from_hex(number_str).unwrap()
)
}
}

#[test]
fn felt_to_bigints() {
let numbers_str = [
"0x0",
"0x1",
"0x10",
"0x8000000000000110000000000",
"0xffffffffffffff",
"0xffffffffefff12380777abcd",
];

for number_str in numbers_str {
assert_eq!(
felt_to_bigint(Felt::from_hex(number_str).unwrap()),
BigInt::from_str_radix(&number_str[2..], 16).unwrap()
);

assert_eq!(
felt_to_biguint(Felt::from_hex(number_str).unwrap()),
BigUint::from_str_radix(&number_str[2..], 16).unwrap()
);
}
}
}