From cb73d220c17713a6e6f97bbda4719fcc419e5d22 Mon Sep 17 00:00:00 2001 From: Jan Ferdinand Sauer Date: Sat, 10 Feb 2024 17:47:41 +0100 Subject: [PATCH] misc: don't call `Default::default()` Improve clarity by using `ConcreteType::default()` instead. --- Cargo.toml | 1 + triton-tui/src/action.rs | 11 +++++------ triton-tui/src/shadow_memory.rs | 20 +++++++------------- triton-tui/src/triton_vm_state.rs | 3 ++- triton-vm/src/fri.rs | 4 ++-- triton-vm/src/op_stack.rs | 12 +++++++++--- triton-vm/src/program.rs | 4 ++-- triton-vm/src/proof_stream.rs | 2 +- triton-vm/src/stark.rs | 6 +++--- triton-vm/src/table/challenges.rs | 4 ++-- triton-vm/src/table/constraint_circuit.rs | 4 ++-- triton-vm/src/table/jump_stack_table.rs | 2 +- triton-vm/src/table/processor_table.rs | 2 +- triton-vm/src/vm.rs | 2 +- 14 files changed, 39 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c24fcc2e..6788ffcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ unicode-width = "0.1" cast_lossless = "warn" cloned_instead_of_copied = "warn" copy_iterator = "warn" +default_trait_access = "warn" doc_link_with_quotes = "warn" expl_impl_clone_on_copy = "warn" explicit_deref_methods = "warn" diff --git a/triton-tui/src/action.rs b/triton-tui/src/action.rs index 1bb34ed3..c48e02e2 100644 --- a/triton-tui/src/action.rs +++ b/triton-tui/src/action.rs @@ -5,10 +5,9 @@ use itertools::Itertools; use serde::de::*; use serde::*; use triton_vm::instruction::Instruction; -use triton_vm::op_stack::NUM_OP_STACK_REGISTERS; -use triton_vm::prelude::*; use crate::mode::Mode; +use crate::shadow_memory::TopOfStack; #[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub(crate) enum Action { @@ -69,8 +68,8 @@ pub(crate) enum Toggle { #[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Arbitrary)] pub(crate) struct ExecutedInstruction { pub instruction: Instruction, - pub old_top_of_stack: [BFieldElement; NUM_OP_STACK_REGISTERS], - pub new_top_of_stack: [BFieldElement; NUM_OP_STACK_REGISTERS], + pub old_top_of_stack: TopOfStack, + pub new_top_of_stack: TopOfStack, } impl<'de> Deserialize<'de> for Action { @@ -169,8 +168,8 @@ impl<'de> Deserialize<'de> for Action { impl ExecutedInstruction { pub fn new( instruction: Instruction, - old_top_of_stack: [BFieldElement; NUM_OP_STACK_REGISTERS], - new_top_of_stack: [BFieldElement; NUM_OP_STACK_REGISTERS], + old_top_of_stack: TopOfStack, + new_top_of_stack: TopOfStack, ) -> Self { Self { instruction, diff --git a/triton-tui/src/shadow_memory.rs b/triton-tui/src/shadow_memory.rs index dae0dcd4..cdb7726e 100644 --- a/triton-tui/src/shadow_memory.rs +++ b/triton-tui/src/shadow_memory.rs @@ -12,6 +12,8 @@ use triton_vm::prelude::*; use crate::action::ExecutedInstruction; use crate::element_type_hint::ElementTypeHint; +pub(crate) type TopOfStack = [BFieldElement; NUM_OP_STACK_REGISTERS]; + /// Mimics the behavior of the actual memory. Helps debugging programs written for Triton VM by /// tracking (manually set) type hints next to stack or RAM elements. #[derive(Debug, Clone, Eq, PartialEq)] @@ -150,11 +152,7 @@ impl ShadowMemory { self.push(self.stack[dup_index].clone()); } - fn read_mem( - &mut self, - n: NumberOfWords, - old_top_of_stack: [BFieldElement; NUM_OP_STACK_REGISTERS], - ) { + fn read_mem(&mut self, n: NumberOfWords, old_top_of_stack: TopOfStack) { let ram_pointer_hint = self.pop(); let mut ram_pointer = old_top_of_stack[0]; for _ in 0..n.num_words() { @@ -165,11 +163,7 @@ impl ShadowMemory { self.push(ram_pointer_hint); } - fn write_mem( - &mut self, - n: NumberOfWords, - old_top_of_stack: [BFieldElement; NUM_OP_STACK_REGISTERS], - ) { + fn write_mem(&mut self, n: NumberOfWords, old_top_of_stack: TopOfStack) { let ram_pointer_hint = self.pop(); let mut ram_pointer = old_top_of_stack[0]; for _ in 0..n.num_words() { @@ -380,13 +374,13 @@ mod tests { type_hints.mimic_instruction(ExecutedInstruction::new( Instruction::WriteMem(num_words), top_of_stack_before_write, - Default::default(), + TopOfStack::default(), )); prop_assert_ne!(&initial_type_hints.stack, &type_hints.stack); type_hints.mimic_instruction(ExecutedInstruction::new( Instruction::ReadMem(num_words), top_of_stack_before_read, - Default::default(), + TopOfStack::default(), )); prop_assert_eq!(initial_type_hints.stack, type_hints.stack); } @@ -438,7 +432,7 @@ mod tests { let executed_instruction = ExecutedInstruction::new( Instruction::Hash, [BFieldElement::zero(); NUM_OP_STACK_REGISTERS], - Default::default(), + TopOfStack::default(), ); let mut type_hints = ShadowMemory::default(); diff --git a/triton-tui/src/triton_vm_state.rs b/triton-tui/src/triton_vm_state.rs index 0b5a32c9..6c68ceb3 100644 --- a/triton-tui/src/triton_vm_state.rs +++ b/triton-tui/src/triton_vm_state.rs @@ -17,6 +17,7 @@ use crate::action::*; use crate::args::TuiArgs; use crate::components::Component; use crate::shadow_memory::ShadowMemory; +use crate::shadow_memory::TopOfStack; #[derive(Debug)] pub(crate) struct TritonVMState { @@ -125,7 +126,7 @@ impl TritonVMState { Ok(NonDeterminism::from(&non_determinism)) } - fn top_of_stack(&self) -> [BFieldElement; NUM_OP_STACK_REGISTERS] { + fn top_of_stack(&self) -> TopOfStack { let stack_len = self.vm_state.op_stack.stack.len(); let index_of_lowest_accessible_element = stack_len - NUM_OP_STACK_REGISTERS; let top_of_stack = &self.vm_state.op_stack.stack[index_of_lowest_accessible_element..]; diff --git a/triton-vm/src/fri.rs b/triton-vm/src/fri.rs index 57c1ca8a..76f882bb 100644 --- a/triton-vm/src/fri.rs +++ b/triton-vm/src/fri.rs @@ -367,7 +367,7 @@ impl<'stream, H: AlgebraicHasher> FriVerifier<'stream, H> { tree_height: round.merkle_tree_height(), indexed_leaves, authentication_structure, - ..Default::default() + ..MerkleTreeInclusionProof::default() }; match inclusion_proof.verify(round.merkle_root) { true => Ok(()), @@ -391,7 +391,7 @@ impl<'stream, H: AlgebraicHasher> FriVerifier<'stream, H> { tree_height: round.merkle_tree_height(), indexed_leaves, authentication_structure, - ..Default::default() + ..MerkleTreeInclusionProof::default() }; match inclusion_proof.verify(round.merkle_root) { true => Ok(()), diff --git a/triton-vm/src/op_stack.rs b/triton-vm/src/op_stack.rs index 8fcedfe9..1828c4a0 100644 --- a/triton-vm/src/op_stack.rs +++ b/triton-vm/src/op_stack.rs @@ -648,10 +648,16 @@ mod tests { use super::*; + // For testing purposes only. + impl Default for OpStack { + fn default() -> Self { + OpStack::new(Digest::default()) + } + } + #[test] fn sanity() { - let digest = Default::default(); - let mut op_stack = OpStack::new(digest); + let mut op_stack = OpStack::default(); // verify height assert!(op_stack.len() == 16); @@ -726,7 +732,7 @@ mod tests { #[test] fn trying_to_access_first_underflow_element_never_panics() { - let mut op_stack = OpStack::new(Default::default()); + let mut op_stack = OpStack::default(); let way_too_long = 2 * op_stack.len(); for _ in 0..way_too_long { let _ = op_stack.pop(); diff --git a/triton-vm/src/program.rs b/triton-vm/src/program.rs index 2c8e73ac..e4d4f946 100644 --- a/triton-vm/src/program.rs +++ b/triton-vm/src/program.rs @@ -114,9 +114,9 @@ impl BFieldCodec for Program { Ok(Box::new(Program { instructions, - address_to_label: Default::default(), + address_to_label: HashMap::default(), breakpoints: vec![], - type_hints: Default::default(), + type_hints: HashMap::default(), })) } diff --git a/triton-vm/src/proof_stream.rs b/triton-vm/src/proof_stream.rs index c26c2636..e34eeb28 100644 --- a/triton-vm/src/proof_stream.rs +++ b/triton-vm/src/proof_stream.rs @@ -273,7 +273,7 @@ mod tests { tree_height, indexed_leaves, authentication_structure: auth_structure, - ..Default::default() + ..MerkleTreeInclusionProof::default() }; assert!(inclusion_proof.verify(merkle_tree.root())); } diff --git a/triton-vm/src/stark.rs b/triton-vm/src/stark.rs index cd1a846b..5d3de793 100644 --- a/triton-vm/src/stark.rs +++ b/triton-vm/src/stark.rs @@ -879,7 +879,7 @@ impl Stark { tree_height: merkle_tree_height, indexed_leaves: index_leaves(leaf_digests_base), authentication_structure: base_authentication_structure, - ..Default::default() + ..MerkleTreeInclusionProof::default() }; if !base_merkle_tree_inclusion_proof.verify(base_merkle_tree_root) { return Err(BaseCodewordAuthenticationFailure); @@ -905,7 +905,7 @@ impl Stark { tree_height: merkle_tree_height, indexed_leaves: index_leaves(leaf_digests_ext), authentication_structure: ext_authentication_structure, - ..Default::default() + ..MerkleTreeInclusionProof::default() }; if !ext_merkle_tree_inclusion_proof.verify(extension_tree_merkle_root) { return Err(ExtensionCodewordAuthenticationFailure); @@ -927,7 +927,7 @@ impl Stark { tree_height: merkle_tree_height, indexed_leaves: index_leaves(revealed_quotient_segments_digests), authentication_structure: revealed_quotient_authentication_structure, - ..Default::default() + ..MerkleTreeInclusionProof::default() }; if !quot_merkle_tree_inclusion_proof.verify(quotient_codeword_merkle_root) { return Err(QuotientCodewordAuthenticationFailure); diff --git a/triton-vm/src/table/challenges.rs b/triton-vm/src/table/challenges.rs index 58384423..f50b8cd7 100644 --- a/triton-vm/src/table/challenges.rs +++ b/triton-vm/src/table/challenges.rs @@ -317,7 +317,7 @@ pub(crate) mod tests { /// If no [`Claim`] is provided, a dummy claim is used. pub fn placeholder(claim: Option<&Claim>) -> Self { let dummy_claim = Claim { - program_digest: Default::default(), + program_digest: Digest::default(), input: vec![], output: vec![], }; @@ -328,7 +328,7 @@ pub(crate) mod tests { pub fn deterministic_placeholder(claim: Option<&Claim>) -> Self { let dummy_claim = Claim { - program_digest: Default::default(), + program_digest: Digest::default(), input: vec![], output: vec![], }; diff --git a/triton-vm/src/table/constraint_circuit.rs b/triton-vm/src/table/constraint_circuit.rs index 3bd058ce..603e3ea1 100644 --- a/triton-vm/src/table/constraint_circuit.rs +++ b/triton-vm/src/table/constraint_circuit.rs @@ -1464,7 +1464,7 @@ mod tests { println!("seed: {seed}"); let dummy_claim = Claim { - program_digest: Default::default(), + program_digest: Digest::default(), input: vec![], output: vec![], }; @@ -2115,7 +2115,7 @@ mod tests { // Use the Schwartz-Zippel lemma to check no two substitution rules are equal. let dummy_claim = Claim { - program_digest: Default::default(), + program_digest: Digest::default(), input: vec![], output: vec![], }; diff --git a/triton-vm/src/table/jump_stack_table.rs b/triton-vm/src/table/jump_stack_table.rs index 101198f9..f3d5f0ee 100644 --- a/triton-vm/src/table/jump_stack_table.rs +++ b/triton-vm/src/table/jump_stack_table.rs @@ -75,7 +75,7 @@ impl ExtJumpStackTable { ) -> Vec> { let one = circuit_builder.b_constant(1u32.into()); let call_opcode = - circuit_builder.b_constant(Instruction::Call(Default::default()).opcode_b()); + circuit_builder.b_constant(Instruction::Call(BFieldElement::default()).opcode_b()); let return_opcode = circuit_builder.b_constant(Instruction::Return.opcode_b()); let clk = circuit_builder.input(CurrentBaseRow(CLK.master_base_table_index())); diff --git a/triton-vm/src/table/processor_table.rs b/triton-vm/src/table/processor_table.rs index 3fe3a74d..16f6853b 100644 --- a/triton-vm/src/table/processor_table.rs +++ b/triton-vm/src/table/processor_table.rs @@ -3602,7 +3602,7 @@ pub(crate) mod tests { let programs = [triton_program!(call label label: halt)]; let test_rows = programs.map(|program| test_row_from_program(program, 0)); let debug_info = TestRowsDebugInfo { - instruction: Call(Default::default()), + instruction: Call(BFieldElement::default()), debug_cols_curr_row: vec![IP, CI, NIA, JSP, JSO, JSD], debug_cols_next_row: vec![IP, CI, NIA, JSP, JSO, JSD], }; diff --git a/triton-vm/src/vm.rs b/triton-vm/src/vm.rs index afe75490..c7073a5e 100644 --- a/triton-vm/src/vm.rs +++ b/triton-vm/src/vm.rs @@ -123,7 +123,7 @@ impl VMState { jump_stack: vec![], cycle_count: 0, instruction_pointer: 0, - sponge_state: Default::default(), + sponge_state: None, halting: false, } }