Skip to content

Commit

Permalink
impl refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Jan 6, 2024
1 parent fa1b453 commit 378f13d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
56 changes: 14 additions & 42 deletions src/proof_of_work/proof_of_work.cairo
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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::<u8>::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::<u8>::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::<u8>::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::<u8>::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::<u128, u256>::into(hash.high) < Into::<felt252, u256>::into(work_limit),
Expand Down
2 changes: 1 addition & 1 deletion src/proof_of_work/tests.cairo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod test_proof_of_work;
mod test_proof_of_work;
10 changes: 10 additions & 0 deletions src/proof_of_work/tests/test_proof_of_work.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 378f13d

Please sign in to comment.