Skip to content

Commit

Permalink
[feat] add and usesha256 function that doesn't rely on sha256 sys…
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 authored Sep 17, 2024
1 parent ef15974 commit e60940a
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/consensus/src/types/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! The data is expected to be prepared in advance and passed as program arguments.

use utils::hash::Digest;
use utils::sha256::double_sha256_u32_array;
use utils::double_sha256::double_sha256_u32_array;
use utils::numeric::u32_byte_reverse;
use super::transaction::Transaction;
use core::fmt::{Display, Formatter, Error};
Expand Down
2 changes: 1 addition & 1 deletion packages/consensus/src/validation/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::types::utxo_set::UtxoSet;
use crate::types::transaction::Transaction;
use crate::codec::{Encode, TransactionCodec};
use utils::{hash::Digest, merkle_tree::merkle_root, sha256::double_sha256_byte_array};
use utils::{hash::Digest, merkle_tree::merkle_root, double_sha256::double_sha256_byte_array};
use super::transaction::validate_transaction;
use core::num::traits::zero::Zero;

Expand Down
2 changes: 1 addition & 1 deletion packages/consensus/src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::types::transaction::{Transaction, TxIn, TxOut};
use utils::{
bit_shifts::shr, hash::{Digest, DigestIntoByteArray}, sha256::{double_sha256_byte_array}
bit_shifts::shr, hash::{Digest, DigestIntoByteArray}, double_sha256::{double_sha256_byte_array}
};

const BIP_34_BLOCK_HEIGHT: u32 = 227_836;
Expand Down
2 changes: 1 addition & 1 deletion packages/consensus/src/validation/transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod tests {
use crate::codec::Encode;
use crate::types::transaction::{Transaction, TxIn, TxOut, OutPoint};
use crate::types::utxo_set::UtxoSet;
use utils::{hex::{from_hex, hex_to_hash_rev}, sha256::double_sha256_byte_array};
use utils::{hex::{from_hex, hex_to_hash_rev}, double_sha256::double_sha256_byte_array};
use super::validate_transaction;

// TODO: tests for coinbase maturity
Expand Down
69 changes: 69 additions & 0 deletions packages/utils/src/double_sha256.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! Helpers for calculating double SHA256 hash digest.

use super::hash::{Digest, DigestTrait};
use super::sha256::{compute_sha256_byte_array, compute_sha256_u32_array};

/// Calculates double sha256 digest of a concatenation of two hashes.
pub fn double_sha256_parent(a: @Digest, b: @Digest) -> Digest {
let mut input1: Array<u32> = array![];
input1.append_span(a.value.span());
input1.append_span(b.value.span());

let mut input2: Array<u32> = array![];
input2.append_span(compute_sha256_u32_array(input1, 0, 0).span());

DigestTrait::new(compute_sha256_u32_array(input2, 0, 0))
}

/// Calculates double sha256 digest of bytes.
pub fn double_sha256_byte_array(bytes: @ByteArray) -> Digest {
let mut input2: Array<u32> = array![];
input2.append_span(compute_sha256_byte_array(bytes).span());

DigestTrait::new(compute_sha256_u32_array(input2, 0, 0))
}

/// Calculates double sha256 digest of an array of full 4 byte words.
///
/// It's important that there are no trailing bytes, otherwise the
/// data will be truncated.
pub fn double_sha256_u32_array(words: Array<u32>) -> Digest {
let mut input2: Array<u32> = array![];
input2.append_span(compute_sha256_u32_array(words, 0, 0).span());

DigestTrait::new(compute_sha256_u32_array(input2, 0, 0))
}

#[cfg(test)]
mod tests {
use crate::{hex::from_hex, hash::Digest};
use super::{double_sha256_byte_array, double_sha256_u32_array, double_sha256_parent};

#[test]
fn test_double_sha256_byte_array() {
// hashlib.sha256(sha256(b"bitcoin").digest()).hexdigest()
assert_eq!(
double_sha256_byte_array(@"bitcoin").into(),
from_hex("f1ef1bf105d788352c052453b15a913403be59b90ddf9f7c1f937edee8938dc5")
)
}

#[test]
fn test_double_sha256_u32_array() {
// hashlib.sha256(sha256(bytes.fromhex("00000001000000020000000300000004000000050000000600000007")).digest()).hexdigest()
assert_eq!(
double_sha256_u32_array(array![1, 2, 3, 4, 5, 6, 7]).into(),
from_hex("489b8eeb4024cb77ab057616ebf7f8d4405aa0bd3ad5f42e6b4c20580e011ac4")
)
}

#[test]
fn test_double_sha256_parent() {
// hashlib.sha256(sha256(bytes.fromhex("00000001" * 8 + "00000002" *
// 8)).digest()).hexdigest()
assert_eq!(
double_sha256_parent(@Digest { value: [1; 8] }, @Digest { value: [2; 8] }).into(),
from_hex("14a6e4a4caef969126944266724d11866b39b3390cee070b0aa4c9390cd77f47")
)
}
}
5 changes: 3 additions & 2 deletions packages/utils/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod bit_shifts;
pub mod bytearray;
pub mod sha256;
pub mod double_sha256;
pub mod hash;
pub mod bit_shifts;
pub mod merkle_tree;
pub mod numeric;
pub mod sha256;

#[cfg(target: 'test')]
pub mod hex;
8 changes: 4 additions & 4 deletions packages/utils/src/merkle_tree.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Merkle tree helpers.

use super::{sha256::double_sha256_parent, hash::Digest};
use super::{double_sha256::double_sha256_parent, hash::Digest};

/// Calculate Merkle tree root given the array of leaves.
pub fn merkle_root(ref hashes: Array<Digest>) -> Digest {
Expand Down Expand Up @@ -93,7 +93,7 @@ mod tests {
}

#[test]
#[available_gas(100000000)]
#[available_gas(150000000)]
fn test_merkle_root_04() {
let mut txids = array![
hex_to_hash_rev("32a46e3fcdb462c16de20e3fe88f988ff9174b7b68faa630040f938566f114e9"),
Expand All @@ -110,7 +110,7 @@ mod tests {
}

#[test]
#[available_gas(150000000)]
#[available_gas(1500000000)]
fn test_merkle_root_05() {
let mut txids = array![
hex_to_hash_rev("216e79c7e528836ab6bd04bd4bfea140c8c4ed3248681b32735fe61e35037ed4"),
Expand Down Expand Up @@ -141,7 +141,7 @@ mod tests {
}

#[test]
#[available_gas(1000000000)]
#[available_gas(10000000000)]
fn test_big_merkle_root() {
let mut txids = array![
hex_to_hash_rev("496ecc406ffede2910d25f16afc69b2f59fbd56ce9e136616d756b179f90ced3"),
Expand Down
Loading

0 comments on commit e60940a

Please sign in to comment.