Skip to content

Commit 419d511

Browse files
committed
clippy: Fixed clippy suggestions
1 parent c3172c6 commit 419d511

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

src/bus.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ pub trait BusDevice: std::fmt::Debug {
1111
fn as_bytes(&self) -> &Vec<u8>;
1212

1313
fn read2(&self, addr: VmAddr) -> Option<u16> {
14-
if let Some(x0) = self.read(addr) {
15-
if let Some(x1) = self.read(addr + 1) {
16-
return Some((x0 as u16) | ((x1 as u16) << 8));
17-
}
14+
if let Some(x0) = self.read(addr)
15+
&& let Some(x1) = self.read(addr + 1)
16+
{
17+
return Some((x0 as u16) | ((x1 as u16) << 8));
1818
};
19+
1920
None
2021
}
2122
fn write2(&mut self, addr: VmAddr, value: u16) -> Result<()> {
@@ -39,9 +40,7 @@ pub trait BusDevice: std::fmt::Debug {
3940
// So from and to are addresses, each address points to one byte in the memory -> [u8; 5000]
4041
// TODO: Maybe its better to pass whole Register object and access the value on that memory address by getter, instead of passing register address like that
4142
if let Some(bytes) = self.read2(from_addr) {
42-
if let Err(err) = self.write2(to_addr, bytes) {
43-
return Err(err);
44-
}
43+
self.write2(to_addr, bytes)?
4544
} else {
4645
return Err(VMError::CopyInstructionFail);
4746
}

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn start_vm() {
2121

2222
// Public inputs, used for the zk logic
2323
let mut public_inputs = ZkContext::new();
24-
if let Err(_) = public_inputs.set_public_program(program.clone()) {
24+
if public_inputs.set_public_program(program.clone()).is_err() {
2525
eprintln!("Error settings public inputs for program");
2626
}
2727

@@ -58,7 +58,10 @@ pub fn start_vm() {
5858
}
5959

6060
// Capture the OUTPUT state of the VM
61-
if let Err(_) = public_inputs.set_public_output(&vm.registers, &vm.memory) {
61+
if public_inputs
62+
.set_public_output(&vm.registers, &*vm.memory)
63+
.is_err()
64+
{
6265
eprintln!("Cannot capture the output state from the VM.");
6366
}
6467

src/register.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Register {
4848
pub fn new(register_type: RegisterId, value: VMWord) -> Self {
4949
Self {
5050
id: register_type,
51-
value: value,
51+
value,
5252
}
5353
}
5454

@@ -65,8 +65,8 @@ pub struct RegisterBank {
6565
pub register_map: BTreeMap<u8, Register>, // TODO: Storing registers like that is not the most efficient way, but i am going to leave it for now, to experiment with zk first.
6666
}
6767

68-
impl RegisterBank {
69-
pub fn new() -> Self {
68+
impl Default for RegisterBank {
69+
fn default() -> Self {
7070
let reg_hashmap: BTreeMap<u8, Register> = [
7171
(
7272
RegisterId::RR0.id(),
@@ -125,6 +125,13 @@ impl RegisterBank {
125125
register_map: reg_hashmap,
126126
}
127127
}
128+
}
129+
130+
impl RegisterBank {
131+
pub fn new() -> Self {
132+
Self::default()
133+
}
134+
128135
pub fn get_register_read_only(&self, name: u8) -> Result<Register> {
129136
if let Some(reg) = self.register_map.get(&name).copied() {
130137
Ok(reg)

src/utils.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ pub fn instruction_builder(opcode: u8, dest: u8, source: u8, immediate: u8) -> u
1010
// | | | | | | | | | | | | | | | |
1111
// -------------------------------------
1212
// Most significant Least significant
13-
let instruction = (opcode << 12) | (dest << 8) | (source << 4) | immediate;
14-
instruction
13+
(opcode << 12) | (dest << 8) | (source << 4) | immediate
1514
}
1615

1716
pub fn build_simple_program() -> Vec<u16> {
@@ -29,14 +28,13 @@ pub fn build_simple_program() -> Vec<u16> {
2928
// Store the result from r0 into memory
3029
let store_out = instruction_builder(0x06, 0x00, 0x00, 0x00);
3130

32-
let program = vec![
31+
// Program
32+
vec![
3333
load_imm_ix_rim,
3434
copy_ix_r0,
3535
load_imm_ix_rim2,
3636
copy_ix_r1,
3737
add_ix,
3838
store_out,
39-
];
40-
41-
program
39+
]
4240
}

src/vm.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ pub struct VM {
7575
pub zk_output_enabled: bool,
7676
}
7777

78-
impl VM {
79-
pub fn new() -> Self {
78+
impl Default for VM {
79+
fn default() -> Self {
8080
Self {
8181
registers: RegisterBank::new(),
8282
memory: Box::new(LinearMemory::new(0)),
@@ -86,6 +86,12 @@ impl VM {
8686
zk_output_enabled: false,
8787
}
8888
}
89+
}
90+
91+
impl VM {
92+
pub fn new() -> Self {
93+
Self::default()
94+
}
8995

9096
pub fn set_memory(&mut self, memory: Box<dyn BusDevice>) {
9197
self.memory = memory;
@@ -173,15 +179,14 @@ impl VM {
173179

174180
// If reg is RIM it will load the immediate value into that register immediately
175181
fn resolve_register_or_immediate(&mut self, reg_i: u8, imm_value: u16) -> Result<Register> {
176-
let reg;
177-
if reg_i == RegisterId::RIM.id() && imm_value != 0 {
182+
let reg = if reg_i == RegisterId::RIM.id() && imm_value != 0 {
178183
let tmp = self.registers.get_register_mut(reg_i)?;
179184
tmp.value = imm_value;
180-
reg = *tmp
185+
*tmp
181186
} else {
182187
// When i deref a mut ref i return a copy of the Register, not a ref to the original
183-
reg = *self.registers.get_register_mut(reg_i)?;
184-
}
188+
*self.registers.get_register_mut(reg_i)?
189+
};
185190
Ok(reg)
186191
}
187192

@@ -283,7 +288,11 @@ impl VMOperations for VM {
283288

284289
fn write(&mut self, source_reg: Register, destination_reg: Register) {
285290
// dst_reg is address
286-
if let Err(_) = self.memory.write2(destination_reg.value, source_reg.value) {
291+
if self
292+
.memory
293+
.write2(destination_reg.value, source_reg.value)
294+
.is_err()
295+
{
287296
self.halted = true;
288297
}
289298
}
@@ -366,7 +375,7 @@ So i decide how much bit/byte to give for my opcode when i decide how much uniqu
366375

367376
/// It depends on the OPCODE, sometimes reg.value is a bytes holding data already taken from memory, at other opcodes reg.value is an address pointing to a location in memory
368377
#[derive(Debug, Copy, Clone)]
369-
#[allow(non_camel_case_types)]
378+
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
370379
enum Opcode {
371380
// These are so called mnemonics, human-readable representations of machine instructions, used to make VM ISA easier to understand
372381
HALT,

src/zk.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ pub struct ZkContext {
2323
pub private_output_sha254: Fr,
2424
}
2525

26-
impl ZkContext {
27-
pub fn new() -> Self {
26+
impl Default for ZkContext {
27+
fn default() -> Self {
2828
Self {
2929
public_program_hash: Fr::ZERO,
3030
public_output_hash: Fr::ZERO,
3131
private_program_sha254: Fr::ZERO,
3232
private_output_sha254: Fr::ZERO,
3333
}
3434
}
35+
}
36+
37+
impl ZkContext {
38+
pub fn new() -> Self {
39+
Self::default()
40+
}
3541

3642
pub fn set_public_program(&mut self, program: Vec<VMWord>) -> Result<()> {
3743
let serialized_program = serialize(&program).unwrap();
@@ -53,7 +59,7 @@ impl ZkContext {
5359
pub fn set_public_output(
5460
&mut self,
5561
registers: &RegisterBank,
56-
memory: &Box<dyn BusDevice>,
62+
memory: &dyn BusDevice,
5763
) -> Result<()> {
5864
// Serialize registers and memory
5965
let pc = registers
@@ -98,8 +104,7 @@ impl Sha256Hash {
98104

99105
let hashed_value = hasher.finalize();
100106
let hashed_big_num = BigUint::from_bytes_be(&hashed_value);
101-
let sha_to_field = Sha256Hash::__sha256_to_field(&hashed_big_num);
102-
sha_to_field
107+
Sha256Hash::__sha256_to_field(&hashed_big_num)
103108
}
104109

105110
/// Hashes multiple byte slices using SHA256, concatenates them, reduces the result modulo the BN254 field,
@@ -112,8 +117,7 @@ impl Sha256Hash {
112117
}
113118
let hashed_value = hasher.finalize();
114119
let hashed_big_num = BigUint::from_bytes_be(&hashed_value);
115-
let sha_to_field = Sha256Hash::__sha256_to_field(&hashed_big_num);
116-
sha_to_field
120+
Sha256Hash::__sha256_to_field(&hashed_big_num)
117121
}
118122

119123
fn __sha256_to_field(sha256: &BigUint) -> Fr {
@@ -126,7 +130,6 @@ impl Sha256Hash {
126130
let reduced_sha = sha256 % modulus;
127131

128132
let bytes = reduced_sha.to_bytes_be();
129-
let field = Fr::from_be_bytes_mod_order(&bytes);
130-
field
133+
Fr::from_be_bytes_mod_order(&bytes)
131134
}
132135
}

0 commit comments

Comments
 (0)