Skip to content

Commit

Permalink
New code (#20)
Browse files Browse the repository at this point in the history
* Move new algorithm into chiavdf

* Fix prover test, move FAST_MACHINE define in makefile

* Decide at runtime if to use FAST_MACHINE

* Better support long VDF runs

* Add documentation

* Add Lipa's document

* Catch some overflows

* Move prover test

* Attempt to catch runtime error.

* Initial commit 1weso

* Let timelord decide between 1weso and nweso use.

* Typos

* 1weso: Ignore all iters, except for 1st one and stop signal

* Fix typos.

* Initial commit refactor

* Small reorg.

* Added back pybind submodules

* Pin pybind11 to v.2.5.0

* Update gitignore to master

* Fix verifier test

* Catch some race conditions.

* 2weso working!

* Fast Nweso working

* Keep only 1 test.

* Fix module name.

* Try to fix test.

* Some fixes.

* Fix typo.

* Better stopping.

* LGTMs

* LGTM - add const references.

* Comment flake8/mypy since there's no python left.

* Document running the vdf_client tests.

* Try to catch more LGTMs.

* Remove some const ref, as it experimentally works faster.

* Try better locking.

* Fix 1weso test.

* Typo.

Co-authored-by: Gene Hoffman <[email protected]>
  • Loading branch information
fchirica and hoffmang9 authored Jun 17, 2020
1 parent 567ed0e commit f870e6a
Show file tree
Hide file tree
Showing 29 changed files with 1,976 additions and 957 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ jobs:
python -m pip install --upgrade pip
pip install cibuildwheel==1.3.0
- name: Lint source with flake8
run: |
pip install flake8
flake8 src setup.py
#- name: Lint source with flake8
# run: |
# pip install flake8
# flake8 src setup.py

- name: Lint source with mypy
run: |
pip install mypy
mypy --config-file mypi.ini src
#- name: Lint source with mypy
# run: |
# pip install mypy
# mypy --config-file mypi.ini src

- name: Build source distribution with MacOS
if: startsWith(matrix.os, 'mac')
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __pycache__/

# Generated assembly file
/asm_compiled.s
/avx2_asm_compiled.s

# Makefiles
CMakeFiles/
Expand Down
258 changes: 122 additions & 136 deletions README.md

Large diffs are not rendered by default.

Binary file added classgroups.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __init__(self, name, sourcedir=''):

def copy_vdf_client(build_dir, install_dir):
shutil.copy("src/vdf_client", install_dir)
shutil.copy("src/prover_test", install_dir)
shutil.copy("src/1weso_test", install_dir)
shutil.copy("src/2weso_test", install_dir)


def copy_vdf_bench(build_dir, install_dir):
Expand Down
63 changes: 63 additions & 0 deletions src/1weso_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "vdf.h"
#include "create_discriminant.h"
#include "verifier.h"

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() {
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;
}
assert(is_vdf_test); //assertions should be disabled in VDF_MODE==0
init_gmp();
allow_integer_constructor=true; //make sure the old gmp allocator isn't used
set_rounding_mode();

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

bool stopped = false;
fast_algorithm = false;

uint64_t iter = 1000000;
OneWesolowskiCallback* weso = new OneWesolowskiCallback(D, iter);
FastStorage* fast_storage = NULL;
std::thread vdf_worker(repeated_square, f, D, L, weso, fast_storage, std::ref(stopped));
Proof proof = ProveOneWesolowski(iter, D, (OneWesolowskiCallback*)weso, stopped);
stopped = true;
vdf_worker.join();
free(weso);

bool is_valid;
form x_init = form::generator(D);
form y, proof_form;
y = form::from_abd(
ConvertBytesToInt(proof.y.data(), 0, 129),
ConvertBytesToInt(proof.y.data(), 129, 2*129),
D
);
proof_form = form::from_abd(
ConvertBytesToInt(proof.proof.data(), 0, 65),
ConvertBytesToInt(proof.proof.data(), 65, 2*65),
D
);
VerifyWesolowskiProof(D, x_init, y, proof_form, iter, is_valid);
std::cout << "Verify result: " << is_valid << "\n";
}
66 changes: 66 additions & 0 deletions src/2weso_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "vdf.h"
#include "create_discriminant.h"
#include "verifier.h"

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, x, bytes.data(), bytes.size(), iteration, proof.witness_type)) {
std::cout << "Correct proof\n";
} else {
std::cout << "Incorrect proof\n";
}
}

int main() {
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;
}
assert(is_vdf_test); //assertions should be disabled in VDF_MODE==0
init_gmp();
allow_integer_constructor=true; //make sure the old gmp allocator isn't used
set_rounding_mode();

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

bool stopped = false;
fast_algorithm = false;
two_weso = true;
TwoWesolowskiCallback* weso = new TwoWesolowskiCallback(D);
FastStorage* fast_storage = NULL;
std::thread vdf_worker(repeated_square, f, D, L, weso, fast_storage, std::ref(stopped));
// Test 1 - 1 million iters.
uint64_t iteration = 1000000;
Proof proof = ProveTwoWeso(D, f, 1000000, 0, weso, 0, stopped);
CheckProof(D, proof, iteration);
// Test 2 - 15 million iters.
iteration = 15000000;
proof = ProveTwoWeso(D, f, iteration, 0, weso, 0, stopped);
CheckProof(D, proof, iteration);
// Test 3 - 100 million iters.
iteration = 100000000;
proof = ProveTwoWeso(D, f, iteration, 0, weso, 0, stopped);
CheckProof(D, proof, iteration);
// Test stopping gracefully.
stopped = true;
vdf_worker.join();
free(weso);
}
10 changes: 5 additions & 5 deletions src/Makefile.vdf-client
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ UNAME := $(shell uname)

LDFLAGS += -no-pie
LDLIBS += -lgmpxx -lgmp -lboost_system -pthread
CXXFLAGS += -std=c++1z -D VDF_MODE=0 -pthread -no-pie
CXXFLAGS += -std=c++1z -D VDF_MODE=0 -D FAST_MACHINE=1 -pthread -no-pie
ifeq ($(UNAME),Darwin)
CXXFLAGS += -D CHIAOSX=1
else
Expand All @@ -11,15 +11,15 @@ endif

.PHONY: all clean

all: vdf_client vdf_bench
all: vdf_client prover_test 1weso_test 2weso_test vdf_bench

clean:
rm -f *.o vdf_client vdf_bench compile_asm
rm -f *.o vdf_client prover_test 1weso_test 2weso_test compile_asm vdf_bench

vdf_client vdf_bench: %: %.o lzcnt.o asm_compiled.o avx2_asm_compiled.o
vdf_client vdf_bench prover_test 1weso_test 2weso_test: %: %.o lzcnt.o asm_compiled.o avx2_asm_compiled.o
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)

vdf_client.o vdf_bench.o: CXXFLAGS += $(OPT_CFLAGS)
vdf_client.o vdf_bench.o prover_test.o 1weso_test.o 2weso_test.o: CXXFLAGS += $(OPT_CFLAGS)

lzcnt.o: refcode/lzcnt.c
$(CC) -c refcode/lzcnt.c
Expand Down
2 changes: 1 addition & 1 deletion src/asm_gcd_128.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ typedef array<reg_scalar, 2> reg_scalar_128;
//preserves inputs. returns low part of result
//regs: RCX, 1x scalar
void shift_right(
reg_alloc regs, array<reg_scalar, 2> v, reg_scalar amount, reg_scalar res,
const reg_alloc& regs, array<reg_scalar, 2> v, reg_scalar amount, reg_scalar res,
reg_scalar tmp_rcx, reg_scalar tmp_res_2
) {
EXPAND_MACROS_SCOPE;
Expand Down
2 changes: 1 addition & 1 deletion src/asm_gcd_unsigned.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ struct asm_integer {
//-first calculate ~(c*d)
//-then calculate a*b+(~(c*d))+1
void mul_add(
reg_alloc regs, asm_integer a, reg_scalar b, asm_integer c, bool invert_output, bool carry_in_is_1
const reg_alloc& regs, asm_integer a, reg_scalar b, asm_integer c, bool invert_output, bool carry_in_is_1
) {
EXPAND_MACROS_SCOPE;

Expand Down
Loading

0 comments on commit f870e6a

Please sign in to comment.