Skip to content

Commit

Permalink
Merge pull request #186 from andrewmilson/change-log2-to-ilog2
Browse files Browse the repository at this point in the history
Remove log2() and use .ilog2() instead
  • Loading branch information
irakliyk authored Apr 16, 2023
2 parents 7c1a56d + ac9f984 commit 5599a38
Show file tree
Hide file tree
Showing 39 changed files with 114 additions and 122 deletions.
4 changes: 2 additions & 2 deletions air/src/air/boundary/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
Assertion, BoundaryConstraint,
};
use crypto::{hashers::Blake3_256, DefaultRandomCoin, RandomCoin};
use math::{fields::f128::BaseElement, log2, polynom, FieldElement, StarkField};
use math::{fields::f128::BaseElement, polynom, FieldElement, StarkField};
use rand_utils::{rand_value, rand_vector, shuffle};
use utils::collections::{BTreeMap, Vec};

Expand Down Expand Up @@ -277,7 +277,7 @@ fn build_constraint_params(
BTreeMap<usize, Vec<BaseElement>>,
DefaultRandomCoin<Blake3_256<BaseElement>>,
) {
let inv_g = BaseElement::get_root_of_unity(log2(trace_length)).inv();
let inv_g = BaseElement::get_root_of_unity(trace_length.ilog2()).inv();
let prng = build_prng();
let twiddle_map = BTreeMap::<usize, Vec<BaseElement>>::new();
(inv_g, twiddle_map, prng)
Expand Down
6 changes: 3 additions & 3 deletions air/src/air/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// LICENSE file in the root directory of this source tree.

use crate::{air::TransitionConstraintDegree, ProofOptions, TraceInfo};
use math::{log2, StarkField};
use math::StarkField;
use utils::collections::Vec;

// AIR CONTEXT
Expand Down Expand Up @@ -154,8 +154,8 @@ impl<B: StarkField> AirContext<B> {
num_main_assertions,
num_aux_assertions,
ce_blowup_factor,
trace_domain_generator: B::get_root_of_unity(log2(trace_length)),
lde_domain_generator: B::get_root_of_unity(log2(lde_domain_size)),
trace_domain_generator: B::get_root_of_unity(trace_length.ilog2()),
lde_domain_generator: B::get_root_of_unity(lde_domain_size.ilog2()),
num_transition_exemptions: 1,
}
}
Expand Down
4 changes: 2 additions & 2 deletions air/src/air/divisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use crate::air::Assertion;
use core::fmt::{Display, Formatter};
use math::{log2, FieldElement, StarkField};
use math::{FieldElement, StarkField};
use utils::collections::Vec;

// CONSTRAINT DIVISOR
Expand Down Expand Up @@ -176,7 +176,7 @@ fn get_trace_domain_value_at<B: StarkField>(trace_length: usize, step: usize) ->
step < trace_length,
"step must be in the trace domain [0, {trace_length})"
);
let g = B::get_root_of_unity(log2(trace_length));
let g = B::get_root_of_unity(trace_length.ilog2());
g.exp((step as u64).into())
}

Expand Down
8 changes: 4 additions & 4 deletions air/src/air/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
};
use crate::{AuxTraceRandElements, FieldExtension};
use crypto::{hashers::Blake3_256, DefaultRandomCoin, RandomCoin};
use math::{fields::f128::BaseElement, get_power_series, log2, polynom, FieldElement, StarkField};
use math::{fields::f128::BaseElement, get_power_series, polynom, FieldElement, StarkField};
use utils::collections::{BTreeMap, Vec};

// PERIODIC COLUMNS
Expand Down Expand Up @@ -88,7 +88,7 @@ fn get_boundary_constraints() {
let trace_length = 16;
let air = MockAir::with_assertions(assertions, trace_length);
let no_poly_offset = (0, BaseElement::ONE);
let g = BaseElement::get_root_of_unity(log2(trace_length)); // trace domain generator
let g = BaseElement::get_root_of_unity(trace_length.ilog2()); // trace domain generator

// build coefficients for random liner combination; these will be derived for assertions
// sorted first by stride, then by first step, and finally by column (similar to the order)
Expand Down Expand Up @@ -307,14 +307,14 @@ pub fn build_prng() -> DefaultRandomCoin<Blake3_256<BaseElement>> {
pub fn build_sequence_poly(values: &[BaseElement], trace_length: usize) -> Vec<BaseElement> {
let cycle_length = trace_length / values.len();
let domain_size = trace_length / cycle_length;
let g = BaseElement::get_root_of_unity(log2(domain_size));
let g = BaseElement::get_root_of_unity(domain_size.ilog2());
let xs = get_power_series(g, domain_size);
polynom::interpolate(&xs, values, false)
}

pub fn build_periodic_column_poly(values: &[BaseElement]) -> Vec<BaseElement> {
let domain_size = values.len();
let g = BaseElement::get_root_of_unity(log2(domain_size));
let g = BaseElement::get_root_of_unity(domain_size.ilog2());
let xs = get_power_series(g, domain_size);
polynom::interpolate(&xs, values, false)
}
6 changes: 3 additions & 3 deletions air/src/proof/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Serializable for Context {
/// Serializes `self` and writes the resulting bytes into the `target`.
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.trace_layout.write_into(target);
target.write_u8(math::log2(self.trace_length) as u8); // store as power of two
target.write_u8(self.trace_length.ilog2() as u8); // store as power of two
target.write_u16(self.trace_meta.len() as u16);
target.write_bytes(&self.trace_meta);
assert!(self.field_modulus_bytes.len() < u8::MAX as usize);
Expand All @@ -157,10 +157,10 @@ impl Deserializable for Context {

// read and validate trace length (which was stored as a power of two)
let trace_length = source.read_u8()?;
if trace_length < math::log2(TraceInfo::MIN_TRACE_LENGTH) as u8 {
if trace_length < TraceInfo::MIN_TRACE_LENGTH.ilog2() as u8 {
return Err(DeserializationError::InvalidValue(format!(
"trace length cannot be smaller than 2^{}, but was 2^{}",
math::log2(TraceInfo::MIN_TRACE_LENGTH),
TraceInfo::MIN_TRACE_LENGTH.ilog2(),
trace_length
)));
}
Expand Down
5 changes: 2 additions & 3 deletions air/src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{ProofOptions, TraceInfo, TraceLayout};
use core::cmp;
use crypto::Hasher;
use fri::FriProof;
use math::log2;
use utils::{
collections::Vec, ByteReader, Deserializable, DeserializationError, Serializable, SliceReader,
};
Expand Down Expand Up @@ -193,7 +192,7 @@ fn get_conjectured_security(
let field_security = field_size - trace_domain_size.trailing_zeros();

// compute security we get by executing multiple query rounds
let security_per_query = log2(options.blowup_factor());
let security_per_query = options.blowup_factor().ilog2();
let mut query_security = security_per_query * options.num_queries() as u32;

// include grinding factor contributions only for proofs adequate security
Expand All @@ -216,7 +215,7 @@ fn get_proven_security(
collision_resistance: u32,
) -> u32 {
let extension_field_bits = (base_field_bits * options.field_extension().degree()) as f64;
let blowup_bits = log2(options.blowup_factor()) as f64;
let blowup_bits = options.blowup_factor().ilog2() as f64;
let num_fri_queries = options.num_queries() as f64;
let lde_size_bits = lde_domain_size.trailing_zeros() as f64;

Expand Down
4 changes: 2 additions & 2 deletions air/src/proof/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use super::Table;
use crypto::{BatchMerkleProof, ElementHasher, Hasher};
use math::{log2, FieldElement};
use math::FieldElement;
use utils::{
collections::Vec, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
SliceReader,
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Queries {

// build batch Merkle proof
let mut reader = SliceReader::new(&self.paths);
let tree_depth = log2(domain_size) as u8;
let tree_depth = domain_size.ilog2() as u8;
let merkle_proof = BatchMerkleProof::deserialize(&mut reader, hashed_queries, tree_depth)?;
if reader.has_more_bytes() {
return Err(DeserializationError::UnconsumedBytes);
Expand Down
3 changes: 1 addition & 2 deletions crypto/src/merkle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use crate::{errors::MerkleTreeError, hash::Hasher};
use core::slice;
use math::log2;
use utils::collections::{BTreeMap, BTreeSet, Vec};

mod proofs;
Expand Down Expand Up @@ -165,7 +164,7 @@ impl<H: Hasher> MerkleTree<H> {
/// The depth of a tree is zero-based. Thus, a tree with two leaves has depth 1, a tree with
/// four leaves has depth 2 etc.
pub fn depth(&self) -> usize {
log2(self.leaves.len()) as usize
self.leaves.len().ilog2() as usize
}

/// Returns leaf nodes of the tree.
Expand Down
4 changes: 2 additions & 2 deletions examples/src/fibonacci/fib2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, log2, FieldElement},
math::{fields::f128::BaseElement, FieldElement},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -112,7 +112,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace_width,
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/fibonacci/fib8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, log2, FieldElement},
math::{fields::f128::BaseElement, FieldElement},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -111,7 +111,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace_width,
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/fibonacci/fib_small/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f64::BaseElement, log2, FieldElement},
math::{fields::f64::BaseElement, FieldElement},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -130,7 +130,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace_width,
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/fibonacci/mulfib2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, log2, FieldElement},
math::{fields::f128::BaseElement, FieldElement},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -106,7 +106,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace_width,
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/fibonacci/mulfib8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, log2, FieldElement},
math::{fields::f128::BaseElement, FieldElement},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -107,7 +107,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace_width,
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/lamport/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, get_power_series, log2, FieldElement, StarkField},
math::{fields::f128::BaseElement, get_power_series, FieldElement, StarkField},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -146,7 +146,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace.width(),
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/lamport/threshold/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
};
use crate::utils::{are_equal, is_binary, is_zero, not, EvaluationResult};
use winterfell::{
math::{fields::f128::BaseElement, log2, FieldElement, StarkField, ToElements},
math::{fields::f128::BaseElement, FieldElement, StarkField, ToElements},
Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo,
TransitionConstraintDegree,
};
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Air for LamportThresholdAir {
// these steps depend on the depth of the public key Merkle tree; for example, if the Merkle
// tree has 4 elements, then the steps are: 24, 1048, 2072, 3096
let num_cycles = self.num_pub_keys.next_power_of_two();
let merkle_root_offset = (log2(num_cycles) + 1) as usize * HASH_CYCLE_LEN;
let merkle_root_offset = (num_cycles.ilog2() + 1) as usize * HASH_CYCLE_LEN;

// distinct key indexes should be used; the sequence starts at the last index of the tree
// (to pad the first cycle) and then wraps around and proceeds with index 0, 1, 2 etc.
Expand Down
4 changes: 2 additions & 2 deletions examples/src/lamport/threshold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, get_power_series, log2, FieldElement, StarkField},
math::{fields::f128::BaseElement, get_power_series, FieldElement, StarkField},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -142,7 +142,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace.width(),
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/merkle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rand_utils::{rand_value, rand_vector};
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, Digest, ElementHasher, MerkleTree},
math::{fields::f128::BaseElement, log2, FieldElement, StarkField},
math::{fields::f128::BaseElement, FieldElement, StarkField},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -130,7 +130,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace.width(),
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/rescue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use log::debug;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, log2, FieldElement},
math::{fields::f128::BaseElement, FieldElement},
ProofOptions, Prover, StarkProof, Trace, TraceTable, VerifierError,
};

Expand Down Expand Up @@ -118,7 +118,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace.width(),
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
6 changes: 3 additions & 3 deletions examples/src/rescue_raps/custom_trace_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use core_utils::{collections::Vec, uninit_vector};
use winterfell::{
math::{log2, FieldElement, StarkField},
math::{FieldElement, StarkField},
ColMatrix, EvaluationFrame, Trace, TraceInfo, TraceLayout,
};

Expand Down Expand Up @@ -85,10 +85,10 @@ impl<B: StarkField> RapTraceTable<B> {
"execution trace length must be a power of 2"
);
assert!(
log2(length) <= B::TWO_ADICITY,
length.ilog2() <= B::TWO_ADICITY,
"execution trace length cannot exceed 2^{} steps, but was 2^{}",
B::TWO_ADICITY,
log2(length)
length.ilog2()
);
assert!(
meta.len() <= TraceInfo::MAX_META_LENGTH,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/rescue_raps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand_utils::rand_array;
use std::time::Instant;
use winterfell::{
crypto::{DefaultRandomCoin, ElementHasher},
math::{fields::f128::BaseElement, log2, ExtensionOf, FieldElement},
math::{fields::f128::BaseElement, ExtensionOf, FieldElement},
ProofOptions, Prover, StarkProof, Trace, VerifierError,
};

Expand Down Expand Up @@ -131,7 +131,7 @@ where
debug!(
"Generated execution trace of {} registers and 2^{} steps in {} ms",
trace.width(),
log2(trace_length),
trace_length.ilog2(),
now.elapsed().as_millis()
);

Expand Down
Loading

0 comments on commit 5599a38

Please sign in to comment.