Skip to content

Commit

Permalink
refactor:Tidy up scalar module (#255)
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?

Moved the Scalar trait to its own "scalar" file within base/scalar.
  • Loading branch information
winrhcp authored Oct 19, 2024
1 parent 863d9f3 commit a30c7d6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 78 deletions.
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod math;
pub(crate) mod polynomial;
pub(crate) mod proof;
pub(crate) mod ref_into;
/// This module contains the `Scalar` trait as well as the main, generic, implementations of it.
pub mod scalar;
mod serialize;
pub(crate) use serialize::{impl_serde_for_ark_serde_checked, impl_serde_for_ark_serde_unchecked};
Expand Down
81 changes: 3 additions & 78 deletions crates/proof-of-sql/src/base/scalar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,90 +1,15 @@
//! This module contains the definition of the `Scalar` trait, which is used to represent the scalar field used in Proof of SQL.
/// This module contains the definition of the `Scalar` trait, which is used to represent the scalar field used in Proof of SQL.
pub mod scalar;
pub use scalar::Scalar;
mod error;
pub use error::ScalarConversionError;
mod mont_scalar;
#[cfg(test)]
mod mont_scalar_test;
use alloc::string::String;
use core::{cmp::Ordering, ops::Sub};
pub use mont_scalar::Curve25519Scalar;
pub(crate) use mont_scalar::MontScalar;
/// Module for a test Scalar
#[cfg(test)]
pub mod test_scalar;
#[cfg(test)]
mod test_scalar_test;
use num_bigint::BigInt;

/// A trait for the scalar field used in Proof of SQL.
pub trait Scalar:
Clone
+ core::fmt::Debug
+ core::fmt::Display
+ PartialEq
+ Default
+ for<'a> From<&'a str>
+ Sync
+ Send
+ num_traits::One
+ core::iter::Sum
+ core::iter::Product
+ Sub<Output = Self>
+ Copy
+ core::ops::MulAssign
+ core::ops::AddAssign
+ num_traits::Zero
+ 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 i8> // 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`
+ for<'a> core::convert::From<&'a u8> // Required for `Column` to implement `MultilinearExtension`
+ core::convert::TryInto <bool>
+ 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
+ core::ops::Neg<Output = Self>
+ num_traits::Zero
+ 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.
+ core::ops::SubAssign
+ super::ref_into::RefInto<[u64; 4]>
+ for<'a> core::convert::From<&'a String>
+ super::encode::VarInt
+ core::convert::From<String>
+ core::convert::From<i128>
+ core::convert::From<i64>
+ core::convert::From<i32>
+ core::convert::From<i16>
+ core::convert::From<i8>
+ 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.
/// It is the largest signed value that can be represented in the field with the natural embedding.
const MAX_SIGNED: Self;
/// The 0 (additive identity) element of the field.
const ZERO: Self;
/// The 1 (multiplicative identity) element of the field.
const ONE: Self;
/// 1 + 1
const TWO: Self;
/// Compare two `Scalar`s as signed numbers.
fn signed_cmp(&self, other: &Self) -> Ordering {
match *self - *other {
x if x.is_zero() => Ordering::Equal,
x if x > Self::MAX_SIGNED => Ordering::Less,
_ => Ordering::Greater,
}
}
}
80 changes: 80 additions & 0 deletions crates/proof-of-sql/src/base/scalar/scalar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#![allow(clippy::module_inception)]

use crate::base::{encode::VarInt, ref_into::RefInto, scalar::ScalarConversionError};
use alloc::string::String;
use core::{cmp::Ordering, ops::Sub};
use num_bigint::BigInt;

/// A trait for the scalar field used in Proof of SQL.
pub trait Scalar:
Clone
+ core::fmt::Debug
+ core::fmt::Display
+ PartialEq
+ Default
+ for<'a> From<&'a str>
+ Sync
+ Send
+ num_traits::One
+ core::iter::Sum
+ core::iter::Product
+ Sub<Output = Self>
+ Copy
+ core::ops::MulAssign
+ core::ops::AddAssign
+ num_traits::Zero
+ 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 i8> // 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`
+ for<'a> core::convert::From<&'a u8> // Required for `Column` to implement `MultilinearExtension`
+ core::convert::TryInto <bool>
+ 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
+ core::ops::Neg<Output = Self>
+ num_traits::Zero
+ 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.
+ core::ops::SubAssign
+ RefInto<[u64; 4]>
+ for<'a> core::convert::From<&'a String>
+ VarInt
+ core::convert::From<String>
+ core::convert::From<i128>
+ core::convert::From<i64>
+ core::convert::From<i32>
+ core::convert::From<i16>
+ core::convert::From<i8>
+ 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.
/// It is the largest signed value that can be represented in the field with the natural embedding.
const MAX_SIGNED: Self;
/// The 0 (additive identity) element of the field.
const ZERO: Self;
/// The 1 (multiplicative identity) element of the field.
const ONE: Self;
/// 1 + 1
const TWO: Self;
/// Compare two `Scalar`s as signed numbers.
fn signed_cmp(&self, other: &Self) -> Ordering {
match *self - *other {
x if x.is_zero() => Ordering::Equal,
x if x > Self::MAX_SIGNED => Ordering::Less,
_ => Ordering::Greater,
}
}
}

0 comments on commit a30c7d6

Please sign in to comment.