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 #255

Merged
merged 29 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
42f4a5f
move trait Scalar
winrhcp Oct 10, 2024
221e25d
update VarInt
winrhcp Oct 10, 2024
fd1f519
update scalar
winrhcp Oct 10, 2024
3b22ecd
rename scalar file
winrhcp Oct 10, 2024
d2c86e6
update scalar mod
winrhcp Oct 10, 2024
9ef1192
add doc for module
winrhcp Oct 10, 2024
1f91f2e
typo
winrhcp Oct 10, 2024
0c84add
update
winrhcp Oct 10, 2024
754bf8e
fix formatting
winrhcp Oct 10, 2024
4bf354b
fix format
winrhcp Oct 10, 2024
e85630b
add blank line
winrhcp Oct 10, 2024
e204b20
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 11, 2024
686b4b9
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 11, 2024
a3b015d
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 11, 2024
0312b17
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 11, 2024
b634d74
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 12, 2024
ec0e640
rename
winrhcp Oct 14, 2024
9b8de3d
Merge branch 'winrhcp-issue-234' of github.com:winrhcp/sxt-proof-of-s…
winrhcp Oct 14, 2024
f61a492
add allow module_inception
winrhcp Oct 14, 2024
03b734c
typo
winrhcp Oct 14, 2024
6a46a21
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 16, 2024
856a58a
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 16, 2024
2a6043f
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 17, 2024
b277abc
Update crates/proof-of-sql/src/base/mod.rs
winrhcp Oct 19, 2024
e4275a0
Merge branch 'main' into winrhcp-issue-234
winrhcp Oct 19, 2024
40ae005
Update crates/proof-of-sql/src/base/scalar/mod.rs
winrhcp Oct 19, 2024
03dde11
update ref_into
winrhcp Oct 19, 2024
4dfba04
format
winrhcp Oct 19, 2024
bc27751
Merge branch 'main' into winrhcp-issue-234
JayWhite2357 Oct 19, 2024
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
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,
}
}
}
Loading