From da1b45a635637f62dae2a3e7dcc36a6163be7c81 Mon Sep 17 00:00:00 2001 From: Jeanmichel7 Date: Mon, 3 Feb 2025 20:53:45 +0100 Subject: [PATCH] fmt --- packages/cmds/src/main.cairo | 26 +++++---- packages/engine/src/engine.cairo | 14 +++-- packages/engine/src/hash_cache.cairo | 58 ++++++++++----------- packages/engine/src/signature/schnorr.cairo | 20 ++++--- packages/engine/src/signature/utils.cairo | 32 ++++++------ packages/engine/src/taproot.cairo | 25 ++++----- packages/utils/src/digest.cairo | 46 ++++------------ packages/utils/src/hash.cairo | 27 +++++----- 8 files changed, 101 insertions(+), 147 deletions(-) diff --git a/packages/cmds/src/main.cairo b/packages/cmds/src/main.cairo index ee1f8594..9873ac66 100644 --- a/packages/cmds/src/main.cairo +++ b/packages/cmds/src/main.cairo @@ -223,20 +223,18 @@ fn run_raw_transaction(mut input: ValidateRawInput) -> u8 { } let mut utxo_hints = array![]; - for hint in input - .utxo_hints - .span() { - println!("UTXO hint: 'amount: {}, script_pubkey: {}'", hint.amount, hint.pubkey_script); - let pubkey_script = hex_to_bytecode(hint.pubkey_script); // TODO: Check - utxo_hints - .append( - UTXO { - amount: *hint.amount, - pubkey_script: pubkey_script, - block_height: *hint.block_height, - }, - ); - }; + for hint in input.utxo_hints.span() { + println!("UTXO hint: 'amount: {}, script_pubkey: {}'", hint.amount, hint.pubkey_script); + let pubkey_script = hex_to_bytecode(hint.pubkey_script); // TODO: Check + utxo_hints + .append( + UTXO { + amount: *hint.amount, + pubkey_script: pubkey_script, + block_height: *hint.block_height, + }, + ); + }; let transaction = EngineInternalTransactionTrait::deserialize( raw_transaction, input.utxo_hints.span(), diff --git a/packages/engine/src/engine.cairo b/packages/engine/src/engine.cairo index 1a2f87ee..3abf5353 100644 --- a/packages/engine/src/engine.cairo +++ b/packages/engine/src/engine.cairo @@ -705,14 +705,12 @@ pub impl EngineInternalImpl< || self.is_witness_active(TAPROOT_WITNESS_VERSION) { // Sanity checks let mut err = ''; - for w in self - .dstack - .stack_to_span() { - if w.len() > MAX_SCRIPT_ELEMENT_SIZE { - err = Error::SCRIPT_PUSH_SIZE; - break; - } - }; + for w in self.dstack.stack_to_span() { + if w.len() > MAX_SCRIPT_ELEMENT_SIZE { + err = Error::SCRIPT_PUSH_SIZE; + break; + } + }; if err != '' { return Result::Err(err); } diff --git a/packages/engine/src/hash_cache.cairo b/packages/engine/src/hash_cache.cairo index ca0a0bbc..8d266057 100644 --- a/packages/engine/src/hash_cache.cairo +++ b/packages/engine/src/hash_cache.cairo @@ -47,30 +47,27 @@ pub impl SigHashMidstateImpl< let mut hasV0Inputs = false; let mut hasV1Inputs = false; - for i in 0 - ..transaction - .get_transaction_inputs() - .len() { - let input = transaction.get_transaction_inputs()[i]; - let input_txid = input.get_prevout_txid(); - let input_vout = input.get_prevout_vout(); - - if (input_vout == 0xFFFFFFFF && input_txid == 0) { - hasV0Inputs = true; - continue; - } - - let utxo = utxos[i]; - if is_witness_v1_pub_key_hash(utxo.get_publickey_script()) { - hasV1Inputs = true; - } else { - hasV0Inputs = true; - } - - if hasV0Inputs && hasV1Inputs { - break; - } - }; + for i in 0..transaction.get_transaction_inputs().len() { + let input = transaction.get_transaction_inputs()[i]; + let input_txid = input.get_prevout_txid(); + let input_vout = input.get_prevout_vout(); + + if (input_vout == 0xFFFFFFFF && input_txid == 0) { + hasV0Inputs = true; + continue; + } + + let utxo = utxos[i]; + if is_witness_v1_pub_key_hash(utxo.get_publickey_script()) { + hasV1Inputs = true; + } else { + hasV0Inputs = true; + } + + if hasV0Inputs && hasV1Inputs { + break; + } + }; let mut prevouts_v0_bytes: ByteArray = ""; let inputs = transaction.get_transaction_inputs(); @@ -238,13 +235,12 @@ pub impl HashCacheImpl< & ScriptFlags::ScriptVerifyWitness.into() == ScriptFlags::ScriptVerifyWitness.into(); let mut has_witness = false; - for input in tx - .get_transaction_inputs() { - if input.get_witness().len() != 0 { - has_witness = true; - break; - } - }; + for input in tx.get_transaction_inputs() { + if input.get_witness().len() != 0 { + has_witness = true; + break; + } + }; if (segwit_active && has_witness) { return HashCache { sigHashes: Option::Some(SigHashMidstateTrait::new(tx, utxos)) }; diff --git a/packages/engine/src/signature/schnorr.cairo b/packages/engine/src/signature/schnorr.cairo index 43741c12..6951bd69 100644 --- a/packages/engine/src/signature/schnorr.cairo +++ b/packages/engine/src/signature/schnorr.cairo @@ -28,17 +28,15 @@ pub fn parse_schnorr_signature(sig_bytes: @ByteArray) -> Result= constants::SECP256_FIELD_VAL { return Result::Err(Error::SCHNORR_INVALID_SIG_R_FIELD); } diff --git a/packages/engine/src/signature/utils.cairo b/packages/engine/src/signature/utils.cairo index a951f366..cec99f09 100644 --- a/packages/engine/src/signature/utils.cairo +++ b/packages/engine/src/signature/utils.cairo @@ -66,27 +66,25 @@ pub fn transaction_procedure< ) -> EngineTransaction { let hash_type_masked = hash_type & constants::SIG_HASH_MASK; let mut transaction_inputs_clone = array![]; - for input in transaction - .get_transaction_inputs() { - let new_transaction_input = EngineTransactionInput { - previous_outpoint: EngineOutPoint { - txid: input.get_prevout_txid(), vout: input.get_prevout_vout(), - }, - signature_script: input.get_signature_script().clone(), - witness: input.get_witness().into(), - sequence: input.get_sequence(), - }; - transaction_inputs_clone.append(new_transaction_input); + for input in transaction.get_transaction_inputs() { + let new_transaction_input = EngineTransactionInput { + previous_outpoint: EngineOutPoint { + txid: input.get_prevout_txid(), vout: input.get_prevout_vout(), + }, + signature_script: input.get_signature_script().clone(), + witness: input.get_witness().into(), + sequence: input.get_sequence(), }; + transaction_inputs_clone.append(new_transaction_input); + }; let mut transaction_outputs_clone = array![]; - for output in transaction - .get_transaction_outputs() { - let new_transaction_output = EngineTransactionOutput { - value: output.get_value(), publickey_script: output.get_publickey_script().clone(), - }; - transaction_outputs_clone.append(new_transaction_output); + for output in transaction.get_transaction_outputs() { + let new_transaction_output = EngineTransactionOutput { + value: output.get_value(), publickey_script: output.get_publickey_script().clone(), }; + transaction_outputs_clone.append(new_transaction_output); + }; let mut transaction_copy = EngineTransaction { version: transaction.get_version(), diff --git a/packages/engine/src/taproot.cairo b/packages/engine/src/taproot.cairo index 495a3f41..f90edd08 100644 --- a/packages/engine/src/taproot.cairo +++ b/packages/engine/src/taproot.cairo @@ -212,18 +212,16 @@ pub impl ControlBlockImpl of ControlBlockTrait { .tap_hash(); let num_nodes = self.inclusion_proof.len() / CONTROL_BLOCK_NODE_SIZE; - for node_offset in 0 - ..num_nodes { - let mut leaf_offset = 32 * node_offset; - let mut next_node = ""; - for i in leaf_offset - ..leaf_offset + 32 { - next_node.append_byte(self.inclusion_proof[i]); - }; - - merkleAccumulator = tap_branch_hash(@merkleAccumulator.into(), @next_node); + for node_offset in 0..num_nodes { + let mut leaf_offset = 32 * node_offset; + let mut next_node = ""; + for i in leaf_offset..leaf_offset + 32 { + next_node.append_byte(self.inclusion_proof[i]); }; + merkleAccumulator = tap_branch_hash(@merkleAccumulator.into(), @next_node); + }; + return merkleAccumulator; } @@ -345,10 +343,9 @@ pub fn parse_control_block(control_block: @ByteArray) -> Result { Digest { value: [ - high_128_96.try_into().unwrap(), - high_96_64.try_into().unwrap(), - high_64_32.try_into().unwrap(), - high_32_0.try_into().unwrap(), - low_128_96.try_into().unwrap(), - low_96_64.try_into().unwrap(), - low_64_32.try_into().unwrap(), - low_32_0.try_into().unwrap(), + high_128_96.try_into().unwrap(), high_96_64.try_into().unwrap(), + high_64_32.try_into().unwrap(), high_32_0.try_into().unwrap(), + low_128_96.try_into().unwrap(), low_96_64.try_into().unwrap(), + low_64_32.try_into().unwrap(), low_32_0.try_into().unwrap(), ], } } @@ -149,13 +145,7 @@ mod tests { let expected_hash = Digest { value: [ - 0xefcdab90, - 0x78563412, - 0xefcdab90, - 0x78563412, - 0x21436587, - 0x09badcfe, - 0x21436587, + 0xefcdab90, 0x78563412, 0xefcdab90, 0x78563412, 0x21436587, 0x09badcfe, 0x21436587, 0x09badcfe, ], }; @@ -167,13 +157,7 @@ mod tests { fn test_hash_to_u256() { let hash_value = Digest { value: [ - 0xfedcba09, - 0x87654321, - 0xfedcba09, - 0x87654321, - 0x12345678, - 0x90abcdef, - 0x12345678, + 0xfedcba09, 0x87654321, 0xfedcba09, 0x87654321, 0x12345678, 0x90abcdef, 0x12345678, 0x90abcdef, ], }; @@ -192,13 +176,7 @@ mod tests { fn test_hash_to_u256_to_hash() { let hash_value = Digest { value: [ - 0xfedcba09, - 0x87654321, - 0xfedcba09, - 0x87654321, - 0x12345678, - 0x90abcdef, - 0x12345678, + 0xfedcba09, 0x87654321, 0xfedcba09, 0x87654321, 0x12345678, 0x90abcdef, 0x12345678, 0x90abcdef, ], }; @@ -226,14 +204,8 @@ mod tests { fn test_hash_into_bytearray() { let hash = Digest { value: [ - 0x12345678_u32, - 0x9abcdef0_u32, - 0x11223344_u32, - 0x55667788_u32, - 0xaabbccdd_u32, - 0xeeff0011_u32, - 0x22334455_u32, - 0x66778899_u32, + 0x12345678_u32, 0x9abcdef0_u32, 0x11223344_u32, 0x55667788_u32, 0xaabbccdd_u32, + 0xeeff0011_u32, 0x22334455_u32, 0x66778899_u32, ], }; diff --git a/packages/utils/src/hash.cairo b/packages/utils/src/hash.cairo index ddd1105d..557814c1 100644 --- a/packages/utils/src/hash.cairo +++ b/packages/utils/src/hash.cairo @@ -18,11 +18,10 @@ pub fn sha256_u256(hash: [u32; 8]) -> u256 { let msg_hash = compute_sha256_byte_array(@bytes); let mut hash_value: u256 = 0; - for word in msg_hash - .span() { - hash_value *= 0x100000000; - hash_value = hash_value + (*word).into(); - }; + for word in msg_hash.span() { + hash_value *= 0x100000000; + hash_value = hash_value + (*word).into(); + }; hash_value } @@ -34,11 +33,10 @@ pub fn double_sha256_bytearray(byte: @ByteArray) -> ByteArray { pub fn simple_sha256(byte: @ByteArray) -> u256 { let msg_hash = compute_sha256_byte_array(byte); let mut hash_value: u256 = 0; - for word in msg_hash - .span() { - hash_value *= 0x100000000; - hash_value = hash_value + (*word).into(); - }; + for word in msg_hash.span() { + hash_value *= 0x100000000; + hash_value = hash_value + (*word).into(); + }; hash_value } @@ -51,11 +49,10 @@ pub fn double_sha256(byte: @ByteArray) -> u256 { }; let msg_hash = compute_sha256_byte_array(@res_bytes); let mut hash_value: u256 = 0; - for word in msg_hash - .span() { - hash_value *= 0x100000000; - hash_value = hash_value + (*word).into(); - }; + for word in msg_hash.span() { + hash_value *= 0x100000000; + hash_value = hash_value + (*word).into(); + }; hash_value }