Skip to content

Commit

Permalink
Add Python bindings to chiavdf and chiapos
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Aug 29, 2024
1 parent 4cdcb4a commit d7a889e
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 0 deletions.
173 changes: 173 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,5 @@ zstd = "0.13.2"
blocking-threadpool = "1.0.1"
libfuzzer-sys = "0.4"
wasm-bindgen = "0.2.93"
chiavdf = "1.1.5"
chiapos = "2.0.5"
2 changes: 2 additions & 0 deletions crates/chia-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ chia-puzzles = { workspace = true }
chia-bls = { workspace = true }
hex-literal = { workspace = true }
thiserror = { workspace = true }
chiavdf = { workspace = true }
chiapos = { workspace = true }

[dev-dependencies]
num-traits = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ pub mod gen;
pub mod generator_rom;
pub mod merkle_set;
pub mod merkle_tree;
pub mod proof_of_space;
pub mod spendbundle_conditions;
pub mod spendbundle_validation;
pub mod vdf;
17 changes: 17 additions & 0 deletions crates/chia-consensus/src/proof_of_space.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use chia_protocol::{Bytes32, ProofOfSpace};
use chiapos::validate_proof;

pub fn get_quality_string(pos: &ProofOfSpace, plot_id: Bytes32) -> Option<Bytes32> {
let mut quality = [0; 32];
if validate_proof(
&plot_id.to_bytes(),
pos.size,
&pos.challenge.to_bytes(),
&pos.proof,
&mut quality,
) {
Some(Bytes32::from(quality))
} else {
None
}
}
28 changes: 28 additions & 0 deletions crates/chia-consensus/src/vdf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use chia_protocol::{ClassgroupElement, VDFInfo, VDFProof};
use chiavdf::{create_discriminant, verify_n_wesolowski};

use crate::consensus_constants::ConsensusConstants;

pub fn validate_vdf_proof(
proof: &VDFProof,
input_el: &ClassgroupElement,
info: &VDFInfo,
constants: &ConsensusConstants,
) -> bool {
if proof.witness_type + 1 > constants.max_vdf_witness_size {
return false;
}

let mut discriminant = vec![0; constants.discriminant_size_bits as usize / 8];
create_discriminant(&info.challenge, &mut discriminant);

let proof_buf = [info.output.data.as_slice(), proof.witness.as_slice()].concat();

verify_n_wesolowski(
&discriminant,
&input_el.data,
&proof_buf,
info.number_of_iterations,
proof.witness_type as u64,
)
}
3 changes: 3 additions & 0 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ class _Unspec:
def solution_generator(spends: Sequence[Tuple[Coin, bytes, bytes]]) -> bytes: ...
def solution_generator_backrefs(spends: Sequence[Tuple[Coin, bytes, bytes]]) -> bytes: ...
def validate_vdf_proof(proof: VDFProof, input_el: ClassgroupElement, info: VDFInfo, constants: ConsensusConstants) -> bool: ...
def get_quality_string(pos: ProofOfSpace, plot_id: bytes32) -> Optional[bytes32]: ...
def compute_merkle_set_root(items: Sequence[bytes]) -> bytes: ...
def supports_fast_forward(spend: CoinSpend) -> bool : ...
Expand Down
3 changes: 3 additions & 0 deletions wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class _Unspec:
def solution_generator(spends: Sequence[Tuple[Coin, bytes, bytes]]) -> bytes: ...
def solution_generator_backrefs(spends: Sequence[Tuple[Coin, bytes, bytes]]) -> bytes: ...

def validate_vdf_proof(proof: VDFProof, input_el: ClassgroupElement, info: VDFInfo, constants: ConsensusConstants) -> bool: ...
def get_quality_string(pos: ProofOfSpace, plot_id: bytes32) -> Optional[bytes32]: ...

def compute_merkle_set_root(items: Sequence[bytes]) -> bytes: ...

def supports_fast_forward(spend: CoinSpend) -> bool : ...
Expand Down
Loading

0 comments on commit d7a889e

Please sign in to comment.