예제 #1
0
    def from_bytes(cls,
                   data: bytes,
                   curve: Optional[Curve] = None) -> 'CapsuleFrag':
        """
        Instantiates a CapsuleFrag object from the serialized data.
        """
        curve = curve if curve is not None else default_curve()

        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)
        arguments = {'curve': curve}

        splitter = BytestringSplitter(
            (Point, point_size, arguments),  # point_e1
            (Point, point_size, arguments),  # point_v1
            bn_size,  # kfrag_id
            (Point, point_size, arguments),  # point_precursor
        )
        components = splitter(data, return_remainder=True)

        proof = components.pop() or None
        components.append(
            CorrectnessProof.from_bytes(proof, curve) if proof else None)

        return cls(*components)
예제 #2
0
    def from_bytes(cls, data: bytes, curve: Optional[Curve] = None) -> 'KFrag':
        """
        Instantiate a KFrag object from the serialized data.
        """
        curve = curve if curve is not None else default_curve()

        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)
        signature_size = Signature.expected_bytes_length(curve)
        arguments = {'curve': curve}

        splitter = BytestringSplitter(
            bn_size,  # id
            (CurveBN, bn_size, arguments),  # bn_key
            (Point, point_size, arguments),  # point_commitment
            (Point, point_size, arguments),  # point_precursor
            1,  # keys_in_signature
            (Signature, signature_size, arguments),  # signature_for_proxy
            (Signature, signature_size, arguments),  # signature_for_bob
        )
        components = splitter(data)

        return cls(identifier=components[0],
                   bn_key=components[1],
                   point_commitment=components[2],
                   point_precursor=components[3],
                   keys_in_signature=components[4],
                   signature_for_proxy=components[5],
                   signature_for_bob=components[6])
예제 #3
0
 def raw_bytes_from_point(point: Point, only_y_coord=False) -> bytes:
     uncompressed_point_bytes = point.to_bytes(is_compressed=False)
     if only_y_coord:
         y_coord_start = (1 + Point.expected_bytes_length(is_compressed=False)) // 2
         return uncompressed_point_bytes[y_coord_start:]
     else:
         return uncompressed_point_bytes[1:]
예제 #4
0
파일: pre.py 프로젝트: soros1321/pyUmbral
    def from_bytes(cls, capsule_bytes: bytes, params: UmbralParameters):
        """
        Instantiates a Capsule object from the serialized data.
        """
        curve = params.curve

        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        capsule_bytes_length = len(capsule_bytes)
        if capsule_bytes_length == cls.expected_bytes_length(curve,
                                                             activated=True):
            splitter = BytestringSplitter(
                (Point, point_size),  # point_e
                (Point, point_size),  # point_v
                (CurveBN, bn_size),  # bn_sig
                (Point, point_size),  # point_e_prime
                (Point, point_size),  # point_v_prime
                (Point, point_size)  # point_noninteractive
            )
        elif capsule_bytes_length == cls.expected_bytes_length(
                curve, activated=False):
            splitter = BytestringSplitter(
                (Point, point_size),  # point_e
                (Point, point_size),  # point_v
                (CurveBN, bn_size)  # bn_sig
            )
        else:
            raise ValueError(
                "Byte string does not have a valid length for a Capsule")

        components = splitter(capsule_bytes)
        return cls(params, *components)
예제 #5
0
    def from_bytes(cls, capsule_bytes: bytes, curve: ec.EllipticCurve = None):
        """
        Instantiates a Capsule object from the serialized data.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        if len(capsule_bytes) == cls.expected_bytes_length(curve,
                                                           activated=True):
            splitter = BytestringSplitter(
                (Point, point_size),  # point_e
                (Point, point_size),  # point_v
                (CurveBN, bn_size),  # bn_sig
                (Point, point_size),  # point_e_prime
                (Point, point_size),  # point_v_prime
                (Point, point_size)  # point_noninteractive
            )
        else:
            splitter = BytestringSplitter(
                (Point, point_size),  # point_e
                (Point, point_size),  # point_v
                (CurveBN, bn_size)  # bn_sig
            )

        components = splitter(capsule_bytes)
        return cls(*components)
 def expected_bytes_length(cls, curve: Optional[Curve] = None,
                           is_compressed: bool = True) -> int:
     """
     Returns the size (in bytes) of an UmbralPublicKey given a curve.
     If no curve is provided, it uses the default curve.
     By default, it assumes compressed representation (is_compressed = True).
     """
     return Point.expected_bytes_length(curve=curve, is_compressed=is_compressed)
예제 #7
0
def test_public_key_to_uncompressed_bytes(random_ec_curvebn1):
    priv_key = random_ec_curvebn1

    params = default_params()
    pub_key = priv_key * params.g

    umbral_key = UmbralPublicKey(pub_key, params)
    key_bytes = umbral_key.to_bytes(is_compressed=False)
    assert len(key_bytes) == Point.expected_bytes_length(is_compressed=False)
예제 #8
0
    def expected_bytes_length(cls, curve: Optional[Curve] = None) -> int:
        """
        Returns the size (in bytes) of a Capsule given the curve.
        If no curve is provided, it will use the default curve.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        return (bn_size * 1) + (point_size * 2)
예제 #9
0
    def expected_bytes_length(cls, curve: ec.EllipticCurve = None):
        """
        Returns the size (in bytes) of a KFrag given the curve.
        If no curve is provided, it will use the default curve.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        return (bn_size * 4) + (point_size * 3)
예제 #10
0
    def expected_bytes_length(cls, curve: Optional[Curve] = None):
        """
        Returns the size (in bytes) of a CorrectnessProof without the metadata.
        If no curve is given, it will use the default curve.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve=curve)
        point_size = Point.expected_bytes_length(curve=curve)

        return (bn_size * 3) + (point_size * 4)
예제 #11
0
    def expected_bytes_length(cls,
                              curve: Optional[EllipticCurve] = None) -> int:
        """
        Returns the size (in bytes) of a CapsuleFrag given the curve without
        the CorrectnessProof.
        If no curve is provided, it will use the default curve.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        return (bn_size * 1) + (point_size * 4)
예제 #12
0
파일: pre.py 프로젝트: soros1321/pyUmbral
    def expected_bytes_length(cls,
                              curve: ec.EllipticCurve = None,
                              activated=False):
        """
        Returns the size (in bytes) of a Capsule given the curve.
        If no curve is provided, it will use the default curve.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        if not activated:
            return (bn_size * 1) + (point_size * 2)
        else:
            return (bn_size * 1) + (point_size * 5)
예제 #13
0
    def expected_bytes_length(cls, curve: Optional[Curve] = None) -> int:
        """
        Returns the size (in bytes) of a KFrag given the curve.
        If no curve is provided, it will use the default curve.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        # self.id --> 1 bn_size
        # self.bn_key --> 1 bn_size
        # self.point_commitment --> 1 point_size
        # self.point_precursor --> 1 point_size
        # self.signature_for_proxy --> 2 bn_size
        # self.signature_for_bob --> 2 bn_size
        # self.keys_in_signature --> 1

        return bn_size * 6 + point_size * 2 + 1
예제 #14
0
    def from_bytes(cls, data: bytes, curve: ec.EllipticCurve = None):
        """
        Instantiate a KFrag object from the serialized data.
        """
        curve = curve if curve is not None else default_curve()

        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        splitter = BytestringSplitter(
            bn_size,  # id
            (CurveBN, bn_size),  # bn_key
            (Point, point_size),  # point_noninteractive
            (Point, point_size),  # point_commitment
            (Point, point_size),  # point_xcoord
            (Signature, Signature.expected_bytes_length(curve)))
        components = splitter(data)

        return cls(*components)
예제 #15
0
    def from_bytes(cls, data: bytes, curve: ec.EllipticCurve = None):
        """
        Instantiate CorrectnessProof from serialized data.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        splitter = BytestringSplitter(
            (Point, point_size),  # point_e2
            (Point, point_size),  # point_v2
            (Point, point_size),  # point_kfrag_commitment
            (Point, point_size),  # point_kfrag_pok
            (CurveBN, bn_size),  # bn_sig
            (Signature),  # kfrag_signature
        )
        components = splitter(data, return_remainder=True)
        metadata = components.pop(-1) or None

        return cls(*components, metadata=metadata)
예제 #16
0
    def from_bytes(cls, data: bytes, curve: Optional[Curve] = None) -> 'CorrectnessProof':
        """
        Instantiate CorrectnessProof from serialized data.
        """
        curve = curve if curve is not None else default_curve()
        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)
        arguments = {'curve': curve}
        splitter = BytestringSplitter(
            (Point, point_size, arguments),  # point_e2
            (Point, point_size, arguments),  # point_v2
            (Point, point_size, arguments),  # point_kfrag_commitment
            (Point, point_size, arguments),  # point_kfrag_pok
            (CurveBN, bn_size, arguments),  # bn_sig
            (Signature, Signature.expected_bytes_length(curve), arguments),  # kfrag_signature
        )
        components = splitter(data, return_remainder=True)
        components.append(components.pop() or None)

        return cls(*components)
예제 #17
0
    def from_bytes(cls, data: bytes, curve: ec.EllipticCurve = None):
        """
        Instantiates a CapsuleFrag object from the serialized data.
        """
        curve = curve if curve is not None else default_curve()

        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)

        splitter = BytestringSplitter(
            (Point, point_size),  # point_e1
            (Point, point_size),  # point_v1
            bn_size,  # kfrag_id
            (Point, point_size),  # point_noninteractive
            (Point, point_size)  # point_xcoord
        )
        components = splitter(data, return_remainder=True)

        proof = components.pop(-1) or None
        proof = CorrectnessProof.from_bytes(proof, curve) if proof else None
        return cls(*components, proof)
예제 #18
0
    def from_bytes(cls, capsule_bytes: bytes,
                   params: UmbralParameters) -> 'Capsule':
        """
        Instantiates a Capsule object from the serialized data.
        """
        curve = params.curve

        bn_size = CurveBN.expected_bytes_length(curve)
        point_size = Point.expected_bytes_length(curve)
        arguments = {'curve': curve}

        if len(capsule_bytes) == cls.expected_bytes_length(curve):
            splitter = BytestringSplitter(
                (Point, point_size, arguments),  # point_e
                (Point, point_size, arguments),  # point_v
                (CurveBN, bn_size, arguments)  # bn_sig
            )
        else:
            raise ValueError(
                "Byte string does not have a valid length for a Capsule")

        components = splitter(capsule_bytes)
        return cls(params, *components)
예제 #19
0
def test_public_key_to_compressed_bytes(random_ec_curvebn1):
    umbral_key = UmbralPrivateKey(random_ec_curvebn1, default_params()).get_pubkey()
    key_bytes = bytes(umbral_key)
    assert len(key_bytes) == Point.expected_bytes_length(is_compressed=True)