Skip to content

Commit

Permalink
Move to main. isort and black
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmang9 committed Mar 11, 2021
1 parent a5032c8 commit b23db7d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 deletions.
59 changes: 36 additions & 23 deletions comparenweso.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from chiavdf import verify_wesolowski, verify_n_wesolowski
import time

from chiavdf import verify_n_wesolowski, verify_wesolowski


class ClassGroup(tuple):
@classmethod
def identity_for_discriminant(class_, d):
Expand Down Expand Up @@ -36,7 +38,12 @@ def __init__(self, t):
self._discriminant = None

def __eq__(self, obj):
return isinstance(obj, ClassGroup) and obj[0] == self[0] and obj[1] == self[1] and obj[2] == self[2]
return (
isinstance(obj, ClassGroup)
and obj[0] == self[0]
and obj[1] == self[1]
and obj[2] == self[2]
)

def identity(self):
return self.identity_for_discriminant(self.discriminant())
Expand Down Expand Up @@ -68,16 +75,23 @@ def serialize(self):
r = self.reduced()
int_size_bits = int(self.discriminant().bit_length())
int_size = (int_size_bits + 16) >> 4
return b"".join([x.to_bytes(int_size, "big", signed=True) for x in [r[0], r[1]]])
return b"".join(
[x.to_bytes(int_size, "big", signed=True) for x in [r[0], r[1]]]
)


def deserialize_proof(proof_blob, discriminant):
int_size = (discriminant.bit_length() + 16) >> 4
proof_arr = [proof_blob[_ : _ + 2 * int_size] for _ in range(0, len(proof_blob), 2 * int_size)]
proof_arr = [
proof_blob[_ : _ + 2 * int_size]
for _ in range(0, len(proof_blob), 2 * int_size)
]
return [ClassGroup.from_bytes(blob, discriminant) for blob in proof_arr]


def check_proof_of_time_nwesolowski(discriminant, x, proof_blob, iterations, int_size_bits, depth):
def check_proof_of_time_nwesolowski(
discriminant, x, proof_blob, iterations, int_size_bits, depth
):
"""
Check the nested wesolowski proof. The proof blob
includes the output of the VDF, along with the proof. The following
Expand All @@ -97,7 +111,9 @@ def check_proof_of_time_nwesolowski(discriminant, x, proof_blob, iterations, int
iter_list = []
for i in range(4 * int_size, len(proof_blob), 4 * int_size + 8):
iter_list.append(int.from_bytes(proof_blob[i : (i + 8)], byteorder="big"))
new_proof_blob = new_proof_blob + proof_blob[(i + 8) : (i + 8 + 4 * int_size)]
new_proof_blob = (
new_proof_blob + proof_blob[(i + 8) : (i + 8 + 4 * int_size)]
)
proof_blob = new_proof_blob

result_bytes = proof_blob[: (2 * int_size)]
Expand Down Expand Up @@ -139,19 +155,11 @@ def check_proof_of_time_nwesolowski(discriminant, x, proof_blob, iterations, int
except Exception:
return False

def oldie(
disc,
a,
b,
proof,
iter,
sb,
witness,
doold
):

def oldie(disc, a, b, proof, iter, sb, witness, doold):
if z == 0:
x = ClassGroup.from_ab_discriminant(int(a), int(b), int(disc))

return check_proof_of_time_nwesolowski(
int(disc),
x,
Expand All @@ -160,32 +168,37 @@ def oldie(
sb,
witness,
)

return verify_n_wesolowski(
disc,
a,b,
a,
b,
proof,
iter,
sb,
witness,
)


for z in range(2):
iters = 1000
t1 = time.time()
for i in range(iters):
is_valid = oldie(
str(-131653324254138636653163861414331698305531090221496467927360326686715180966094250598321899621249972220387687148397451395672779897144571112116763666653213748473909547482437246405018707472153290116227072825447643324530509016778432769802300913461285128339119844239772697652504835780459732685000796733645621728639),
str(
-131653324254138636653163861414331698305531090221496467927360326686715180966094250598321899621249972220387687148397451395672779897144571112116763666653213748473909547482437246405018707472153290116227072825447643324530509016778432769802300913461285128339119844239772697652504835780459732685000796733645621728639
),
str(2),
str(1),
bytes.fromhex("003f360be667de706fe886f766fe20240de04fe2c2f91207f1bbdddf20c554ab8d168b2ce9664d75f4613375a0ab12bf8158983574c9f5cd61c6b8a905fd3fa6bbffc5401b4ccedbe093b560293263a226e46302e720726586251116bc689ef09dc70d99e0a090c4409f928e218e85032fdbee02fedd563073be555b75a70a2d6a430033bc7a4926e3504e87698a0ace0dee6364cced2e9142b4e4cbe55a6371aab41e501ceed21d79d3a0dbbd82ce913c5de40b13eb7c59b1b52b6ef270ee603bd5e7fffcc9f5fae6dbd5aeec394181af130c0fdd195b22be745449b7a584ac80fc75ed49acfdb4d650f5cd344f86377ebbbaef5b19a0af3ae08101d1697f5656a52193000000000071c6f40024c342868a0c2a201b1b26a5d52c5d2f92a106c19ff926deb3fba1e74a444ecee3f8f507c062b949a2eaadd442b049417f82e8811526fa83c6d099d75323e068ffeca9dcd163761000c65d21dede72787ac350f25bdd3d29db6e9cb0e22c8124c724db33660c88784e2871b62ecf816846db7b469c71cad9a5dcfc5548ed2dd781006fa15b968facf4d79219646267eb187a670306d1ff1a59fc28ae00d36bb5a1cba659f48aa64a9022711a66105ef14401ff3948add265240aaad329ee76ba4c2300496746b86bcccacff5947c3fcb956cde2cffae10435960d7097f989aac742cf1047887f11584d20297958385e1715fe0f9b69141750c20d8134420eafec68fd10000000001555540006958aabfe4cc5d870e61fef82bcf1f2c3859e2bd8b1177e8a8872376b5cabace5dcb59b6fecada7e522d05f6f0e352939a6bfdf8c454fbe822cfa5ce97d17be0ffde44a4812cde9d04ec5c08dce6f9146586fdc8e081e05ec690b7effe24ea756f3d300f361203b61e1a39220c6eafa7852842674e317dcae5549c78c7144296ff004a6d0d2854c55e4c1de2f17dc4480b81652cfec37124ef41560a28c853482732434d1c006763b2e341528ae0bcc29fb76f1a4dafd99ade4fd75ec9cc9ca3f3d7001bcb6eb71e43eb22169ab721637551a8ec93838eb0825e9ecba9175297a00b146e9fdd244c5b722f29d3c46ec38840ba18f1f06ddec3dea844867386c2e1ac95"),
bytes.fromhex(
"003f360be667de706fe886f766fe20240de04fe2c2f91207f1bbdddf20c554ab8d168b2ce9664d75f4613375a0ab12bf8158983574c9f5cd61c6b8a905fd3fa6bbffc5401b4ccedbe093b560293263a226e46302e720726586251116bc689ef09dc70d99e0a090c4409f928e218e85032fdbee02fedd563073be555b75a70a2d6a430033bc7a4926e3504e87698a0ace0dee6364cced2e9142b4e4cbe55a6371aab41e501ceed21d79d3a0dbbd82ce913c5de40b13eb7c59b1b52b6ef270ee603bd5e7fffcc9f5fae6dbd5aeec394181af130c0fdd195b22be745449b7a584ac80fc75ed49acfdb4d650f5cd344f86377ebbbaef5b19a0af3ae08101d1697f5656a52193000000000071c6f40024c342868a0c2a201b1b26a5d52c5d2f92a106c19ff926deb3fba1e74a444ecee3f8f507c062b949a2eaadd442b049417f82e8811526fa83c6d099d75323e068ffeca9dcd163761000c65d21dede72787ac350f25bdd3d29db6e9cb0e22c8124c724db33660c88784e2871b62ecf816846db7b469c71cad9a5dcfc5548ed2dd781006fa15b968facf4d79219646267eb187a670306d1ff1a59fc28ae00d36bb5a1cba659f48aa64a9022711a66105ef14401ff3948add265240aaad329ee76ba4c2300496746b86bcccacff5947c3fcb956cde2cffae10435960d7097f989aac742cf1047887f11584d20297958385e1715fe0f9b69141750c20d8134420eafec68fd10000000001555540006958aabfe4cc5d870e61fef82bcf1f2c3859e2bd8b1177e8a8872376b5cabace5dcb59b6fecada7e522d05f6f0e352939a6bfdf8c454fbe822cfa5ce97d17be0ffde44a4812cde9d04ec5c08dce6f9146586fdc8e081e05ec690b7effe24ea756f3d300f361203b61e1a39220c6eafa7852842674e317dcae5549c78c7144296ff004a6d0d2854c55e4c1de2f17dc4480b81652cfec37124ef41560a28c853482732434d1c006763b2e341528ae0bcc29fb76f1a4dafd99ade4fd75ec9cc9ca3f3d7001bcb6eb71e43eb22169ab721637551a8ec93838eb0825e9ecba9175297a00b146e9fdd244c5b722f29d3c46ec38840ba18f1f06ddec3dea844867386c2e1ac95"
),
33554432,
1024,
2,
z
z,
)
assert is_valid

t2 = time.time()
print(f"{z} IPS: {iters / (t2 - t1)}")

9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import os
import platform
import re
import shutil
import sys
import platform
import subprocess

import sys
from distutils.command.build import build # type: ignore
from distutils.command.install import install
from distutils.version import LooseVersion
from setuptools.command.build_ext import build_ext
from setuptools import setup, setuptools, Extension, Command

from setuptools import Command, Extension, setup, setuptools
from setuptools.command.build_ext import build_ext

BUILD_HOOKS = []
INSTALL_HOOKS = []
Expand Down
3 changes: 2 additions & 1 deletion tests/test_verifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from chiavdf import prove, verify_wesolowski, create_discriminant
import secrets
import time

from chiavdf import create_discriminant, prove, verify_wesolowski


def test_prove_and_verify():
discriminant_challenge = secrets.token_bytes(10)
Expand Down
66 changes: 53 additions & 13 deletions tools/gen_pprods.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
#!/usr/bin/python3
import sys, math
import math
import sys


def dbg(*args):
print(*args, file=sys.stderr)

primes = 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, \
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, \
139, 149

primes = (
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
)


def is_prime(n):
if n in primes:
Expand All @@ -21,8 +59,9 @@ def is_prime(n):
return False
raise ValueError("Too big n=%d" % (n,))


def print_pprods_h(prods, factors):
tab = ' '
tab = " "
tmpl = """
#ifndef PPRODS_H
#define PPRODS_H
Expand All @@ -37,13 +76,14 @@ def print_pprods_h(prods, factors):
#endif // PPRODS_H
"""
tmpl = tmpl.strip()
prods_str = ''
prods_str = ""
for i, pr in enumerate(prods):
factors_str = '*'.join(map(str, factors[i]))
extra_spaces = ' ' * (16 - (pr.bit_length() + 3) // 4)
prods_str += tab + '0x%x, %s// %s\n' % (pr, extra_spaces, factors_str)
factors_str = "*".join(map(str, factors[i]))
extra_spaces = " " * (16 - (pr.bit_length() + 3) // 4)
prods_str += tab + "0x%x, %s// %s\n" % (pr, extra_spaces, factors_str)
print(tmpl % (prods_str.rstrip(),))


n = int(sys.argv[1])
cnt = 0
pr = 1
Expand All @@ -52,12 +92,12 @@ def print_pprods_h(prods, factors):
factors = []
for i in range(3, n):
if is_prime(i):
#print(i, '', end='')
# print(i, '', end='')
cnt += 1
pr *= i
if pr.bit_length() > 64:
pr //= i
#print('%s: %s' % (hex(pr), arr))
# print('%s: %s' % (hex(pr), arr))
prods.append(pr)
factors.append(arr)
pr = i
Expand All @@ -67,5 +107,5 @@ def print_pprods_h(prods, factors):
print_pprods_h(prods, factors)

dbg()
dbg('n_primes=%d n_prods=%d last_prime=%d' % (cnt, len(prods), factors[-1][-1]))
#dbg(*list('%s,' % (hex(i),) for i in prods))
dbg("n_primes=%d n_prods=%d last_prime=%d" % (cnt, len(prods), factors[-1][-1]))
# dbg(*list('%s,' % (hex(i),) for i in prods))

0 comments on commit b23db7d

Please sign in to comment.