Skip to content

Commit f870e6a

Browse files
fchiricahoffmang9
andauthored
New code (#20)
* 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]>
1 parent 567ed0e commit f870e6a

29 files changed

+1976
-957
lines changed

.github/workflows/build.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ jobs:
4242
python -m pip install --upgrade pip
4343
pip install cibuildwheel==1.3.0
4444
45-
- name: Lint source with flake8
46-
run: |
47-
pip install flake8
48-
flake8 src setup.py
45+
#- name: Lint source with flake8
46+
# run: |
47+
# pip install flake8
48+
# flake8 src setup.py
4949

50-
- name: Lint source with mypy
51-
run: |
52-
pip install mypy
53-
mypy --config-file mypi.ini src
50+
#- name: Lint source with mypy
51+
# run: |
52+
# pip install mypy
53+
# mypy --config-file mypi.ini src
5454

5555
- name: Build source distribution with MacOS
5656
if: startsWith(matrix.os, 'mac')

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __pycache__/
55

66
# Generated assembly file
77
/asm_compiled.s
8+
/avx2_asm_compiled.s
89

910
# Makefiles
1011
CMakeFiles/

README.md

+122-136
Large diffs are not rendered by default.

classgroups.pdf

454 KB
Binary file not shown.

setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def __init__(self, name, sourcedir=''):
6262

6363
def copy_vdf_client(build_dir, install_dir):
6464
shutil.copy("src/vdf_client", install_dir)
65+
shutil.copy("src/prover_test", install_dir)
66+
shutil.copy("src/1weso_test", install_dir)
67+
shutil.copy("src/2weso_test", install_dir)
6568

6669

6770
def copy_vdf_bench(build_dir, install_dir):

src/1weso_test.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "vdf.h"
2+
#include "create_discriminant.h"
3+
#include "verifier.h"
4+
5+
int segments = 7;
6+
int thread_count = 3;
7+
8+
Proof CreateProof(ProverManager& pm, uint64_t iteration) {
9+
return pm.Prove(iteration);
10+
}
11+
12+
int gcd_base_bits=50;
13+
int gcd_128_max_iter=3;
14+
15+
int main() {
16+
debug_mode = true;
17+
if(hasAVX2())
18+
{
19+
gcd_base_bits=63;
20+
gcd_128_max_iter=2;
21+
}
22+
std::vector<uint8_t> challenge_hash({0, 0, 1, 2, 3, 3, 4, 4});
23+
integer D = CreateDiscriminant(challenge_hash, 1024);
24+
25+
if (getenv( "warn_on_corruption_in_production" )!=nullptr) {
26+
warn_on_corruption_in_production=true;
27+
}
28+
assert(is_vdf_test); //assertions should be disabled in VDF_MODE==0
29+
init_gmp();
30+
allow_integer_constructor=true; //make sure the old gmp allocator isn't used
31+
set_rounding_mode();
32+
33+
integer L=root(-D, 4);
34+
form f=form::generator(D);
35+
36+
bool stopped = false;
37+
fast_algorithm = false;
38+
39+
uint64_t iter = 1000000;
40+
OneWesolowskiCallback* weso = new OneWesolowskiCallback(D, iter);
41+
FastStorage* fast_storage = NULL;
42+
std::thread vdf_worker(repeated_square, f, D, L, weso, fast_storage, std::ref(stopped));
43+
Proof proof = ProveOneWesolowski(iter, D, (OneWesolowskiCallback*)weso, stopped);
44+
stopped = true;
45+
vdf_worker.join();
46+
free(weso);
47+
48+
bool is_valid;
49+
form x_init = form::generator(D);
50+
form y, proof_form;
51+
y = form::from_abd(
52+
ConvertBytesToInt(proof.y.data(), 0, 129),
53+
ConvertBytesToInt(proof.y.data(), 129, 2*129),
54+
D
55+
);
56+
proof_form = form::from_abd(
57+
ConvertBytesToInt(proof.proof.data(), 0, 65),
58+
ConvertBytesToInt(proof.proof.data(), 65, 2*65),
59+
D
60+
);
61+
VerifyWesolowskiProof(D, x_init, y, proof_form, iter, is_valid);
62+
std::cout << "Verify result: " << is_valid << "\n";
63+
}

src/2weso_test.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "vdf.h"
2+
#include "create_discriminant.h"
3+
#include "verifier.h"
4+
5+
int segments = 7;
6+
int thread_count = 3;
7+
8+
int gcd_base_bits=50;
9+
int gcd_128_max_iter=3;
10+
11+
void CheckProof(integer& D, Proof& proof, uint64_t iteration) {
12+
form x = form::generator(D);
13+
std::vector<unsigned char> bytes;
14+
bytes.insert(bytes.end(), proof.y.begin(), proof.y.end());
15+
bytes.insert(bytes.end(), proof.proof.begin(), proof.proof.end());
16+
if (CheckProofOfTimeNWesolowski(D, x, bytes.data(), bytes.size(), iteration, proof.witness_type)) {
17+
std::cout << "Correct proof\n";
18+
} else {
19+
std::cout << "Incorrect proof\n";
20+
}
21+
}
22+
23+
int main() {
24+
debug_mode = true;
25+
if(hasAVX2())
26+
{
27+
gcd_base_bits=63;
28+
gcd_128_max_iter=2;
29+
}
30+
std::vector<uint8_t> challenge_hash({0, 0, 1, 2, 3, 3, 4, 4});
31+
integer D = CreateDiscriminant(challenge_hash, 1024);
32+
33+
if (getenv( "warn_on_corruption_in_production" )!=nullptr) {
34+
warn_on_corruption_in_production=true;
35+
}
36+
assert(is_vdf_test); //assertions should be disabled in VDF_MODE==0
37+
init_gmp();
38+
allow_integer_constructor=true; //make sure the old gmp allocator isn't used
39+
set_rounding_mode();
40+
41+
integer L=root(-D, 4);
42+
form f=form::generator(D);
43+
44+
bool stopped = false;
45+
fast_algorithm = false;
46+
two_weso = true;
47+
TwoWesolowskiCallback* weso = new TwoWesolowskiCallback(D);
48+
FastStorage* fast_storage = NULL;
49+
std::thread vdf_worker(repeated_square, f, D, L, weso, fast_storage, std::ref(stopped));
50+
// Test 1 - 1 million iters.
51+
uint64_t iteration = 1000000;
52+
Proof proof = ProveTwoWeso(D, f, 1000000, 0, weso, 0, stopped);
53+
CheckProof(D, proof, iteration);
54+
// Test 2 - 15 million iters.
55+
iteration = 15000000;
56+
proof = ProveTwoWeso(D, f, iteration, 0, weso, 0, stopped);
57+
CheckProof(D, proof, iteration);
58+
// Test 3 - 100 million iters.
59+
iteration = 100000000;
60+
proof = ProveTwoWeso(D, f, iteration, 0, weso, 0, stopped);
61+
CheckProof(D, proof, iteration);
62+
// Test stopping gracefully.
63+
stopped = true;
64+
vdf_worker.join();
65+
free(weso);
66+
}

src/Makefile.vdf-client

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UNAME := $(shell uname)
22

33
LDFLAGS += -no-pie
44
LDLIBS += -lgmpxx -lgmp -lboost_system -pthread
5-
CXXFLAGS += -std=c++1z -D VDF_MODE=0 -pthread -no-pie
5+
CXXFLAGS += -std=c++1z -D VDF_MODE=0 -D FAST_MACHINE=1 -pthread -no-pie
66
ifeq ($(UNAME),Darwin)
77
CXXFLAGS += -D CHIAOSX=1
88
else
@@ -11,15 +11,15 @@ endif
1111

1212
.PHONY: all clean
1313

14-
all: vdf_client vdf_bench
14+
all: vdf_client prover_test 1weso_test 2weso_test vdf_bench
1515

1616
clean:
17-
rm -f *.o vdf_client vdf_bench compile_asm
17+
rm -f *.o vdf_client prover_test 1weso_test 2weso_test compile_asm vdf_bench
1818

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

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

2424
lzcnt.o: refcode/lzcnt.c
2525
$(CC) -c refcode/lzcnt.c

src/asm_gcd_128.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ typedef array<reg_scalar, 2> reg_scalar_128;
77
//preserves inputs. returns low part of result
88
//regs: RCX, 1x scalar
99
void shift_right(
10-
reg_alloc regs, array<reg_scalar, 2> v, reg_scalar amount, reg_scalar res,
10+
const reg_alloc& regs, array<reg_scalar, 2> v, reg_scalar amount, reg_scalar res,
1111
reg_scalar tmp_rcx, reg_scalar tmp_res_2
1212
) {
1313
EXPAND_MACROS_SCOPE;

src/asm_gcd_unsigned.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ struct asm_integer {
278278
//-first calculate ~(c*d)
279279
//-then calculate a*b+(~(c*d))+1
280280
void mul_add(
281-
reg_alloc regs, asm_integer a, reg_scalar b, asm_integer c, bool invert_output, bool carry_in_is_1
281+
const reg_alloc& regs, asm_integer a, reg_scalar b, asm_integer c, bool invert_output, bool carry_in_is_1
282282
) {
283283
EXPAND_MACROS_SCOPE;
284284

0 commit comments

Comments
 (0)