Skip to content

Commit

Permalink
misc: don't call Default::default()
Browse files Browse the repository at this point in the history
Improve clarity by using `ConcreteType::default()` instead.
  • Loading branch information
jan-ferdinand committed Feb 10, 2024
1 parent c309d75 commit cb73d22
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 5 additions & 6 deletions triton-tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 7 additions & 13 deletions triton-tui/src/shadow_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion triton-tui/src/triton_vm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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..];
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()),
Expand All @@ -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(()),
Expand Down
12 changes: 9 additions & 3 deletions triton-vm/src/op_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}))
}

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/proof_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
6 changes: 3 additions & 3 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
};
Expand All @@ -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![],
};
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/constraint_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
};
Expand Down Expand Up @@ -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![],
};
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/jump_stack_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ExtJumpStackTable {
) -> Vec<ConstraintCircuitMonad<DualRowIndicator>> {
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()));
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/processor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
};
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl VMState {
jump_stack: vec![],
cycle_count: 0,
instruction_pointer: 0,
sponge_state: Default::default(),
sponge_state: None,
halting: false,
}
}
Expand Down

0 comments on commit cb73d22

Please sign in to comment.