Skip to content

Commit

Permalink
Testing prebuilt crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Sep 15, 2024
1 parent bd28e8d commit bc74f16
Show file tree
Hide file tree
Showing 141 changed files with 57,186 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,5 @@ zstd = "0.13.2"
blocking-threadpool = "1.0.1"
libfuzzer-sys = "0.4"
wasm-bindgen = "0.2.93"
chiavdf = { git = "https://github.com/Chia-Network/chiavdf", rev = "0a033dc8d7273fdd389c761975fb44f7081da4e8" }
chiavdf = { path = "./chiavdf-1.1.5" }
chiapos = "2.0.6"
45 changes: 45 additions & 0 deletions chiavdf-1.1.5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.

[package]
edition = "2021"
name = "chiavdf"
version = "1.1.5"
authors = ["Brandon Haggstrom <b.haggstrom@chia.net>"]
build = "build.rs"
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Bindings to the chiavdf C++ library."
homepage = "https://github.com/Chia-Network/chiavdf"
readme = false
license = "Apache-2.0"
repository = "https://github.com/Chia-Network/chiavdf"

[lib]
name = "chiavdf"
path = "src/lib.rs"

[dependencies.link-cplusplus]
version = "1.0.9"

[dev-dependencies.hex]
version = "0.4.3"

[dev-dependencies.hex-literal]
version = "0.4.1"

[build-dependencies.bindgen]
version = "0.69.4"

[build-dependencies.cmake]
version = "0.1.50"
20 changes: 20 additions & 0 deletions chiavdf-1.1.5/Cargo.toml.orig

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

65 changes: 65 additions & 0 deletions chiavdf-1.1.5/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::env;
use std::path::PathBuf;
use std::str::FromStr;

use cmake::Config;

fn main() {
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=../src/c_bindings/c_wrapper.h");
println!("cargo:rerun-if-changed=../src/c_bindings/c_wrapper.cpp");

let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());

let mut src_dir = manifest_dir.join("cpp");
if !src_dir.exists() {
src_dir = manifest_dir
.parent()
.expect("can't access ../")
.join("src")
.to_path_buf();
}

let dst = Config::new(src_dir.as_path())
.build_target("chiavdfc_static")
.define("BUILD_CHIAVDFC", "ON")
.env("BUILD_VDF_CLIENT", "N")
.define("BUILD_PYTHON", "OFF")
.build();

println!(
"cargo:rustc-link-search=native={}",
PathBuf::from_str(dst.display().to_string().as_str())
.unwrap()
.join("build")
.join("lib")
.join("static")
.to_str()
.unwrap()
);
println!("cargo:rustc-link-lib=static=chiavdfc");
println!("cargo:rustc-link-lib=gmp");

let bindings = bindgen::Builder::default()
.header(manifest_dir.join("wrapper.h").to_str().unwrap())
.clang_arg("-x")
.clang_arg("c++")
.clang_arg(format!(
"-I{}",
src_dir.join("c_bindings").to_str().unwrap()
))
.clang_arg("-std=c++14")
.allowlist_function("verify_n_wesolowski_wrapper")
.allowlist_function("create_discriminant_wrapper")
.allowlist_function("prove_wrapper")
.allowlist_function("free")
.allowlist_function("delete_byte_array")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
68 changes: 68 additions & 0 deletions chiavdf-1.1.5/cpp/1weso_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "vdf.h"
#include "create_discriminant.h"
#include "verifier.h"

#include <atomic>
#include <cassert>

int segments = 7;
int thread_count = 3;

Proof CreateProof(ProverManager& pm, uint64_t iteration) {
return pm.Prove(iteration);
}

int gcd_base_bits=50;
int gcd_128_max_iter=3;

int main(int argc, char const* argv[]) try
{
// allow setting the multiplier for the number of iterations to test on the
// command line. This can be used to run smaller and faster tests on CI,
// specifically with instrumented binaries that aren't as fast
std::uint64_t const iter_multiplier = (argc > 1)
? std::stoull(argv[1]) : 1000000;

assert(is_vdf_test); //assertions should be disabled in VDF_MODE==0
init_gmp();
debug_mode = true;
if(hasAVX2())
{
gcd_base_bits=63;
gcd_128_max_iter=2;
}
std::vector<uint8_t> challenge_hash({0, 0, 1, 2, 3, 3, 4, 4});
int d_bits = 1024;
integer D = CreateDiscriminant(challenge_hash, d_bits);

if (getenv( "warn_on_corruption_in_production" )!=nullptr) {
warn_on_corruption_in_production=true;
}
set_rounding_mode();

integer L=root(-D, 4);
form f=form::generator(D);

std::atomic<bool> stopped = false;
fast_algorithm = false;

uint64_t iter = iter_multiplier;
OneWesolowskiCallback weso(D, f, iter);
FastStorage* fast_storage = nullptr;
std::thread vdf_worker(repeated_square, iter, f, D, L, &weso, fast_storage, std::ref(stopped));
Proof const proof = ProveOneWesolowski(iter, D, f, &weso, stopped);
stopped = true;
vdf_worker.join();

bool is_valid;
form x_init = form::generator(D);
form y = DeserializeForm(D, proof.y.data(), proof.y.size());
form proof_form = DeserializeForm(D, proof.proof.data(), proof.proof.size());
VerifyWesolowskiProof(D, x_init, y, proof_form, iter, is_valid);
std::cout << "Verify result: " << is_valid << "\n";
assert(is_valid);
}
catch (std::exception const& e) {
std::cerr << "Exception " << e.what() << '\n';
return 1;
}
79 changes: 79 additions & 0 deletions chiavdf-1.1.5/cpp/2weso_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "vdf.h"
#include "create_discriminant.h"
#include "verifier.h"

#include <atomic>

int segments = 7;
int thread_count = 3;

int gcd_base_bits=50;
int gcd_128_max_iter=3;

void CheckProof(integer& D, Proof& proof, uint64_t iteration) {
form x = form::generator(D);
std::vector<unsigned char> bytes;
bytes.insert(bytes.end(), proof.y.begin(), proof.y.end());
bytes.insert(bytes.end(), proof.proof.begin(), proof.proof.end());
if (CheckProofOfTimeNWesolowski(D, DEFAULT_ELEMENT, bytes.data(), bytes.size(), iteration, 1024, proof.witness_type)) {
std::cout << "Correct proof\n";
} else {
std::cout << "Incorrect proof\n";
throw std::runtime_error("incorrect proof");
}
}

int main(int argc, char const* argv[]) try
{
// allow setting the multiplier for the number of iterations to test on the
// command line. This can be used to run smaller and faster tests on CI,
// specifically with instrumented binaries that aren't as fast
std::uint64_t const iter_multiplier = (argc > 1)
? std::stoull(argv[1]) : 1000000;

assert(is_vdf_test); //assertions should be disabled in VDF_MODE==0
init_gmp();
debug_mode = true;
if(hasAVX2())
{
gcd_base_bits=63;
gcd_128_max_iter=2;
}
std::vector<uint8_t> challenge_hash({0, 0, 1, 2, 3, 3, 4, 4});
integer D = CreateDiscriminant(challenge_hash, 1024);

if (getenv( "warn_on_corruption_in_production" )!=nullptr) {
warn_on_corruption_in_production=true;
}
set_rounding_mode();

integer L=root(-D, 4);
form f=form::generator(D);

std::atomic<bool> stopped = false;
fast_algorithm = false;
two_weso = true;
TwoWesolowskiCallback weso(D, f);
FastStorage* fast_storage = NULL;
std::thread vdf_worker(repeated_square, 0, f, D, L, &weso, fast_storage, std::ref(stopped));
// Test 1 - 1 million iters.
uint64_t iteration = 1 * iter_multiplier;
Proof proof = ProveTwoWeso(D, f, iteration, 0, &weso, 0, stopped);
CheckProof(D, proof, iteration);
// Test 2 - 15 million iters.
iteration = 15 * iter_multiplier;
proof = ProveTwoWeso(D, f, iteration, 0, &weso, 0, stopped);
CheckProof(D, proof, iteration);
// Test 3 - 100 million iters.
iteration = 100 * iter_multiplier;
proof = ProveTwoWeso(D, f, iteration, 0, &weso, 0, stopped);
CheckProof(D, proof, iteration);
// Test stopping gracefully.
stopped = true;
vdf_worker.join();
return 0;
}
catch (std::exception const& e) {
std::cerr << "Exception " << e.what() << '\n';
return 1;
}
Loading

0 comments on commit bc74f16

Please sign in to comment.