Skip to content

Commit

Permalink
refac: refactor while I < data.len to let data_len = data.len() (#234)
Browse files Browse the repository at this point in the history
Resolves #218

---------

Co-authored-by: Gift-Naomi <emmanuelgiftnaomi.com>
Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
Mystic-Nayy and b-j-roberts authored Oct 4, 2024
1 parent 1f00826 commit e462096
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 28 deletions.
18 changes: 12 additions & 6 deletions packages/engine/src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ pub impl EngineExtrasImpl<T, +Drop<T>> of EngineExtrasTrait<T> {

let mut sub_script = "";
let mut i = self.last_code_sep;
while i != script.len() {
let script_len = script.len();
while i != script_len {
sub_script.append_byte(script[i]);
i += 1;
};
Expand Down Expand Up @@ -298,7 +299,8 @@ pub impl EngineInternalImpl of EngineInternalTrait {

let mut i = 0;
let mut valid_sizes = true;
while i != engine.scripts.len() {
let scripts_len = engine.scripts.len();
while i != scripts_len {
let script = *(engine.scripts[i]);
if script.len() > MAX_SCRIPT_SIZE {
valid_sizes = false;
Expand Down Expand Up @@ -338,7 +340,8 @@ pub impl EngineInternalImpl of EngineInternalTrait {
let mut remaining = "";
let mut i = 1;
// TODO: Optimize
while i != sig_clone.len() {
let sig_len = sig_clone.len();
while i != sig_len {
remaining.append_byte(sig_clone[i]);
i += 1;
};
Expand Down Expand Up @@ -382,7 +385,8 @@ pub impl EngineInternalImpl of EngineInternalTrait {
let script: @ByteArray = *(self.scripts[0]);
let mut i = 0;
let mut is_push_only = true;
while i != script.len() {
let script_len = script.len();
while i != script_len {
// TODO: Error handling if i outside bounds
let opcode = script[i];
if opcode > Opcode::OP_16 {
Expand Down Expand Up @@ -534,7 +538,8 @@ pub impl EngineInternalImpl of EngineInternalTrait {
// TODO: Optimize with != instead of < and check for bounds errors within the loop
while self.script_idx < self.scripts.len() {
let script: @ByteArray = *self.scripts[self.script_idx];
while self.opcode_idx < script.len() {
let script_len = script.len();
while self.opcode_idx < script_len {
let opcode = script[self.opcode_idx];

// Check if the opcode is always illegal (reserved).
Expand Down Expand Up @@ -656,7 +661,8 @@ pub impl EngineInternalImpl of EngineInternalTrait {
let ret_val = top_stack.clone();
let mut is_ok = false;
let mut i = 0;
while i != top_stack.len() {
let top_stack_len = top_stack.len();
while i != top_stack_len {
if top_stack[i] != 0 {
is_ok = true;
break;
Expand Down
15 changes: 10 additions & 5 deletions packages/engine/src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub fn opcode_sha256<T, +Drop<T>>(ref engine: Engine<T>) -> Result<(), felt252>
let res = compute_sha256_byte_array(arr).span();
let mut res_bytes: ByteArray = "";
let mut i: usize = 0;
while i != res.len() {
let end = res.len();
while i != end {
res_bytes.append_word((*res[i]).into(), 4);
i += 1;
};
Expand All @@ -33,7 +34,8 @@ pub fn opcode_hash160<T, +Drop<T>>(ref engine: Engine<T>) -> Result<(), felt252>
let res = compute_sha256_byte_array(@m).span();
let mut res_bytes: ByteArray = "";
let mut i: usize = 0;
while i != res.len() {
let end = res.len();
while i != end {
res_bytes.append_word((*res[i]).into(), 4);
i += 1;
};
Expand All @@ -47,14 +49,16 @@ pub fn opcode_hash256<T, +Drop<T>>(ref engine: Engine<T>) -> Result<(), felt252>
let res = compute_sha256_byte_array(@m).span();
let mut res_bytes: ByteArray = "";
let mut i: usize = 0;
while i != res.len() {
let end = res.len();
while i != end {
res_bytes.append_word((*res[i]).into(), 4);
i += 1;
};
let res2 = compute_sha256_byte_array(@res_bytes).span();
let mut res2_bytes: ByteArray = "";
let mut j: usize = 0;
while j != res2.len() {
let end = res2.len();
while j != end {
res2_bytes.append_word((*res2[j]).into(), 4);
j += 1;
};
Expand Down Expand Up @@ -204,7 +208,8 @@ pub fn opcode_checkmultisig<

// TODO: add witness context inside engine to check if witness is active
let mut s: u32 = 0;
while s != sigs.len() {
let end = sigs.len();
while s != end {
script = signature::remove_signature(script, sigs.at(s));
s += 1;
};
Expand Down
12 changes: 8 additions & 4 deletions packages/engine/src/opcodes/tests/test_disabled.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn disabled_opcodes() -> core::array::Array<ByteArray> {
fn test_op_code_disabled() {
let disabled_opcodes = disabled_opcodes();
let mut i: usize = 0;
while i != disabled_opcodes.len() {
let disabled_opcodes_len = disabled_opcodes.len();
while i != disabled_opcodes_len {
let mut engine = utils::test_compile_and_run_err(
disabled_opcodes.at(i).clone(), Error::OPCODE_DISABLED
);
Expand All @@ -39,7 +40,8 @@ fn test_op_code_disabled() {
fn test_disabled_opcodes_if_block() {
let disabled_opcodes = disabled_opcodes();
let mut i: usize = 0;
while i != disabled_opcodes.len() {
let disabled_opcodes_len = disabled_opcodes.len();
while i != disabled_opcodes_len {
let program = format!(
"OP_1 OP_IF {} OP_ELSE OP_DROP OP_ENDIF", disabled_opcodes.at(i).clone()
);
Expand All @@ -53,7 +55,8 @@ fn test_disabled_opcodes_if_block() {
fn test_disabled_opcodes_else_block() {
let disabled_opcodes = disabled_opcodes();
let mut i: usize = 0;
while i != disabled_opcodes.len() {
let disabled_opcodes_len = disabled_opcodes.len();
while i != disabled_opcodes_len {
let program = format!(
"OP_0 OP_IF OP_DROP OP_ELSE {} OP_ENDIF", disabled_opcodes.at(i).clone()
);
Expand All @@ -68,7 +71,8 @@ fn test_disabled_opcodes_else_block() {
fn test_disabled_opcode_in_unexecd_if_block() {
let disabled_opcodes = disabled_opcodes();
let mut i: usize = 0;
while i != disabled_opcodes.len() {
let disabled_opcodes_len = disabled_opcodes.len();
while i != disabled_opcodes_len {
let program = format!(
"OP_0 OP_IF {} OP_ELSE OP_DROP OP_ENDIF", disabled_opcodes.at(i).clone()
);
Expand Down
6 changes: 4 additions & 2 deletions packages/engine/src/scriptnum.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub mod ScriptNum {
return Result::Ok(0);
}
let snap_input = @input;
while i != snap_input.len() - 1 {
let end = snap_input.len() - 1;
while i != end {
result += snap_input.at(i).unwrap().into() * multiplier;
multiplier *= BYTESHIFT;
i += 1;
Expand Down Expand Up @@ -117,7 +118,8 @@ pub mod ScriptNum {
return Result::Ok(0);
}
let snap_input = @input;
while i != snap_input.len() - 1 {
let end = snap_input.len() - 1;
while i != end {
result += snap_input.at(i).unwrap().into() * multiplier;
multiplier *= BYTESHIFT;
i += 1;
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/src/signature/sighash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ pub fn calc_witness_transaction_hash(
sig_hash_bytes.append_byte(0xa9);
sig_hash_bytes.append_byte(0x14);
i = 2;
while i != sub_script.len() {
let subscript_len = sub_script.len();
while i != subscript_len {
sig_hash_bytes.append_byte(sub_script[i]);
i += 1;
};
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/src/signature/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ pub fn remove_signature(script: ByteArray, sig_bytes: @ByteArray) -> ByteArray {
let mut processed_script: ByteArray = "";
let mut i: usize = 0;

while i < script.len() {
let script_len = script.len();
while i < script_len {
let push_data: u8 = script[i];
if push_data >= 8 && push_data <= 72 {
let mut len: usize = push_data.into();
Expand All @@ -420,7 +421,7 @@ pub fn remove_signature(script: ByteArray, sig_bytes: @ByteArray) -> ByteArray {
continue;
}
processed_script.append_byte(push_data);
while len != 0 && i - len < script.len() {
while len != 0 && i - len < script_len {
processed_script.append_byte(script[i - len + 1]);
len -= 1;
};
Expand Down
6 changes: 4 additions & 2 deletions packages/engine/src/signature/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub fn remove_opcodeseparator(script: @ByteArray) -> @ByteArray {
let mut i: usize = 0;

// TODO: tokenizer/standardize script parsing
while i < script.len() {
let script_len = script.len();
while i < script_len {
let opcode = script[i];
// TODO: Error handling
if opcode == Opcode::OP_CODESEPARATOR {
Expand Down Expand Up @@ -92,7 +93,8 @@ pub fn transaction_procedure<
TransactionOutput
>::new();

while i != transaction_input.len() {
let tx_input_len = transaction_input.len();
while i != tx_input_len {
// TODO: Optimize this
let mut temp_transaction_input: TransactionInput = transaction_input[i].clone();

Expand Down
8 changes: 5 additions & 3 deletions packages/engine/src/stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ pub impl ScriptStackImpl of ScriptStackTrait {
fn json(ref self: ScriptStack) {
let mut i = 0;
print!("[");
while i != self.len {
let end = self.len;
while i != end {
let (entry, arr) = self.data.entry(i.into());
let arr = arr.deref();
print!("\"{}\"", bytecode_to_hex(@arr.clone()));
self.data = entry.finalize(NullableTrait::new(arr));
if i < self.len - 1 {
if i < end - 1 {
print!(",");
}
i += 1;
Expand Down Expand Up @@ -145,7 +146,8 @@ pub impl ScriptStackImpl of ScriptStackTrait {
fn stack_to_span(ref self: ScriptStack) -> Span<ByteArray> {
let mut result = array![];
let mut i = 0;
while i != self.len {
let end = self.len;
while i != end {
let (entry, arr) = self.data.entry(i.into());
let arr = arr.deref();
result.append(arr.clone());
Expand Down
9 changes: 6 additions & 3 deletions packages/utils/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub fn byte_array_value_at_be(byte_array: @ByteArray, ref offset: usize, len: us
let byte_shift = 256;
let mut value = 0;
let mut i = offset;
while i != offset + len {
let end = offset + len;
while i != end {
value = value * byte_shift + byte_array[i].into();
i += 1;
};
Expand Down Expand Up @@ -59,7 +60,8 @@ pub fn byte_array_value_at_le(
pub fn sub_byte_array(byte_array: @ByteArray, ref offset: usize, len: usize) -> ByteArray {
let mut sub_byte_array = "";
let mut i = offset;
while i != offset + len {
let end = offset + len;
while i != end {
sub_byte_array.append_byte(byte_array[i]);
i += 1;
};
Expand Down Expand Up @@ -116,7 +118,8 @@ pub fn u256_from_byte_array_with_offset(arr: @ByteArray, offset: usize, len: usi
pub fn byte_array_to_bool(bytes: @ByteArray) -> bool {
let mut i = 0;
let mut ret_bool = false;
while i < bytes.len() {
let byte_array_len = bytes.len();
while i != byte_array_len {
if bytes.at(i).unwrap() != 0 {
// Can be negative zero
if i == bytes.len() - 1 && bytes.at(i).unwrap() == 0x80 {
Expand Down

0 comments on commit e462096

Please sign in to comment.