mirror of https://github.com/nucypher/nucypher.git
Utility function nucypher.crypto.utils.get_coordinate_as_bytes
parent
569dd23f2b
commit
6ce55d8d60
|
@ -19,6 +19,7 @@ from coincurve import PublicKey
|
|||
from eth_keys import KeyAPI as EthKeyAPI
|
||||
from typing import Any, Union
|
||||
from umbral.keys import UmbralPublicKey
|
||||
from umbral.point import Point
|
||||
from umbral.signing import Signature
|
||||
|
||||
from nucypher.crypto.api import keccak_digest
|
||||
|
@ -113,3 +114,16 @@ def get_signature_recovery_value(message: bytes,
|
|||
else:
|
||||
raise ValueError("Signature recovery failed. "
|
||||
"Either the message, the signature or the public key is not correct")
|
||||
|
||||
|
||||
def get_coordinates_as_bytes(point: Union[Point, UmbralPublicKey], x_coord=True, y_coord=True) -> bytes:
|
||||
coordinates_as_bytes = point.to_bytes(is_compressed=False)[1:]
|
||||
middle = len(coordinates_as_bytes)//2
|
||||
if x_coord and y_coord:
|
||||
return coordinates_as_bytes
|
||||
elif x_coord:
|
||||
return coordinates_as_bytes[:middle]
|
||||
elif y_coord:
|
||||
return coordinates_as_bytes[middle:]
|
||||
else:
|
||||
raise ValueError("At least one coordinate must be set")
|
||||
|
|
|
@ -33,7 +33,6 @@ from umbral.config import default_params
|
|||
from umbral.curvebn import CurveBN
|
||||
from umbral.keys import UmbralPublicKey
|
||||
from umbral.kfrags import KFrag
|
||||
from umbral.point import Point
|
||||
from umbral.pre import Capsule
|
||||
|
||||
from nucypher.characters.lawful import Alice, Bob, Ursula, Character
|
||||
|
@ -43,7 +42,10 @@ from nucypher.crypto.kits import UmbralMessageKit, RevocationKit
|
|||
from nucypher.crypto.powers import SigningPower, DecryptingPower
|
||||
from nucypher.crypto.signing import Signature, InvalidSignature, signature_splitter
|
||||
from nucypher.crypto.splitters import key_splitter, capsule_splitter
|
||||
from nucypher.crypto.utils import canonical_address_from_umbral_key, get_signature_recovery_value, construct_policy_id
|
||||
from nucypher.crypto.utils import (canonical_address_from_umbral_key,
|
||||
construct_policy_id,
|
||||
get_coordinates_as_bytes,
|
||||
get_signature_recovery_value)
|
||||
from nucypher.network.exceptions import NodeSeemsToBeDown
|
||||
from nucypher.network.middleware import RestMiddleware, NotFound
|
||||
|
||||
|
@ -829,9 +831,7 @@ class IndisputableEvidence:
|
|||
|
||||
hash_input = (e, e1, e2, v, v1, v2, u, u1, u2, metadata)
|
||||
|
||||
h = hash_to_curvebn(*hash_input,
|
||||
params=umbral_params,
|
||||
hash_class=ExtendedKeccak)
|
||||
h = hash_to_curvebn(*hash_input, params=umbral_params, hash_class=ExtendedKeccak)
|
||||
return h
|
||||
|
||||
def precompute_values(self) -> bytes:
|
||||
|
@ -858,31 +858,24 @@ class IndisputableEvidence:
|
|||
vz = z * v
|
||||
uz = z * u
|
||||
|
||||
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:]
|
||||
|
||||
only_y_coord = dict(x_coord=False, y_coord=True)
|
||||
# E points
|
||||
e_y = raw_bytes_from_point(e, only_y_coord=True)
|
||||
ez_xy = raw_bytes_from_point(ez)
|
||||
e1_y = raw_bytes_from_point(e1, only_y_coord=True)
|
||||
e1h_xy = raw_bytes_from_point(e1h)
|
||||
e2_y = raw_bytes_from_point(e2, only_y_coord=True)
|
||||
e_y = get_coordinates_as_bytes(e, **only_y_coord)
|
||||
ez_xy = get_coordinates_as_bytes(ez)
|
||||
e1_y = get_coordinates_as_bytes(e1, **only_y_coord)
|
||||
e1h_xy = get_coordinates_as_bytes(e1h)
|
||||
e2_y = get_coordinates_as_bytes(e2, **only_y_coord)
|
||||
# V points
|
||||
v_y = raw_bytes_from_point(v, only_y_coord=True)
|
||||
vz_xy = raw_bytes_from_point(vz)
|
||||
v1_y = raw_bytes_from_point(v1, only_y_coord=True)
|
||||
v1h_xy = raw_bytes_from_point(v1h)
|
||||
v2_y = raw_bytes_from_point(v2, only_y_coord=True)
|
||||
v_y = get_coordinates_as_bytes(v, **only_y_coord)
|
||||
vz_xy = get_coordinates_as_bytes(vz)
|
||||
v1_y = get_coordinates_as_bytes(v1, **only_y_coord)
|
||||
v1h_xy = get_coordinates_as_bytes(v1h)
|
||||
v2_y = get_coordinates_as_bytes(v2, **only_y_coord)
|
||||
# U points
|
||||
uz_xy = raw_bytes_from_point(uz)
|
||||
u1_y = raw_bytes_from_point(u1, only_y_coord=True)
|
||||
u1h_xy = raw_bytes_from_point(u1h)
|
||||
u2_y = raw_bytes_from_point(u2, only_y_coord=True)
|
||||
uz_xy = get_coordinates_as_bytes(uz)
|
||||
u1_y = get_coordinates_as_bytes(u1, **only_y_coord)
|
||||
u1h_xy = get_coordinates_as_bytes(u1h)
|
||||
u2_y = get_coordinates_as_bytes(u2, **only_y_coord)
|
||||
|
||||
# Get hashed KFrag validity message
|
||||
hash_function = hashes.Hash(hashes.SHA256(), backend=backend)
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
"""
|
||||
This file is part of nucypher.
|
||||
|
||||
nucypher is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
nucypher is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from umbral.keys import UmbralPrivateKey
|
||||
|
||||
from nucypher.crypto.utils import get_coordinates_as_bytes
|
||||
|
||||
|
||||
def test_coordinates_as_bytes():
|
||||
pubkey = UmbralPrivateKey.gen_key().pubkey
|
||||
point = pubkey.point_key
|
||||
|
||||
x, y = point.to_affine()
|
||||
x = x.to_bytes(32, 'big')
|
||||
y = y.to_bytes(32, 'big')
|
||||
|
||||
for p in (point, pubkey):
|
||||
assert get_coordinates_as_bytes(p) == x + y
|
||||
assert get_coordinates_as_bytes(p, x_coord=False) == y
|
||||
assert get_coordinates_as_bytes(p, y_coord=False) == x
|
||||
with pytest.raises(ValueError):
|
||||
_ = get_coordinates_as_bytes(p, x_coord=False, y_coord=False)
|
Loading…
Reference in New Issue