Skip to content

Commit

Permalink
'while i < n' to 'while x != n' code optimization (keep-starknet-stra…
Browse files Browse the repository at this point in the history
…nge#210)

<!-- enter the gh issue after hash -->

- [✅] issue keep-starknet-strange#207 
- [✅] follows contribution
[guide](https://github.com/keep-starknet-strange/shinigami/blob/main/CONTRIBUTING.md)
- [✅] code change includes tests


<!-- PR description below -->

Change all the posible 'while i < n' to 'while x != n'. The ones that
were not changed generated certain errors in the tests

Closes keep-starknet-strange#207 



![image](https://github.com/user-attachments/assets/0c4b4a04-11a8-4304-9f04-6d563a0b2dbd)

---------

Co-authored-by: [email protected] <jsandinoDevjsandinoDevjsandinoDev>
Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
jsandinoDev and b-j-roberts authored Sep 17, 2024
1 parent 8681f6b commit 921f3f9
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub impl CompilerImpl of CompilerTrait {
let mut current = "";
let mut i = 0;
let script_len = script.len();
while i < script_len {
while i != script_len {
let char = script[i].into();
if char == seperator {
if current == "" {
Expand All @@ -354,7 +354,7 @@ pub impl CompilerImpl of CompilerTrait {
// Compile the script into bytecode
let mut i = 0;
let script_len = split_script.len();
while i < script_len {
while i != script_len {
let script_item = split_script.at(i);
if utils::is_hex(script_item) {
ByteArrayTrait::append(ref bytecode, @utils::hex_to_bytecode(script_item));
Expand Down
16 changes: 9 additions & 7 deletions src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub impl EngineImpl of EngineTrait {

let mut i = 0;
let mut valid_sizes = true;
while i < engine.scripts.len() {
while i != engine.scripts.len() {
let script = *(engine.scripts[i]);
if script.len() > MAX_SCRIPT_SIZE {
valid_sizes = false;
Expand Down Expand Up @@ -181,7 +181,7 @@ pub impl EngineImpl of EngineTrait {
let mut remaining = "";
let mut i = 1;
// TODO: Optimize
while i < sig_clone.len() {
while i != sig_clone.len() {
remaining.append_byte(sig_clone[i]);
i += 1;
};
Expand Down Expand Up @@ -225,7 +225,8 @@ pub impl EngineImpl of EngineTrait {
let script: @ByteArray = *(self.scripts[0]);
let mut i = 0;
let mut is_push_only = true;
while i < script.len() {
while i != script.len() {
// TODO: Error handling if i outside bounds
let opcode = script[i];
if opcode > Opcode::OP_16 {
is_push_only = false;
Expand Down Expand Up @@ -255,7 +256,7 @@ pub impl EngineImpl of EngineTrait {
if end > script.len() {
return Result::Err(Error::SCRIPT_INVALID);
}
while i < end {
while i != end {
data.append_byte(script[i]);
i += 1;
};
Expand All @@ -271,7 +272,7 @@ pub impl EngineImpl of EngineTrait {
if end > script.len() {
return Result::Err(Error::SCRIPT_INVALID);
}
while i < end {
while i != end {
data.append_byte(script[i]);
i += 1;
};
Expand Down Expand Up @@ -408,6 +409,7 @@ pub impl EngineImpl of EngineTrait {

fn execute(ref self: Engine) -> Result<ByteArray, felt252> {
let mut err = '';
// 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() {
Expand Down Expand Up @@ -532,7 +534,7 @@ pub impl EngineImpl of EngineTrait {
let ret_val = top_stack.clone();
let mut is_ok = false;
let mut i = 0;
while i < top_stack.len() {
while i != top_stack.len() {
if top_stack[i] != 0 {
is_ok = true;
break;
Expand Down Expand Up @@ -624,7 +626,7 @@ pub impl EngineImpl of EngineTrait {

let mut sub_script = "";
let mut i = self.last_code_sep;
while i < script.len() {
while i != script.len() {
sub_script.append_byte(script[i]);
i += 1;
};
Expand Down
2 changes: 1 addition & 1 deletion src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn byte_array_err(err: felt252) -> ByteArray {
let mut bytes = "";
let mut word_len = 0;
let mut byte_shift: u256 = 256;
while (err.into() / byte_shift) > 0 {
while (err.into() / byte_shift) != 0 {
word_len += 1;
byte_shift *= 256;
};
Expand Down
16 changes: 8 additions & 8 deletions src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn opcode_sha256(ref engine: Engine) -> Result<(), felt252> {
let res = compute_sha256_byte_array(arr).span();
let mut res_bytes: ByteArray = "";
let mut i: usize = 0;
while i < res.len() {
while i != res.len() {
res_bytes.append_word((*res[i]).into(), 4);
i += 1;
};
Expand All @@ -30,7 +30,7 @@ pub fn opcode_hash160(ref engine: Engine) -> Result<(), felt252> {
let res = compute_sha256_byte_array(@m).span();
let mut res_bytes: ByteArray = "";
let mut i: usize = 0;
while i < res.len() {
while i != res.len() {
res_bytes.append_word((*res[i]).into(), 4);
i += 1;
};
Expand All @@ -44,14 +44,14 @@ pub fn opcode_hash256(ref engine: Engine) -> Result<(), felt252> {
let res = compute_sha256_byte_array(@m).span();
let mut res_bytes: ByteArray = "";
let mut i: usize = 0;
while i < res.len() {
while i != res.len() {
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() {
while j != res2.len() {
res2_bytes.append_word((*res2[j]).into(), 4);
j += 1;
};
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn opcode_checkmultisig(ref engine: Engine) -> Result<(), felt252> {
let mut pub_keys = ArrayTrait::<ByteArray>::new();
let mut i: i64 = 0;
let mut err: felt252 = 0;
while i < num_pub_keys {
while i != num_pub_keys {
match engine.dstack.pop_byte_array() {
Result::Ok(pk) => pub_keys.append(pk),
Result::Err(e) => err = e
Expand All @@ -151,7 +151,7 @@ pub fn opcode_checkmultisig(ref engine: Engine) -> Result<(), felt252> {
let mut sigs = ArrayTrait::<ByteArray>::new();
i = 0;
err = 0;
while i < num_sigs {
while i != num_sigs {
match engine.dstack.pop_byte_array() {
Result::Ok(s) => sigs.append(s),
Result::Err(e) => err = e
Expand All @@ -173,7 +173,7 @@ pub fn opcode_checkmultisig(ref engine: Engine) -> Result<(), felt252> {

// TODO: add witness context inside engine to check if witness is active
let mut s: u32 = 0;
while s < sigs.len() {
while s != sigs.len() {
script = signature::remove_signature(script, sigs.at(s));
s += 1;
};
Expand All @@ -183,7 +183,7 @@ pub fn opcode_checkmultisig(ref engine: Engine) -> Result<(), felt252> {
let mut pub_key_idx: i64 = -1;
let mut sig_idx: i64 = 0;

while num_sigs > 0 {
while num_sigs != 0 {
pub_key_idx += 1;
num_pub_keys -= 1;
if num_sigs > num_pub_keys {
Expand Down
2 changes: 1 addition & 1 deletion src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ pub mod Opcode {
if end > script.len() {
return Result::Err(Error::SCRIPT_INVALID);
}
while i < end {
while i != end {
data.append_byte(script[i]);
i += 1;
};
Expand Down
4 changes: 2 additions & 2 deletions src/opcodes/tests/test_constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_op_1negate() {
fn test_op_data(value: u8) {
let mut hex_data: ByteArray = "0x";
let mut i = 0;
while i < value {
while i != value {
hex_data.append_word(int_to_hex(i + 1), 2);
i += 1;
};
Expand All @@ -78,7 +78,7 @@ fn test_op_data(value: u8) {
fn test_op_data_all() {
let mut n = 1;

while n <= 75 {
while n != 76 {
test_op_data(n);
n += 1;
}
Expand Down
8 changes: 4 additions & 4 deletions src/opcodes/tests/test_disabled.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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() {
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 +39,7 @@ 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() {
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 +53,7 @@ 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() {
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 +68,7 @@ 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() {
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
4 changes: 2 additions & 2 deletions src/opcodes/tests/test_stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn test_max_stack() {
let mut program: ByteArray = "";
let op_1_string = "OP_1 ";
let mut index: u64 = 0;
while index < 1000 {
while index != 1000 {
program.append(@op_1_string);
index += 1;
};
Expand All @@ -310,7 +310,7 @@ fn test_exceed_stack() {
let mut program: ByteArray = "";
let op_1_string = "OP_1 ";
let mut index: u64 = 0;
while index < 1001 {
while index != 1001 {
program.append(@op_1_string);
index += 1;
};
Expand Down
4 changes: 2 additions & 2 deletions src/scriptflags.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn parse_flags(flags: ByteArray) -> u32 {
let mut current = "";
let mut i = 0;
let flags_len = flags.len();
while i < flags_len {
while i != flags_len {
let char = flags[i].into();
if char == seperator {
if current == "" {
Expand All @@ -143,7 +143,7 @@ pub fn parse_flags(flags: ByteArray) -> u32 {
// Compile the flags into a single integer.
let mut i = 0;
let flags_len = split_flags.len();
while i < flags_len {
while i != flags_len {
let flag = split_flags.at(i);
let flag_value = flag_from_string(utils::byte_array_to_felt252_be(flag));
script_flags += flag_value;
Expand Down
4 changes: 2 additions & 2 deletions src/scriptnum/scriptnum.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub mod ScriptNum {
return Result::Ok(0);
}
let snap_input = @input;
while i < snap_input.len() - 1 {
while i != snap_input.len() - 1 {
result += snap_input.at(i).unwrap().into() * multiplier;
multiplier *= BYTESHIFT;
i += 1;
Expand Down Expand Up @@ -117,7 +117,7 @@ pub mod ScriptNum {
return Result::Ok(0);
}
let snap_input = @input;
while i < snap_input.len() - 1 {
while i != snap_input.len() - 1 {
result += snap_input.at(i).unwrap().into() * multiplier;
multiplier *= BYTESHIFT;
i += 1;
Expand Down
6 changes: 3 additions & 3 deletions src/signature/sighash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn calc_witness_transaction_hash(
// Serialize each input in the transaction.
let input_len: usize = transaction.transaction_inputs.len();
let mut i: usize = 0;
while i < input_len {
while i != input_len {
let input: @TransactionInput = transaction.transaction_inputs.at(i);

let input_txid: u256 = *input.previous_outpoint.txid;
Expand All @@ -69,7 +69,7 @@ pub fn calc_witness_transaction_hash(
let output_len: usize = transaction.transaction_outputs.len();

i = 0;
while i < output_len {
while i != output_len {
let output: @TransactionOutput = transaction.transaction_outputs.at(i);
let value: i64 = *output.value;
let script: @ByteArray = output.publickey_script;
Expand Down Expand Up @@ -133,7 +133,7 @@ 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() {
while i != sub_script.len() {
sig_hash_bytes.append_byte(sub_script[i]);
i += 1;
};
Expand Down
6 changes: 3 additions & 3 deletions src/signature/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn compare_data(script: @ByteArray, sig_bytes: @ByteArray, i: u32, push_data
let mut len: usize = push_data.into();
let mut found = true;

while j < len {
while j != len {
if script[i + j + 1] != sig_bytes[j] {
found = false;
break;
Expand Down Expand Up @@ -296,7 +296,7 @@ pub fn parse_signature(sig_bytes: @ByteArray) -> Result<Signature, felt252> {
let mut i = 0;

//Strip leading zero
while s_len > 0 && sig_bytes[i + r_len + 6] == 0x00 {
while s_len != 0 && sig_bytes[i + r_len + 6] == 0x00 {
sig_len -= 1;
s_len -= 1;
s_offset += 1;
Expand All @@ -307,7 +307,7 @@ pub fn parse_signature(sig_bytes: @ByteArray) -> Result<Signature, felt252> {

i = 0;

while r_len > 0 && sig_bytes[i + 4] == 0x00 {
while r_len != 0 && sig_bytes[i + 4] == 0x00 {
sig_len -= 1;
r_len -= 1;
r_offset += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/signature/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn transaction_procedure(
TransactionOutput
>::new();

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

Expand Down
Loading

0 comments on commit 921f3f9

Please sign in to comment.