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

fix: Remove inner product types from generic test. #264

Merged
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
255 changes: 39 additions & 216 deletions crates/proof-of-sql/src/base/arrow/arrow_array_to_column_conversion.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
base::{
database::{owned_table_utility::*, OwnedColumn, OwnedTable},
map::IndexMap,
scalar::Curve25519Scalar,
scalar::test_scalar::TestScalar,
},
record_batch,
};
Expand All @@ -15,7 +15,7 @@ use arrow::{
};

fn we_can_convert_between_owned_column_and_array_ref_impl(
owned_column: &OwnedColumn<Curve25519Scalar>,
owned_column: &OwnedColumn<TestScalar>,
array_ref: ArrayRef,
) {
let ic_to_ar = ArrayRef::from(owned_column.clone());
Expand All @@ -26,19 +26,19 @@ fn we_can_convert_between_owned_column_and_array_ref_impl(
}
fn we_can_convert_between_boolean_owned_column_and_array_ref_impl(data: Vec<bool>) {
we_can_convert_between_owned_column_and_array_ref_impl(
&OwnedColumn::<Curve25519Scalar>::Boolean(data.clone()),
&OwnedColumn::<TestScalar>::Boolean(data.clone()),
Arc::new(BooleanArray::from(data)),
);
}
fn we_can_convert_between_bigint_owned_column_and_array_ref_impl(data: Vec<i64>) {
we_can_convert_between_owned_column_and_array_ref_impl(
&OwnedColumn::<Curve25519Scalar>::BigInt(data.clone()),
&OwnedColumn::<TestScalar>::BigInt(data.clone()),
Arc::new(Int64Array::from(data)),
);
}
fn we_can_convert_between_int128_owned_column_and_array_ref_impl(data: Vec<i128>) {
we_can_convert_between_owned_column_and_array_ref_impl(
&OwnedColumn::<Curve25519Scalar>::Int128(data.clone()),
&OwnedColumn::<TestScalar>::Int128(data.clone()),
Arc::new(
Decimal128Array::from(data)
.with_precision_and_scale(38, 0)
Expand All @@ -48,7 +48,7 @@ fn we_can_convert_between_int128_owned_column_and_array_ref_impl(data: Vec<i128>
}
fn we_can_convert_between_varchar_owned_column_and_array_ref_impl(data: Vec<String>) {
we_can_convert_between_owned_column_and_array_ref_impl(
&OwnedColumn::<Curve25519Scalar>::VarChar(data.clone()),
&OwnedColumn::<TestScalar>::VarChar(data.clone()),
Arc::new(StringArray::from(data)),
);
}
Expand All @@ -75,13 +75,13 @@ fn we_get_an_unsupported_type_error_when_trying_to_convert_from_a_float32_array_
) {
let array_ref: ArrayRef = Arc::new(Float32Array::from(vec![0.0]));
assert!(matches!(
OwnedColumn::<Curve25519Scalar>::try_from(array_ref),
OwnedColumn::<TestScalar>::try_from(array_ref),
Err(OwnedArrowConversionError::UnsupportedType { .. })
));
}

fn we_can_convert_between_owned_table_and_record_batch_impl(
owned_table: &OwnedTable<Curve25519Scalar>,
owned_table: &OwnedTable<TestScalar>,
record_batch: &RecordBatch,
) {
let it_to_rb = RecordBatch::try_from(owned_table.clone()).unwrap();
Expand All @@ -93,7 +93,7 @@ fn we_can_convert_between_owned_table_and_record_batch_impl(
#[test]
fn we_can_convert_between_owned_table_and_record_batch() {
we_can_convert_between_owned_table_and_record_batch_impl(
&OwnedTable::<Curve25519Scalar>::try_new(IndexMap::default()).unwrap(),
&OwnedTable::<TestScalar>::try_new(IndexMap::default()).unwrap(),
&RecordBatch::new_empty(Arc::new(Schema::empty())),
);
we_can_convert_between_owned_table_and_record_batch_impl(
Expand Down Expand Up @@ -136,14 +136,14 @@ fn we_cannot_convert_a_record_batch_if_it_has_repeated_column_names() {
"A" => [0_i128; 0],
);
assert!(matches!(
OwnedTable::<Curve25519Scalar>::try_from(record_batch),
OwnedTable::<TestScalar>::try_from(record_batch),
Err(OwnedArrowConversionError::DuplicateIdentifiers)
));
}

#[test]
#[should_panic(expected = "not implemented: Cannot convert Scalar type to arrow type")]
fn we_panic_when_converting_an_owned_table_with_a_scalar_column() {
let owned_table = owned_table::<Curve25519Scalar>([scalar("a", [0; 0])]);
let owned_table = owned_table::<TestScalar>([scalar("a", [0; 0])]);
let _ = RecordBatch::try_from(owned_table);
}
112 changes: 57 additions & 55 deletions crates/proof-of-sql/src/base/arrow/scalar_and_i256_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ pub fn convert_i256_to_scalar<S: Scalar>(value: &i256) -> Option<S> {

#[cfg(test)]
mod tests {
use super::{
convert_i256_to_scalar, convert_scalar_to_i256, MAX_SUPPORTED_I256, MIN_SUPPORTED_I256,

use super::{convert_i256_to_scalar, convert_scalar_to_i256};
use crate::base::{
database::scalar_and_i256_conversions::{MAX_SUPPORTED_I256, MIN_SUPPORTED_I256},
scalar::{test_scalar::TestScalar, Curve25519Scalar, Scalar},
};
use crate::base::scalar::{Curve25519Scalar, Scalar};
use arrow::datatypes::i256;
use num_traits::Zero;
use rand::RngCore;
Expand All @@ -66,7 +68,7 @@ mod tests {
fn random_i256<R: RngCore + ?Sized>(rng: &mut R) -> i256 {
use rand::Rng;
let max_signed_as_parts: (u128, i128) =
convert_scalar_to_i256(&Curve25519Scalar::MAX_SIGNED).to_parts();
convert_scalar_to_i256(&TestScalar::MAX_SIGNED).to_parts();

// Generate a random high part
let high: i128 = rng.gen_range(-max_signed_as_parts.1..=max_signed_as_parts.1);
Expand All @@ -81,117 +83,117 @@ mod tests {
i256::from_parts(low, high)
}

impl TryFrom<i256> for Curve25519Scalar {
impl TryFrom<i256> for TestScalar {
type Error = ();

// Must fit inside 252 bits and so requires fallible
fn try_from(value: i256) -> Result<Self, ()> {
convert_i256_to_scalar(&value).ok_or(())
}
}

impl From<Curve25519Scalar> for i256 {
fn from(value: Curve25519Scalar) -> Self {
convert_scalar_to_i256(&value)
}
}

impl From<TestScalar> for i256 {
fn from(value: TestScalar) -> Self {
convert_scalar_to_i256(&value)
}
}

#[test]
fn test_curve25519scalar_to_i256_conversion() {
let positive_scalar = Curve25519Scalar::from(12345);
fn test_testscalar_to_i256_conversion() {
let positive_scalar = TestScalar::from(12345);
let expected_i256 = i256::from(12345);
assert_eq!(i256::from(positive_scalar), expected_i256);

let negative_scalar = Curve25519Scalar::from(-12345);
let negative_scalar = TestScalar::from(-12345);
let expected_i256 = i256::from(-12345);
assert_eq!(i256::from(negative_scalar), expected_i256);

let max_scalar = Curve25519Scalar::MAX_SIGNED;
let expected_max = i256::from(Curve25519Scalar::MAX_SIGNED);
let max_scalar = TestScalar::MAX_SIGNED;
let expected_max = i256::from(TestScalar::MAX_SIGNED);
assert_eq!(i256::from(max_scalar), expected_max);

let min_scalar = Curve25519Scalar::from(0);
let expected_min = i256::from(Curve25519Scalar::from(0));
let min_scalar = TestScalar::from(0);
let expected_min = i256::from(TestScalar::from(0));
assert_eq!(i256::from(min_scalar), expected_min);
}

#[test]
fn test_curve25519scalar_i256_overflow_and_underflow() {
fn test_testscalar_i256_overflow_and_underflow() {
// 2^256 overflows
assert!(Curve25519Scalar::try_from(i256::MAX).is_err());
assert!(TestScalar::try_from(i256::MAX).is_err());

// MAX_SIGNED + 1 overflows
assert!(Curve25519Scalar::try_from(MAX_SUPPORTED_I256 + i256::from(1)).is_err());
assert!(TestScalar::try_from(MAX_SUPPORTED_I256 + i256::from(1)).is_err());

// -2^255 underflows
assert!(i256::MIN < -(i256::from(Curve25519Scalar::MAX_SIGNED)));
assert!(Curve25519Scalar::try_from(i256::MIN).is_err());
assert!(i256::MIN < -(i256::from(TestScalar::MAX_SIGNED)));
assert!(TestScalar::try_from(i256::MIN).is_err());

// -MAX-SIGNED - 1 underflows
assert!(Curve25519Scalar::try_from(MIN_SUPPORTED_I256 - i256::from(1)).is_err());
assert!(TestScalar::try_from(MIN_SUPPORTED_I256 - i256::from(1)).is_err());
}

#[test]
fn test_i256_curve25519scalar_negative() {
// Test conversion from i256(-1) to Curve25519Scalar
let neg_one_i256_curve25519scalar = Curve25519Scalar::try_from(i256::from(-1));
assert!(neg_one_i256_curve25519scalar.is_ok());
let neg_one_curve25519scalar = Curve25519Scalar::from(-1);
assert_eq!(
neg_one_i256_curve25519scalar.unwrap(),
neg_one_curve25519scalar
);
fn test_i256_testscalar_negative() {
// Test conversion from i256(-1) to TestScalar
let neg_one_i256_testscalar = TestScalar::try_from(i256::from(-1));
assert!(neg_one_i256_testscalar.is_ok());
let neg_one_testscalar = TestScalar::from(-1);
assert_eq!(neg_one_i256_testscalar.unwrap(), neg_one_testscalar);
}

#[test]
fn test_i256_curve25519scalar_zero() {
// Test conversion from i256(0) to Curve25519Scalar
let zero_i256_curve25519scalar = Curve25519Scalar::try_from(i256::from(0));
assert!(zero_i256_curve25519scalar.is_ok());
let zero_curve25519scalar = Curve25519Scalar::zero();
assert_eq!(zero_i256_curve25519scalar.unwrap(), zero_curve25519scalar);
fn test_i256_testscalar_zero() {
// Test conversion from i256(0) to TestScalar
let zero_i256_testscalar = TestScalar::try_from(i256::from(0));
assert!(zero_i256_testscalar.is_ok());
let zero_testscalar = TestScalar::zero();
assert_eq!(zero_i256_testscalar.unwrap(), zero_testscalar);
}

#[test]
fn test_i256_curve25519scalar_positive() {
// Test conversion from i256(42) to Curve25519Scalar
let forty_two_i256_curve25519scalar = Curve25519Scalar::try_from(i256::from(42));
let forty_two_curve25519scalar = Curve25519Scalar::from(42);
assert_eq!(
forty_two_i256_curve25519scalar.unwrap(),
forty_two_curve25519scalar
);
fn test_i256_testscalar_positive() {
// Test conversion from i256(42) to TestScalar
let forty_two_i256_testscalar = TestScalar::try_from(i256::from(42));
let forty_two_testscalar = TestScalar::from(42);
assert_eq!(forty_two_i256_testscalar.unwrap(), forty_two_testscalar);
}

#[test]
fn test_i256_curve25519scalar_max_signed() {
fn test_i256_testscalar_max_signed() {
let max_signed = MAX_SUPPORTED_I256;
// max signed value
let max_signed_scalar = Curve25519Scalar::MAX_SIGNED;
// Test conversion from i256 to Curve25519Scalar
let i256_curve25519scalar = Curve25519Scalar::try_from(max_signed);
assert!(i256_curve25519scalar.is_ok());
assert_eq!(i256_curve25519scalar.unwrap(), max_signed_scalar);
let max_signed_scalar = TestScalar::MAX_SIGNED;
// Test conversion from i256 to TestScalar
let i256_testscalar = TestScalar::try_from(max_signed);
assert!(i256_testscalar.is_ok());
assert_eq!(i256_testscalar.unwrap(), max_signed_scalar);
}

#[test]
fn test_i256_curve25519scalar_min_signed() {
fn test_i256_testscalar_min_signed() {
let min_signed = MIN_SUPPORTED_I256;
let i256_curve25519scalar = Curve25519Scalar::try_from(min_signed);
let i256_testscalar = TestScalar::try_from(min_signed);
// -MAX_SIGNED is ok
assert!(i256_curve25519scalar.is_ok());
assert!(i256_testscalar.is_ok());
assert_eq!(
i256_curve25519scalar.unwrap(),
Curve25519Scalar::MAX_SIGNED + Curve25519Scalar::from(1)
i256_testscalar.unwrap(),
TestScalar::MAX_SIGNED + TestScalar::from(1)
);
}

#[test]
fn test_i256_curve25519scalar_random() {
fn test_i256_testscalar_random() {
let mut rng = rand::thread_rng();
for _ in 0..1000 {
let i256_value = random_i256(&mut rng);
let curve25519_scalar =
Curve25519Scalar::try_from(i256_value).expect("Conversion failed");
let curve25519_scalar = TestScalar::try_from(i256_value).expect("Conversion failed");
let back_to_i256 = i256::from(curve25519_scalar);
assert_eq!(i256_value, back_to_i256, "Round-trip conversion failed");
}
Expand Down
Loading
Loading