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

refactor:Tidy up scalar module #251

Merged
merged 3 commits into from
Oct 10, 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
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
Loading