Hand-picked type data to follow-up monkeytype

pull/191/head
Kieran Prasch 2018-07-08 10:05:46 -07:00 committed by tuxxy
parent cbfe8573e6
commit 19b1991de6
7 changed files with 59 additions and 52 deletions

1
.gitignore vendored
View File

@ -249,3 +249,4 @@ pytest.ini
/tests/metrics/.benchmarks/
tests/metrics/histograms/
.circleci/execute_build.sh
/monkeytype.sqlite3

View File

@ -1,4 +1,4 @@
from typing import Optional, Type, Union
from typing import Optional, Type
from warnings import warn
from umbral.curve import Curve, SECP256K1
@ -26,7 +26,7 @@ class _CONFIG:
return cls.__params
@classmethod
def curve(cls) -> Union[Type[SECP256R1], Type[SECP256K1]]:
def curve(cls) -> Type[Curve]:
if not cls.__curve:
cls.__set_curve_by_default()
return cls.__curve
@ -48,7 +48,7 @@ def set_default_curve(curve: Optional[Curve] = None) -> None:
return _CONFIG.set_curve(curve)
def default_curve() -> Union[Type[SECP256R1], Type[SECP256K1]]:
def default_curve() -> Type[Curve]:
return _CONFIG.curve()

View File

@ -1,13 +1,11 @@
import os
from cryptography.hazmat.backends.openssl import backend
from cryptography.hazmat.primitives import hashes
from umbral import openssl
from umbral.config import default_curve, default_params
from umbral.config import default_curve
from umbral.curve import Curve
from umbral.utils import get_field_order_size_in_bytes
from umbral.params import UmbralParameters
from umbral.utils import get_field_order_size_in_bytes
class CurveBN(object):
@ -27,7 +25,7 @@ class CurveBN(object):
self.curve = curve
@classmethod
def expected_bytes_length(cls, curve: Curve=None):
def expected_bytes_length(cls, curve: Curve=None) -> int:
"""
Returns the size (in bytes) of a CurveBN given the curve.
If no curve is provided, it uses the default.
@ -36,7 +34,7 @@ class CurveBN(object):
return get_field_order_size_in_bytes(curve)
@classmethod
def gen_rand(cls, curve: Curve=None):
def gen_rand(cls, curve: Curve=None) -> 'CurveBN':
"""
Returns a CurveBN object with a cryptographically secure OpenSSL BIGNUM
based on the given curve.
@ -56,7 +54,7 @@ class CurveBN(object):
return cls(new_rand_bn, curve)
@classmethod
def from_int(cls, num, curve: Curve=None):
def from_int(cls, num: int, curve: Curve=None) -> 'CurveBN':
"""
Returns a CurveBN object from a given integer on a curve.
By default, the underlying OpenSSL BIGNUM has BN_FLG_CONSTTIME set for
@ -67,7 +65,7 @@ class CurveBN(object):
return cls(conv_bn, curve)
@classmethod
def hash(cls, *crypto_items, params: UmbralParameters):
def hash(cls, *crypto_items, params: UmbralParameters) -> 'CurveBN':
# TODO: Clean this in an upcoming cleanup of pyUmbral
blake2b = hashes.Hash(hashes.BLAKE2b(64), backend=backend)
for item in crypto_items:
@ -101,7 +99,7 @@ class CurveBN(object):
return cls(bignum, params.curve)
@classmethod
def from_bytes(cls, data, curve: Curve=None):
def from_bytes(cls, data: bytes, curve: Curve=None) -> 'CurveBN':
"""
Returns a CurveBN object from the given byte data that's within the size
of the provided curve's order.
@ -112,20 +110,20 @@ class CurveBN(object):
num = int.from_bytes(data, 'big')
return cls.from_int(num, curve)
def to_bytes(self):
def to_bytes(self) -> bytes:
"""
Returns the CurveBN as bytes.
"""
size = backend._lib.BN_num_bytes(self.curve.order)
return int.to_bytes(int(self), size, 'big')
def __int__(self):
def __int__(self) -> int:
"""
Converts the CurveBN to a Python int.
"""
return backend._bn_to_int(self.bignum)
def __eq__(self, other):
def __eq__(self, other) -> bool:
"""
Compares the two BIGNUMS or int.
"""
@ -137,7 +135,7 @@ class CurveBN(object):
# -1 less than, 0 is equal to, 1 is greater than
return not bool(backend._lib.BN_cmp(self.bignum, other.bignum))
def __pow__(self, other):
def __pow__(self, other) -> 'CurveBN':
"""
Performs a BN_mod_exp on two BIGNUMS.
@ -157,7 +155,7 @@ class CurveBN(object):
return CurveBN(power, self.curve)
def __mul__(self, other):
def __mul__(self, other) -> 'CurveBN':
"""
Performs a BN_mod_mul between two BIGNUMS.
"""
@ -173,7 +171,7 @@ class CurveBN(object):
return CurveBN(product, self.curve)
def __truediv__(self, other):
def __truediv__(self, other) -> 'CurveBN':
"""
Performs a BN_div on two BIGNUMs (modulo the order of the curve).
@ -193,7 +191,7 @@ class CurveBN(object):
return CurveBN(product, self.curve)
def __add__(self, other):
def __add__(self, other) -> 'CurveBN':
"""
Performs a BN_mod_add on two BIGNUMs.
"""
@ -206,7 +204,7 @@ class CurveBN(object):
return CurveBN(op_sum, self.curve)
def __sub__(self, other):
def __sub__(self, other) -> 'CurveBN':
"""
Performs a BN_mod_sub on two BIGNUMS.
"""
@ -219,7 +217,7 @@ class CurveBN(object):
return CurveBN(diff, self.curve)
def __invert__(self):
def __invert__(self) -> 'CurveBN':
"""
Performs a BN_mod_inverse.
@ -235,7 +233,7 @@ class CurveBN(object):
return CurveBN(inv, self.curve)
def __mod__(self, other):
def __mod__(self, other) -> 'CurveBN':
"""
Performs a BN_nnmod on two BIGNUMS.
"""

View File

@ -154,7 +154,8 @@ class CapsuleFrag(object):
point_v1: Point,
kfrag_id: bytes,
point_noninteractive: Point,
point_xcoord: Point, proof: Optional[CorrectnessProof] = None) -> None:
point_xcoord: Point,
proof: Optional[CorrectnessProof] = None) -> None:
self._point_e1 = point_e1
self._point_v1 = point_v1
@ -203,7 +204,7 @@ class CapsuleFrag(object):
proof = components.pop(-1) or None
proof = CorrectnessProof.from_bytes(proof, curve) if proof else None
return cls(*components, proof)
return cls(*components, proof=proof)
def to_bytes(self) -> bytes:
"""

View File

@ -1,13 +1,15 @@
from umbral.curvebn import CurveBN
from typing import Optional, Tuple
from cryptography.exceptions import InternalError
from cryptography.hazmat.backends.openssl import backend
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InternalError
from umbral import openssl
from umbral.config import default_curve
from umbral.curve import Curve
from umbral.utils import get_field_order_size_in_bytes
from umbral.curvebn import CurveBN
from umbral.params import UmbralParameters
from umbral.utils import get_field_order_size_in_bytes
class Point(object):
@ -15,12 +17,12 @@ class Point(object):
Represents an OpenSSL EC_POINT except more Pythonic
"""
def __init__(self, ec_point, curve: Curve):
def __init__(self, ec_point, curve: Curve) -> None:
self.ec_point = ec_point
self.curve = curve
@classmethod
def expected_bytes_length(cls, curve: Curve=None):
def expected_bytes_length(cls, curve: Optional[Curve] = None) -> int:
"""
Returns the size (in bytes) of a compressed Point given a curve.
If no curve is provided, it uses the default curve.
@ -29,7 +31,7 @@ class Point(object):
return get_field_order_size_in_bytes(curve) + 1
@classmethod
def gen_rand(cls, curve: Curve=None):
def gen_rand(cls, curve: Optional[Curve] = None) -> 'Point':
"""
Returns a Point object with a cryptographically secure EC_POINT based
on the provided curve.
@ -49,7 +51,7 @@ class Point(object):
return cls(rand_point, curve)
@classmethod
def from_affine(cls, coords, curve: Curve=None):
def from_affine(cls, coords: Tuple[int, int], curve: Optional[Curve] = None) -> 'Point':
"""
Returns a Point object from the given affine coordinates in a tuple in
the format of (x, y) and a given curve.
@ -76,7 +78,7 @@ class Point(object):
return (backend._bn_to_int(affine_x), backend._bn_to_int(affine_y))
@classmethod
def from_bytes(cls, data, curve: Curve=None):
def from_bytes(cls, data: bytes, curve: Optional[Curve] = None) -> 'Point':
"""
Returns a Point object from the given byte data on the curve provided.
"""
@ -113,7 +115,7 @@ class Point(object):
else:
raise ValueError("Invalid point serialization.")
def to_bytes(self, is_compressed=True):
def to_bytes(self, is_compressed: bool=True) -> bytes:
"""
Returns the Point serialized as bytes. It will return a compressed form
if is_compressed is set to True.
@ -133,7 +135,7 @@ class Point(object):
return data
@classmethod
def get_generator_from_curve(cls, curve: Curve=None):
def get_generator_from_curve(cls, curve: Optional[Curve] = None) -> 'Point':
"""
Returns the generator Point from the given curve as a Point object.
"""
@ -153,7 +155,7 @@ class Point(object):
# 1 is not-equal, 0 is equal, -1 is error
return not bool(is_equal)
def __mul__(self, other):
def __mul__(self, other) -> 'Point':
"""
Performs an EC_POINT_mul on an EC_POINT and a BIGNUM.
"""
@ -170,7 +172,7 @@ class Point(object):
__rmul__ = __mul__
def __add__(self, other):
def __add__(self, other) -> 'Point':
"""
Performs an EC_POINT_add on two EC_POINTS.
"""
@ -188,7 +190,7 @@ class Point(object):
"""
return (self + (~other))
def __invert__(self):
def __invert__(self) -> 'Point':
"""
Performs an EC_POINT_invert on itself.
"""
@ -203,11 +205,11 @@ class Point(object):
backend.openssl_assert(res == 1)
return Point(inv, self.curve)
def __bytes__(self):
def __bytes__(self) -> bytes:
return self.to_bytes()
def unsafe_hash_to_point(data, params : UmbralParameters, label=None):
def unsafe_hash_to_point(data, params: UmbralParameters, label=None) -> 'Point':
"""
Hashes arbitrary data into a valid EC point of the specified curve,
using the try-and-increment method.
@ -234,8 +236,7 @@ def unsafe_hash_to_point(data, params : UmbralParameters, label=None):
compressed02 = b"\x02" + hash_digest
try:
h = Point.from_bytes(compressed02, params.curve)
return h
return Point.from_bytes(compressed02, params.curve)
except InternalError as e:
# We want to catch specific InternalExceptions:
# - Point not in the curve (code 107)

View File

@ -1,4 +1,5 @@
import os
import typing
from typing import Dict, List, Optional, Tuple, Union
from cryptography.hazmat.backends.openssl import backend
@ -67,7 +68,7 @@ class Capsule(object):
self._point_v_prime = point_v_prime
self._point_noninteractive = point_noninteractive
self._attached_cfrags = list()
self._attached_cfrags = list() # type: list
@classmethod
def expected_bytes_length(cls, curve: Optional[EllipticCurve] = None, activated: bool = False) -> int:
@ -149,15 +150,16 @@ class Capsule(object):
def set_correctness_keys(self,
delegating: Optional[UmbralPublicKey] = None,
receiving: Optional[UmbralPublicKey] = None,
verifying: Optional[UmbralPublicKey] = None
verifying: Optional[UmbralPublicKey] = None,
) -> Tuple[bool, bool, bool]:
delegating_key_details = self._set_cfrag_correctness_key("delegating", delegating)
receiving_key_details = self._set_cfrag_correctness_key("receiving", receiving)
verifying_key_details = self._set_cfrag_correctness_key("verifying", verifying)
delegating_key_details = self._set_cfrag_correctness_key(key_type="delegating", key=delegating)
receiving_key_details = self._set_cfrag_correctness_key(key_type="receiving", key=receiving)
verifying_key_details = self._set_cfrag_correctness_key(key_type="verifying", key=verifying)
return delegating_key_details, receiving_key_details, verifying_key_details
@typing.no_type_check
def _original_to_bytes(self) -> bytes:
return bytes().join(c.to_bytes() for c in self.original_components())
@ -178,7 +180,8 @@ class Capsule(object):
s = self._bn_sig
h = CurveBN.hash(e, v, params=self._umbral_params)
return s * g == v + (h * e)
result = s * g == v + (h * e) # type: bool
return result
def attach_cfrag(self, cfrag: CapsuleFrag) -> None:
if cfrag.verify_correctness(self):
@ -261,6 +264,7 @@ class Capsule(object):
# Again, it's hard to imagine why.
return False
@typing.no_type_check
def __hash__(self) -> int:
# We only ever want to store in a hash table based on original components;
# A Capsule that is part of a dict needs to continue to be lookup-able even
@ -342,9 +346,13 @@ def split_rekey(delegating_privkey: UmbralPrivateKey, signer: Signer,
bytes(material) for material in (id, pubkey_a_point, pubkey_b_point, u1, ni, xcoord))
signature = signer(kfrag_validity_message)
kfrag = KFrag(id=id, bn_key=rk,
point_noninteractive=ni, point_commitment=u1,
point_xcoord=xcoord, signature=signature)
kfrag = KFrag(id=id,
bn_key=rk,
point_noninteractive=ni,
point_commitment=u1,
point_xcoord=xcoord,
signature=signature)
kfrags.append(kfrag)
return kfrags
@ -356,7 +364,6 @@ def reencrypt(kfrag: KFrag, capsule: Capsule, provide_proof: bool = True,
if not capsule.verify():
raise capsule.NotValid
rk = kfrag._bn_key
e1 = rk * capsule._point_e
v1 = rk * capsule._point_v

View File

@ -4,7 +4,6 @@ from typing import Optional
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.ec import ECDSA
from cryptography.hazmat.primitives.asymmetric.ec import Curve
from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature, encode_dss_signature
from umbral.config import default_curve