Skip to content

Commit

Permalink
feat: add Into<BigInt> to Scalar & improvement to Scalar in gen…
Browse files Browse the repository at this point in the history
…eral (#78)

# Rationale for this change
This PR is a prerequisite of #77. 
<!--
Why are you proposing this change? If this is already explained clearly
in the linked Jira ticket then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

# What changes are included in this PR?
- add implementation as a part of `scalar_conversion_to_int` macro which
is necessary since blanket implementation of `TryFrom` or `TryInto` is
not allowed for traits by Rust
- improve the macro so that `TryInto` is replaced with the more natural
`TryFrom`
- replace `std` with `core`
<!--
There is no need to duplicate the description in the ticket here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

# Are these changes tested?
Yes
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
  • Loading branch information
iajoiner authored Aug 2, 2024
1 parent 2fd9fe1 commit 3b131c7
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const MAX_SUPPORTED_I256: i256 = i256::from_parts(
pub fn convert_scalar_to_i256<S: Scalar>(val: &S) -> i256 {
let is_negative = val > &S::MAX_SIGNED;
let abs_scalar = if is_negative { -*val } else { *val };
let limbs = abs_scalar.into();
let limbs: [u64; 4] = abs_scalar.into();

let low = (limbs[0] as u128) | ((limbs[1] as u128) << 64);
let high = (limbs[2] as i128) | ((limbs[3] as i128) << 64);
Expand Down
140 changes: 78 additions & 62 deletions crates/proof-of-sql/src/base/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,44 @@ pub trait Scalar:
+ Sync
+ Send
+ num_traits::One
+ std::iter::Sum
+ std::iter::Product
+ core::iter::Sum
+ core::iter::Product
+ Sub<Output = Self>
+ Copy
+ std::ops::MulAssign
+ std::ops::AddAssign
+ core::ops::MulAssign
+ core::ops::AddAssign
+ num_traits::Zero
+ for<'a> std::convert::From<&'a Self> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> std::convert::From<&'a bool> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> std::convert::From<&'a i16> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> std::convert::From<&'a i32> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> std::convert::From<&'a i64> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> std::convert::From<&'a i128> // Required for `Column` to implement `MultilinearExtension`
+ std::convert::TryInto <i8>
+ std::convert::TryInto <i16>
+ std::convert::TryInto <i32>
+ std::convert::TryInto <i64>
+ std::convert::TryInto <i128>
+ std::convert::Into<[u64; 4]>
+ std::convert::From<[u64; 4]>
+ for<'a> core::convert::From<&'a Self> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> core::convert::From<&'a bool> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> core::convert::From<&'a i16> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> core::convert::From<&'a i32> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> core::convert::From<&'a i64> // Required for `Column` to implement `MultilinearExtension`
+ for<'a> core::convert::From<&'a i128> // Required for `Column` to implement `MultilinearExtension`
+ core::convert::TryInto <i8>
+ core::convert::TryInto <i16>
+ core::convert::TryInto <i32>
+ core::convert::TryInto <i64>
+ core::convert::TryInto <i128>
+ core::convert::Into<[u64; 4]>
+ core::convert::From<[u64; 4]>
+ core::cmp::Ord
+ std::ops::Neg<Output = Self>
+ core::ops::Neg<Output = Self>
+ num_traits::Zero
+ std::ops::AddAssign
+ core::ops::AddAssign
+ ark_serialize::CanonicalSerialize //This enables us to put `Scalar`s on the transcript
+ ark_std::UniformRand //This enables us to get `Scalar`s as challenges from the transcript
+ num_traits::Inv<Output = Option<Self>> // Note: `inv` should return `None` exactly when the element is zero.
+ std::ops::SubAssign
+ core::ops::SubAssign
+ super::ref_into::RefInto<[u64; 4]>
+ for<'a> std::convert::From<&'a String>
+ for<'a> core::convert::From<&'a String>
+ super::encode::VarInt
+ std::convert::From<String>
+ std::convert::From<i128>
+ std::convert::From<i64>
+ std::convert::From<i32>
+ std::convert::From<i16>
+ std::convert::From<bool>
+ core::convert::From<String>
+ core::convert::From<i128>
+ core::convert::From<i64>
+ core::convert::From<i32>
+ core::convert::From<i16>
+ core::convert::From<bool>
+ core::convert::Into<BigInt>
+ TryFrom<BigInt, Error = ScalarConversionError>
{
/// The value (p - 1) / 2. This is "mid-point" of the field - the "six" on the clock.
Expand All @@ -90,114 +91,114 @@ pub trait Scalar:

macro_rules! scalar_conversion_to_int {
($scalar:ty) => {
impl TryInto<i8> for $scalar {
impl TryFrom<$scalar> for i8 {
type Error = ScalarConversionError;
fn try_into(self) -> Result<i8, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if self > Self::MAX_SIGNED {
(-1, (-self).into())
fn try_from(value: $scalar) -> Result<Self, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
(-1, (-value).into())
} else {
(1, self.into())
(1, value.into())
};
if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
return Err(ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i8",
self
value
)));
}
let val: i128 = sign * abs[0] as i128;
val.try_into().map_err(|_| {
ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i8",
self
value
))
})
}
}

impl TryInto<i16> for $scalar {
impl TryFrom<$scalar> for i16 {
type Error = ScalarConversionError;
fn try_into(self) -> Result<i16, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if self > Self::MAX_SIGNED {
(-1, (-self).into())
fn try_from(value: $scalar) -> Result<Self, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
(-1, (-value).into())
} else {
(1, self.into())
(1, value.into())
};
if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
return Err(ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i16",
self
value
)));
}
let val: i128 = sign * abs[0] as i128;
val.try_into().map_err(|_| {
ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i16",
self
value
))
})
}
}

impl TryInto<i32> for $scalar {
impl TryFrom<$scalar> for i32 {
type Error = ScalarConversionError;
fn try_into(self) -> Result<i32, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if self > Self::MAX_SIGNED {
(-1, (-self).into())
fn try_from(value: $scalar) -> Result<Self, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
(-1, (-value).into())
} else {
(1, self.into())
(1, value.into())
};
if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
return Err(ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i32",
self
value
)));
}
let val: i128 = sign * abs[0] as i128;
val.try_into().map_err(|_| {
ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i32",
self
value
))
})
}
}

impl TryInto<i64> for $scalar {
impl TryFrom<$scalar> for i64 {
type Error = ScalarConversionError;
fn try_into(self) -> Result<i64, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if self > Self::MAX_SIGNED {
(-1, (-self).into())
fn try_from(value: $scalar) -> Result<Self, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
(-1, (-value).into())
} else {
(1, self.into())
(1, value.into())
};
if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
return Err(ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i64",
self
value
)));
}
let val: i128 = sign * abs[0] as i128;
val.try_into().map_err(|_| {
ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i64",
self
value
))
})
}
}

impl TryInto<i128> for $scalar {
impl TryFrom<$scalar> for i128 {
type Error = ScalarConversionError;
fn try_into(self) -> Result<i128, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if self > Self::MAX_SIGNED {
(-1, (-self).into())
fn try_from(value: $scalar) -> Result<Self, Self::Error> {
let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
(-1, (-value).into())
} else {
(1, self.into())
(1, value.into())
};
if abs[2] != 0 || abs[3] != 0 {
return Err(ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i128",
self
value
)));
}
let val: u128 = (abs[1] as u128) << 64 | (abs[0] as u128);
Expand All @@ -207,11 +208,26 @@ macro_rules! scalar_conversion_to_int {
(-1, v) if v == i128::MAX as u128 + 1 => Ok(i128::MIN),
_ => Err(ScalarConversionError::Overflow(format!(
"{} is too large to fit in an i128",
self
value
))),
}
}
}

impl From<$scalar> for BigInt {
fn from(value: $scalar) -> Self {
// Since we wrap around in finite fields anything greater than the max signed value is negative
let is_negative = value > <$scalar>::MAX_SIGNED;
let sign = if is_negative {
num_bigint::Sign::Minus
} else {
num_bigint::Sign::Plus
};
let value_abs: [u64; 4] = (if is_negative { -value } else { value }).into();
let bits: &[u8] = bytemuck::cast_slice(&value_abs);
BigInt::from_bytes_le(sign, &bits)
}
}
};
}

Expand Down
Loading

0 comments on commit 3b131c7

Please sign in to comment.