From 54f56ac8f1109cbf8f02ba1bde24463f0f07a5dc Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 12 Nov 2024 22:05:37 +0100 Subject: [PATCH] simplify BaseField for 0 and 1 --- .../src/components/instruction/table.rs | 8 ++++---- .../src/components/memory/table.rs | 6 +++--- crates/brainfuck_vm/src/compiler.rs | 3 ++- crates/brainfuck_vm/src/machine.rs | 14 +++++++------- crates/brainfuck_vm/src/registers.rs | 2 +- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/brainfuck_prover/src/components/instruction/table.rs b/crates/brainfuck_prover/src/components/instruction/table.rs index adee241..91564b8 100644 --- a/crates/brainfuck_prover/src/components/instruction/table.rs +++ b/crates/brainfuck_prover/src/components/instruction/table.rs @@ -139,12 +139,12 @@ mod tests { // Create a vector of rows to add to the table let rows = vec![ InstructionTableRow { - ip: BaseField::from(0), + ip: BaseField::zero(), ci: BaseField::from(43), ni: BaseField::from(91), }, InstructionTableRow { - ip: BaseField::from(1), + ip: BaseField::one(), ci: BaseField::from(91), ni: BaseField::from(9), }, @@ -165,7 +165,7 @@ mod tests { let mut instruction_table = InstructionTable::new(); // Create a row to add to the table let row = InstructionTableRow { - ip: BaseField::from(0), + ip: BaseField::zero(), ci: BaseField::from(43), ni: BaseField::from(91), }; @@ -182,7 +182,7 @@ mod tests { let instruction_table = InstructionTable::new(); // Create a row to search for in the table let row = InstructionTableRow { - ip: BaseField::from(0), + ip: BaseField::zero(), ci: BaseField::from(43), ni: BaseField::from(91), }; diff --git a/crates/brainfuck_prover/src/components/memory/table.rs b/crates/brainfuck_prover/src/components/memory/table.rs index 3fe5989..aac7cc5 100644 --- a/crates/brainfuck_prover/src/components/memory/table.rs +++ b/crates/brainfuck_prover/src/components/memory/table.rs @@ -278,7 +278,7 @@ mod tests { let mut memory_table = MemoryTable::new(); // Create a row to add to the table let row = MemoryTableRow { - clk: BaseField::from(0), + clk: BaseField::zero(), mp: BaseField::from(43), mv: BaseField::from(91), d: BaseField::zero(), @@ -296,7 +296,7 @@ mod tests { let memory_table = MemoryTable::new(); // Create a row to search for in the table let row = MemoryTableRow { - clk: BaseField::from(0), + clk: BaseField::zero(), mp: BaseField::from(43), mv: BaseField::from(91), d: BaseField::zero(), @@ -337,7 +337,7 @@ mod tests { expected_memory_table.add_rows(vec![ row1, row2, - MemoryTableRow::new_dummy(BaseField::from(1), BaseField::one(), BaseField::zero()), + MemoryTableRow::new_dummy(BaseField::one(), BaseField::one(), BaseField::zero()), MemoryTableRow::new_dummy(BaseField::from(2), BaseField::one(), BaseField::zero()), MemoryTableRow::new_dummy(BaseField::from(3), BaseField::one(), BaseField::zero()), MemoryTableRow::new_dummy(BaseField::from(4), BaseField::one(), BaseField::zero()), diff --git a/crates/brainfuck_vm/src/compiler.rs b/crates/brainfuck_vm/src/compiler.rs index a05c6b0..aeffba9 100644 --- a/crates/brainfuck_vm/src/compiler.rs +++ b/crates/brainfuck_vm/src/compiler.rs @@ -1,5 +1,6 @@ // Adapted from rkdud007 brainfuck-zkvm https://github.com/rkdud007/brainfuck-zkvm/blob/main/src/compiler.rs +use num_traits::Zero; use stwo_prover::core::fields::m31::BaseField; #[derive(Debug, Clone, Default)] @@ -20,7 +21,7 @@ impl Compiler { match *symbol { '[' => { - self.instructions.push(BaseField::from(0)); + self.instructions.push(BaseField::zero()); loop_stack.push(self.instructions.len() - 1); } ']' => { diff --git a/crates/brainfuck_vm/src/machine.rs b/crates/brainfuck_vm/src/machine.rs index 3839402..e849c7b 100644 --- a/crates/brainfuck_vm/src/machine.rs +++ b/crates/brainfuck_vm/src/machine.rs @@ -301,7 +301,7 @@ mod tests { let (mut machine, _) = create_test_machine(&code, &[]); machine.execute()?; - assert_eq!(machine.state.registers.mp, BaseField::from(1)); + assert_eq!(machine.state.registers.mp, BaseField::one()); Ok(()) } @@ -312,8 +312,8 @@ mod tests { let (mut machine, _) = create_test_machine(&code, &[]); machine.execute()?; - assert_eq!(machine.state.ram[0], BaseField::from(1)); - assert_eq!(machine.state.registers.mv, BaseField::from(1)); + assert_eq!(machine.state.ram[0], BaseField::one()); + assert_eq!(machine.state.registers.mv, BaseField::one()); Ok(()) } @@ -415,8 +415,8 @@ mod tests { let final_state = Registers { clk: BaseField::from(2), ip: BaseField::from(2), - ci: BaseField::from(0), - ni: BaseField::from(0), + ci: BaseField::zero(), + ni: BaseField::zero(), mp: BaseField::zero(), mv: BaseField::from(2), mvi: BaseField::from(2).inverse(), @@ -456,8 +456,8 @@ mod tests { let final_state = Registers { clk: BaseField::from(2), ip: BaseField::from(2), - ci: BaseField::from(0), - ni: BaseField::from(0), + ci: BaseField::zero(), + ni: BaseField::zero(), mp: BaseField::zero(), mv: BaseField::from(2), mvi: BaseField::from(2).inverse(), diff --git a/crates/brainfuck_vm/src/registers.rs b/crates/brainfuck_vm/src/registers.rs index 9b2676a..91044e3 100644 --- a/crates/brainfuck_vm/src/registers.rs +++ b/crates/brainfuck_vm/src/registers.rs @@ -83,7 +83,7 @@ mod tests { #[test] fn test_registers_display() { let registers = Registers { - clk: BaseField::from(1), + clk: BaseField::one(), ip: BaseField::from(2), ci: BaseField::from(3), ni: BaseField::from(4),