diff --git a/src/compiler.cairo b/src/compiler.cairo index 7d108f6b..2ce91900 100644 --- a/src/compiler.cairo +++ b/src/compiler.cairo @@ -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 == "" { @@ -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)); diff --git a/src/engine.cairo b/src/engine.cairo index 7469793e..d43c4008 100644 --- a/src/engine.cairo +++ b/src/engine.cairo @@ -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; @@ -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; }; @@ -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; @@ -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; }; @@ -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; }; @@ -408,6 +409,7 @@ pub impl EngineImpl of EngineTrait { fn execute(ref self: Engine) -> Result { 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() { @@ -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; @@ -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; }; diff --git a/src/errors.cairo b/src/errors.cairo index 99f75704..447347c6 100644 --- a/src/errors.cairo +++ b/src/errors.cairo @@ -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; }; diff --git a/src/opcodes/crypto.cairo b/src/opcodes/crypto.cairo index 62f0f174..62c96284 100644 --- a/src/opcodes/crypto.cairo +++ b/src/opcodes/crypto.cairo @@ -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; }; @@ -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; }; @@ -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; }; @@ -128,7 +128,7 @@ pub fn opcode_checkmultisig(ref engine: Engine) -> Result<(), felt252> { let mut pub_keys = ArrayTrait::::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 @@ -151,7 +151,7 @@ pub fn opcode_checkmultisig(ref engine: Engine) -> Result<(), felt252> { let mut sigs = ArrayTrait::::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 @@ -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; }; @@ -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 { diff --git a/src/opcodes/opcodes.cairo b/src/opcodes/opcodes.cairo index 1b14cbb5..34e2d015 100644 --- a/src/opcodes/opcodes.cairo +++ b/src/opcodes/opcodes.cairo @@ -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; }; diff --git a/src/opcodes/tests/test_constants.cairo b/src/opcodes/tests/test_constants.cairo index 5c03e226..ee01fe5a 100644 --- a/src/opcodes/tests/test_constants.cairo +++ b/src/opcodes/tests/test_constants.cairo @@ -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; }; @@ -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; } diff --git a/src/opcodes/tests/test_disabled.cairo b/src/opcodes/tests/test_disabled.cairo index 5cf85bbb..c2d5ff0f 100644 --- a/src/opcodes/tests/test_disabled.cairo +++ b/src/opcodes/tests/test_disabled.cairo @@ -26,7 +26,7 @@ fn disabled_opcodes() -> core::array::Array { 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 ); @@ -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() ); @@ -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() ); @@ -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() ); diff --git a/src/opcodes/tests/test_stack.cairo b/src/opcodes/tests/test_stack.cairo index 4d13a767..52740319 100644 --- a/src/opcodes/tests/test_stack.cairo +++ b/src/opcodes/tests/test_stack.cairo @@ -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; }; @@ -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; }; diff --git a/src/scriptflags.cairo b/src/scriptflags.cairo index 02e76797..51be0575 100644 --- a/src/scriptflags.cairo +++ b/src/scriptflags.cairo @@ -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 == "" { @@ -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; diff --git a/src/scriptnum/scriptnum.cairo b/src/scriptnum/scriptnum.cairo index 44030aab..89407eb7 100644 --- a/src/scriptnum/scriptnum.cairo +++ b/src/scriptnum/scriptnum.cairo @@ -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; @@ -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; diff --git a/src/signature/sighash.cairo b/src/signature/sighash.cairo index efd55b60..c2d87109 100644 --- a/src/signature/sighash.cairo +++ b/src/signature/sighash.cairo @@ -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; @@ -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; @@ -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; }; diff --git a/src/signature/signature.cairo b/src/signature/signature.cairo index ea5124aa..1618076e 100644 --- a/src/signature/signature.cairo +++ b/src/signature/signature.cairo @@ -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; @@ -296,7 +296,7 @@ pub fn parse_signature(sig_bytes: @ByteArray) -> Result { 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; @@ -307,7 +307,7 @@ pub fn parse_signature(sig_bytes: @ByteArray) -> Result { 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; diff --git a/src/signature/utils.cairo b/src/signature/utils.cairo index 76f34171..f3292c3d 100644 --- a/src/signature/utils.cairo +++ b/src/signature/utils.cairo @@ -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(); diff --git a/src/stack.cairo b/src/stack.cairo index 5784f841..e4d96441 100644 --- a/src/stack.cairo +++ b/src/stack.cairo @@ -97,7 +97,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { fn print(ref self: ScriptStack) { let mut i = self.len; - while i > 0 { + while i != 0 { i -= 1; self.print_element(i.into()); } @@ -106,7 +106,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { fn json(ref self: ScriptStack) { let mut i = 0; print!("["); - while i < self.len { + while i != self.len { let (entry, arr) = self.data.entry(i.into()); let arr = arr.deref(); print!("\"{}\"", utils::bytecode_to_hex(@arr.clone())); @@ -126,7 +126,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { let mut err = ''; let entry_index = 3 * n - 1; let mut i = n; - while i > 0 { + while i != 0 { let res = self.nip_n(entry_index); if res.is_err() { err = res.unwrap_err(); @@ -144,7 +144,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { fn stack_to_span(ref self: ScriptStack) -> Span { let mut result = array![]; let mut i = 0; - while i < self.len { + while i != self.len { let (entry, arr) = self.data.entry(i.into()); let arr = arr.deref(); result.append(arr.clone()); @@ -161,7 +161,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { } let mut i = n; let mut err = ''; - while i > 0 { + while i != 0 { i -= 1; let value = self.peek_byte_array(n - 1); if value.is_err() { @@ -191,7 +191,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { // Shift all elements above idx down by one let mut i = 0; - while i < idx { + while i != idx { let next_value = self.peek_byte_array(idx - i - 1).unwrap(); let (entry, _) = self.data.entry((self.len - idx + i - 1).into()); self.data = entry.finalize(NullableTrait::new(next_value)); @@ -239,7 +239,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { } let entry: u32 = (2 * n) - 1; let mut err = ''; - while n > 0 { + while n != 0 { let res = self.peek_byte_array(entry); if res.is_err() { err = res.unwrap_err(); @@ -263,7 +263,7 @@ pub impl ScriptStackImpl of ScriptStackTrait { self.len = 0; let mut i = start; let end = start + len; - while i < end { + while i != end { self.push_byte_array(stack.at(i).clone()); i += 1; }; diff --git a/src/transaction.cairo b/src/transaction.cairo index 948d8ff9..48d46519 100644 --- a/src/transaction.cairo +++ b/src/transaction.cairo @@ -117,7 +117,7 @@ pub impl TransactionImpl of TransactionTrait { // TODO: Byte orderings let mut i = 0; let mut inputs: Array = array![]; - while i < input_len { + while i != input_len { let tx_id = u256 { high: utils::byte_array_value_at_be(@raw, ref offset, 16).try_into().unwrap(), low: utils::byte_array_value_at_be(@raw, ref offset, 16).try_into().unwrap(), @@ -141,7 +141,7 @@ pub impl TransactionImpl of TransactionTrait { let output_len: u8 = utils::byte_array_value_at_le(@raw, ref offset, 1).try_into().unwrap(); let mut i = 0; let mut outputs: Array = array![]; - while i < output_len { + while i != output_len { // TODO: negative values let value: i64 = utils::byte_array_value_at_le(@raw, ref offset, 8).try_into().unwrap(); let script_len = utils::byte_array_value_at_le(@raw, ref offset, 1).try_into().unwrap(); @@ -178,7 +178,7 @@ pub impl TransactionImpl of TransactionTrait { let input_len: usize = self.transaction_inputs.len(); bytes.append_word_rev(input_len.into(), utils::int_size_in_bytes(input_len)); let mut i: usize = 0; - while i < input_len { + while i != input_len { let input: @TransactionInput = self.transaction_inputs.at(i); let input_txid: u256 = *input.previous_outpoint.txid; let vout: u32 = *input.previous_outpoint.vout; @@ -200,7 +200,7 @@ pub impl TransactionImpl of TransactionTrait { let output_len: usize = self.transaction_outputs.len(); bytes.append_word_rev(output_len.into(), utils::int_size_in_bytes(output_len)); i = 0; - while i < output_len { + while i != output_len { let output: @TransactionOutput = self.transaction_outputs.at(i); let value: i64 = *output.value; let script: @ByteArray = output.publickey_script; @@ -260,7 +260,7 @@ pub impl TransactionImpl of TransactionTrait { let mut total_out: i64 = 0; let output_len = self.transaction_outputs.len(); let mut i = 0; - while i < output_len { + while i != output_len { let output = self.transaction_outputs.at(i); total_out += *output.value; i += 1; diff --git a/src/utils.cairo b/src/utils.cairo index e1987883..dff7a46d 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -57,7 +57,7 @@ pub fn hex_to_bytecode(script_item: @ByteArray) -> ByteArray { let mut i = 2; let mut bytecode = ""; let script_item_len = script_item.len(); - while i < script_item_len { + while i != script_item_len { let mut upper_half_byte = 0; let mut lower_half_byte = 0; if script_item[i] >= a_string_lower { @@ -91,7 +91,7 @@ pub fn bytecode_to_hex(bytecode: @ByteArray) -> ByteArray { if bytecode_len == 0 { return "0x00"; } - while i < bytecode_len { + while i != bytecode_len { let upper_half_byte = bytecode[i] / half_byte_shift; let lower_half_byte = bytecode[i] % half_byte_shift; let upper_half: u8 = if upper_half_byte < 10 { @@ -142,7 +142,7 @@ pub fn string_to_bytecode(script_item: @ByteArray) -> ByteArray { bytecode.append_byte(Opcode::OP_PUSHDATA4); bytecode.append(@ScriptNum::wrap(word_len.into())); } - while i < end { + while i != end { bytecode.append_byte(script_item[i]); i += 1; }; @@ -162,7 +162,7 @@ pub fn number_to_bytecode(script_item: @ByteArray) -> ByteArray { i += 1; } let mut value: i64 = 0; - while i < script_item_len { + while i != script_item_len { value = value * 10 + script_item[i].into() - zero; i += 1; }; @@ -197,7 +197,7 @@ pub fn byte_array_to_felt252_be(byte_array: @ByteArray) -> felt252 { let mut value = 0; let mut i = 0; let byte_array_len = byte_array.len(); - while i < byte_array_len { + while i != byte_array_len { value = value * byte_shift + byte_array[i].into(); i += 1; }; @@ -224,7 +224,7 @@ 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 { + while i != offset + len { value = value * byte_shift + byte_array[i].into(); i += 1; }; @@ -251,7 +251,7 @@ pub fn byte_array_value_at_le(byte_array: @ByteArray, ref offset: usize, len: us 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 { + while i != offset + len { sub_byte_array.append_byte(byte_array[i]); i += 1; }; @@ -264,7 +264,7 @@ pub fn felt252_to_byte_array(value: felt252) -> ByteArray { let byte_shift = 256; let mut byte_array = ""; let mut valueU256: u256 = value.into(); - while valueU256 > 0 { + while valueU256 != 0 { byte_array.append_byte((valueU256 % byte_shift).try_into().unwrap()); valueU256 /= byte_shift; }; @@ -305,7 +305,7 @@ pub fn int_to_hex(value: u8) -> felt252 { pub fn byte_array_to_bool(bytes: @ByteArray) -> bool { let mut i = 0; let mut ret_bool = false; - while i < bytes.len() { + while i != bytes.len() { if bytes.at(i).unwrap() != 0 { // Can be negative zero if i == bytes.len() - 1 && bytes.at(i).unwrap() == 0x80 { @@ -342,11 +342,11 @@ pub fn u256_from_byte_array_with_offset(arr: @ByteArray, offset: usize, len: usi if read_bytes > 16 { high_bytes = read_bytes - 16; } - while i < high_bytes { + while i != high_bytes { high = high * 256 + arr[i + offset].into(); i += 1; }; - while i < read_bytes { + while i != read_bytes { low = low * 256 + arr[i + offset].into(); i += 1; }; @@ -357,7 +357,7 @@ pub fn int_size_in_bytes(u_32: u32) -> u32 { let mut value: u32 = u_32; let mut size = 0; - while value > 0 { + while value != 0 { size += 1; value /= 256; }; diff --git a/src/validate.cairo b/src/validate.cairo index 93b08560..f22344ee 100644 --- a/src/validate.cairo +++ b/src/validate.cairo @@ -16,7 +16,7 @@ pub fn validate_transaction( let mut i = 0; let mut err = ''; - while i < input_count { + while i != input_count { let utxo = utxo_hints[i]; // TODO: Error handling let mut engine = EngineImpl::new(utxo.pubkey_script, tx.clone(), i, flags, *utxo.amount) diff --git a/src/witness.cairo b/src/witness.cairo index 233c49d9..71a84212 100644 --- a/src/witness.cairo +++ b/src/witness.cairo @@ -35,7 +35,7 @@ pub fn parse_witness_input(input: ByteArray) -> Array { let mut witness_data: Array = array![]; let mut i = 0; let mut temp_witness: ByteArray = ""; - while i < input.len() { + while i != input.len() { if input[i] == ',' { let witness_bytes = utils::hex_to_bytecode(@temp_witness); witness_data.append(witness_bytes);