-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:manojkgorle/brainfuckvm
- Loading branch information
Showing
7 changed files
with
353 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::fields::{Field, FieldElement}; | ||
use super::{Table, roundup_npow2, derive_omicron}; | ||
pub struct InstructionTable { | ||
pub table: Table, | ||
} | ||
|
||
pub enum Indices { | ||
// Named indices for base columns | ||
Address, | ||
CurrentInstruction, | ||
NextInstruction, | ||
// Named indices for extension columns | ||
PermutationArg, | ||
EvaluationArg, | ||
} | ||
|
||
impl InstructionTable { | ||
pub fn new(field: Field, length:u128, num_randomizers: u128, generator: FieldElement, order: u128) -> Self { | ||
let base_width = 3; | ||
let full_width = base_width + 2; | ||
let height = roundup_npow2(length); | ||
let omicron = derive_omicron(generator, order, height); | ||
let matrix = vec![vec![FieldElement::zero(field); full_width as usize]; height as usize]; | ||
let table = Table::new(field, base_width, full_width, length, num_randomizers, height, omicron, generator, order, matrix); | ||
Self { table: table } | ||
} | ||
|
||
pub fn get_table(&self) -> &Table { | ||
&self.table | ||
} | ||
|
||
// Note: Before padding initiate the matrix in table. | ||
// Add padding rows to convert the matrix derived from trace to a matrix of length of a power of 2 | ||
pub fn pad(&mut self) { | ||
let zero = FieldElement::new(0, self.table.field); | ||
for _ in 0..(self.table.height - self.table.length) { | ||
let new_row = vec![self.table.matrix.last().unwrap()[Indices::Address as usize],zero, zero, zero, zero]; | ||
self.table.matrix.push(new_row); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::fields::{Field, FieldElement}; | ||
use super::processor::Indices as ProcessorIndices; | ||
use super::Table; | ||
pub struct Memory { | ||
pub table : Table, | ||
} | ||
|
||
pub enum Indices { | ||
// Named indices for base columns | ||
Cycle, | ||
MemoryPointer, | ||
MemoryValue, | ||
Dummy, | ||
// Named indices for extension columns | ||
PermutationArg, | ||
} | ||
|
||
impl Memory { | ||
pub fn derive_matrix(processor_matrix: &[Vec::<FieldElement>]) -> Vec<Vec<FieldElement>> { | ||
let field = processor_matrix[0][0].1; | ||
let zero = FieldElement::zero(field); | ||
let one = FieldElement::one(field); | ||
|
||
let mut matrix: Vec<Vec<FieldElement>> = processor_matrix | ||
.iter() | ||
.filter(|pt| pt[ProcessorIndices::CurrentInstruction as usize] != zero) | ||
.map(|pt| vec![ | ||
pt[ProcessorIndices::Cycle as usize].clone(), | ||
pt[ProcessorIndices::MemoryPointer as usize].clone(), | ||
pt[ProcessorIndices::MemoryValue as usize].clone(), | ||
zero, // Equivalent to 'zero' in Python | ||
]) | ||
.collect(); | ||
|
||
// Sort by memory_pointer | ||
matrix.sort_by(|a, b| { | ||
a[Indices::MemoryPointer as usize] | ||
.partial_cmp(&b[Indices::MemoryPointer as usize]) | ||
.unwrap_or(std::cmp::Ordering::Equal) | ||
}); | ||
|
||
// Insert dummy rows for smooth clock jumps | ||
let mut i = 0; | ||
while i < matrix.len() - 1 { | ||
if matrix[i][Indices::MemoryPointer as usize] == matrix[i + 1][Indices::MemoryPointer as usize] && matrix[i + 1][Indices::Cycle as usize] != matrix[i][Indices::Cycle as usize] + one{ | ||
matrix.insert(i + 1,vec![ | ||
matrix[i][Indices::Cycle as usize].clone() + one, | ||
matrix[i][Indices::MemoryPointer as usize].clone(), | ||
matrix[i][Indices::MemoryValue as usize].clone(), | ||
one | ||
], | ||
); | ||
} | ||
i += 1; | ||
} | ||
matrix | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pub enum Indices { | ||
// Named indices for registers | ||
Cycle, | ||
InstructionPointer, | ||
CurrentInstruction, | ||
NextInstruction, | ||
MemoryPointer, | ||
MemoryValue, | ||
MemoryValueInvers, | ||
// Named indices for extension columns | ||
InstructionPermutaion, | ||
MemoryPermuation, | ||
InputEvaluation, | ||
OutputEvaluation, | ||
} |
Oops, something went wrong.