diff --git a/onchain/Scarb.lock b/onchain/Scarb.lock index f88bfeea..c12d4b9a 100644 --- a/onchain/Scarb.lock +++ b/onchain/Scarb.lock @@ -8,7 +8,15 @@ dependencies = [ "snforge_std", ] +[[package]] +name = "snforge_scarb_plugin" +version = "0.1.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.28.0#4dfe39d96690ed6b3d56971512700de3f58288ea" + [[package]] name = "snforge_std" -version = "0.25.0" -source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.25.0#5b366e24821e530fea97f11b211d220e8493fbea" +version = "0.28.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.28.0#4dfe39d96690ed6b3d56971512700de3f58288ea" +dependencies = [ + "snforge_scarb_plugin", +] diff --git a/onchain/Scarb.toml b/onchain/Scarb.toml index 1e3b4ae7..7c2693ea 100644 --- a/onchain/Scarb.toml +++ b/onchain/Scarb.toml @@ -1,15 +1,16 @@ [package] name = "joyboy" version = "0.1.0" -edition = "2023_11" +edition = "2024_07" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html [dependencies] -starknet = "2.6.3" +starknet = "2.7.1" +assert_macros = "0.1.0" [dev-dependencies] -snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.25.0" } +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.28.0" } #[lib] diff --git a/onchain/src/bip340.cairo b/onchain/src/bip340.cairo index a73b1603..c1eb504f 100644 --- a/onchain/src/bip340.cairo +++ b/onchain/src/bip340.cairo @@ -3,13 +3,12 @@ use core::byte_array::ByteArrayTrait; use core::option::OptionTrait; use core::result::ResultTrait; -// TODO: uncomment once Cairo 2.7 is available -// use core::sha256::compute_sha256_byte_array; +use core::sha256::compute_sha256_byte_array; use core::starknet::SyscallResultTrait; use core::to_byte_array::{AppendFormattedToByteArray, FormatAsByteArray}; use core::traits::Into; -use joyboy::utils::{shl, shr, compute_sha256_byte_array}; +use joyboy::utils::{shl, shr}; use starknet::{secp256k1::{Secp256k1Point}, secp256_trait::{Secp256Trait, Secp256PointTrait}}; const TWO_POW_32: u128 = 0x100000000; diff --git a/onchain/src/erc20.cairo b/onchain/src/erc20.cairo index afe8f2f5..b5b2f3d3 100644 --- a/onchain/src/erc20.cairo +++ b/onchain/src/erc20.cairo @@ -25,6 +25,10 @@ pub mod ERC20 { use starknet::ContractAddress; use starknet::contract_address_const; use starknet::get_caller_address; + use starknet::storage::{ + StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess, + StoragePointerWriteAccess + }; #[storage] struct Storage { @@ -32,8 +36,8 @@ pub mod ERC20 { symbol: felt252, decimals: u8, total_supply: u256, - balances: LegacyMap::, - allowances: LegacyMap::<(ContractAddress, ContractAddress), u256>, + balances: starknet::storage::Map::, + allowances: starknet::storage::Map::<(ContractAddress, ContractAddress), u256>, } #[event] diff --git a/onchain/src/lib.cairo b/onchain/src/lib.cairo index 8b8dacae..1089e80a 100644 --- a/onchain/src/lib.cairo +++ b/onchain/src/lib.cairo @@ -1,5 +1,4 @@ pub mod bip340; pub mod erc20; -pub mod sha256; pub mod social; pub mod utils; diff --git a/onchain/src/sha256.cairo b/onchain/src/sha256.cairo deleted file mode 100644 index f0156369..00000000 --- a/onchain/src/sha256.cairo +++ /dev/null @@ -1,262 +0,0 @@ -// from alexandria - -use core::integer::{u32_wrapping_add, BoundedInt}; - -fn ch(x: u32, y: u32, z: u32) -> u32 { - (x & y) ^ ((x ^ BoundedInt::::max().into()) & z) -} - -fn maj(x: u32, y: u32, z: u32) -> u32 { - (x & y) ^ (x & z) ^ (y & z) -} - -fn bsig0(x: u32) -> u32 { - let x: u128 = x.into(); - let x1 = (x / 0x4) | (x * 0x40000000); - let x2 = (x / 0x2000) | (x * 0x80000); - let x3 = (x / 0x400000) | (x * 0x400); - let result = (x1 ^ x2 ^ x3) & BoundedInt::::max().into(); - result.try_into().unwrap() -} - -fn bsig1(x: u32) -> u32 { - let x: u128 = x.into(); - let x1 = (x / 0x40) | (x * 0x4000000); - let x2 = (x / 0x800) | (x * 0x200000); - let x3 = (x / 0x2000000) | (x * 0x80); - let result = (x1 ^ x2 ^ x3) & BoundedInt::::max().into(); - result.try_into().unwrap() -} - -fn ssig0(x: u32) -> u32 { - let x: u128 = x.into(); - let x1 = (x / 0x80) | (x * 0x2000000); - let x2 = (x / 0x40000) | (x * 0x4000); - let x3 = (x / 0x8); - let result = (x1 ^ x2 ^ x3) & BoundedInt::::max().into(); - result.try_into().unwrap() -} - -fn ssig1(x: u32) -> u32 { - let x: u128 = x.into(); - let x1 = (x / 0x20000) | (x * 0x8000); - let x2 = (x / 0x80000) | (x * 0x2000); - let x3 = (x / 0x400); - let result = (x1 ^ x2 ^ x3) & BoundedInt::::max().into(); - result.try_into().unwrap() -} - -pub fn sha256(mut data: Array) -> Array { - let data_len: u64 = (data.len() * 8).into(); - - // add one - data.append(0x80); - // add padding - while ((64 * ((data.len() - 1) / 64 + 1)) - 8 != data.len()) { - data.append(0); - }; - - // add length to the end - let mut res = (data_len & 0xff00000000000000) / 0x100000000000000; - data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff000000000000) / 0x1000000000000; - data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff0000000000) / 0x10000000000; - data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff00000000) / 0x100000000; - data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff000000) / 0x1000000; - data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff0000) / 0x10000; - data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff00) / 0x100; - data.append(res.try_into().unwrap()); - res = data_len.into() & 0xff; - data.append(res.try_into().unwrap()); - - let data = from_u8Array_to_u32Array(data.span()); - let h = get_h(); - let k = get_k(); - let res = sha256_inner(data.span(), 0, k.span(), h.span()); - - from_u32Array_to_u8Array(res) -} - -fn from_u32Array_to_u8Array(mut data: Span) -> Array { - let mut result = array![]; - while let Option::Some(val) = data - .pop_front() { - let mut res = (*val & 0xff000000) / 0x1000000; - result.append(res.try_into().unwrap()); - res = (*val & 0xff0000) / 0x10000; - result.append(res.try_into().unwrap()); - res = (*val & 0xff00) / 0x100; - result.append(res.try_into().unwrap()); - res = *val & 0xff; - result.append(res.try_into().unwrap()); - }; - result -} - -fn sha256_inner(mut data: Span, i: usize, k: Span, mut h: Span) -> Span { - if 16 * i >= data.len() { - return h; - } - let w = create_message_schedule(data, i); - let h2 = compression(w, 0, k, h); - - let mut t = array![]; - t.append(u32_wrapping_add(*h[0], *h2[0])); - t.append(u32_wrapping_add(*h[1], *h2[1])); - t.append(u32_wrapping_add(*h[2], *h2[2])); - t.append(u32_wrapping_add(*h[3], *h2[3])); - t.append(u32_wrapping_add(*h[4], *h2[4])); - t.append(u32_wrapping_add(*h[5], *h2[5])); - t.append(u32_wrapping_add(*h[6], *h2[6])); - t.append(u32_wrapping_add(*h[7], *h2[7])); - h = t.span(); - sha256_inner(data, i + 1, k, h) -} - -fn compression(w: Span, i: usize, k: Span, mut h: Span) -> Span { - if i >= 64 { - return h; - } - let s1 = bsig1(*h[4]); - let ch = ch(*h[4], *h[5], *h[6]); - let temp1 = u32_wrapping_add( - u32_wrapping_add(u32_wrapping_add(u32_wrapping_add(*h[7], s1), ch), *k[i]), *w[i] - ); - let s0 = bsig0(*h[0]); - let maj = maj(*h[0], *h[1], *h[2]); - let temp2 = u32_wrapping_add(s0, maj); - let mut t = array![]; - t.append(u32_wrapping_add(temp1, temp2)); - t.append(*h[0]); - t.append(*h[1]); - t.append(*h[2]); - t.append(u32_wrapping_add(*h[3], temp1)); - t.append(*h[4]); - t.append(*h[5]); - t.append(*h[6]); - h = t.span(); - compression(w, i + 1, k, h) -} - -fn create_message_schedule(data: Span, i: usize) -> Span { - let mut j = 0; - let mut result = array![]; - while (j < 16) { - result.append(*data[i * 16 + j]); - j += 1; - }; - let mut i = 16; - while (i < 64) { - let s0 = ssig0(*result[i - 15]); - let s1 = ssig1(*result[i - 2]); - let res = u32_wrapping_add( - u32_wrapping_add(u32_wrapping_add(*result[i - 16], s0), *result[i - 7]), s1 - ); - result.append(res); - i += 1; - }; - result.span() -} - -fn from_u8Array_to_u32Array(mut data: Span) -> Array { - let mut result = array![]; - while let Option::Some(val1) = data - .pop_front() { - let val2 = data.pop_front().unwrap(); - let val3 = data.pop_front().unwrap(); - let val4 = data.pop_front().unwrap(); - let mut value = (*val1).into() * 0x1000000; - value = value + (*val2).into() * 0x10000; - value = value + (*val3).into() * 0x100; - value = value + (*val4).into(); - result.append(value); - }; - result -} - -fn get_h() -> Array { - array![ - 0x6a09e667, - 0xbb67ae85, - 0x3c6ef372, - 0xa54ff53a, - 0x510e527f, - 0x9b05688c, - 0x1f83d9ab, - 0x5be0cd19 - ] -} - -fn get_k() -> Array { - array![ - 0x428a2f98, - 0x71374491, - 0xb5c0fbcf, - 0xe9b5dba5, - 0x3956c25b, - 0x59f111f1, - 0x923f82a4, - 0xab1c5ed5, - 0xd807aa98, - 0x12835b01, - 0x243185be, - 0x550c7dc3, - 0x72be5d74, - 0x80deb1fe, - 0x9bdc06a7, - 0xc19bf174, - 0xe49b69c1, - 0xefbe4786, - 0x0fc19dc6, - 0x240ca1cc, - 0x2de92c6f, - 0x4a7484aa, - 0x5cb0a9dc, - 0x76f988da, - 0x983e5152, - 0xa831c66d, - 0xb00327c8, - 0xbf597fc7, - 0xc6e00bf3, - 0xd5a79147, - 0x06ca6351, - 0x14292967, - 0x27b70a85, - 0x2e1b2138, - 0x4d2c6dfc, - 0x53380d13, - 0x650a7354, - 0x766a0abb, - 0x81c2c92e, - 0x92722c85, - 0xa2bfe8a1, - 0xa81a664b, - 0xc24b8b70, - 0xc76c51a3, - 0xd192e819, - 0xd6990624, - 0xf40e3585, - 0x106aa070, - 0x19a4c116, - 0x1e376c08, - 0x2748774c, - 0x34b0bcb5, - 0x391c0cb3, - 0x4ed8aa4a, - 0x5b9cca4f, - 0x682e6ff3, - 0x748f82ee, - 0x78a5636f, - 0x84c87814, - 0x8cc70208, - 0x90befffa, - 0xa4506ceb, - 0xbef9a3f7, - 0xc67178f2 - ] -} diff --git a/onchain/src/social.cairo b/onchain/src/social.cairo index 27ea89b8..e6e7075e 100644 --- a/onchain/src/social.cairo +++ b/onchain/src/social.cairo @@ -4,3 +4,4 @@ pub mod deposit; pub mod profile; pub mod request; pub mod transfer; +pub mod utils; \ No newline at end of file diff --git a/onchain/src/social/account.cairo b/onchain/src/social/account.cairo index c7477b28..15651a66 100644 --- a/onchain/src/social/account.cairo +++ b/onchain/src/social/account.cairo @@ -34,6 +34,10 @@ pub mod SocialAccount { }; use starknet::account::Call; use starknet::{get_caller_address, get_contract_address, get_tx_info, ContractAddress}; + use starknet::storage::{ + StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess, + StoragePointerWriteAccess + }; use super::ISRC6; use super::super::request::{ @@ -46,19 +50,19 @@ pub mod SocialAccount { struct Storage { #[key] public_key: u256, - transfers: LegacyMap, + transfers: starknet::storage::Map, } #[event] #[derive(Drop, starknet::Event)] - enum Event { + pub enum Event { AccountCreated: AccountCreated, } #[derive(Drop, starknet::Event)] - struct AccountCreated { + pub struct AccountCreated { #[key] - public_key: u256 + pub public_key: u256 } #[constructor] @@ -159,56 +163,51 @@ mod tests { use core::traits::Into; use joyboy::erc20::{ERC20, IERC20Dispatcher, IERC20DispatcherTrait}; use snforge_std::{ - declare, ContractClass, ContractClassTrait, spy_events, SpyOn, EventSpy, EventFetcher, - Event, EventAssertions, cheat_transaction_hash_global, cheat_signature_global, + ContractClass, ContractClassTrait, spy_events, EventSpy, EventSpyTrait, + Event, EventSpyAssertionsTrait, start_cheat_transaction_hash_global, start_cheat_signature_global, stop_cheat_transaction_hash_global, stop_cheat_signature_global }; use starknet::{ ContractAddress, get_caller_address, get_contract_address, contract_address_const }; use super::super::profile::NostrProfile; - + use super::SocialAccount; use super::super::request::{SocialRequest, Signature, Encode}; use super::super::transfer::Transfer; use super::{ ISocialAccountDispatcher, ISocialAccountDispatcherTrait, ISocialAccountSafeDispatcher, ISocialAccountSafeDispatcherTrait }; + use super::super::utils::tests::declare_contract; use super::{ISRC6Dispatcher, ISRC6DispatcherTrait}; fn declare_account() -> ContractClass { - declare("SocialAccount").unwrap() + declare_contract("SocialAccount") } fn declare_erc20() -> ContractClass { - declare("ERC20").unwrap() + declare_contract("ERC20") } fn deploy_account(class: ContractClass, public_key: u256) -> ISocialAccountDispatcher { let mut calldata = array![]; public_key.serialize(ref calldata); - let address = class.precalculate_address(@calldata); - - let mut spy = spy_events(SpyOn::One(address)); + let mut spy = spy_events(); let (contract_address, _) = class.deploy(@calldata).unwrap(); - spy.fetch_events(); - - assert(spy.events.len() == 1, 'there should be one event'); - - // TODO: deserialize event instead of manual decoding - let (_, event) = spy.events.at(0); - assert(event.keys.at(0) == @selector!("AccountCreated"), 'wrong event name'); - - let event_key = u256 { - low: (*event.keys.at(1)).try_into().unwrap(), - high: (*event.keys.at(2)).try_into().unwrap() - }; + assert(spy.get_events().events.len() == 1, 'there should be one event'); - assert(event_key == public_key, 'wrong public key'); + spy.assert_emitted(@array![ + ( + contract_address, + SocialAccount::Event::AccountCreated( + SocialAccount::AccountCreated { public_key } + ) + ) + ]); ISocialAccountDispatcher { contract_address } } @@ -426,13 +425,13 @@ mod tests { r.serialize(ref signature); s.serialize(ref signature); - cheat_transaction_hash_global(hash); - cheat_signature_global(signature.span()); + start_cheat_transaction_hash_global(hash); + start_cheat_signature_global(signature.span()); assert!(account.__validate__(Default::default()) == starknet::VALIDATED); let invalid_hash = 0x5a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c89; - cheat_transaction_hash_global(invalid_hash); + start_cheat_transaction_hash_global(invalid_hash); assert!(account.__validate__(Default::default()) != starknet::VALIDATED); diff --git a/onchain/src/social/bech32.cairo b/onchain/src/social/bech32.cairo index 453f0b0f..26a1d837 100644 --- a/onchain/src/social/bech32.cairo +++ b/onchain/src/social/bech32.cairo @@ -1,8 +1,7 @@ use core::array::ArrayTrait; use core::byte_array::ByteArrayTrait; use core::cmp::min; -// TODO: uncomment once Cairo 2.7 is available -// use core::array::ToSpanTrait; +use core::array::ToSpanTrait; use core::option::OptionTrait; use core::to_byte_array::FormatAsByteArray; //! bech32 encoding implementation diff --git a/onchain/src/social/deposit.cairo b/onchain/src/social/deposit.cairo index e5807960..eb351c62 100644 --- a/onchain/src/social/deposit.cairo +++ b/onchain/src/social/deposit.cairo @@ -68,6 +68,10 @@ pub mod DepositEscrow { use starknet::{ get_block_timestamp, get_caller_address, get_contract_address, get_tx_info, ContractAddress }; + use starknet::storage::{ + StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess, + StoragePointerWriteAccess + }; use super::super::request::{ SocialRequest, SocialRequestImpl, SocialRequestTrait, Encode, Signature }; @@ -90,8 +94,8 @@ pub mod DepositEscrow { #[storage] struct Storage { next_deposit_id: DepositId, - deposits: LegacyMap, - nostr_to_sn: LegacyMap + deposits: starknet::storage::Map, + nostr_to_sn: starknet::storage::Map } #[derive(Drop, starknet::Event)] @@ -292,8 +296,8 @@ mod tests { use joyboy::erc20::{ERC20, IERC20Dispatcher, IERC20DispatcherTrait}; use snforge_std::{ - declare, ContractClass, ContractClassTrait, spy_events, SpyOn, EventSpy, EventFetcher, - Event, EventAssertions, start_cheat_caller_address, cheat_caller_address_global, + ContractClass, ContractClassTrait, spy_events, EventSpy, + Event, start_cheat_caller_address, start_cheat_caller_address_global, stop_cheat_caller_address_global, start_cheat_block_timestamp, }; use starknet::{ @@ -303,15 +307,16 @@ mod tests { use super::super::request::{SocialRequest, Signature, Encode}; use super::super::transfer::Transfer; + use super::super::utils::tests::declare_contract; use super::{Deposit, DepositId, DepositResult, IDepositEscrow, NostrPublicKey, Claim}; use super::{IDepositEscrowDispatcher, IDepositEscrowDispatcherTrait}; fn declare_escrow() -> ContractClass { - declare("DepositEscrow").unwrap() + declare_contract("DepositEscrow") } fn declare_erc20() -> ContractClass { - declare("ERC20").unwrap() + declare_contract("ERC20") } fn deploy_escrow(class: ContractClass) -> IDepositEscrowDispatcher { @@ -405,7 +410,7 @@ mod tests { let recipient_address: ContractAddress = 678.try_into().unwrap(); let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -468,7 +473,7 @@ mod tests { ..request }; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -522,7 +527,7 @@ mod tests { let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -540,7 +545,7 @@ mod tests { let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -568,7 +573,7 @@ mod tests { let recipient_address: ContractAddress = 789.try_into().unwrap(); let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -593,7 +598,7 @@ mod tests { let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -615,7 +620,7 @@ mod tests { let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -634,7 +639,7 @@ mod tests { let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -657,7 +662,7 @@ mod tests { let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); @@ -676,7 +681,7 @@ mod tests { let recipient_address: ContractAddress = 345.try_into().unwrap(); let amount = 100_u256; - cheat_caller_address_global(sender_address); + start_cheat_caller_address_global(sender_address); erc20.approve(escrow.contract_address, amount); stop_cheat_caller_address_global(); diff --git a/onchain/src/social/profile.cairo b/onchain/src/social/profile.cairo index fdb610be..d80f792c 100644 --- a/onchain/src/social/profile.cairo +++ b/onchain/src/social/profile.cairo @@ -2,8 +2,7 @@ use core::array::SpanTrait; use core::byte_array::ByteArrayTrait; use core::option::OptionTrait; use core::traits::TryInto; -// TODO: uncomment once Cairo 2.7 is available -// use core::array::ToSpanTrait; +use core::array::ToSpanTrait; //! Representation of Nostr profiles diff --git a/onchain/src/social/request.cairo b/onchain/src/social/request.cairo index cb75ac2a..2ff710ac 100644 --- a/onchain/src/social/request.cairo +++ b/onchain/src/social/request.cairo @@ -1,8 +1,8 @@ use core::fmt::Display; +use core::sha256::compute_sha256_byte_array; use core::to_byte_array::FormatAsByteArray; use core::traits::Into; use joyboy::bip340; -use joyboy::utils::{compute_sha256_byte_array}; const TWO_POW_32: u128 = 0x100000000; const TWO_POW_64: u128 = 0x10000000000000000; diff --git a/onchain/src/social/utils.cairo b/onchain/src/social/utils.cairo new file mode 100644 index 00000000..50d8f584 --- /dev/null +++ b/onchain/src/social/utils.cairo @@ -0,0 +1,11 @@ +#[cfg(test)] +pub mod tests { + use snforge_std::{DeclareResult, ContractClass, declare}; + + pub fn declare_contract(contract: ByteArray) -> ContractClass { + match declare(contract).unwrap() { + DeclareResult::Success(class) => class, + DeclareResult::AlreadyDeclared(class) => class + } + } +} \ No newline at end of file diff --git a/onchain/src/utils.cairo b/onchain/src/utils.cairo index 1b3788cd..cb22e1e6 100644 --- a/onchain/src/utils.cairo +++ b/onchain/src/utils.cairo @@ -1,6 +1,7 @@ use core::ecdsa::check_ecdsa_signature; -use core::integer::{u32_wide_mul, u8_wide_mul, BoundedInt}; -use joyboy::sha256::sha256; +use core::traits::BitAnd; +use core::num::traits::Bounded; +use core::num::traits::WideMul as CoreWideMul; use starknet::SyscallResultTrait; use starknet::account::Call; @@ -79,13 +80,13 @@ trait WideMul { impl WideMuluU32 of WideMul { fn wide_mul(x: u32, y: u32) -> u64 { - u32_wide_mul(x, y) + CoreWideMul::wide_mul(x, y) } } impl WideMuluU8 of WideMul { fn wide_mul(x: u8, y: u8) -> u16 { - u8_wide_mul(x, y) + CoreWideMul::wide_mul(x, y) } } @@ -96,7 +97,7 @@ pub fn shl< N, +BitAnd, +Pow2, - +BoundedInt, + +Bounded, +WideMul, +Into, +TryInto, @@ -105,59 +106,9 @@ pub fn shl< >( x: V, n: N ) -> V { - (WideMul::wide_mul(x, Pow2::pow2(n)) & BoundedInt::::max().into()).try_into().unwrap() + (WideMul::wide_mul(x, Pow2::pow2(n)) & Bounded::::MAX.into()).try_into().unwrap() } -pub fn compute_sha256_byte_array(m: @ByteArray) -> [u32; 8] { - let mut ba = ArrayTrait::new(); - let len = m.len(); - let mut i = 0; - while i != len { - ba.append(m.at(i).unwrap()); - i += 1; - }; - - let sha = sha256(ba); - - let r = [ - shl((*sha.at(0)).into(), 24_u32) - + shl((*sha.at(1)).into(), 16_u32) - + shl((*sha.at(2)).into(), 8_u32) - + (*sha.at(3)).into(), - shl((*sha.at(4)).into(), 24_u32) - + shl((*sha.at(5)).into(), 16_u32) - + shl((*sha.at(6)).into(), 8_u32) - + (*sha.at(7)).into(), - shl((*sha.at(8)).into(), 24_u32) - + shl((*sha.at(9)).into(), 16_u32) - + shl((*sha.at(10)).into(), 8_u32) - + (*sha.at(11)).into(), - shl((*sha.at(12)).into(), 24_u32) - + shl((*sha.at(13)).into(), 16_u32) - + shl((*sha.at(14)).into(), 8_u32) - + (*sha.at(15)).into(), - shl((*sha.at(16)).into(), 24_u32) - + shl((*sha.at(17)).into(), 16_u32) - + shl((*sha.at(18)).into(), 8_u32) - + (*sha.at(19)).into(), - shl((*sha.at(20)).into(), 24_u32) - + shl((*sha.at(21)).into(), 16_u32) - + shl((*sha.at(22)).into(), 8_u32) - + (*sha.at(23)).into(), - shl((*sha.at(24)).into(), 24_u32) - + shl((*sha.at(25)).into(), 16_u32) - + shl((*sha.at(26)).into(), 8_u32) - + (*sha.at(27)).into(), - shl((*sha.at(28)).into(), 24_u32) - + shl((*sha.at(29)).into(), 16_u32) - + shl((*sha.at(30)).into(), 8_u32) - + (*sha.at(31)).into(), - ]; - - r -} - - pub const MIN_TRANSACTION_VERSION: u256 = 1; pub const QUERY_OFFSET: u256 = 0x100000000000000000000000000000000; // QUERY_OFFSET + TRANSACTION_VERSION