Skip to content

Commit

Permalink
refactor:Tidy up scalar module (#251)
Browse files Browse the repository at this point in the history
# Rationale for this change
MontScalar and Scalar logic is currently disorganized. We should consolidate this logic.

# What changes are included in this PR?
Combined logic held within the four files "mont_scalar", "mont_scalar_from", "mont_scalar_test", "mont_scalar_from_test" into the files "mont_scalar" and "mont_scalar_test"
  • Loading branch information
winrhcp authored Oct 10, 2024
1 parent cc8e2f0 commit 69e27c7
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 252 deletions.
3 changes: 0 additions & 3 deletions crates/proof-of-sql/src/base/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use alloc::string::String;
use core::{cmp::Ordering, ops::Sub};
pub use mont_scalar::Curve25519Scalar;
pub(crate) use mont_scalar::MontScalar;
mod mont_scalar_from;
#[cfg(test)]
mod mont_scalar_from_test;
/// Module for a test Scalar
#[cfg(test)]
pub mod test_scalar;
Expand Down
64 changes: 62 additions & 2 deletions crates/proof-of-sql/src/base/scalar/mont_scalar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Scalar, ScalarConversionError};
use crate::base::math::decimal::MAX_SUPPORTED_PRECISION;
use alloc::{format, vec::Vec};
use alloc::{format, string::String, vec::Vec};
use ark_ff::{BigInteger, Field, Fp, Fp256, MontBackend, MontConfig, PrimeField};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use bytemuck::TransparentWrapper;
Expand All @@ -13,7 +13,7 @@ use core::{
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use num_bigint::BigInt;
use num_traits::Signed;
use num_traits::{Signed, Zero};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(CanonicalSerialize, CanonicalDeserialize, TransparentWrapper)]
Expand Down Expand Up @@ -117,6 +117,66 @@ impl<T: MontConfig<4>> Ord for MontScalar<T> {
// end replacement for #[derive(...)]
// --------------------------------------------------------------------------------

/// TODO: add docs
macro_rules! impl_from_for_mont_scalar_for_type_supported_by_from {
($tt:ty) => {
impl<T: MontConfig<4>> From<$tt> for MontScalar<T> {
fn from(x: $tt) -> Self {
Self(x.into())
}
}
};
}

/// Implement `From<&[u8]>` for `MontScalar`
impl<T: MontConfig<4>> From<&[u8]> for MontScalar<T> {
fn from(x: &[u8]) -> Self {
if x.is_empty() {
return Self::zero();
}

let hash = blake3::hash(x);
let mut bytes: [u8; 32] = hash.into();
bytes[31] &= 0b0000_1111_u8;

Self::from_le_bytes_mod_order(&bytes)
}
}

/// TODO: add docs
macro_rules! impl_from_for_mont_scalar_for_string {
($tt:ty) => {
impl<T: MontConfig<4>> From<$tt> for MontScalar<T> {
fn from(x: $tt) -> Self {
x.as_bytes().into()
}
}
};
}

impl_from_for_mont_scalar_for_type_supported_by_from!(bool);
impl_from_for_mont_scalar_for_type_supported_by_from!(u8);
impl_from_for_mont_scalar_for_type_supported_by_from!(u16);
impl_from_for_mont_scalar_for_type_supported_by_from!(u32);
impl_from_for_mont_scalar_for_type_supported_by_from!(u64);
impl_from_for_mont_scalar_for_type_supported_by_from!(u128);
impl_from_for_mont_scalar_for_type_supported_by_from!(i8);
impl_from_for_mont_scalar_for_type_supported_by_from!(i16);
impl_from_for_mont_scalar_for_type_supported_by_from!(i32);
impl_from_for_mont_scalar_for_type_supported_by_from!(i64);
impl_from_for_mont_scalar_for_type_supported_by_from!(i128);
impl_from_for_mont_scalar_for_string!(&str);
impl_from_for_mont_scalar_for_string!(String);

impl<F: MontConfig<4>, T> From<&T> for MontScalar<F>
where
T: Into<MontScalar<F>> + Clone,
{
fn from(x: &T) -> Self {
x.clone().into()
}
}

/// A wrapper type around the field element `ark_curve25519::Fr` and should be used in place of `ark_curve25519::Fr`.
///
/// Using the `Scalar` trait rather than this type is encouraged to allow for easier switching of the underlying field.
Expand Down
61 changes: 0 additions & 61 deletions crates/proof-of-sql/src/base/scalar/mont_scalar_from.rs

This file was deleted.

183 changes: 0 additions & 183 deletions crates/proof-of-sql/src/base/scalar/mont_scalar_from_test.rs

This file was deleted.

Loading

0 comments on commit 69e27c7

Please sign in to comment.