forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: move
Sha3CodeGen
struct and rand_bytes
function to mock…
… crate (privacy-scaling-explorations#1481) ### Description 1. Move `Sha3CodeGen` and `rand_bytes` from bus-mapping to mock crate. Since they are only used for test. 2. Delete `rand` dependency for bus-mapping. ### Issue Link `cargo build` fails in `bus-mapping` folder. ### Type of change - [x] Bug fix (non-breaking change which fixes an issue)
- Loading branch information
1 parent
0ecd781
commit 1e1d104
Showing
10 changed files
with
129 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,101 @@ | ||
use eth_types::{bytecode, Bytecode, U256}; | ||
use rand::{rngs::ThreadRng, Rng}; | ||
|
||
/// Generate Sha3 opcode | ||
pub struct Sha3CodeGen { | ||
/// The offset | ||
pub offset: usize, | ||
/// The size | ||
pub size: usize, | ||
data_len: usize, | ||
rng: ThreadRng, | ||
} | ||
impl Sha3CodeGen { | ||
/// Construct with memory less than size | ||
pub fn mem_lt_size(offset: usize, size: usize) -> Self { | ||
let mut rng = rand::thread_rng(); | ||
let data_len = offset | ||
+ if size.gt(&0) { | ||
rng.gen_range(0..size) | ||
} else { | ||
0 | ||
}; | ||
Self { | ||
offset, | ||
size, | ||
data_len, | ||
rng, | ||
} | ||
} | ||
/// Construct with memory equal to size | ||
pub fn mem_eq_size(offset: usize, size: usize) -> Self { | ||
let data_len = offset + size; | ||
Self { | ||
offset, | ||
size, | ||
data_len, | ||
rng: rand::thread_rng(), | ||
} | ||
} | ||
/// Construct with memory greater than size | ||
pub fn mem_gt_size(offset: usize, size: usize) -> Self { | ||
let mut rng = rand::thread_rng(); | ||
let data_len = offset | ||
+ size | ||
+ if size.gt(&0) { | ||
rng.gen_range(0..size) | ||
} else { | ||
0 | ||
}; | ||
Self { | ||
offset, | ||
size, | ||
data_len, | ||
rng, | ||
} | ||
} | ||
/// Construct with empty memory | ||
pub fn mem_empty(offset: usize, size: usize) -> Self { | ||
Self { | ||
offset, | ||
size, | ||
data_len: 0, | ||
rng: rand::thread_rng(), | ||
} | ||
} | ||
fn rand_bytes(&mut self) -> Vec<u8> { | ||
(0..self.data_len) | ||
.map(|_| self.rng.gen()) | ||
.collect::<Vec<u8>>() | ||
} | ||
/// Generate bytecode for SHA3 opcode after having populated sufficient | ||
/// memory given the offset and size arguments for SHA3. | ||
pub fn gen_sha3_code(&mut self) -> (Bytecode, Vec<u8>) { | ||
let data = self.rand_bytes(); | ||
let mut memory = Vec::with_capacity(self.data_len); | ||
|
||
// add opcodes to populate memory in the current context. | ||
let mut code = Bytecode::default(); | ||
for (i, mem_chunk) in data.chunks(32).enumerate() { | ||
let mem_value = if mem_chunk.len() < 32 { | ||
std::iter::repeat(0u8) | ||
.take(32 - mem_chunk.len()) | ||
.chain(mem_chunk.to_vec()) | ||
.collect::<Vec<u8>>() | ||
} else { | ||
mem_chunk.to_vec() | ||
}; | ||
memory.extend_from_slice(&mem_value); | ||
code.op_mstore(32 * i, U256::from_big_endian(&mem_value)); | ||
} | ||
// append SHA3 related opcodes at the tail end. | ||
let code_tail = bytecode! { | ||
PUSH32(self.size) | ||
PUSH32(self.offset) | ||
SHA3 | ||
STOP | ||
}; | ||
code.append(&code_tail); | ||
(code, memory) | ||
} | ||
} |
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