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

BigUint wraps BigInt object #1689

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/base/src/io/finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
FA::finish_api_impl().finish_managed_buffer_raw(managed_buffer.handle.clone());
Ok(())
} else if let Some(big_uint) = value.try_cast_ref::<BigUint<FA>>() {
FA::finish_api_impl().finish_big_uint_raw(big_uint.handle.clone());
FA::finish_api_impl().finish_big_uint_raw(big_uint.value.handle.clone());
Ok(())
} else if let Some(big_int) = value.try_cast_ref::<BigInt<FA>>() {
FA::finish_api_impl().finish_big_int_raw(big_int.handle.clone());
Expand Down
2 changes: 1 addition & 1 deletion framework/base/src/types/managed/basic/big_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<M: ManagedTypeApi> BigFloat<M> {
pub fn from_big_uint(big_uint: &BigUint<M>) -> Self {
let new_bf_handle: M::BigFloatHandle =
use_raw_handle(M::static_var_api_impl().next_handle());
M::managed_type_impl().bf_set_bi(new_bf_handle.clone(), big_uint.handle.clone());
M::managed_type_impl().bf_set_bi(new_bf_handle.clone(), big_uint.value.handle.clone());
BigFloat::from_handle(new_bf_handle)
}

Expand Down
4 changes: 2 additions & 2 deletions framework/base/src/types/managed/basic/big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ impl<M: ManagedTypeApi> BigInt<M> {
pub fn from_biguint(sign: Sign, unsigned: BigUint<M>) -> Self {
let api = M::managed_type_impl();
if sign.is_minus() {
api.bi_neg(unsigned.handle.clone(), unsigned.handle.clone());
api.bi_neg(unsigned.value.handle.clone(), unsigned.value.handle.clone());
}
BigInt::from_handle(unsigned.handle)
BigInt::from_handle(unsigned.value.handle)
}

/// Returns the sign of the `BigInt` as a `Sign`.
Expand Down
31 changes: 20 additions & 11 deletions framework/base/src/types/managed/basic/big_int_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ impl<M: ManagedTypeApi> Ord for BigInt<M> {
}
}

impl<M: ManagedTypeApi> PartialEq<i64> for BigInt<M> {
#[inline]
fn eq(&self, other: &i64) -> bool {
cmp_i64(self, *other).is_eq()
}
macro_rules! partial_eq_and_ord {
($small_int_type:ident) => {
impl<M: ManagedTypeApi> PartialEq<$small_int_type> for BigInt<M> {
#[inline]
fn eq(&self, other: &$small_int_type) -> bool {
cmp_i64(self, *other as i64).is_eq()
}
}

impl<M: ManagedTypeApi> PartialOrd<$small_int_type> for BigInt<M> {
#[inline]
fn partial_cmp(&self, other: &$small_int_type) -> Option<Ordering> {
Some(cmp_i64(self, *other as i64))
}
}
};
}

impl<M: ManagedTypeApi> PartialOrd<i64> for BigInt<M> {
#[inline]
fn partial_cmp(&self, other: &i64) -> Option<Ordering> {
Some(cmp_i64(self, *other))
}
}
partial_eq_and_ord! {i32}
partial_eq_and_ord! {i64}
partial_eq_and_ord! {u32}
partial_eq_and_ord! {u64}
13 changes: 2 additions & 11 deletions framework/base/src/types/managed/basic/big_num_cmp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use core::{cmp::Ordering, convert::TryInto};
use core::cmp::Ordering;

use crate::{
api::{const_handles, BigIntApiImpl, ManagedTypeApi},
types::ManagedType,
};

use super::{cast_to_i64::cast_to_i64, BigInt};
use super::BigInt;

pub(crate) fn cmp_i64<M, B>(bi: &B, other: i64) -> Ordering
where
Expand All @@ -24,12 +24,3 @@ where
api.bi_cmp(bi.get_handle(), big_int_temp_1)
}
}

pub(crate) fn cmp_conv_i64<M, B, T>(bi: &B, other: T) -> Ordering
where
M: ManagedTypeApi,
B: ManagedType<M, OwnHandle = M::BigIntHandle>,
T: TryInto<i64>,
{
cmp_i64(bi, cast_to_i64::<M, _>(other))
}
46 changes: 29 additions & 17 deletions framework/base/src/types/managed/basic/elliptic_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {
x_result_handle.clone(),
y_result_handle.clone(),
self.handle.clone(),
x_first_point.handle,
y_first_point.handle,
x_second_point.handle,
y_second_point.handle,
x_first_point.value.handle,
y_first_point.value.handle,
x_second_point.value.handle,
y_second_point.value.handle,
);
(
BigUint::from_handle(x_result_handle),
Expand All @@ -137,8 +137,8 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {
x_result_handle.clone(),
y_result_handle.clone(),
self.handle.clone(),
x_point.handle,
y_point.handle,
x_point.value.handle,
y_point.value.handle,
);
(
BigUint::from_handle(x_result_handle),
Expand All @@ -148,7 +148,11 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {

pub fn is_on_curve(&self, x_point: BigUint<M>, y_point: BigUint<M>) -> bool {
let api = M::managed_type_impl();
api.ec_is_on_curve(self.handle.clone(), x_point.handle, y_point.handle)
api.ec_is_on_curve(
self.handle.clone(),
x_point.value.handle,
y_point.value.handle,
)
}

#[deprecated(since = "0.41.0", note = "Please use method `scalar_mult` instead.")]
Expand All @@ -165,8 +169,8 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {
x_result_handle.clone(),
y_result_handle.clone(),
self.handle.clone(),
x_point.handle,
y_point.handle,
x_point.value.handle,
y_point.value.handle,
data,
);
(
Expand All @@ -188,8 +192,8 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {
x_result_handle.clone(),
y_result_handle.clone(),
self.handle.clone(),
x_point.handle,
y_point.handle,
x_point.value.handle,
y_point.value.handle,
data.get_handle(),
);
(
Expand Down Expand Up @@ -242,16 +246,20 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {
y_pair: BigUint<M>,
) -> crate::types::heap::BoxedBytes {
let api = M::managed_type_impl();
api.ec_marshal_legacy(self.handle.clone(), x_pair.handle, y_pair.handle)
api.ec_marshal_legacy(
self.handle.clone(),
x_pair.value.handle,
y_pair.value.handle,
)
}

pub fn marshal(&self, x_pair: BigUint<M>, y_pair: BigUint<M>) -> ManagedBuffer<M> {
let result_handle: M::ManagedBufferHandle =
use_raw_handle(M::static_var_api_impl().next_handle());
M::managed_type_impl().ec_marshal(
self.handle.clone(),
x_pair.handle,
y_pair.handle,
x_pair.value.handle,
y_pair.value.handle,
result_handle.clone(),
);
ManagedBuffer::from_handle(result_handle)
Expand All @@ -268,16 +276,20 @@ impl<M: ManagedTypeApi> EllipticCurve<M> {
y_pair: BigUint<M>,
) -> crate::types::heap::BoxedBytes {
let api = M::managed_type_impl();
api.ec_marshal_compressed_legacy(self.handle.clone(), x_pair.handle, y_pair.handle)
api.ec_marshal_compressed_legacy(
self.handle.clone(),
x_pair.value.handle,
y_pair.value.handle,
)
}

pub fn marshal_compressed(&self, x_pair: BigUint<M>, y_pair: BigUint<M>) -> ManagedBuffer<M> {
let result_handle: M::ManagedBufferHandle =
use_raw_handle(M::static_var_api_impl().next_handle());
M::managed_type_impl().ec_marshal_compressed(
self.handle.clone(),
x_pair.handle,
y_pair.handle,
x_pair.value.handle,
y_pair.value.handle,
result_handle.clone(),
);
ManagedBuffer::from_handle(result_handle)
Expand Down
6 changes: 1 addition & 5 deletions framework/base/src/types/managed/basic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ mod big_int_cmp;
mod big_int_operators;
mod big_int_sign;
mod big_num_cmp;
mod big_uint;
mod big_uint_cmp;
mod big_uint_operators;
mod cast_to_i64;
pub(crate) mod cast_to_i64;
mod elliptic_curve;
mod managed_buffer;
mod managed_map;

pub use big_float::BigFloat;
pub use big_int::BigInt;
pub use big_int_sign::Sign;
pub use big_uint::BigUint;
pub use elliptic_curve::{EllipticCurve, EllipticCurveComponents};
pub use managed_buffer::ManagedBuffer;
pub use managed_map::ManagedMap;
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ use crate::{
contract_base::ErrorHelper,
formatter::{hex_util::encode_bytes_as_hex, FormatBuffer, FormatByteReceiver, SCDisplay},
types::{
heap::BoxedBytes, ConstDecimals, Decimals, ManagedBuffer, ManagedBufferCachedBuilder,
ManagedDecimal, ManagedRef, ManagedType,
heap::BoxedBytes, BigInt, ConstDecimals, Decimals, ManagedBuffer,
ManagedBufferCachedBuilder, ManagedDecimal, ManagedRef, ManagedType,
},
};

use super::cast_to_i64::cast_to_i64;

#[repr(transparent)]
pub struct BigUint<M: ManagedTypeApi> {
pub(crate) handle: M::BigIntHandle,
pub(crate) value: BigInt<M>,
}

impl<M: ManagedTypeApi> ManagedType<M> for BigUint<M> {
type OwnHandle = M::BigIntHandle;

fn from_handle(handle: M::BigIntHandle) -> Self {
BigUint { handle }
BigUint {
value: BigInt::from_handle(handle),
}
}

fn get_handle(&self) -> M::BigIntHandle {
self.handle.clone()
self.value.handle.clone()
}

fn transmute_from_handle_ref(handle_ref: &M::BigIntHandle) -> &Self {
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<M: ManagedTypeApi> BigUint<M> {
where
T: TryInto<i64> + num_traits::Unsigned,
{
M::managed_type_impl().bi_set_int64(handle, cast_to_i64::<M, _>(value));
BigInt::<M>::set_value(handle, value);
}

pub(crate) fn new_from_num<T>(value: T) -> Self
Expand Down Expand Up @@ -175,12 +175,12 @@ impl<M: ManagedTypeApi> BigUint<M> {
#[inline]
pub fn to_u64(&self) -> Option<u64> {
let api = M::managed_type_impl();
api.bi_to_i64(self.handle.clone()).map(|bi| bi as u64)
api.bi_to_i64(self.value.handle.clone()).map(|bi| bi as u64)
}

#[inline]
pub fn overwrite_u64(&mut self, value: u64) {
Self::set_value(self.handle.clone(), value);
Self::set_value(self.value.handle.clone(), value);
}

#[inline]
Expand All @@ -195,7 +195,8 @@ impl<M: ManagedTypeApi> BigUint<M> {
#[inline]
pub fn to_bytes_be(&self) -> BoxedBytes {
let mb_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1);
M::managed_type_impl().mb_from_big_int_unsigned(self.handle.clone(), mb_handle.clone());
M::managed_type_impl()
.mb_from_big_int_unsigned(self.value.handle.clone(), mb_handle.clone());
M::managed_type_impl().mb_to_boxed_bytes(mb_handle)
}

Expand All @@ -211,7 +212,8 @@ impl<M: ManagedTypeApi> BigUint<M> {
pub fn to_bytes_be_buffer(&self) -> ManagedBuffer<M> {
let mb_handle: M::ManagedBufferHandle =
use_raw_handle(M::static_var_api_impl().next_handle());
M::managed_type_impl().mb_from_big_int_unsigned(self.handle.clone(), mb_handle.clone());
M::managed_type_impl()
.mb_from_big_int_unsigned(self.value.handle.clone(), mb_handle.clone());
ManagedBuffer::from_handle(mb_handle)
}
}
Expand All @@ -222,22 +224,26 @@ impl<M: ManagedTypeApi> BigUint<M> {
pub fn sqrt(&self) -> Self {
let api = M::managed_type_impl();
let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle());
api.bi_sqrt(result_handle.clone(), self.handle.clone());
api.bi_sqrt(result_handle.clone(), self.value.handle.clone());
BigUint::from_handle(result_handle)
}

#[must_use]
pub fn pow(&self, exp: u32) -> Self {
let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle());
let big_int_temp_1 = BigUint::<M>::make_temp(const_handles::BIG_INT_TEMPORARY_1, exp);
M::managed_type_impl().bi_pow(result_handle.clone(), self.handle.clone(), big_int_temp_1);
M::managed_type_impl().bi_pow(
result_handle.clone(),
self.value.handle.clone(),
big_int_temp_1,
);
BigUint::from_handle(result_handle)
}

#[inline]
pub fn log2(&self) -> u32 {
let api = M::managed_type_impl();
api.bi_log2(self.handle.clone())
api.bi_log2(self.value.handle.clone())
}

/// Natural logarithm of a number.
Expand Down Expand Up @@ -299,7 +305,7 @@ impl<M: ManagedTypeApi> Clone for BigUint<M> {
api.bi_add(
clone_handle.clone(),
clone_handle.clone(),
self.handle.clone(),
self.value.handle.clone(),
);
BigUint::from_handle(clone_handle)
}
Expand Down Expand Up @@ -366,7 +372,7 @@ impl<M: ManagedTypeApi> TopDecode for BigUint<M> {
impl<M: ManagedTypeApi> SCDisplay for BigUint<M> {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
let str_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1);
M::managed_type_impl().bi_to_string(self.handle.clone(), str_handle.clone());
M::managed_type_impl().bi_to_string(self.value.handle.clone(), str_handle.clone());
f.append_managed_buffer(&ManagedBuffer::from_handle(
str_handle.cast_or_signal_error::<M, _>(),
));
Expand All @@ -385,7 +391,7 @@ impl<M: ManagedTypeApi> BigUint<M> {
impl<M: ManagedTypeApi> core::fmt::Debug for BigUint<M> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BigUint")
.field("handle", &self.handle.clone())
.field("handle", &self.value.handle.clone())
.field(
"hex-value-be",
&encode_bytes_as_hex(self.to_bytes_be().as_slice()),
Expand Down
Loading
Loading