From 378f13d94881a8fcb25323ee4f530c5b5e1ce4e3 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Sat, 6 Jan 2024 19:27:49 +0100 Subject: [PATCH] impl refactor --- src/proof_of_work/proof_of_work.cairo | 56 +++++-------------- src/proof_of_work/tests.cairo | 2 +- .../tests/test_proof_of_work.cairo | 10 ++++ 3 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/proof_of_work/proof_of_work.cairo b/src/proof_of_work/proof_of_work.cairo index 0b3505be4..a0a8b550b 100644 --- a/src/proof_of_work/proof_of_work.cairo +++ b/src/proof_of_work/proof_of_work.cairo @@ -1,17 +1,13 @@ -use core::array::SpanTrait; -use core::array::ArrayTrait; -use cairo_verifier::common::flip_endianness::FlipEndiannessTrait; -use core::debug::PrintTrait; use cairo_verifier::{ - common::{blake2s_u8::blake2s, array_append::ArrayAppendTrait, math::pow}, + common::{ + flip_endianness::FlipEndiannessTrait, array_print::{SpanPrintTrait, ArrayPrintTrait}, + blake2s_u8::blake2s, array_append::ArrayAppendTrait, math::pow, + }, channel::channel::{Channel, ChannelTrait}, proof_of_work::config::{ProofOfWorkConfig, BYTE_UPPER_BOUND, WORD_UPPER_BOUND} }; -use cairo_verifier::common::array_print::{SpanPrintTrait, ArrayPrintTrait}; -const POW_2_12: u256 = 79228162514264337593543950336; -const POW_2_4: u256 = 4294967296; -const POW_2_3: u256 = 16777216; +const MAGIC: u64 = 0x0123456789abcded; #[derive(Drop, Copy)] struct ProofOfWorkUnsentCommitment { @@ -31,45 +27,21 @@ fn verify_proof_of_work(digest: u256, n_bits: u8, nonce: u64) { // 8 bytes || 32 bytes || 1 byte // Total of 0x29 = 41 bytes. - // let init_hash_value: u256 = 0x0123456789abcded000000000000000000000000000000000000000000000000 - // // digest >> 12 -> digest << 4 -> nbits << 3 - // + digest / POW_2_12 * POW_2_4 + n_bits.into() * POW_2_3; - let mut init_hash_data = ArrayTrait::::new(); - init_hash_data.append_big_endian(u256{low: 0xD7CA1D48A19D8FF802A71D94169DE383, high: 0x0123456789ABCDED1C5A5F4381DF1F5C}); - init_hash_data.append_big_endian(u256{low: 0x00000000000000000000000000000000, high: 0x82621FDC5514A10A1400000000000000}); - let span = init_hash_data.span().slice(0,0x29); - let mut arr = ArrayTrait::::new(); - let mut i:u32 = 0; - loop { - if i == span.len() { - break; - } - - arr.append(*span.at(i)); - i+=1; - }; - let init_hash = blake2s(arr).flip_endianness(); + init_hash_data.append_big_endian(MAGIC); + init_hash_data.append_big_endian(digest); + init_hash_data.append(n_bits); + let init_hash = blake2s(init_hash_data).flip_endianness(); - // // Compute Hash(init_hash || nonce ) - // // 32 bytes || 8 bytes - // // Total of 0x28 = 40 bytes. + // Compute Hash(init_hash || nonce ) + // 32 bytes || 8 bytes + // Total of 0x28 = 40 bytes. let mut hash_data = ArrayTrait::::new(); hash_data.append_big_endian(init_hash); - hash_data.append_big_endian(u256{low: 0x00000000000000000000000000000000, high: 0x000000000001683b0000000000000000}); - let span = hash_data.span().slice(0,0x28); - let mut arr = ArrayTrait::::new(); - let mut i:u32 = 0; - loop { - if i == span.len() { - break; - } + hash_data.append_big_endian(nonce); + let hash = blake2s(hash_data).flip_endianness(); - arr.append(*span.at(i)); - i+=1; - }; - let hash = blake2s(arr).flip_endianness(); let work_limit = pow(2, 128 - n_bits.into()); assert( Into::::into(hash.high) < Into::::into(work_limit), diff --git a/src/proof_of_work/tests.cairo b/src/proof_of_work/tests.cairo index 1164558f8..57bb36e70 100644 --- a/src/proof_of_work/tests.cairo +++ b/src/proof_of_work/tests.cairo @@ -1 +1 @@ -mod test_proof_of_work; \ No newline at end of file +mod test_proof_of_work; diff --git a/src/proof_of_work/tests/test_proof_of_work.cairo b/src/proof_of_work/tests/test_proof_of_work.cairo index 0f1401677..78716fabc 100644 --- a/src/proof_of_work/tests/test_proof_of_work.cairo +++ b/src/proof_of_work/tests/test_proof_of_work.cairo @@ -8,3 +8,13 @@ fn test_verify_proof_of_work_0() { let n_bits: u8 = 20; verify_proof_of_work(digest, n_bits, nonce); } + +#[test] +#[should_panic] +#[available_gas(9999999999)] +fn test_verify_proof_of_work_1() { + let digest: u256 = 0x1c5a5f4381df1f5cd7ca1d48a19d8ff802a71d94169de38382621fdc5514a10a; + let nonce: u64 = 0x1683b + 1; + let n_bits: u8 = 20; + verify_proof_of_work(digest, n_bits, nonce); +}