Skip to content

Commit

Permalink
test: add tests for testing u256 conversion functions for TestScalar
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarttimwhite committed Jan 9, 2025
1 parent f91d910 commit 6499d81
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions crates/proof-of-sql/src/base/scalar/test_scalar_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use core::str::FromStr;

use crate::base::scalar::{test_scalar::TestScalar, Scalar};
use bnum::types::U256;
use num_traits::Inv;

use super::ScalarExt;

const MAX_TEST_SCALAR_SIGNED_VALUE_AS_STRING: &str = "3618502788666131106986593281521497120428558179689953803000975469142727125494";

#[test]
fn we_can_get_test_scalar_constants_from_z_p() {
assert_eq!(TestScalar::from(0), TestScalar::ZERO);
Expand All @@ -10,3 +17,51 @@ fn we_can_get_test_scalar_constants_from_z_p() {
assert_eq!(-TestScalar::TWO.inv().unwrap(), TestScalar::MAX_SIGNED);
assert_eq!(TestScalar::from(10), TestScalar::TEN);
}

#[test]
fn we_can_convert_u256_to_test_scalar_with_wrapping(){
// ARRANGE
let u256_value = U256::TWO;

// ACT
let test_scalar = TestScalar::from_wrapping(u256_value);

// ASSERT
assert_eq!(test_scalar, TestScalar::TWO);
}

#[test]
fn we_can_convert_u256_to_test_scalar_with_wrapping_of_large_value(){
// ARRANGE
let u256_value = U256::from_str(MAX_TEST_SCALAR_SIGNED_VALUE_AS_STRING).unwrap() * U256::TWO + U256::ONE;

// ACT
let test_scalar = TestScalar::from_wrapping(u256_value);

// ASSERT
assert_eq!(test_scalar, TestScalar::ZERO);
}

#[test]
fn we_can_convert_test_scalar_to_u256_with_wrapping(){
// ARRANGE
let test_scalar = TestScalar::TWO;

// ACT
let u256_value = test_scalar.into_u256_wrapping();

// ASSERT
assert_eq!(u256_value, U256::TWO);
}

#[test]
fn we_can_convert_test_scalar_to_256_with_wrapping_of_negative_value(){
// ARRANGE
let test_scalar = -TestScalar::ONE;

// ACT
let u256: bnum::BUint<4> = test_scalar.into_u256_wrapping();

// ASSERT
assert_eq!(u256, U256::from_str(MAX_TEST_SCALAR_SIGNED_VALUE_AS_STRING).unwrap() * U256::TWO);
}

0 comments on commit 6499d81

Please sign in to comment.