Skip to content

Commit

Permalink
Merge pull request #1 from 0xPolygonHermez/feature/wc
Browse files Browse the repository at this point in the history
Merge new folders structure
  • Loading branch information
eduadiez authored Jul 16, 2024
2 parents 7c53684 + eb3160e commit 8c58a79
Show file tree
Hide file tree
Showing 28 changed files with 1,314 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
[workspace]
members = ["cli","examples/fib"]
members = [
"cli",
"common",
"examples/fib",
"riscv/riscv2zisk",
"simulator",
"state-machines/main",
"state-machines/mem",
"witness-computation"
]

resolver = "2"

Expand Down
9 changes: 9 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "zisk-common"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = { version = "1.0.66", features = ["preserve_order"] }
indexmap = { version = "2.2.6", features = ["serde"] }
5 changes: 5 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod zisk_opcodes;
mod rom;

pub use zisk_opcodes::*;
pub use rom::*;
197 changes: 197 additions & 0 deletions common/src/rom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
use indexmap::IndexMap;
use serde::{Deserialize, Deserializer};
use serde_json::Value;

use crate::ZiskOperator;

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct RomProgram {
#[serde(rename = "nextInitInstAddr")]
pub next_init_inst_addr: usize,
#[serde(rename = "insts")]
#[serde(deserialize_with = "deserialize_insts")]
pub insts: IndexMap<String, RomInstruction>,
#[serde(rename = "roData")]
pub ro_data: Vec<RomRoData>,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct RomInstruction {
pub paddr: u64,
#[serde(deserialize_with = "deserialize_bool")]
pub store_ra: bool,
#[serde(deserialize_with = "deserialize_bool")]
pub store_use_sp: bool,
pub store: RomStore,
pub store_offset: i64,
#[serde(deserialize_with = "deserialize_bool")]
pub set_pc: bool,
#[serde(deserialize_with = "deserialize_bool")]
pub set_sp: bool,
pub ind_width: u64,
pub inc_sp: i64,
#[serde(deserialize_with = "deserialize_bool")]
pub end: bool,
pub a_src: RomSrc,
pub a_use_sp_imm1: isize,
pub a_offset_imm0: isize,
pub b_src: RomSrc,
pub b_use_sp_imm1: isize,
pub b_offset_imm0: isize,
pub jmp_offset1: isize,
pub jmp_offset2: isize,
#[serde(deserialize_with = "deserialize_bool")]
pub is_external_op: bool,
pub op: ZiskOperator,
#[serde(rename = "opStr")]
pub op_str: String,
pub verbose: String,
}

#[derive(Debug)]
pub enum RomStore {
StoreNone,
StoreMem,
StoreInd,
}

impl<'de> Deserialize<'de> for RomStore {
fn deserialize<D>(deserializer: D) -> Result<RomStore, D::Error>
where
D: Deserializer<'de>,
{
let value: u64 = Deserialize::deserialize(deserializer)?;
match value {
0 => Ok(RomStore::StoreNone),
1 => Ok(RomStore::StoreMem),
2 => Ok(RomStore::StoreInd),
_ => Err(serde::de::Error::custom("Invalid value for RomStore")),
}
}
}

#[derive(Debug)]
pub enum RomSrc {
SrcC,
SrcMem,
SrcImm,
SrcStep,
SrcSp,
SrcInd,
}

impl<'de> Deserialize<'de> for RomSrc {
fn deserialize<D>(deserializer: D) -> Result<RomSrc, D::Error>
where
D: Deserializer<'de>,
{
let value: u64 = Deserialize::deserialize(deserializer)?;
match value {
0 => Ok(RomSrc::SrcC),
1 => Ok(RomSrc::SrcMem),
2 => Ok(RomSrc::SrcImm),
3 => Ok(RomSrc::SrcStep),
4 => Ok(RomSrc::SrcSp),
5 => Ok(RomSrc::SrcInd),
_ => Err(serde::de::Error::custom("Invalid value for RomSrc")),
}
}
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct RomRoData {
pub start: usize,
pub data: RomRoData2,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct RomRoData2 {
#[serde(rename = "type")]
pub type_: RomRoDataType,
pub data: Vec<usize>,
}

#[derive(Debug, Deserialize)]
pub enum RomRoDataType {
#[serde(rename = "Buffer")]
Buffer,
}

fn deserialize_insts<'de, D>(deserializer: D) -> Result<IndexMap<String, RomInstruction>, D::Error>
where
D: Deserializer<'de>,
{
let value: IndexMap<String, Value> = Deserialize::deserialize(deserializer)?;
value
.into_iter()
.map(|(k, v)| serde_json::from_value(v).map(|inst| (k, inst)).map_err(serde::de::Error::custom))
.collect()
}

fn deserialize_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let value: u8 = Deserialize::deserialize(deserializer)?;
match value {
0 => Ok(false),
1 => Ok(true),
_ => Err(serde::de::Error::custom("expected 0 or 1")),
}
}

#[allow(dead_code)]
impl RomProgram {
pub fn from_file(file_path: &str) -> Result<RomProgram, std::io::Error> {
let path = std::path::Path::new(file_path);
if !path.exists() {
println!("File {} does not exist", file_path);
return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "File not found"));
} else {
println!("File exists");
}
let file_contents = std::fs::read_to_string(file_path)?;

let parsed_json: RomProgram = serde_json::from_str(&file_contents)?;

Ok(parsed_json)
}

pub fn from_json(input_json: &str) -> Result<RomProgram, serde_json::Error> {
let parsed_json: RomProgram = serde_json::from_str(input_json)?;

Ok(parsed_json)
}
}

#[cfg(test)]
mod tests {
use super::*;

// Test deserialization and parsing of JSON input
#[test]
fn test_parse_rom_json() {
let rom_program_json = RomProgram::from_file("./data/rom.json");
assert!(rom_program_json.is_ok());

// let rom_program = rom_program_json.unwrap();
// println!("{:?}", rom_program.insts);
}

// Test deserialization and parsing of JSON input with wrong fields
#[test]
fn test_parse_input_json_wrong_fields() {
let input_json = r#"
{
"wrong": "fields"
}
"#;

let rom_program_json = RomProgram::from_json(input_json);
assert!(rom_program_json.is_err());
}
}
118 changes: 118 additions & 0 deletions common/src/zisk_opcodes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use serde::{de, Deserialize, Deserializer};

#[allow(dead_code)]
#[derive(Debug)]
pub enum ZiskOperator {
Flag,
CopyB,
SignExtendB,
SignExtendH,
SignExtendW,
Add,
AddW,
Sub,
SubW,
Sll,
SllW,
Sra,
Srl,
SraW,
SrlW,
Eq,
EqW,
Ltu,
Lt,
LtuW,
LtW,
Leu,
Le,
LeuW,
LeW,
And,
Or,
Xor,
Mulu,
Mul,
MulW,
MulUh,
MulH,
MulSuh,
Divu,
Div,
DivuW,
DivW,
Remu,
Rem,
RemuW,
RemW,
Minu,
Min,
MinuW,
MinW,
Maxu,
Max,
MaxuW,
MaxW,
}

impl<'de> Deserialize<'de> for ZiskOperator {
fn deserialize<D>(deserializer: D) -> Result<ZiskOperator, D::Error>
where
D: Deserializer<'de>,
{
let value: u8 = Deserialize::deserialize(deserializer)?;
match value {
0x00 => Ok(ZiskOperator::Flag),
0x01 => Ok(ZiskOperator::CopyB),
0x02 => Ok(ZiskOperator::SignExtendB),
0x03 => Ok(ZiskOperator::SignExtendH),
0x04 => Ok(ZiskOperator::SignExtendW),
0x10 => Ok(ZiskOperator::Add),
0x14 => Ok(ZiskOperator::AddW),
0x20 => Ok(ZiskOperator::Sub),
0x24 => Ok(ZiskOperator::SubW),
0x30 => Ok(ZiskOperator::Sll),
0x34 => Ok(ZiskOperator::SllW),
0x40 => Ok(ZiskOperator::Sra),
0x41 => Ok(ZiskOperator::Srl),
0x44 => Ok(ZiskOperator::SraW),
0x45 => Ok(ZiskOperator::SrlW),
0x50 => Ok(ZiskOperator::Eq),
0x54 => Ok(ZiskOperator::EqW),
0x60 => Ok(ZiskOperator::Ltu),
0x61 => Ok(ZiskOperator::Lt),
0x64 => Ok(ZiskOperator::LtuW),
0x65 => Ok(ZiskOperator::LtW),
0x70 => Ok(ZiskOperator::Leu),
0x71 => Ok(ZiskOperator::Le),
0x74 => Ok(ZiskOperator::LeuW),
0x75 => Ok(ZiskOperator::LeW),
0x80 => Ok(ZiskOperator::And),
0x90 => Ok(ZiskOperator::Or),
0xa0 => Ok(ZiskOperator::Xor),
0xb0 => Ok(ZiskOperator::Mulu),
0xb1 => Ok(ZiskOperator::Mul),
0xb5 => Ok(ZiskOperator::MulW),
0xb8 => Ok(ZiskOperator::MulUh),
0xb9 => Ok(ZiskOperator::MulH),
0xbb => Ok(ZiskOperator::MulSuh),
0xc0 => Ok(ZiskOperator::Divu),
0xc1 => Ok(ZiskOperator::Div),
0xc4 => Ok(ZiskOperator::DivuW),
0xc5 => Ok(ZiskOperator::DivW),
0xc8 => Ok(ZiskOperator::Remu),
0xc9 => Ok(ZiskOperator::Rem),
0xcc => Ok(ZiskOperator::RemuW),
0xcd => Ok(ZiskOperator::RemW),
0xd0 => Ok(ZiskOperator::Minu),
0xd1 => Ok(ZiskOperator::Min),
0xd4 => Ok(ZiskOperator::MinuW),
0xd5 => Ok(ZiskOperator::MinW),
0xe0 => Ok(ZiskOperator::Maxu),
0xe1 => Ok(ZiskOperator::Max),
0xe4 => Ok(ZiskOperator::MaxuW),
0xe5 => Ok(ZiskOperator::MaxW),
_ => Err(de::Error::custom(format!("Unknown ZiskOperator code: {}", value))),
}
}
}
6 changes: 6 additions & 0 deletions riscv/riscv2zisk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "riscv2zisk"
version = "0.1.0"
edition = "2021"

[dependencies]
Empty file added riscv/riscv2zisk/src/lib.rs
Empty file.
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn_params_layout = "Tall"
use_small_heuristics = "Max"
max_width = 120
reorder_modules = false
reorder_imports = false
6 changes: 6 additions & 0 deletions simulator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "zisk-simulator"
version = "0.1.0"
edition = "2021"

[dependencies]
Empty file added simulator/src/lib.rs
Empty file.
18 changes: 18 additions & 0 deletions state-machines/main/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "sm-main"
version = "0.1.0"
edition = "2021"

[dependencies]
zisk-common = { path = "../../common" }
# common = { git = "https://github.com/0xPolygonHermez/pil2-proofman" }
# proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman" }
# wchelpers = { git = "https://github.com/0xPolygonHermez/pil2-proofman" }
log = { version = "0.4", default-features = false }
p3-goldilocks = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" }
p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" }

#Local development
common = { path = "../../../pil2-proofman/common" }
proofman = { path = "../../../pil2-proofman/proofman" }
wchelpers = { path = "../../../pil2-proofman/wchelpers" }
Loading

0 comments on commit 8c58a79

Please sign in to comment.