diff --git a/evm/src/all_stark.rs b/evm/src/all_stark.rs index 1d785579..74f211d6 100644 --- a/evm/src/all_stark.rs +++ b/evm/src/all_stark.rs @@ -83,7 +83,7 @@ pub(crate) const NUM_TABLES: usize = Table::Memory as usize + 1; impl Table { /// Returns all STARK table indices. - pub(crate) fn all() -> [Self; NUM_TABLES] { + pub(crate) const fn all() -> [Self; NUM_TABLES] { [ Self::Arithmetic, Self::BytePacking, diff --git a/evm/src/arithmetic/mod.rs b/evm/src/arithmetic/mod.rs index 4b84a351..f9a816c1 100644 --- a/evm/src/arithmetic/mod.rs +++ b/evm/src/arithmetic/mod.rs @@ -91,7 +91,7 @@ impl BinaryOperator { } /// Maps a binary arithmetic operation to its associated flag column in the trace. - pub(crate) fn row_filter(&self) -> usize { + pub(crate) const fn row_filter(&self) -> usize { match self { BinaryOperator::Add => columns::IS_ADD, BinaryOperator::Mul => columns::IS_MUL, @@ -130,7 +130,7 @@ impl TernaryOperator { } /// Maps a ternary arithmetic operation to its associated flag column in the trace. - pub(crate) fn row_filter(&self) -> usize { + pub(crate) const fn row_filter(&self) -> usize { match self { TernaryOperator::AddMod => columns::IS_ADDMOD, TernaryOperator::MulMod => columns::IS_MULMOD, @@ -208,7 +208,7 @@ impl Operation { } } - pub(crate) fn range_check( + pub(crate) const fn range_check( input0: U256, input1: U256, input2: U256, diff --git a/evm/src/byte_packing/byte_packing_stark.rs b/evm/src/byte_packing/byte_packing_stark.rs index faed8fe6..e9dcd01e 100644 --- a/evm/src/byte_packing/byte_packing_stark.rs +++ b/evm/src/byte_packing/byte_packing_stark.rs @@ -222,7 +222,7 @@ impl, const D: usize> BytePackingStark { row } - fn generate_padding_row(&self) -> [F; NUM_COLUMNS] { + const fn generate_padding_row(&self) -> [F; NUM_COLUMNS] { [F::ZERO; NUM_COLUMNS] } diff --git a/evm/src/config.rs b/evm/src/config.rs index a593c827..24ddb6a7 100644 --- a/evm/src/config.rs +++ b/evm/src/config.rs @@ -14,7 +14,7 @@ pub struct StarkConfig { impl StarkConfig { /// A typical configuration with a rate of 2, resulting in fast but large proofs. /// Targets ~100 bit conjectured security. - pub fn standard_fast_config() -> Self { + pub const fn standard_fast_config() -> Self { Self { security_bits: 100, num_challenges: 2, diff --git a/evm/src/cpu/kernel/constants/context_metadata.rs b/evm/src/cpu/kernel/constants/context_metadata.rs index fd710eec..664ce6e9 100644 --- a/evm/src/cpu/kernel/constants/context_metadata.rs +++ b/evm/src/cpu/kernel/constants/context_metadata.rs @@ -34,7 +34,7 @@ pub(crate) enum ContextMetadata { impl ContextMetadata { pub(crate) const COUNT: usize = 14; - pub(crate) fn all() -> [Self; Self::COUNT] { + pub(crate) const fn all() -> [Self; Self::COUNT] { [ Self::ParentContext, Self::ParentProgramCounter, @@ -54,7 +54,7 @@ impl ContextMetadata { } /// The variable name that gets passed into kernel assembly code. - pub(crate) fn var_name(&self) -> &'static str { + pub(crate) const fn var_name(&self) -> &'static str { match self { ContextMetadata::ParentContext => "CTX_METADATA_PARENT_CONTEXT", ContextMetadata::ParentProgramCounter => "CTX_METADATA_PARENT_PC", diff --git a/evm/src/cpu/kernel/constants/global_metadata.rs b/evm/src/cpu/kernel/constants/global_metadata.rs index fa80d006..8b85c0b5 100644 --- a/evm/src/cpu/kernel/constants/global_metadata.rs +++ b/evm/src/cpu/kernel/constants/global_metadata.rs @@ -93,7 +93,7 @@ pub(crate) enum GlobalMetadata { impl GlobalMetadata { pub(crate) const COUNT: usize = 48; - pub(crate) fn all() -> [Self; Self::COUNT] { + pub(crate) const fn all() -> [Self; Self::COUNT] { [ Self::LargestContext, Self::MemorySize, @@ -147,7 +147,7 @@ impl GlobalMetadata { } /// The variable name that gets passed into kernel assembly code. - pub(crate) fn var_name(&self) -> &'static str { + pub(crate) const fn var_name(&self) -> &'static str { match self { Self::LargestContext => "GLOBAL_METADATA_LARGEST_CONTEXT", Self::MemorySize => "GLOBAL_METADATA_MEMORY_SIZE", diff --git a/evm/src/cpu/kernel/constants/journal_entry.rs b/evm/src/cpu/kernel/constants/journal_entry.rs index 5fd86851..d84f2ade 100644 --- a/evm/src/cpu/kernel/constants/journal_entry.rs +++ b/evm/src/cpu/kernel/constants/journal_entry.rs @@ -16,7 +16,7 @@ pub(crate) enum JournalEntry { impl JournalEntry { pub(crate) const COUNT: usize = 11; - pub(crate) fn all() -> [Self; Self::COUNT] { + pub(crate) const fn all() -> [Self; Self::COUNT] { [ Self::AccountLoaded, Self::AccountDestroyed, @@ -33,7 +33,7 @@ impl JournalEntry { } /// The variable name that gets passed into kernel assembly code. - pub(crate) fn var_name(&self) -> &'static str { + pub(crate) const fn var_name(&self) -> &'static str { match self { Self::AccountLoaded => "JOURNAL_ENTRY_ACCOUNT_LOADED", Self::AccountDestroyed => "JOURNAL_ENTRY_ACCOUNT_DESTROYED", diff --git a/evm/src/cpu/kernel/constants/trie_type.rs b/evm/src/cpu/kernel/constants/trie_type.rs index 7f936529..5120c163 100644 --- a/evm/src/cpu/kernel/constants/trie_type.rs +++ b/evm/src/cpu/kernel/constants/trie_type.rs @@ -26,7 +26,7 @@ impl PartialTrieType { } } - pub(crate) fn all() -> [Self; Self::COUNT] { + pub(crate) const fn all() -> [Self; Self::COUNT] { [ Self::Empty, Self::Hash, @@ -37,7 +37,7 @@ impl PartialTrieType { } /// The variable name that gets passed into kernel assembly code. - pub(crate) fn var_name(&self) -> &'static str { + pub(crate) const fn var_name(&self) -> &'static str { match self { Self::Empty => "MPT_NODE_EMPTY", Self::Hash => "MPT_NODE_HASH", diff --git a/evm/src/cpu/kernel/constants/txn_fields.rs b/evm/src/cpu/kernel/constants/txn_fields.rs index 0eae47f0..d62159a2 100644 --- a/evm/src/cpu/kernel/constants/txn_fields.rs +++ b/evm/src/cpu/kernel/constants/txn_fields.rs @@ -28,7 +28,7 @@ pub(crate) enum NormalizedTxnField { impl NormalizedTxnField { pub(crate) const COUNT: usize = 16; - pub(crate) fn all() -> [Self; Self::COUNT] { + pub(crate) const fn all() -> [Self; Self::COUNT] { [ Self::ChainIdPresent, Self::ChainId, @@ -50,7 +50,7 @@ impl NormalizedTxnField { } /// The variable name that gets passed into kernel assembly code. - pub(crate) fn var_name(&self) -> &'static str { + pub(crate) const fn var_name(&self) -> &'static str { match self { NormalizedTxnField::ChainIdPresent => "TXN_FIELD_CHAIN_ID_PRESENT", NormalizedTxnField::ChainId => "TXN_FIELD_CHAIN_ID", diff --git a/evm/src/cpu/kernel/cost_estimator.rs b/evm/src/cpu/kernel/cost_estimator.rs index bb2b0a40..fdfeee56 100644 --- a/evm/src/cpu/kernel/cost_estimator.rs +++ b/evm/src/cpu/kernel/cost_estimator.rs @@ -26,12 +26,12 @@ fn cost_estimate_item(item: &Item) -> u32 { } } -fn cost_estimate_standard_op(_op: &str) -> u32 { +const fn cost_estimate_standard_op(_op: &str) -> u32 { // For now we just treat any standard operation as having the same cost. This is pretty naive, // but should work fine with our current set of simple optimization rules. 1 } -fn cost_estimate_push(num_bytes: usize) -> u32 { +const fn cost_estimate_push(num_bytes: usize) -> u32 { num_bytes as u32 } diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 3be12e1f..e72b4078 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -912,7 +912,7 @@ impl<'a> Interpreter<'a> { self.generation_state.registers.gas_used = gas_used_val; } - pub(crate) fn stack_len(&self) -> usize { + pub(crate) const fn stack_len(&self) -> usize { self.generation_state.registers.stack_len } @@ -924,7 +924,7 @@ impl<'a> Interpreter<'a> { } } - pub(crate) fn is_kernel(&self) -> bool { + pub(crate) const fn is_kernel(&self) -> bool { self.generation_state.registers.is_kernel } @@ -932,7 +932,7 @@ impl<'a> Interpreter<'a> { self.generation_state.registers.is_kernel = is_kernel } - pub(crate) fn context(&self) -> usize { + pub(crate) const fn context(&self) -> usize { self.generation_state.registers.context } diff --git a/evm/src/cpu/kernel/utils.rs b/evm/src/cpu/kernel/utils.rs index 3470904c..eea033d4 100644 --- a/evm/src/cpu/kernel/utils.rs +++ b/evm/src/cpu/kernel/utils.rs @@ -31,7 +31,7 @@ pub(crate) fn u256_to_trimmed_be_bytes(u256: &U256) -> Vec { (0..num_bytes).rev().map(|i| u256.byte(i)).collect() } -pub(crate) fn u256_from_bool(b: bool) -> U256 { +pub(crate) const fn u256_from_bool(b: bool) -> U256 { if b { U256::one() } else { diff --git a/evm/src/cpu/memio.rs b/evm/src/cpu/memio.rs index afb0c19f..3fcdfc96 100644 --- a/evm/src/cpu/memio.rs +++ b/evm/src/cpu/memio.rs @@ -11,13 +11,13 @@ use crate::cpu::membus::NUM_GP_CHANNELS; use crate::cpu::stack; use crate::memory::segments::Segment; -fn get_addr_load(lv: &CpuColumnsView) -> (T, T, T) { +const fn get_addr_load(lv: &CpuColumnsView) -> (T, T, T) { let addr_context = lv.mem_channels[0].value[0]; let addr_segment = lv.mem_channels[1].value[0]; let addr_virtual = lv.mem_channels[2].value[0]; (addr_context, addr_segment, addr_virtual) } -fn get_addr_store(lv: &CpuColumnsView) -> (T, T, T) { +const fn get_addr_store(lv: &CpuColumnsView) -> (T, T, T) { let addr_context = lv.mem_channels[1].value[0]; let addr_segment = lv.mem_channels[2].value[0]; let addr_virtual = lv.mem_channels[3].value[0]; diff --git a/evm/src/curve_pairings.rs b/evm/src/curve_pairings.rs index eca880b2..1380c96e 100644 --- a/evm/src/curve_pairings.rs +++ b/evm/src/curve_pairings.rs @@ -17,7 +17,7 @@ where } impl Curve { - pub(crate) fn unit() -> Self { + pub(crate) const fn unit() -> Self { Curve { x: T::ZERO, y: T::ZERO, @@ -237,7 +237,7 @@ pub(crate) fn bn_cord(p1: Curve, p2: Curve, q: Curve>) /// The tangent and cord functions output sparse Fp12 elements. /// This map embeds the nonzero coefficients into an Fp12. -pub(crate) fn bn_sparse_embed(g000: BN254, g01: Fp2, g11: Fp2) -> Fp12 { +pub(crate) const fn bn_sparse_embed(g000: BN254, g01: Fp2, g11: Fp2) -> Fp12 { let g0 = Fp6 { t0: Fp2 { re: g000, diff --git a/evm/src/generation/mpt.rs b/evm/src/generation/mpt.rs index ba3aa20d..8d944b27 100644 --- a/evm/src/generation/mpt.rs +++ b/evm/src/generation/mpt.rs @@ -298,7 +298,7 @@ fn parse_storage_value(value_rlp: &[u8]) -> Result, ProgramError> { Ok(vec![value]) } -fn empty_nibbles() -> Nibbles { +const fn empty_nibbles() -> Nibbles { Nibbles { count: 0, packed: U512::zero(), diff --git a/evm/src/generation/trie_extractor.rs b/evm/src/generation/trie_extractor.rs index 42c50c6d..fd06cec6 100644 --- a/evm/src/generation/trie_extractor.rs +++ b/evm/src/generation/trie_extractor.rs @@ -29,7 +29,7 @@ pub(crate) fn read_state_trie_value(slice: &[U256]) -> Result U256 { +pub(crate) const fn read_storage_trie_value(slice: &[U256]) -> U256 { slice[0] } diff --git a/evm/src/keccak_sponge/keccak_sponge_stark.rs b/evm/src/keccak_sponge/keccak_sponge_stark.rs index 47908f8a..32bc7905 100644 --- a/evm/src/keccak_sponge/keccak_sponge_stark.rs +++ b/evm/src/keccak_sponge/keccak_sponge_stark.rs @@ -136,7 +136,7 @@ pub(crate) fn ctl_looking_memory(i: usize) -> Vec> { } /// Returns the number of `KeccakSponge` tables looking into the `LogicStark`. -pub(crate) fn num_logic_ctls() -> usize { +pub(crate) const fn num_logic_ctls() -> usize { const U8S_PER_CTL: usize = 32; ceil_div_usize(KECCAK_RATE_BYTES, U8S_PER_CTL) } diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index d5da3141..51aa8be2 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -70,7 +70,7 @@ pub(crate) enum Segment { impl Segment { pub(crate) const COUNT: usize = 36; - pub(crate) fn all() -> [Self; Self::COUNT] { + pub(crate) const fn all() -> [Self; Self::COUNT] { [ Self::Code, Self::Stack, @@ -112,7 +112,7 @@ impl Segment { } /// The variable name that gets passed into kernel assembly code. - pub(crate) fn var_name(&self) -> &'static str { + pub(crate) const fn var_name(&self) -> &'static str { match self { Segment::Code => "SEGMENT_CODE", Segment::Stack => "SEGMENT_STACK", @@ -153,7 +153,7 @@ impl Segment { } } - pub(crate) fn bit_range(&self) -> usize { + pub(crate) const fn bit_range(&self) -> usize { match self { Segment::Code => 8, Segment::Stack => 256, diff --git a/evm/src/witness/gas.rs b/evm/src/witness/gas.rs index 67339e35..54597a3e 100644 --- a/evm/src/witness/gas.rs +++ b/evm/src/witness/gas.rs @@ -8,7 +8,7 @@ pub(crate) const G_LOW: u64 = 5; pub(crate) const G_MID: u64 = 8; pub(crate) const G_HIGH: u64 = 10; -pub(crate) fn gas_to_charge(op: Operation) -> u64 { +pub(crate) const fn gas_to_charge(op: Operation) -> u64 { use crate::arithmetic::BinaryOperator::*; use crate::arithmetic::TernaryOperator::*; use crate::witness::operation::Operation::*; diff --git a/evm/src/witness/memory.rs b/evm/src/witness/memory.rs index 58b1f6c2..8cb7daf7 100644 --- a/evm/src/witness/memory.rs +++ b/evm/src/witness/memory.rs @@ -38,7 +38,7 @@ pub(crate) struct MemoryAddress { } impl MemoryAddress { - pub(crate) fn new(context: usize, segment: Segment, virt: usize) -> Self { + pub(crate) const fn new(context: usize, segment: Segment, virt: usize) -> Self { Self { context, segment: segment as usize, @@ -120,7 +120,11 @@ impl MemoryOp { } } - pub(crate) fn new_dummy_read(address: MemoryAddress, timestamp: usize, value: U256) -> Self { + pub(crate) const fn new_dummy_read( + address: MemoryAddress, + timestamp: usize, + value: U256, + ) -> Self { Self { filter: false, timestamp, @@ -130,7 +134,7 @@ impl MemoryOp { } } - pub(crate) fn sorting_key(&self) -> (usize, usize, usize, usize) { + pub(crate) const fn sorting_key(&self) -> (usize, usize, usize, usize) { ( self.address.context, self.address.segment, diff --git a/evm/src/witness/state.rs b/evm/src/witness/state.rs index 392109b3..1070ee64 100644 --- a/evm/src/witness/state.rs +++ b/evm/src/witness/state.rs @@ -20,7 +20,7 @@ pub struct RegistersState { } impl RegistersState { - pub(crate) fn code_context(&self) -> usize { + pub(crate) const fn code_context(&self) -> usize { if self.is_kernel { KERNEL_CONTEXT } else { diff --git a/evm/src/witness/transition.rs b/evm/src/witness/transition.rs index 7e716fb7..6cdc5f6a 100644 --- a/evm/src/witness/transition.rs +++ b/evm/src/witness/transition.rs @@ -187,7 +187,7 @@ fn fill_op_flag(op: Operation, row: &mut CpuColumnsView) { } // Equal to the number of pops if an operation pops without pushing, and `None` otherwise. -fn get_op_special_length(op: Operation) -> Option { +const fn get_op_special_length(op: Operation) -> Option { let behavior_opt = match op { Operation::Push(0) | Operation::Pc => STACK_BEHAVIORS.pc_push0, Operation::Push(1..) => STACK_BEHAVIORS.push, @@ -229,7 +229,7 @@ fn get_op_special_length(op: Operation) -> Option { // These operations might trigger a stack overflow, typically those pushing without popping. // Kernel-only pushing instructions aren't considered; they can't overflow. -fn might_overflow_op(op: Operation) -> bool { +const fn might_overflow_op(op: Operation) -> bool { match op { Operation::Push(1..) => MIGHT_OVERFLOW.push, Operation::Dup(_) | Operation::Swap(_) => MIGHT_OVERFLOW.dup_swap, diff --git a/evm/tests/erc20.rs b/evm/tests/erc20.rs index aea97618..89a516bf 100644 --- a/evm/tests/erc20.rs +++ b/evm/tests/erc20.rs @@ -278,6 +278,7 @@ fn signed_tx() -> Vec { fn bloom_bytes() -> [u8; 256] { hex!("00000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000008000000000000000000000000000000000040000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000002000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000020000000000080000000000000000000000000000000000000000000000000000000000000000") } + fn bloom() -> [U256; 8] { let bloom = bloom_bytes() .chunks_exact(32) diff --git a/field/src/batch_util.rs b/field/src/batch_util.rs index 4338b7e4..ab7ee3d5 100644 --- a/field/src/batch_util.rs +++ b/field/src/batch_util.rs @@ -2,7 +2,7 @@ use crate::packable::Packable; use crate::packed::PackedField; use crate::types::Field; -fn pack_with_leftovers_split_point(slice: &[P::Scalar]) -> usize { +const fn pack_with_leftovers_split_point(slice: &[P::Scalar]) -> usize { let n = slice.len(); let n_leftover = n % P::WIDTH; n - n_leftover diff --git a/field/src/extension/algebra.rs b/field/src/extension/algebra.rs index 8ca939b2..f7ca3cae 100644 --- a/field/src/extension/algebra.rs +++ b/field/src/extension/algebra.rs @@ -17,11 +17,11 @@ impl, const D: usize> ExtensionAlgebra { F::ONE.into() } - pub fn from_basefield_array(arr: [F; D]) -> Self { + pub const fn from_basefield_array(arr: [F; D]) -> Self { Self(arr) } - pub fn to_basefield_array(self) -> [F; D] { + pub const fn to_basefield_array(self) -> [F; D] { self.0 } diff --git a/field/src/goldilocks_extensions.rs b/field/src/goldilocks_extensions.rs index 8b53f8b5..6dd15ce0 100644 --- a/field/src/goldilocks_extensions.rs +++ b/field/src/goldilocks_extensions.rs @@ -114,14 +114,14 @@ impl Mul for QuinticExtension { /// Return `a`, `b` such that `a + b*2^128 = 3*(x + y*2^128)` with `a < 2^128` and `b < 2^32`. #[inline(always)] -fn u160_times_3(x: u128, y: u32) -> (u128, u32) { +const fn u160_times_3(x: u128, y: u32) -> (u128, u32) { let (s, cy) = x.overflowing_add(x << 1); (s, 3 * y + (x >> 127) as u32 + cy as u32) } /// Return `a`, `b` such that `a + b*2^128 = 7*(x + y*2^128)` with `a < 2^128` and `b < 2^32`. #[inline(always)] -fn u160_times_7(x: u128, y: u32) -> (u128, u32) { +const fn u160_times_7(x: u128, y: u32) -> (u128, u32) { let (d, br) = (x << 3).overflowing_sub(x); // NB: subtracting the borrow can't underflow (d, 7 * y + (x >> (128 - 3)) as u32 - br as u32) diff --git a/field/src/goldilocks_field.rs b/field/src/goldilocks_field.rs index 6e4361fc..94db5b6b 100644 --- a/field/src/goldilocks_field.rs +++ b/field/src/goldilocks_field.rs @@ -381,7 +381,7 @@ unsafe fn add_no_canonicalize_trashing_input(x: u64, y: u64) -> u64 { #[inline(always)] #[cfg(not(target_arch = "x86_64"))] -unsafe fn add_no_canonicalize_trashing_input(x: u64, y: u64) -> u64 { +const unsafe fn add_no_canonicalize_trashing_input(x: u64, y: u64) -> u64 { let (res_wrapped, carry) = x.overflowing_add(y); // Below cannot overflow unless the assumption if x + y < 2**64 + ORDER is incorrect. res_wrapped + EPSILON * (carry as u64) @@ -415,7 +415,7 @@ fn reduce128(x: u128) -> GoldilocksField { } #[inline] -fn split(x: u128) -> (u64, u64) { +const fn split(x: u128) -> (u64, u64) { (x as u64, (x >> 64) as u64) } diff --git a/plonky2/src/fri/mod.rs b/plonky2/src/fri/mod.rs index 5121d755..100ae851 100644 --- a/plonky2/src/fri/mod.rs +++ b/plonky2/src/fri/mod.rs @@ -51,7 +51,7 @@ impl FriConfig { } } - pub fn num_cap_elements(&self) -> usize { + pub const fn num_cap_elements(&self) -> usize { 1 << self.cap_height } } @@ -85,11 +85,11 @@ impl FriParams { self.reduction_arity_bits.iter().copied().max() } - pub fn lde_bits(&self) -> usize { + pub const fn lde_bits(&self) -> usize { self.degree_bits + self.config.rate_bits } - pub fn lde_size(&self) -> usize { + pub const fn lde_size(&self) -> usize { 1 << self.lde_bits() } diff --git a/plonky2/src/gates/arithmetic_base.rs b/plonky2/src/gates/arithmetic_base.rs index dca1c506..f47bd18f 100644 --- a/plonky2/src/gates/arithmetic_base.rs +++ b/plonky2/src/gates/arithmetic_base.rs @@ -29,28 +29,28 @@ pub struct ArithmeticGate { } impl ArithmeticGate { - pub fn new_from_config(config: &CircuitConfig) -> Self { + pub const fn new_from_config(config: &CircuitConfig) -> Self { Self { num_ops: Self::num_ops(config), } } /// Determine the maximum number of operations that can fit in one gate for the given config. - pub(crate) fn num_ops(config: &CircuitConfig) -> usize { + pub(crate) const fn num_ops(config: &CircuitConfig) -> usize { let wires_per_op = 4; config.num_routed_wires / wires_per_op } - pub fn wire_ith_multiplicand_0(i: usize) -> usize { + pub const fn wire_ith_multiplicand_0(i: usize) -> usize { 4 * i } - pub fn wire_ith_multiplicand_1(i: usize) -> usize { + pub const fn wire_ith_multiplicand_1(i: usize) -> usize { 4 * i + 1 } - pub fn wire_ith_addend(i: usize) -> usize { + pub const fn wire_ith_addend(i: usize) -> usize { 4 * i + 2 } - pub fn wire_ith_output(i: usize) -> usize { + pub const fn wire_ith_output(i: usize) -> usize { 4 * i + 3 } } diff --git a/plonky2/src/gates/arithmetic_extension.rs b/plonky2/src/gates/arithmetic_extension.rs index b43d8d3a..084a14d6 100644 --- a/plonky2/src/gates/arithmetic_extension.rs +++ b/plonky2/src/gates/arithmetic_extension.rs @@ -25,28 +25,28 @@ pub struct ArithmeticExtensionGate { } impl ArithmeticExtensionGate { - pub fn new_from_config(config: &CircuitConfig) -> Self { + pub const fn new_from_config(config: &CircuitConfig) -> Self { Self { num_ops: Self::num_ops(config), } } /// Determine the maximum number of operations that can fit in one gate for the given config. - pub(crate) fn num_ops(config: &CircuitConfig) -> usize { + pub(crate) const fn num_ops(config: &CircuitConfig) -> usize { let wires_per_op = 4 * D; config.num_routed_wires / wires_per_op } - pub fn wires_ith_multiplicand_0(i: usize) -> Range { + pub const fn wires_ith_multiplicand_0(i: usize) -> Range { 4 * D * i..4 * D * i + D } - pub fn wires_ith_multiplicand_1(i: usize) -> Range { + pub const fn wires_ith_multiplicand_1(i: usize) -> Range { 4 * D * i + D..4 * D * i + 2 * D } - pub fn wires_ith_addend(i: usize) -> Range { + pub const fn wires_ith_addend(i: usize) -> Range { 4 * D * i + 2 * D..4 * D * i + 3 * D } - pub fn wires_ith_output(i: usize) -> Range { + pub const fn wires_ith_output(i: usize) -> Range { 4 * D * i + 3 * D..4 * D * i + 4 * D } } diff --git a/plonky2/src/gates/base_sum.rs b/plonky2/src/gates/base_sum.rs index 28131c1b..087f2bf6 100644 --- a/plonky2/src/gates/base_sum.rs +++ b/plonky2/src/gates/base_sum.rs @@ -31,7 +31,7 @@ pub struct BaseSumGate { } impl BaseSumGate { - pub fn new(num_limbs: usize) -> Self { + pub const fn new(num_limbs: usize) -> Self { Self { num_limbs } } @@ -45,7 +45,7 @@ impl BaseSumGate { pub const START_LIMBS: usize = 1; /// Returns the index of the `i`th limb wire. - pub fn limbs(&self) -> Range { + pub const fn limbs(&self) -> Range { Self::START_LIMBS..Self::START_LIMBS + self.num_limbs } } diff --git a/plonky2/src/gates/constant.rs b/plonky2/src/gates/constant.rs index 965b30b6..144e1ca3 100644 --- a/plonky2/src/gates/constant.rs +++ b/plonky2/src/gates/constant.rs @@ -27,7 +27,7 @@ pub struct ConstantGate { } impl ConstantGate { - pub fn new(num_consts: usize) -> Self { + pub const fn new(num_consts: usize) -> Self { Self { num_consts } } diff --git a/plonky2/src/gates/coset_interpolation.rs b/plonky2/src/gates/coset_interpolation.rs index 99046ec4..331f1820 100644 --- a/plonky2/src/gates/coset_interpolation.rs +++ b/plonky2/src/gates/coset_interpolation.rs @@ -86,16 +86,16 @@ impl, const D: usize> CosetInterpolationGate } } - fn num_points(&self) -> usize { + const fn num_points(&self) -> usize { 1 << self.subgroup_bits } /// Wire index of the coset shift. - pub(crate) fn wire_shift(&self) -> usize { + pub(crate) const fn wire_shift(&self) -> usize { 0 } - fn start_values(&self) -> usize { + const fn start_values(&self) -> usize { 1 } @@ -106,31 +106,31 @@ impl, const D: usize> CosetInterpolationGate start..start + D } - fn start_evaluation_point(&self) -> usize { + const fn start_evaluation_point(&self) -> usize { self.start_values() + self.num_points() * D } /// Wire indices of the point to evaluate the interpolant at. - pub(crate) fn wires_evaluation_point(&self) -> Range { + pub(crate) const fn wires_evaluation_point(&self) -> Range { let start = self.start_evaluation_point(); start..start + D } - fn start_evaluation_value(&self) -> usize { + const fn start_evaluation_value(&self) -> usize { self.start_evaluation_point() + D } /// Wire indices of the interpolated value. - pub(crate) fn wires_evaluation_value(&self) -> Range { + pub(crate) const fn wires_evaluation_value(&self) -> Range { let start = self.start_evaluation_value(); start..start + D } - fn start_intermediates(&self) -> usize { + const fn start_intermediates(&self) -> usize { self.start_evaluation_value() + D } - pub fn num_routed_wires(&self) -> usize { + pub const fn num_routed_wires(&self) -> usize { self.start_intermediates() } diff --git a/plonky2/src/gates/exponentiation.rs b/plonky2/src/gates/exponentiation.rs index 8e708b97..0011f011 100644 --- a/plonky2/src/gates/exponentiation.rs +++ b/plonky2/src/gates/exponentiation.rs @@ -32,7 +32,7 @@ pub struct ExponentiationGate, const D: usize> { } impl, const D: usize> ExponentiationGate { - pub fn new(num_power_bits: usize) -> Self { + pub const fn new(num_power_bits: usize) -> Self { Self { num_power_bits, _phantom: PhantomData, @@ -51,7 +51,7 @@ impl, const D: usize> ExponentiationGate { max_for_routed_wires.min(max_for_wires) } - pub fn wire_base(&self) -> usize { + pub const fn wire_base(&self) -> usize { 0 } @@ -61,7 +61,7 @@ impl, const D: usize> ExponentiationGate { 1 + i } - pub fn wire_output(&self) -> usize { + pub const fn wire_output(&self) -> usize { 1 + self.num_power_bits } diff --git a/plonky2/src/gates/lookup.rs b/plonky2/src/gates/lookup.rs index 3c03f6ba..42b3bb92 100644 --- a/plonky2/src/gates/lookup.rs +++ b/plonky2/src/gates/lookup.rs @@ -51,16 +51,16 @@ impl LookupGate { lut_hash: keccak(table_bytes).0, } } - pub(crate) fn num_slots(config: &CircuitConfig) -> usize { + pub(crate) const fn num_slots(config: &CircuitConfig) -> usize { let wires_per_lookup = 2; config.num_routed_wires / wires_per_lookup } - pub fn wire_ith_looking_inp(i: usize) -> usize { + pub const fn wire_ith_looking_inp(i: usize) -> usize { 2 * i } - pub fn wire_ith_looking_out(i: usize) -> usize { + pub const fn wire_ith_looking_out(i: usize) -> usize { 2 * i + 1 } } diff --git a/plonky2/src/gates/lookup_table.rs b/plonky2/src/gates/lookup_table.rs index a683a87d..ad01e092 100644 --- a/plonky2/src/gates/lookup_table.rs +++ b/plonky2/src/gates/lookup_table.rs @@ -56,23 +56,23 @@ impl LookupTableGate { } } - pub(crate) fn num_slots(config: &CircuitConfig) -> usize { + pub(crate) const fn num_slots(config: &CircuitConfig) -> usize { let wires_per_entry = 3; config.num_routed_wires / wires_per_entry } /// Wire for the looked input. - pub fn wire_ith_looked_inp(i: usize) -> usize { + pub const fn wire_ith_looked_inp(i: usize) -> usize { 3 * i } // Wire for the looked output. - pub fn wire_ith_looked_out(i: usize) -> usize { + pub const fn wire_ith_looked_out(i: usize) -> usize { 3 * i + 1 } /// Wire for the multiplicity. Set after the trace has been generated. - pub fn wire_ith_multiplicity(i: usize) -> usize { + pub const fn wire_ith_multiplicity(i: usize) -> usize { 3 * i + 2 } } diff --git a/plonky2/src/gates/multiplication_extension.rs b/plonky2/src/gates/multiplication_extension.rs index ba578d8b..11fd88eb 100644 --- a/plonky2/src/gates/multiplication_extension.rs +++ b/plonky2/src/gates/multiplication_extension.rs @@ -25,25 +25,25 @@ pub struct MulExtensionGate { } impl MulExtensionGate { - pub fn new_from_config(config: &CircuitConfig) -> Self { + pub const fn new_from_config(config: &CircuitConfig) -> Self { Self { num_ops: Self::num_ops(config), } } /// Determine the maximum number of operations that can fit in one gate for the given config. - pub(crate) fn num_ops(config: &CircuitConfig) -> usize { + pub(crate) const fn num_ops(config: &CircuitConfig) -> usize { let wires_per_op = 3 * D; config.num_routed_wires / wires_per_op } - pub fn wires_ith_multiplicand_0(i: usize) -> Range { + pub const fn wires_ith_multiplicand_0(i: usize) -> Range { 3 * D * i..3 * D * i + D } - pub fn wires_ith_multiplicand_1(i: usize) -> Range { + pub const fn wires_ith_multiplicand_1(i: usize) -> Range { 3 * D * i + D..3 * D * i + 2 * D } - pub fn wires_ith_output(i: usize) -> Range { + pub const fn wires_ith_output(i: usize) -> Range { 3 * D * i + 2 * D..3 * D * i + 3 * D } } diff --git a/plonky2/src/gates/poseidon.rs b/plonky2/src/gates/poseidon.rs index 0842930d..5f353cd6 100644 --- a/plonky2/src/gates/poseidon.rs +++ b/plonky2/src/gates/poseidon.rs @@ -30,17 +30,17 @@ use crate::util::serialization::{Buffer, IoResult, Read, Write}; pub struct PoseidonGate, const D: usize>(PhantomData); impl, const D: usize> PoseidonGate { - pub fn new() -> Self { + pub const fn new() -> Self { Self(PhantomData) } /// The wire index for the `i`th input to the permutation. - pub fn wire_input(i: usize) -> usize { + pub const fn wire_input(i: usize) -> usize { i } /// The wire index for the `i`th output to the permutation. - pub fn wire_output(i: usize) -> usize { + pub const fn wire_output(i: usize) -> usize { SPONGE_WIDTH + i } @@ -90,7 +90,7 @@ impl, const D: usize> PoseidonGate { } /// End of wire indices, exclusive. - fn end() -> usize { + const fn end() -> usize { Self::START_FULL_1 + SPONGE_WIDTH * poseidon::HALF_N_FULL_ROUNDS } } diff --git a/plonky2/src/gates/poseidon_mds.rs b/plonky2/src/gates/poseidon_mds.rs index 75f9ae5c..9b97cac7 100644 --- a/plonky2/src/gates/poseidon_mds.rs +++ b/plonky2/src/gates/poseidon_mds.rs @@ -25,7 +25,7 @@ use crate::util::serialization::{Buffer, IoResult, Read, Write}; pub struct PoseidonMdsGate + Poseidon, const D: usize>(PhantomData); impl + Poseidon, const D: usize> PoseidonMdsGate { - pub fn new() -> Self { + pub const fn new() -> Self { Self(PhantomData) } diff --git a/plonky2/src/gates/public_input.rs b/plonky2/src/gates/public_input.rs index f770e2e6..8be41d40 100644 --- a/plonky2/src/gates/public_input.rs +++ b/plonky2/src/gates/public_input.rs @@ -22,7 +22,7 @@ use crate::util::serialization::{Buffer, IoResult}; pub struct PublicInputGate; impl PublicInputGate { - pub fn wires_public_inputs_hash() -> Range { + pub const fn wires_public_inputs_hash() -> Range { 0..4 } } diff --git a/plonky2/src/gates/random_access.rs b/plonky2/src/gates/random_access.rs index df1bb576..59af01a5 100644 --- a/plonky2/src/gates/random_access.rs +++ b/plonky2/src/gates/random_access.rs @@ -41,7 +41,7 @@ pub struct RandomAccessGate, const D: usize> { } impl, const D: usize> RandomAccessGate { - fn new(num_copies: usize, bits: usize, num_extra_constants: usize) -> Self { + const fn new(num_copies: usize, bits: usize, num_extra_constants: usize) -> Self { Self { bits, num_copies, @@ -71,7 +71,7 @@ impl, const D: usize> RandomAccessGate { } /// Length of the list being accessed. - fn vec_size(&self) -> usize { + const fn vec_size(&self) -> usize { 1 << self.bits } @@ -94,7 +94,7 @@ impl, const D: usize> RandomAccessGate { (2 + self.vec_size()) * copy + 2 + i } - fn start_extra_constants(&self) -> usize { + const fn start_extra_constants(&self) -> usize { (2 + self.vec_size()) * self.num_copies } @@ -104,7 +104,7 @@ impl, const D: usize> RandomAccessGate { } /// All above wires are routed. - pub fn num_routed_wires(&self) -> usize { + pub const fn num_routed_wires(&self) -> usize { self.start_extra_constants() + self.num_extra_constants } diff --git a/plonky2/src/gates/reducing.rs b/plonky2/src/gates/reducing.rs index 46ff69c8..c9daf538 100644 --- a/plonky2/src/gates/reducing.rs +++ b/plonky2/src/gates/reducing.rs @@ -23,7 +23,7 @@ pub struct ReducingGate { } impl ReducingGate { - pub fn new(num_coeffs: usize) -> Self { + pub const fn new(num_coeffs: usize) -> Self { Self { num_coeffs } } @@ -31,23 +31,23 @@ impl ReducingGate { (num_routed_wires - 3 * D).min((num_wires - 2 * D) / (D + 1)) } - pub fn wires_output() -> Range { + pub const fn wires_output() -> Range { 0..D } - pub fn wires_alpha() -> Range { + pub const fn wires_alpha() -> Range { D..2 * D } - pub fn wires_old_acc() -> Range { + pub const fn wires_old_acc() -> Range { 2 * D..3 * D } const START_COEFFS: usize = 3 * D; - pub fn wires_coeffs(&self) -> Range { + pub const fn wires_coeffs(&self) -> Range { Self::START_COEFFS..Self::START_COEFFS + self.num_coeffs } - fn start_accs(&self) -> usize { + const fn start_accs(&self) -> usize { Self::START_COEFFS + self.num_coeffs } - fn wires_accs(&self, i: usize) -> Range { + const fn wires_accs(&self, i: usize) -> Range { if i == self.num_coeffs - 1 { // The last accumulator is the output. return Self::wires_output(); diff --git a/plonky2/src/gates/reducing_extension.rs b/plonky2/src/gates/reducing_extension.rs index 77c1f39a..b1dc5e85 100644 --- a/plonky2/src/gates/reducing_extension.rs +++ b/plonky2/src/gates/reducing_extension.rs @@ -23,7 +23,7 @@ pub struct ReducingExtensionGate { } impl ReducingExtensionGate { - pub fn new(num_coeffs: usize) -> Self { + pub const fn new(num_coeffs: usize) -> Self { Self { num_coeffs } } @@ -33,20 +33,20 @@ impl ReducingExtensionGate { ((num_routed_wires - 3 * D) / D).min((num_wires - 2 * D) / (D * 2)) } - pub fn wires_output() -> Range { + pub const fn wires_output() -> Range { 0..D } - pub fn wires_alpha() -> Range { + pub const fn wires_alpha() -> Range { D..2 * D } - pub fn wires_old_acc() -> Range { + pub const fn wires_old_acc() -> Range { 2 * D..3 * D } const START_COEFFS: usize = 3 * D; - pub fn wires_coeff(i: usize) -> Range { + pub const fn wires_coeff(i: usize) -> Range { Self::START_COEFFS + i * D..Self::START_COEFFS + (i + 1) * D } - fn start_accs(&self) -> usize { + const fn start_accs(&self) -> usize { Self::START_COEFFS + self.num_coeffs * D } fn wires_accs(&self, i: usize) -> Range { diff --git a/plonky2/src/hash/poseidon.rs b/plonky2/src/hash/poseidon.rs index 8858bc7b..1556dd1d 100644 --- a/plonky2/src/hash/poseidon.rs +++ b/plonky2/src/hash/poseidon.rs @@ -36,7 +36,7 @@ pub const N_ROUNDS: usize = N_FULL_ROUNDS_TOTAL + N_PARTIAL_ROUNDS; const MAX_WIDTH: usize = 12; // we only have width 8 and 12, and 12 is bigger. :) #[inline(always)] -fn add_u160_u128((x_lo, x_hi): (u128, u32), y: u128) -> (u128, u32) { +const fn add_u160_u128((x_lo, x_hi): (u128, u32), y: u128) -> (u128, u32) { let (res_lo, over) = x_lo.overflowing_add(y); let res_hi = x_hi + (over as u32); (res_lo, res_hi) diff --git a/plonky2/src/hash/poseidon_goldilocks.rs b/plonky2/src/hash/poseidon_goldilocks.rs index e2c72d85..3e089e64 100644 --- a/plonky2/src/hash/poseidon_goldilocks.rs +++ b/plonky2/src/hash/poseidon_goldilocks.rs @@ -315,7 +315,7 @@ mod poseidon12_mds { /// Split 3 x 4 FFT-based MDS vector-multiplication with the Poseidon circulant MDS matrix. #[inline(always)] - pub(crate) fn mds_multiply_freq(state: [u64; 12]) -> [u64; 12] { + pub(crate) const fn mds_multiply_freq(state: [u64; 12]) -> [u64; 12] { let [s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11] = state; let (u0, u1, u2) = fft4_real([s0, s3, s6, s9]); @@ -343,7 +343,7 @@ mod poseidon12_mds { } #[inline(always)] - fn block1(x: [i64; 3], y: [i64; 3]) -> [i64; 3] { + const fn block1(x: [i64; 3], y: [i64; 3]) -> [i64; 3] { let [x0, x1, x2] = x; let [y0, y1, y2] = y; let z0 = x0 * y0 + x1 * y2 + x2 * y1; @@ -354,7 +354,7 @@ mod poseidon12_mds { } #[inline(always)] - fn block2(x: [(i64, i64); 3], y: [(i64, i64); 3]) -> [(i64, i64); 3] { + const fn block2(x: [(i64, i64); 3], y: [(i64, i64); 3]) -> [(i64, i64); 3] { let [(x0r, x0i), (x1r, x1i), (x2r, x2i)] = x; let [(y0r, y0i), (y1r, y1i), (y2r, y2i)] = y; let x0s = x0r + x0i; @@ -392,7 +392,7 @@ mod poseidon12_mds { } #[inline(always)] - fn block3(x: [i64; 3], y: [i64; 3]) -> [i64; 3] { + const fn block3(x: [i64; 3], y: [i64; 3]) -> [i64; 3] { let [x0, x1, x2] = x; let [y0, y1, y2] = y; let z0 = x0 * y0 - x1 * y2 - x2 * y1; @@ -404,20 +404,20 @@ mod poseidon12_mds { /// Real 2-FFT over u64 integers. #[inline(always)] - pub(crate) fn fft2_real(x: [u64; 2]) -> [i64; 2] { + pub(crate) const fn fft2_real(x: [u64; 2]) -> [i64; 2] { [(x[0] as i64 + x[1] as i64), (x[0] as i64 - x[1] as i64)] } /// Real 2-iFFT over u64 integers. /// Division by two to complete the inverse FFT is not performed here. #[inline(always)] - pub(crate) fn ifft2_real_unreduced(y: [i64; 2]) -> [u64; 2] { + pub(crate) const fn ifft2_real_unreduced(y: [i64; 2]) -> [u64; 2] { [(y[0] + y[1]) as u64, (y[0] - y[1]) as u64] } /// Real 4-FFT over u64 integers. #[inline(always)] - pub(crate) fn fft4_real(x: [u64; 4]) -> (i64, (i64, i64), i64) { + pub(crate) const fn fft4_real(x: [u64; 4]) -> (i64, (i64, i64), i64) { let [z0, z2] = fft2_real([x[0], x[2]]); let [z1, z3] = fft2_real([x[1], x[3]]); let y0 = z0 + z1; @@ -429,7 +429,7 @@ mod poseidon12_mds { /// Real 4-iFFT over u64 integers. /// Division by four to complete the inverse FFT is not performed here. #[inline(always)] - pub(crate) fn ifft4_real_unreduced(y: (i64, (i64, i64), i64)) -> [u64; 4] { + pub(crate) const fn ifft4_real_unreduced(y: (i64, (i64, i64), i64)) -> [u64; 4] { let z0 = y.0 + y.2; let z1 = y.0 - y.2; let z2 = y.1 .0; diff --git a/plonky2/src/iop/ext_target.rs b/plonky2/src/iop/ext_target.rs index 21eb3e55..68d81fef 100644 --- a/plonky2/src/iop/ext_target.rs +++ b/plonky2/src/iop/ext_target.rs @@ -19,7 +19,7 @@ impl Default for ExtensionTarget { } impl ExtensionTarget { - pub fn to_target_array(&self) -> [Target; D] { + pub const fn to_target_array(&self) -> [Target; D] { self.0 } @@ -77,7 +77,7 @@ impl TryFrom> for ExtensionTarget { pub struct ExtensionAlgebraTarget(pub [ExtensionTarget; D]); impl ExtensionAlgebraTarget { - pub fn to_ext_target_array(&self) -> [ExtensionTarget; D] { + pub const fn to_ext_target_array(&self) -> [ExtensionTarget; D] { self.0 } } diff --git a/plonky2/src/iop/target.rs b/plonky2/src/iop/target.rs index b5efd89d..c0a1e286 100644 --- a/plonky2/src/iop/target.rs +++ b/plonky2/src/iop/target.rs @@ -26,11 +26,11 @@ impl Default for Target { } impl Target { - pub fn wire(row: usize, column: usize) -> Self { + pub const fn wire(row: usize, column: usize) -> Self { Self::Wire(Wire { row, column }) } - pub fn is_routable(&self, config: &CircuitConfig) -> bool { + pub const fn is_routable(&self, config: &CircuitConfig) -> bool { match self { Target::Wire(wire) => wire.is_routable(config), Target::VirtualTarget { .. } => true, @@ -49,7 +49,7 @@ impl Target { } /// Conversion to an `ExtensionTarget`. - pub fn to_ext_target(self, zero: Self) -> ExtensionTarget { + pub const fn to_ext_target(self, zero: Self) -> ExtensionTarget { let mut arr = [zero; D]; arr[0] = self; ExtensionTarget(arr) @@ -66,7 +66,7 @@ pub struct BoolTarget { } impl BoolTarget { - pub fn new_unsafe(target: Target) -> BoolTarget { + pub const fn new_unsafe(target: Target) -> BoolTarget { BoolTarget { target, _private: (), diff --git a/plonky2/src/iop/wire.rs b/plonky2/src/iop/wire.rs index 5f8d3b23..435479ce 100644 --- a/plonky2/src/iop/wire.rs +++ b/plonky2/src/iop/wire.rs @@ -15,7 +15,7 @@ pub struct Wire { } impl Wire { - pub fn is_routable(&self, config: &CircuitConfig) -> bool { + pub const fn is_routable(&self, config: &CircuitConfig) -> bool { self.column < config.num_routed_wires } diff --git a/plonky2/src/plonk/circuit_builder.rs b/plonky2/src/plonk/circuit_builder.rs index 0da3ed60..3bc34099 100644 --- a/plonky2/src/plonk/circuit_builder.rs +++ b/plonky2/src/plonk/circuit_builder.rs @@ -704,7 +704,7 @@ impl, const D: usize> CircuitBuilder { } /// The number of (base field) `arithmetic` operations that can be performed in a single gate. - pub(crate) fn num_base_arithmetic_ops_per_gate(&self) -> usize { + pub(crate) const fn num_base_arithmetic_ops_per_gate(&self) -> usize { if self.config.use_base_arithmetic_gate { ArithmeticGate::new_from_config(&self.config).num_ops } else { @@ -713,7 +713,7 @@ impl, const D: usize> CircuitBuilder { } /// The number of `arithmetic_extension` operations that can be performed in a single gate. - pub(crate) fn num_ext_arithmetic_ops_per_gate(&self) -> usize { + pub(crate) const fn num_ext_arithmetic_ops_per_gate(&self) -> usize { ArithmeticExtensionGate::::new_from_config(&self.config).num_ops } diff --git a/plonky2/src/plonk/circuit_data.rs b/plonky2/src/plonk/circuit_data.rs index e850cb42..1b07a8b2 100644 --- a/plonky2/src/plonk/circuit_data.rs +++ b/plonky2/src/plonk/circuit_data.rs @@ -64,12 +64,12 @@ impl Default for CircuitConfig { } impl CircuitConfig { - pub fn num_advice_wires(&self) -> usize { + pub const fn num_advice_wires(&self) -> usize { self.num_wires - self.num_routed_wires } /// A typical recursion config, without zero-knowledge, targeting ~100 bit security. - pub fn standard_recursion_config() -> Self { + pub const fn standard_recursion_config() -> Self { Self { num_wires: 135, num_routed_wires: 80, @@ -442,11 +442,11 @@ impl, const D: usize> CommonCircuitData { self.fri_params.degree_bits } - pub fn degree(&self) -> usize { + pub const fn degree(&self) -> usize { 1 << self.degree_bits() } - pub fn lde_size(&self) -> usize { + pub const fn lde_size(&self) -> usize { self.fri_params.lde_size() } @@ -462,37 +462,37 @@ impl, const D: usize> CommonCircuitData { .expect("No gates?") } - pub fn quotient_degree(&self) -> usize { + pub const fn quotient_degree(&self) -> usize { self.quotient_degree_factor * self.degree() } /// Range of the constants polynomials in the `constants_sigmas_commitment`. - pub fn constants_range(&self) -> Range { + pub const fn constants_range(&self) -> Range { 0..self.num_constants } /// Range of the sigma polynomials in the `constants_sigmas_commitment`. - pub fn sigmas_range(&self) -> Range { + pub const fn sigmas_range(&self) -> Range { self.num_constants..self.num_constants + self.config.num_routed_wires } /// Range of the `z`s polynomials in the `zs_partial_products_commitment`. - pub fn zs_range(&self) -> Range { + pub const fn zs_range(&self) -> Range { 0..self.config.num_challenges } /// Range of the partial products polynomials in the `zs_partial_products_lookup_commitment`. - pub fn partial_products_range(&self) -> Range { + pub const fn partial_products_range(&self) -> Range { self.config.num_challenges..(self.num_partial_products + 1) * self.config.num_challenges } /// Range of lookup polynomials in the `zs_partial_products_lookup_commitment`. - pub fn lookup_range(&self) -> RangeFrom { + pub const fn lookup_range(&self) -> RangeFrom { self.num_zs_partial_products_polys().. } /// Range of lookup polynomials needed for evaluation at `g * zeta`. - pub fn next_lookup_range(&self, i: usize) -> Range { + pub const fn next_lookup_range(&self, i: usize) -> Range { self.num_zs_partial_products_polys() + i * self.num_lookup_polys ..self.num_zs_partial_products_polys() + i * self.num_lookup_polys + 2 } @@ -573,7 +573,7 @@ impl, const D: usize> CommonCircuitData { ) } - pub(crate) fn num_preprocessed_polys(&self) -> usize { + pub(crate) const fn num_preprocessed_polys(&self) -> usize { self.sigmas_range().end } @@ -589,12 +589,12 @@ impl, const D: usize> CommonCircuitData { ) } - pub(crate) fn num_zs_partial_products_polys(&self) -> usize { + pub(crate) const fn num_zs_partial_products_polys(&self) -> usize { self.config.num_challenges * (1 + self.num_partial_products) } /// Returns the total number of lookup polynomials. - pub(crate) fn num_all_lookup_polys(&self) -> usize { + pub(crate) const fn num_all_lookup_polys(&self) -> usize { self.config.num_challenges * self.num_lookup_polys } fn fri_zs_polys(&self) -> Vec { @@ -618,7 +618,7 @@ impl, const D: usize> CommonCircuitData { ..self.num_zs_partial_products_polys() + self.num_all_lookup_polys(), ) } - pub(crate) fn num_quotient_polys(&self) -> usize { + pub(crate) const fn num_quotient_polys(&self) -> usize { self.config.num_challenges * self.quotient_degree_factor } diff --git a/plonky2/src/plonk/copy_constraint.rs b/plonky2/src/plonk/copy_constraint.rs index 50e85fbf..ea92ec1c 100644 --- a/plonky2/src/plonk/copy_constraint.rs +++ b/plonky2/src/plonk/copy_constraint.rs @@ -18,7 +18,7 @@ impl From<(Target, Target)> for CopyConstraint { } impl CopyConstraint { - pub fn new(pair: (Target, Target), name: String) -> Self { + pub const fn new(pair: (Target, Target), name: String) -> Self { Self { pair, name } } } diff --git a/plonky2/src/plonk/plonk_common.rs b/plonky2/src/plonk/plonk_common.rs index 53c75af1..f064cbac 100644 --- a/plonky2/src/plonk/plonk_common.rs +++ b/plonky2/src/plonk/plonk_common.rs @@ -38,7 +38,7 @@ impl PlonkOracle { }; } -pub fn salt_size(salted: bool) -> usize { +pub const fn salt_size(salted: bool) -> usize { if salted { SALT_SIZE } else { diff --git a/plonky2/src/plonk/vars.rs b/plonky2/src/plonk/vars.rs index 758018f5..727f7651 100644 --- a/plonky2/src/plonk/vars.rs +++ b/plonky2/src/plonk/vars.rs @@ -80,11 +80,11 @@ impl<'a, F: Field> EvaluationVarsBaseBatch<'a, F> { self.local_constants = &self.local_constants[num_selectors * self.len()..]; } - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.batch_size } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -100,7 +100,7 @@ impl<'a, F: Field> EvaluationVarsBaseBatch<'a, F> { } } - pub fn iter(&self) -> EvaluationVarsBaseBatchIter<'a, F> { + pub const fn iter(&self) -> EvaluationVarsBaseBatchIter<'a, F> { EvaluationVarsBaseBatchIter::new(*self) } @@ -136,7 +136,7 @@ pub struct EvaluationVarsBaseBatchIter<'a, F: Field> { } impl<'a, F: Field> EvaluationVarsBaseBatchIter<'a, F> { - pub fn new(vars_batch: EvaluationVarsBaseBatch<'a, F>) -> Self { + pub const fn new(vars_batch: EvaluationVarsBaseBatch<'a, F>) -> Self { EvaluationVarsBaseBatchIter { i: 0, vars_batch } } } diff --git a/plonky2/src/util/context_tree.rs b/plonky2/src/util/context_tree.rs index 565e2d35..a0a69971 100644 --- a/plonky2/src/util/context_tree.rs +++ b/plonky2/src/util/context_tree.rs @@ -30,7 +30,7 @@ impl ContextTree { } /// Whether this context is still in scope. - fn is_open(&self) -> bool { + const fn is_open(&self) -> bool { self.exit_gate_count.is_none() } diff --git a/plonky2/src/util/mod.rs b/plonky2/src/util/mod.rs index 9a54cea0..fc2c4f09 100644 --- a/plonky2/src/util/mod.rs +++ b/plonky2/src/util/mod.rs @@ -27,7 +27,7 @@ pub fn transpose(matrix: &[Vec]) -> Vec> { .collect() } -pub(crate) fn reverse_bits(n: usize, num_bits: usize) -> usize { +pub(crate) const fn reverse_bits(n: usize, num_bits: usize) -> usize { // NB: The only reason we need overflowing_shr() here as opposed // to plain '>>' is to accommodate the case n == num_bits == 0, // which would become `0 >> 64`. Rust thinks that any shift of 64 diff --git a/plonky2/src/util/reducing.rs b/plonky2/src/util/reducing.rs index bde484e8..e1ba397b 100644 --- a/plonky2/src/util/reducing.rs +++ b/plonky2/src/util/reducing.rs @@ -28,7 +28,7 @@ pub struct ReducingFactor { } impl ReducingFactor { - pub fn new(base: F) -> Self { + pub const fn new(base: F) -> Self { Self { base, count: 0 } } @@ -117,7 +117,7 @@ pub struct ReducingFactorTarget { } impl ReducingFactorTarget { - pub fn new(base: ExtensionTarget) -> Self { + pub const fn new(base: ExtensionTarget) -> Self { Self { base, count: 0 } } diff --git a/plonky2/src/util/serialization/mod.rs b/plonky2/src/util/serialization/mod.rs index 1861cba2..94551bdf 100644 --- a/plonky2/src/util/serialization/mod.rs +++ b/plonky2/src/util/serialization/mod.rs @@ -2173,19 +2173,19 @@ pub struct Buffer<'a> { impl<'a> Buffer<'a> { /// Builds a new [`Buffer`] over `buffer`. #[inline] - pub fn new(bytes: &'a [u8]) -> Self { + pub const fn new(bytes: &'a [u8]) -> Self { Self { bytes, pos: 0 } } /// Returns the inner position. #[inline] - pub fn pos(&self) -> usize { + pub const fn pos(&self) -> usize { self.pos } /// Returns the inner buffer. #[inline] - pub fn bytes(&self) -> &'a [u8] { + pub const fn bytes(&self) -> &'a [u8] { self.bytes } diff --git a/plonky2/src/util/strided_view.rs b/plonky2/src/util/strided_view.rs index c165da2c..bab978a7 100644 --- a/plonky2/src/util/strided_view.rs +++ b/plonky2/src/util/strided_view.rs @@ -84,7 +84,7 @@ impl<'a, P: PackedField> PackedStridedView<'a, P> { } #[inline] - pub fn get(&self, index: usize) -> Option<&'a P> { + pub const fn get(&self, index: usize) -> Option<&'a P> { if index < self.length { // Cast scalar pointer to vector pointer. let res_ptr = unsafe { self.start_ptr.add(index * self.stride) }.cast(); @@ -109,7 +109,7 @@ impl<'a, P: PackedField> PackedStridedView<'a, P> { } #[inline] - pub fn iter(&self) -> PackedStridedViewIter<'a, P> { + pub const fn iter(&self) -> PackedStridedViewIter<'a, P> { PackedStridedViewIter::new( self.start_ptr, // See comment at the top of the `impl`. Below will point more than one byte past the @@ -120,12 +120,12 @@ impl<'a, P: PackedField> PackedStridedView<'a, P> { } #[inline] - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.length } #[inline] - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } } @@ -183,7 +183,7 @@ pub struct PackedStridedViewIter<'a, P: PackedField> { } impl<'a, P: PackedField> PackedStridedViewIter<'a, P> { - pub(self) fn new(start: *const P::Scalar, end: *const P::Scalar, stride: usize) -> Self { + pub(self) const fn new(start: *const P::Scalar, end: *const P::Scalar, stride: usize) -> Self { Self { start, end, diff --git a/plonky2/src/util/timing.rs b/plonky2/src/util/timing.rs index 42033038..faa74427 100644 --- a/plonky2/src/util/timing.rs +++ b/plonky2/src/util/timing.rs @@ -54,7 +54,7 @@ impl TimingTree { /// Whether this scope is still in scope. #[cfg(feature = "timing")] - fn is_open(&self) -> bool { + const fn is_open(&self) -> bool { self.exit_time.is_none() } diff --git a/starky/src/config.rs b/starky/src/config.rs index a593c827..24ddb6a7 100644 --- a/starky/src/config.rs +++ b/starky/src/config.rs @@ -14,7 +14,7 @@ pub struct StarkConfig { impl StarkConfig { /// A typical configuration with a rate of 2, resulting in fast but large proofs. /// Targets ~100 bit conjectured security. - pub fn standard_fast_config() -> Self { + pub const fn standard_fast_config() -> Self { Self { security_bits: 100, num_challenges: 2, diff --git a/starky/src/fibonacci_stark.rs b/starky/src/fibonacci_stark.rs index 28cd59f8..d34ccfd2 100644 --- a/starky/src/fibonacci_stark.rs +++ b/starky/src/fibonacci_stark.rs @@ -34,7 +34,7 @@ impl, const D: usize> FibonacciStark { // `num_rows`-th Fibonacci number. const PI_INDEX_RES: usize = 2; - fn new(num_rows: usize) -> Self { + const fn new(num_rows: usize) -> Self { Self { num_rows, _phantom: PhantomData, diff --git a/util/src/lib.rs b/util/src/lib.rs index 6cccd29c..6cd7ffb0 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -15,7 +15,7 @@ use crate::transpose_util::transpose_in_place_square; mod transpose_util; -pub fn bits_u64(n: u64) -> usize { +pub const fn bits_u64(n: u64) -> usize { (64 - n.leading_zeros()) as usize } @@ -25,7 +25,7 @@ pub const fn ceil_div_usize(a: usize, b: usize) -> usize { /// Computes `ceil(log_2(n))`. #[must_use] -pub fn log2_ceil(n: usize) -> usize { +pub const fn log2_ceil(n: usize) -> usize { (usize::BITS - n.saturating_sub(1).leading_zeros()) as usize }