Skip to content

Commit

Permalink
refactor: eliminate std usage (#187)
Browse files Browse the repository at this point in the history
# Rationale for this change

In order to support `no_std`, we need to eliminate uses of `std` from
the repo as much as possible.

# What changes are included in this PR?

* explicit uses of `std` are removed.
* implicit uses of `std` are no longer required via usage of `use
alloc::vec::Vec` and friends.

It is possible I missed a few spots, but these can be cleaned up in the
final PR that enables `no_std`. This PR functionally does nothing until
we enable `no_std`.

# Are these changes tested?

Yes, by existing tests.
  • Loading branch information
JayWhite2357 authored Sep 28, 2024
2 parents fdfb722 + aa1062d commit d001f3c
Show file tree
Hide file tree
Showing 92 changed files with 148 additions and 59 deletions.
3 changes: 2 additions & 1 deletion crates/proof-of-sql/examples/posql_db/commit_accessor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use core::error::Error;
use proof_of_sql::base::{
commitment::{Commitment, QueryCommitments, TableCommitment},
database::{CommitmentAccessor, MetadataAccessor, SchemaAccessor, TableRef},
};
use serde::{Deserialize, Serialize};
use std::{error::Error, fs, path::PathBuf};
use std::{fs, path::PathBuf};
pub struct CommitAccessor<C: Commitment> {
base_path: PathBuf,
inner: QueryCommitments<C>,
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/examples/posql_db/csv_accessor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::record_batch_accessor::RecordBatchAccessor;
use arrow::{datatypes::Schema, record_batch::RecordBatch};
use arrow_csv::{ReaderBuilder, WriterBuilder};
use core::error::Error;
use proof_of_sql::base::{
database::{Column, ColumnRef, DataAccessor, MetadataAccessor, SchemaAccessor, TableRef},
scalar::Scalar,
};
use std::{
error::Error,
fs::{File, OpenOptions},
path::{Path, PathBuf},
sync::Arc,
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/base/bit/bit_distribution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::base::{bit::make_abs_bit_mask, scalar::Scalar};
use bit_iter::BitIter;
use core::convert::Into;
use serde::{Deserialize, Serialize};
use std::convert::Into;

/// Describe the distribution of bit values in a table column
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/bit/bit_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::base::{
bit::{make_abs_bit_mask, BitDistribution},
scalar::Scalar,
};
use alloc::vec::Vec;
use bumpalo::Bump;

/// Let x1, ..., xn denote the values of a data column. Let
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use arrow::{
datatypes::{i256, DataType, TimeUnit as ArrowTimeUnit},
};
use bumpalo::Bump;
use core::ops::Range;
use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone, PoSQLTimestampError};
use snafu::Snafu;
use std::ops::Range;

#[derive(Snafu, Debug, PartialEq)]
/// Errors caused by conversions between Arrow and owned types.
Expand Down Expand Up @@ -378,8 +378,9 @@ mod tests {

use super::*;
use crate::{base::scalar::Curve25519Scalar, proof_primitive::dory::DoryScalar};
use alloc::sync::Arc;
use arrow::array::Decimal256Builder;
use std::{str::FromStr, sync::Arc};
use core::str::FromStr;

#[test]
fn we_can_convert_timestamp_array_normal_range() {
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/column_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::base::{
math::decimal::{scale_scalar, DecimalError, Precision, MAX_SUPPORTED_PRECISION},
scalar::Scalar,
};
use alloc::{format, string::ToString, vec::Vec};
use core::{cmp::Ordering, fmt::Debug};
use num_bigint::BigInt;
use num_traits::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::base::{database::ColumnType, math::decimal::DecimalError};
use alloc::string::String;
use core::result::Result;
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};
use snafu::Snafu;

Expand Down Expand Up @@ -54,4 +56,4 @@ pub enum ColumnOperationError {
}

/// Result type for column operations
pub type ColumnOperationResult<T> = std::result::Result<T, ColumnOperationError>;
pub type ColumnOperationResult<T> = Result<T, ColumnOperationError>;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::base::{
math::decimal::{try_into_to_scalar, Precision},
scalar::Scalar,
};
use alloc::{format, string::ToString, vec};
use proof_of_sql_parser::{
intermediate_ast::{BinaryOperator, Expression, Literal, UnaryOperator},
Identifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::base::{database::ColumnOperationError, math::decimal::DecimalError};
use alloc::string::String;
use core::result::Result;
use snafu::Snafu;

/// Errors from evaluation of `Expression`s.
Expand Down Expand Up @@ -31,4 +33,4 @@ pub enum ExpressionEvaluationError {
}

/// Result type for expression evaluation
pub type ExpressionEvaluationResult<T> = std::result::Result<T, ExpressionEvaluationError>;
pub type ExpressionEvaluationResult<T> = Result<T, ExpressionEvaluationError>;
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/filter_util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::base::{database::Column, scalar::Scalar};
use alloc::vec::Vec;
use bumpalo::Bump;

/// This function takes a selection vector and a set of columns and returns a
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/group_by_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::base::{
if_rayon,
scalar::Scalar,
};
use alloc::vec::Vec;
use bumpalo::Bump;
use core::cmp::Ordering;
use itertools::Itertools;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::base::{
math::decimal::Precision,
scalar::Scalar,
};
use alloc::sync::Arc;
use arrow::{
array::{
ArrayRef, BooleanArray, Decimal128Array, Decimal256Array, Int16Array, Int32Array,
Expand All @@ -37,7 +38,6 @@ use proof_of_sql_parser::{
Identifier, ParseError,
};
use snafu::Snafu;
use std::sync::Arc;

#[derive(Snafu, Debug)]
#[non_exhaustive]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::{
},
record_batch,
};
use alloc::sync::Arc;
use arrow::{
array::{ArrayRef, BooleanArray, Decimal128Array, Float32Array, Int64Array, StringArray},
datatypes::Schema,
record_batch::RecordBatch,
};
use std::sync::Arc;

fn we_can_convert_between_owned_column_and_array_ref_impl(
owned_column: OwnedColumn<Curve25519Scalar>,
Expand Down
6 changes: 3 additions & 3 deletions crates/proof-of-sql/src/base/database/record_batch_utility.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use alloc::sync::Arc;
use arrow::array::{
TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray,
TimestampSecondArray,
};
use proof_of_sql_parser::posql_time::PoSQLTimeUnit;
use std::sync::Arc;

/// Extension trait for Vec<T> to convert it to an Arrow array
pub trait ToArrow {
Expand Down Expand Up @@ -155,7 +155,7 @@ string_to_arrow_array!(
macro_rules! record_batch {
($($col_name:expr => $slice:expr), + $(,)?) => {
{
use std::sync::Arc;
use alloc::sync::Arc;
use arrow::datatypes::Field;
use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
Expand All @@ -176,11 +176,11 @@ macro_rules! record_batch {
#[cfg(test)]
mod tests {
use crate::record_batch;
use alloc::sync::Arc;
use arrow::{
datatypes::{Field, Schema},
record_batch::RecordBatch,
};
use std::sync::Arc;

#[test]
fn test_record_batch_macro() {
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/table_ref.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::string::ToString;
use core::{
fmt,
fmt::{Display, Formatter},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::base::{map::IndexMap, scalar::Scalar};
use alloc::{rc::Rc, vec::Vec};
/**
* Adopted from arkworks
*
* See third_party/license/arkworks.LICENSE
*/
use std::cmp::max;
use std::{rc::Rc, vec::Vec};
use core::cmp::max;

/// Stores a list of products of `DenseMultilinearExtension` that is meant to be added together.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::CompositePolynomial;
use crate::base::scalar::Curve25519Scalar;
use std::rc::Rc;
use alloc::rc::Rc;

#[test]
fn test_composite_polynomial_evaluation() {
Expand Down
7 changes: 5 additions & 2 deletions crates/proof-of-sql/src/base/polynomial/evaluation_vector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::base::if_rayon;
use core::ops::{Mul, MulAssign, Sub, SubAssign};
use core::{
cmp,
ops::{Mul, MulAssign, Sub, SubAssign},
};
use num_traits::One;
#[cfg(feature = "rayon")]
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};
Expand All @@ -13,7 +16,7 @@ fn compute_evaluation_vector_impl<F>(left: &mut [F], right: &mut [F], p: F)
where
F: One + Sub<Output = F> + MulAssign + SubAssign + Mul<Output = F> + Send + Sync + Copy,
{
let k = std::cmp::min(left.len(), right.len());
let k = cmp::min(left.len(), right.len());
let one_minus_p = F::one() - p;
if_rayon!(
left.par_iter_mut().with_min_len(MIN_PARALLEL_LEN),
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/polynomial/interpolate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::{vec, vec::Vec};
/**
* Adapted from arkworks
*
Expand Down
3 changes: 2 additions & 1 deletion crates/proof-of-sql/src/base/polynomial/interpolate_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use super::interpolate::*;
use crate::base::scalar::{Curve25519Scalar, Curve25519Scalar as S};
use ark_std::UniformRand;
use core::iter;
use num_traits::{Inv, Zero};

#[test]
Expand All @@ -15,7 +16,7 @@ fn test_interpolate_uni_poly_for_random_polynomials() {
let num_points = vec![0, 1, 2, 3, 4, 5, 10, 20, 32, 33, 64, 65];

for n in num_points {
let poly = std::iter::repeat_with(|| Curve25519Scalar::rand(&mut prng))
let poly = iter::repeat_with(|| Curve25519Scalar::rand(&mut prng))
.take(n)
.collect::<Vec<_>>();
let evals = (0..n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::base::{
scalar::Curve25519Scalar,
};
use ark_std::UniformRand;
use core::iter;
use num_traits::Zero;
use std::iter;

#[test]
fn compute_truncated_lagrange_basis_sum_gives_correct_values_with_0_variables() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::base::{database::Column, if_rayon, scalar::Scalar, slice_ops};
use alloc::{rc::Rc, vec::Vec};
use core::ffi::c_void;
use num_traits::Zero;
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{ffi::c_void, rc::Rc};

/// Interface for operating on multilinear extension's in-place
pub trait MultilinearExtension<S: Scalar> {
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/slice_ops/batch_inverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! Additionally, `num_elem_per_thread` rounds up instead of down.

use crate::base::if_rayon;
use alloc::vec::Vec;
#[cfg(feature = "rayon")]
use core::cmp::max;
use core::ops::{Mul, MulAssign};
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/slice_ops/slice_cast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::base::if_rayon;
use alloc::vec::Vec;
#[cfg(feature = "rayon")]
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{
compute_L_R_vec, compute_l_r_tensors, compute_v_vec, DeferredGT, G1Affine, VMVProverState,
VMVVerifierState, F,
};
use alloc::vec::Vec;

/// Builds a VMVProverState from the given parameters.
pub(super) fn build_vmv_prover_state(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::{vec, vec::Vec};
use ark_ec::VariableBaseMSM;
use core::ops::{Add, AddAssign, Mul, MulAssign};
use itertools::Itertools;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::base::{
impl_serde_for_ark_serde_checked,
scalar::{MontScalar, Scalar},
};
use alloc::vec::Vec;
use ark_ec::pairing::PairingOutput;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use core::ops::Mul;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{pairings, DoryCommitment, DoryProverPublicSetup, DoryScalar, G1Projective};
use crate::base::commitment::CommittableColumn;
use alloc::vec::Vec;
use ark_ec::VariableBaseMSM;
use core::iter::once;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{DoryScalar, G1Affine, G2Affine, F, GT};
use crate::base::{impl_serde_for_ark_serde_checked, proof::Transcript};
use alloc::vec::Vec;
use ark_ff::Field;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use num_traits::Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use super::{transpose, G1Affine, ProverSetup, F};
use crate::base::polynomial::compute_evaluation_vector;
#[cfg(feature = "blitzar")]
use crate::base::slice_ops::slice_cast;
use alloc::{vec, vec::Vec};
#[cfg(not(feature = "blitzar"))]
use ark_ec::{AffineRepr, VariableBaseMSM};
use ark_ff::{BigInt, MontBackend};
#[cfg(feature = "blitzar")]
use blitzar::compute::ElementP2;
#[cfg(feature = "blitzar")]
use core::mem;
use num_traits::{One, Zero};

/// Compute the evaluations of the columns of the matrix M that is derived from `a`.
Expand Down Expand Up @@ -43,7 +46,7 @@ pub(super) fn compute_T_vec_prime(
) -> Vec<G1Affine> {
let num_columns = 1 << sigma;
let num_outputs = 1 << nu;
let data_size = std::mem::size_of::<F>();
let data_size = mem::size_of::<F>();

let a_array = convert_scalar_to_array(a);
let a_transpose =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{
dynamic_dory_helper::{compute_dynamic_v_vec, compute_dynamic_vecs},
DeferredGT, G1Affine, VMVProverState, VMVVerifierState, F,
};
use alloc::vec::Vec;

/// Builds a VMVProverState from the given parameters.
pub(super) fn build_dynamic_vmv_prover_state(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::base::{
commitment::{Commitment, CommittableColumn},
impl_serde_for_ark_serde_checked,
};
use alloc::vec::Vec;
use ark_ec::pairing::PairingOutput;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use core::ops::Mul;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
G1Affine, G1Projective, ProverSetup,
};
use crate::base::commitment::CommittableColumn;
use alloc::{vec, vec::Vec};

#[tracing::instrument(name = "compute_dory_commitment_impl (cpu)", level = "debug", skip_all)]
fn compute_dory_commitment_impl<'a, T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
ProverSetup, F,
};
use crate::proof_primitive::dory::dynamic_dory_standard_basis_helper::compute_dynamic_standard_basis_vecs;
use alloc::{vec, vec::Vec};
use ark_ff::Field;
use itertools::Itertools;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
ProverSetup, VMVProverState, VMVVerifierState, VerifierSetup,
};
use crate::base::{if_rayon, proof::Transcript};
use alloc::vec::Vec;
use ark_ec::VariableBaseMSM;
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
};
#[cfg(test)]
use super::{G1Affine, G1Projective, G2Projective, ProverSetup};
use alloc::{vec, vec::Vec};
#[cfg(test)]
use ark_ec::VariableBaseMSM;

Expand Down
Loading

0 comments on commit d001f3c

Please sign in to comment.