Skip to content

Commit

Permalink
refactor: removal of std references for initial no_std support (#172
Browse files Browse the repository at this point in the history
)
  • Loading branch information
iajoiner authored Sep 22, 2024
2 parents cf5ec19 + 25014de commit 067d7c9
Show file tree
Hide file tree
Showing 32 changed files with 91 additions and 33 deletions.
2 changes: 2 additions & 0 deletions crates/proof-of-sql/src/base/commitment/column_bounds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::committable_column::CommittableColumn;
use alloc::boxed::Box;
use serde::{Deserialize, Serialize};
use thiserror::Error;

Expand Down Expand Up @@ -290,6 +291,7 @@ impl ColumnBounds {
mod tests {
use super::*;
use crate::base::{database::OwnedColumn, math::decimal::Precision, scalar::Curve25519Scalar};
use alloc::{string::String, vec};
use itertools::Itertools;
use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{column_bounds::BoundsInner, committable_column::CommittableColumn, ColumnBounds};
use crate::base::database::ColumnType;
use core::fmt::Debug;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use thiserror::Error;

/// Errors that can occur when constructing invalid [`ColumnCommitmentMetadata`].
Expand Down Expand Up @@ -169,6 +169,7 @@ mod tests {
commitment::column_bounds::Bounds, database::OwnedColumn, math::decimal::Precision,
scalar::Curve25519Scalar,
};
use alloc::string::String;
use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
CommittableColumn,
};
use crate::base::database::ColumnField;
use alloc::string::{String, ToString};
use indexmap::IndexMap;
use proof_of_sql_parser::Identifier;
use thiserror::Error;
Expand Down Expand Up @@ -132,6 +133,7 @@ mod tests {
database::{owned_table_utility::*, ColumnType, OwnedTable},
scalar::Curve25519Scalar,
};
use alloc::vec::Vec;
use itertools::Itertools;

fn metadata_map_from_owned_table(
Expand Down
18 changes: 11 additions & 7 deletions crates/proof-of-sql/src/base/commitment/column_commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use super::{
ColumnCommitmentMetadataMapExt, ColumnCommitmentsMismatch, Commitment, VecCommitmentExt,
};
use crate::base::database::{ColumnField, ColumnRef, CommitmentAccessor, TableRef};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
vec,
vec::Vec,
};
use core::{iter, slice};
use indexmap::IndexSet;
use proof_of_sql_parser::Identifier;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -269,8 +276,8 @@ impl<C: Commitment> ColumnCommitments<C> {
}

/// Owning iterator for [`ColumnCommitments`].
pub type IntoIter<C> = std::iter::Map<
std::iter::Zip<<ColumnCommitmentMetadataMap as IntoIterator>::IntoIter, std::vec::IntoIter<C>>,
pub type IntoIter<C> = iter::Map<
iter::Zip<<ColumnCommitmentMetadataMap as IntoIterator>::IntoIter, vec::IntoIter<C>>,
fn(((Identifier, ColumnCommitmentMetadata), C)) -> (Identifier, ColumnCommitmentMetadata, C),
>;

Expand All @@ -286,11 +293,8 @@ impl<C> IntoIterator for ColumnCommitments<C> {
}

/// Borrowing iterator for [`ColumnCommitments`].
pub type Iter<'a, C> = std::iter::Map<
std::iter::Zip<
<&'a ColumnCommitmentMetadataMap as IntoIterator>::IntoIter,
std::slice::Iter<'a, C>,
>,
pub type Iter<'a, C> = iter::Map<
iter::Zip<<&'a ColumnCommitmentMetadataMap as IntoIterator>::IntoIter, slice::Iter<'a, C>>,
fn(
((&'a Identifier, &'a ColumnCommitmentMetadata), &'a C),
) -> (&'a Identifier, &'a ColumnCommitmentMetadata, &'a C),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::base::{
ref_into::RefInto,
scalar::Scalar,
};
use alloc::vec::Vec;
#[cfg(feature = "blitzar")]
use blitzar::sequence::Sequence;
use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};
Expand Down
12 changes: 7 additions & 5 deletions crates/proof-of-sql/src/base/commitment/naive_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::base::{
commitment::CommittableColumn,
scalar::{test_scalar::TestScalar, Scalar},
};
use serde::{Deserialize, Serialize};
use std::{
use alloc::{vec, vec::Vec};
use core::{
cmp,
fmt::Debug,
ops::{Add, AddAssign, Neg, Sub, SubAssign},
};
use serde::{Deserialize, Serialize};

/// A naive [Commitment] implementation that should only be used for the purpose of unit testing.
#[derive(Clone, Debug, Eq, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -62,13 +64,13 @@ impl AddAssign for NaiveCommitment {
impl PartialEq for NaiveCommitment {
fn eq(&self, other: &Self) -> bool {
match self.0.len().cmp(&other.0.len()) {
std::cmp::Ordering::Less => {
cmp::Ordering::Less => {
let mut extended_self = self.0.clone();
extended_self.extend((self.0.len()..other.0.len()).map(|_i| TestScalar::ZERO));
extended_self == other.0
}
std::cmp::Ordering::Equal => self.0 == other.0,
std::cmp::Ordering::Greater => {
cmp::Ordering::Equal => self.0 == other.0,
cmp::Ordering::Greater => {
let mut extended_other = other.0.clone();
extended_other.extend((other.0.len()..self.0.len()).map(|_i| TestScalar::ZERO));
extended_other == self.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::base::{
commitment::naive_commitment::NaiveCommitment,
scalar::{test_scalar::TestScalar, Scalar},
};
use alloc::vec::Vec;

// PartialEq Tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::base::database::{
ColumnField, ColumnRef, ColumnType, CommitmentAccessor, MetadataAccessor, SchemaAccessor,
TableRef,
};
use alloc::vec::Vec;
use indexmap::IndexMap;
use proof_of_sql_parser::Identifier;

Expand Down
3 changes: 2 additions & 1 deletion crates/proof-of-sql/src/base/commitment/table_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use crate::base::{
database::{Column, ColumnField, CommitmentAccessor, OwnedTable, TableRef},
scalar::Scalar,
};
use alloc::vec::Vec;
#[cfg(feature = "arrow")]
use arrow::record_batch::RecordBatch;
use bumpalo::Bump;
use core::ops::Range;
use proof_of_sql_parser::{Identifier, ParseError};
use serde::{Deserialize, Serialize};
use std::ops::Range;
use thiserror::Error;

/// Cannot create a [`TableCommitment`] with a negative range.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Commitment;
use crate::base::commitment::committable_column::CommittableColumn;
use alloc::{vec, vec::Vec};
use thiserror::Error;

/// Cannot update commitment collections with different column counts
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::base::{
database::{Column, ColumnRef, ColumnType, TableRef},
scalar::Scalar,
};
use alloc::vec::Vec;
use proof_of_sql_parser::Identifier;

/// Access metadata of a table span in a database.
Expand Down
33 changes: 21 additions & 12 deletions crates/proof-of-sql/src/base/database/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ use crate::base::{
math::decimal::{scale_scalar, Precision},
scalar::Scalar,
};
use alloc::{sync::Arc, vec::Vec};
#[cfg(feature = "arrow")]
use arrow::datatypes::{DataType, Field, TimeUnit as ArrowTimeUnit};
use bumpalo::Bump;
use core::{
fmt,
fmt::{Display, Formatter},
mem::size_of,
};
use proof_of_sql_parser::{
posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
Identifier,
};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Represents a read-only view of a column in an in-memory,
/// column-oriented database.
Expand Down Expand Up @@ -314,9 +319,9 @@ impl ColumnType {
return None;
}
self.to_integer_bits().and_then(|self_bits| {
other.to_integer_bits().and_then(|other_bits| {
Self::from_integer_bits(std::cmp::max(self_bits, other_bits))
})
other
.to_integer_bits()
.and_then(|other_bits| Self::from_integer_bits(self_bits.max(other_bits)))
})
}

Expand Down Expand Up @@ -353,12 +358,12 @@ impl ColumnType {
/// Returns the byte size of the column type.
pub fn byte_size(&self) -> usize {
match self {
Self::Boolean => std::mem::size_of::<bool>(),
Self::SmallInt => std::mem::size_of::<i16>(),
Self::Int => std::mem::size_of::<i32>(),
Self::BigInt | Self::TimestampTZ(_, _) => std::mem::size_of::<i64>(),
Self::Int128 => std::mem::size_of::<i128>(),
Self::Scalar | Self::Decimal75(_, _) | Self::VarChar => std::mem::size_of::<[u64; 4]>(),
Self::Boolean => size_of::<bool>(),
Self::SmallInt => size_of::<i16>(),
Self::Int => size_of::<i32>(),
Self::BigInt | Self::TimestampTZ(_, _) => size_of::<i64>(),
Self::Int128 => size_of::<i128>(),
Self::Scalar | Self::Decimal75(_, _) | Self::VarChar => size_of::<[u64; 4]>(),
}
}

Expand Down Expand Up @@ -441,8 +446,8 @@ impl TryFrom<DataType> for ColumnType {
}

/// Display the column type as a str name (in all caps)
impl std::fmt::Display for ColumnType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl Display for ColumnType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
ColumnType::Boolean => write!(f, "BOOLEAN"),
ColumnType::SmallInt => write!(f, "SMALLINT"),
Expand Down Expand Up @@ -542,6 +547,10 @@ impl From<&ColumnField> for Field {
mod tests {
use super::*;
use crate::{base::scalar::Curve25519Scalar, proof_primitive::dory::DoryScalar};
use alloc::{
string::{String, ToString},
vec,
};

#[test]
fn column_type_serializes_to_string() {
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/literal_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::base::{database::ColumnType, math::decimal::Precision, scalar::Scalar};
use alloc::string::String;
use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};
use serde::{Deserialize, Serialize};

Expand Down
5 changes: 5 additions & 0 deletions crates/proof-of-sql/src/base/database/owned_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use crate::base::{
},
scalar::Scalar,
};
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::cmp::Ordering;
use proof_of_sql_parser::{
intermediate_ast::OrderByDirection,
Expand Down Expand Up @@ -344,6 +348,7 @@ pub(crate) fn compare_indexes_by_owned_columns_with_direction<S: Scalar>(
mod test {
use super::*;
use crate::base::{math::decimal::Precision, scalar::Curve25519Scalar};
use alloc::vec;
use bumpalo::Bump;
use proof_of_sql_parser::intermediate_ast::OrderByDirection;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::base::database::ColumnType;
use alloc::string::String;
use thiserror::Error;

/// Errors from operations related to `OwnedColumn`s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! ```
use super::{OwnedColumn, OwnedTable};
use crate::base::scalar::Scalar;
use alloc::string::String;
use core::ops::Deref;
use proof_of_sql_parser::{
posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
Expand Down
10 changes: 7 additions & 3 deletions crates/proof-of-sql/src/base/database/table_ref.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use core::{
fmt,
fmt::{Display, Formatter},
str::FromStr,
};
use proof_of_sql_parser::{impl_serde_from_str, Identifier, ResourceId};
use std::str::FromStr;

/// Expression for an SQL table
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
Expand Down Expand Up @@ -37,8 +41,8 @@ impl FromStr for TableRef {
}
}

impl std::fmt::Display for TableRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl Display for TableRef {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.resource_id.fmt(f)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/base/encode/scalar_varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::base::{
scalar::MontScalar,
};
use ark_ff::MontConfig;
use std::cmp::{max, Ordering};
use core::cmp::{max, Ordering};

/// This function writes the input scalar x as a varint encoding to buf slice
///
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/encode/scalar_varint_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::scalar_varint::{
write_scalar_varint, write_scalar_varints,
};
use crate::base::{encode::U256, scalar::Curve25519Scalar};
use alloc::vec;

#[test]
fn small_scalars_are_encoded_as_positive_varints_and_consume_few_bytes() {
Expand Down
2 changes: 2 additions & 0 deletions crates/proof-of-sql/src/base/encode/varint_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use super::{
U256,
};
use crate::base::scalar::MontScalar;
#[cfg(test)]
use alloc::{vec, vec::Vec};
use ark_ff::MontConfig;

/// Most-significant byte, == 0x80
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/encode/varint_trait_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::VarInt;
use crate::base::scalar::{Curve25519Scalar, Scalar};
use alloc::{vec, vec::Vec};
use core::{
fmt::Debug,
ops::{Add, Neg},
Expand Down
4 changes: 4 additions & 0 deletions crates/proof-of-sql/src/base/math/decimal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Module for parsing an `IntermediateDecimal` into a `Decimal75`.
use crate::base::scalar::{Scalar, ScalarConversionError};
use alloc::{
format,
string::{String, ToString},
};
use proof_of_sql_parser::intermediate_decimal::{IntermediateDecimal, IntermediateDecimalError};
use serde::{Deserialize, Deserializer, Serialize};
use thiserror::Error;
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/base/math/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::mem;
use num_traits::{PrimInt, Unsigned};
use std::mem;

pub fn log2_down<T: PrimInt + Unsigned>(x: T) -> usize {
mem::size_of::<T>() * 8 - (x.leading_zeros() as usize) - 1
Expand Down
2 changes: 2 additions & 0 deletions crates/proof-of-sql/src/base/math/permutation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::{format, string::String, vec::Vec};
use thiserror::Error;

/// An error that occurs when working with permutations
Expand Down Expand Up @@ -78,6 +79,7 @@ impl Permutation {
#[cfg(test)]
mod test {
use super::*;
use alloc::vec;

#[test]
fn test_apply_permutation() {
Expand Down
2 changes: 2 additions & 0 deletions crates/proof-of-sql/src/base/proof/transcript.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::base::scalar::Scalar;
use alloc::vec::Vec;
use zerocopy::{AsBytes, FromBytes};

/// A public-coin transcript.
Expand Down Expand Up @@ -55,6 +56,7 @@ pub trait Transcript {
mod tests {
use super::Transcript;
use crate::base::proof::Keccak256Transcript;
use alloc::{string::ToString, vec};

#[test]
fn we_can_extend_transcript_with_serialize() {
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/scalar/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::string::String;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down
Loading

0 comments on commit 067d7c9

Please sign in to comment.