From a30c7d6d62279b547bbcd465fb67f628d8b53e10 Mon Sep 17 00:00:00 2001 From: winrhcp Date: Sun, 20 Oct 2024 02:54:35 +0700 Subject: [PATCH] refactor:Tidy up scalar module (#255) # 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. --- crates/proof-of-sql/src/base/mod.rs | 1 + crates/proof-of-sql/src/base/scalar/mod.rs | 81 +------------------ crates/proof-of-sql/src/base/scalar/scalar.rs | 80 ++++++++++++++++++ 3 files changed, 84 insertions(+), 78 deletions(-) create mode 100644 crates/proof-of-sql/src/base/scalar/scalar.rs diff --git a/crates/proof-of-sql/src/base/mod.rs b/crates/proof-of-sql/src/base/mod.rs index f8fccd6a3..ad5573639 100644 --- a/crates/proof-of-sql/src/base/mod.rs +++ b/crates/proof-of-sql/src/base/mod.rs @@ -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}; diff --git a/crates/proof-of-sql/src/base/scalar/mod.rs b/crates/proof-of-sql/src/base/scalar/mod.rs index 1759554f3..c4af60f39 100644 --- a/crates/proof-of-sql/src/base/scalar/mod.rs +++ b/crates/proof-of-sql/src/base/scalar/mod.rs @@ -1,11 +1,11 @@ -//! 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 @@ -13,78 +13,3 @@ pub(crate) use mont_scalar::MontScalar; 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 - + 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 - + core::convert::TryInto - + core::convert::TryInto - + core::convert::TryInto - + core::convert::TryInto - + core::convert::TryInto - + core::convert::Into<[u64; 4]> - + core::convert::From<[u64; 4]> - + core::cmp::Ord - + core::ops::Neg - + 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> // 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 - + core::convert::From - + core::convert::From - + core::convert::From - + core::convert::From - + core::convert::From - + core::convert::From - + core::convert::Into - + TryFrom -{ - /// 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, - } - } -} diff --git a/crates/proof-of-sql/src/base/scalar/scalar.rs b/crates/proof-of-sql/src/base/scalar/scalar.rs new file mode 100644 index 000000000..38c77cdea --- /dev/null +++ b/crates/proof-of-sql/src/base/scalar/scalar.rs @@ -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 + + 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 + + core::convert::TryInto + + core::convert::TryInto + + core::convert::TryInto + + core::convert::TryInto + + core::convert::TryInto + + core::convert::Into<[u64; 4]> + + core::convert::From<[u64; 4]> + + core::cmp::Ord + + core::ops::Neg + + 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> // 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 + + core::convert::From + + core::convert::From + + core::convert::From + + core::convert::From + + core::convert::From + + core::convert::From + + core::convert::Into + + TryFrom +{ + /// 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, + } + } +}