forked from Chia-Network/chia-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdf.py
86 lines (73 loc) · 2.32 KB
/
vdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
import logging
import traceback
from enum import IntEnum
from functools import lru_cache
from typing import Optional
from chik_rs import VDFInfo, VDFProof
from chikvdf import create_discriminant, verify_n_wesolowski
from chik.consensus.constants import ConsensusConstants
from chik.types.blockchain_format.classgroup import ClassgroupElement
from chik.types.blockchain_format.sized_bytes import bytes32, bytes100
from chik.util.ints import uint8, uint64
log = logging.getLogger(__name__)
__all__ = ["VDFInfo", "VDFProof"]
@lru_cache(maxsize=200)
def get_discriminant(challenge: bytes32, size_bites: int) -> int:
return int(
create_discriminant(challenge, size_bites),
16,
)
@lru_cache(maxsize=1000)
def verify_vdf(
disc: int,
input_el: bytes100,
output: bytes,
number_of_iterations: uint64,
discriminant_size: int,
witness_type: uint8,
) -> bool:
# TODO: chikvdf needs hinted
return verify_n_wesolowski( # type:ignore[no-any-return]
str(disc),
input_el,
output,
number_of_iterations,
discriminant_size,
witness_type,
)
def validate_vdf(
proof: VDFProof,
constants: ConsensusConstants,
input_el: ClassgroupElement,
info: VDFInfo,
target_vdf_info: Optional[VDFInfo] = None,
) -> bool:
"""
If target_vdf_info is passed in, it is compared with info.
"""
if target_vdf_info is not None and info != target_vdf_info:
tb = traceback.format_stack()
log.error(f"{tb} INVALID VDF INFO. Have: {info} Expected: {target_vdf_info}")
return False
if proof.witness_type + 1 > constants.MAX_VDF_WITNESS_SIZE:
return False
try:
disc: int = get_discriminant(info.challenge, constants.DISCRIMINANT_SIZE_BITS)
# TODO: parallelize somehow, this might included multiple mini proofs (n weso)
return verify_vdf(
disc,
input_el.data,
info.output.data + bytes(proof.witness),
info.number_of_iterations,
constants.DISCRIMINANT_SIZE_BITS,
proof.witness_type,
)
except Exception:
return False
# Stores, for a given VDF, the field that uses it.
class CompressibleVDFField(IntEnum):
CC_EOS_VDF = 1
ICC_EOS_VDF = 2
CC_SP_VDF = 3
CC_IP_VDF = 4