Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeanmichel7 committed Feb 3, 2025
1 parent 43ea504 commit da1b45a
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 147 deletions.
26 changes: 12 additions & 14 deletions packages/cmds/src/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 6 additions & 8 deletions packages/engine/src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
58 changes: 27 additions & 31 deletions packages/engine/src/hash_cache.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)) };
Expand Down
20 changes: 9 additions & 11 deletions packages/engine/src/signature/schnorr.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ pub fn parse_schnorr_signature(sig_bytes: @ByteArray) -> Result<Signature, felt2

let mut r: u256 = 0;
let mut s: u256 = 0;
for i in 0
..sig_bytes
.len() {
if i < 32 {
r *= 256;
r += sig_bytes[i].into();
} else {
s *= 256;
s += sig_bytes[i].into();
}
};
for i in 0..sig_bytes.len() {
if i < 32 {
r *= 256;
r += sig_bytes[i].into();
} else {
s *= 256;
s += sig_bytes[i].into();
}
};
if r >= constants::SECP256_FIELD_VAL {
return Result::Err(Error::SCHNORR_INVALID_SIG_R_FIELD);
}
Expand Down
32 changes: 15 additions & 17 deletions packages/engine/src/signature/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
25 changes: 11 additions & 14 deletions packages/engine/src/taproot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -345,10 +343,9 @@ pub fn parse_control_block(control_block: @ByteArray) -> Result<ControlBlock, fe
};

let mut inclusion_proof = "";
for i in CONTROL_BLOCK_BASE_SIZE
..control_block_len {
inclusion_proof.append_byte(control_block[i]);
};
for i in CONTROL_BLOCK_BASE_SIZE..control_block_len {
inclusion_proof.append_byte(control_block[i]);
};

let pubkey = parse_schnorr_pub_key(@raw_pubkey)?;
return Result::Ok(
Expand Down
46 changes: 9 additions & 37 deletions packages/utils/src/digest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ pub impl U256IntoDigest of Into<u256, Digest> {

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(),
],
}
}
Expand Down Expand Up @@ -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,
],
};
Expand All @@ -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,
],
};
Expand All @@ -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,
],
};
Expand Down Expand Up @@ -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,
],
};

Expand Down
27 changes: 12 additions & 15 deletions packages/utils/src/hash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit da1b45a

Please sign in to comment.