Coder Social home page Coder Social logo

keep-starknet-strange / raito Goto Github PK

View Code? Open in Web Editor NEW
19.0 8.0 25.0 11.48 MB

Bitcoin ZK client written in Cairo.

Home Page: https://raito.wtf

License: MIT License

Cairo 99.12% JavaScript 0.04% TypeScript 0.40% CSS 0.04% Python 0.09% jq 0.12% Shell 0.18%
bitcoin cairo stark zero-knowledge-proofs

raito's People

Contributors

abdelstark avatar b-j-roberts avatar harsh-ps-2003 avatar jeanmichel7 avatar lana-shanghai avatar lomasson avatar m-kus avatar maciejka avatar manlikehb avatar msghais avatar mubarak23 avatar oluwa-peski avatar stevencartavia avatar tadev0 avatar xavek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

raito's Issues

[bug] fee sub overflow calculation

ver:
The coinbase transaction reward should not be included in the total output of the fee() function.

How to reproduce:
validate_and_apply on genesis block or block 170

Implement ChainState Struct

Define a ChainState struct to represent the current state of the blockchain.

Acceptance Criteria:

  • Define ChainState struct with fields:
    • block_height: u32
    • total_work: u128
    • best_block_hash: [u8; 32]
    • current_target: u32
    • epoch_start_time: u32
    • prev_timestamps: [u32; 11]
  • Implement Default trait for ChainState

Resources:

  • ZeroSync Cairo 0 code: block_header.cairo

[bug] List of previous block timestamps is updated incorrectly

Current behavior: oldest timestamp is removed from the beginning of the array, new timestamp also added to the beginning of the array.

Expected behavior: oldest timestamp is removed from the beginning of the array, new timestamp added to the end.

Notes: in Zerosync implementation they use reversed order i.e. oldest timestamps are in the end of the array while the recent ones in the beginning. However for validation purposes (more specifically for computing the median) it does not matter.

Implement target_to_bits Function

Create a function to convert a 256-bit target back to the compact bits representation.

Acceptance Criteria:

  • Implement target_to_bits(target: U256) -> Result<u32, Error>
  • Calculate the appropriate exponent and mantissa
  • Combine exponent and mantissa into the compact format
  • Ensure the result is valid and normalized
  • Handle potential overflow errors

Resources:

  • ZeroSync Cairo 0 code: block_header.cairo

[dev] Script to generate a test data.

Prepare a script to fetch bitcoin blocks and format them as Cairo structs:

  • given a block hash it should generate a Cairo file named: block_<block_hash>.cairo containing a declaration of a Block struct (see:
    pub struct Block {
    )
  • script should be integrated with scarb
  • script should use sh i jq, examples here and here

[bug] Calculation of total_output_amount in coinbase tx seems wrong

ver:
The validate_coinbase function takes into account all the outputs of all the transactions in the block to verify the coinbase transaction.

How to reproduce:

Execute this function with the transactions of block 170, it reverts.
2txs:
tx1: ouput 50BTC (coinbase)
tx2: input 50
ouput (10 + 40)
fee: 0

Therefore:
total_output = 100 > 0 +50

Implement compute_merkle_root Function

Create a function to compute the Merkle root of a list of transaction hashes.

Acceptance Criteria:

  • Implement compute_merkle_root(leaves: Span<u256>) -> Result<u256, Error>
  • Handle the case of an empty list of hashes
  • Implement the Merkle tree algorithm
  • Duplicate the last hash if there's an odd number of hashes
  • Implement the CVE-2012-2459 fix (check for duplicate hashes)
  • Return the computed Merkle root
  • unit tests

Resources:

  • ZeroSync Cairo 0 code: merkle_tree.cairo

Implement read_block_header Function

Create a function to read a block header from a byte stream.

Acceptance Criteria:

  • Implement read_block_header(reader: &mut Reader) -> Result<BlockHeader, Error>
  • Read each field of the block header in order
  • Handle potential read errors
  • Return a BlockHeader struct if successful

Resources:

  • ZeroSync Cairo 0 code: block_header.cairo
  • Shinigami codebase for error handling examples

[dev] make `utils::{shr, shl}` generic over input types

Make shr and shl functions generic over its input types(not type). Functions are here:

pub fn shl(value: u256, shift: u32) -> u256 {

Some ideas are here:

but note, we are using Cairo 2.7.0, wide mull is in core lib now.

[feat] Using `scarb get_block` generate test blocks

Using scarb get_block should generate interesting bitcoin blocks. What means interesting? For sure we need blocks representing all soft forks, halvings, historial blocks like block containing pizza transaction. I assume we can start with 10 - 20 blocks.

In order to use get_block you will need RPC endpoint and credentials. You will get those in pm on tg.

When assigned please join project tg group.

[feat] Try to optimize `merkle_tree::merkle_root`

Current merkle_root calculation uses u256 to represent internal hashes in the tree. While this seems very natural, it is not how things work internally. Under the hood compute_sha256_u32_array which accepts Array<u32> and returns [u32; 8], so some work is wasted doing conversions. Try to optimise the process. Don't go too far though merkle_root function should still return u256 but can accept Array<Array<u32>>.

Provide speedup measurements.

Implement bits_to_target Function

Create a function to convert the compact bits representation to the full 256-bit target.

Acceptance Criteria:

  • Implement bits_to_target(bits: u32) -> Result<U256, Error>
  • Extract exponent and mantissa from bits
  • Calculate the full target value
  • Ensure the target doesn't exceed the maximum allowed value
  • Handle potential overflow errors

Resources:

  • ZeroSync Cairo 0 code: block_header.cairo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.