How to use the umbral.curvebn.CurveBN.from_bytes function in umbral

To help you get started, we’ve selected a few umbral examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nucypher / pyUmbral / tests / functional / test_vectors.py View on Github external
def test_curvebn_operations():

    vector_file = os.path.join('vectors', 'vectors_curvebn_operations.json')
    try:
        with open(vector_file) as f:
            vector_suite = json.load(f)
    except OSError:
        raise

    bn1 = CurveBN.from_bytes(bytes.fromhex(vector_suite['first operand']))
    bn2 = CurveBN.from_bytes(bytes.fromhex(vector_suite['second operand']))

    expected = dict()
    for op_result in vector_suite['vectors']:
        result = bytes.fromhex(op_result['result'])
        expected[op_result['operation']] = CurveBN.from_bytes(result)

    test = [('Addition', bn1 + bn2),
            ('Subtraction', bn1 - bn2),
            ('Multiplication', bn1 * bn2),
            ('Division', bn1 / bn2),
            ('Pow', bn1 ** bn2),
            ('Mod', bn1 % bn2),
            ('Inverse', ~bn1),
            ('Neg', -bn1),
            ]

    for (operation, result) in test:
        assert result == expected[operation], 'Error in {}'.format(operation)
github nucypher / pyUmbral / tests / unit / test_primitives / test_curvebn_serializers.py View on Github external
def test_invalid_deserialization(curve):
	size_in_bytes = CurveBN.expected_bytes_length(curve)
	
	# All-zeros bytestring are invalid (i.e., 0 < bn < order of the curve)
	zero_bytes = bytes(size_in_bytes)
	with pytest.raises(ValueError):
		_bn = CurveBN.from_bytes(zero_bytes, curve)

	# All-ones bytestring is invalid too (since it's greater than order)
	lots_of_ones = 2**(8*size_in_bytes) - 1
	lots_of_ones = lots_of_ones.to_bytes(size_in_bytes, 'big')
	with pytest.raises(ValueError):
		_bn = CurveBN.from_bytes(lots_of_ones, curve)

	# Serialization of `order` is invalid since it's not strictly lower than 
	# the order of the curve
	order = default_backend()._bn_to_int(curve.order)
	with pytest.raises(ValueError):
		_bn = CurveBN.from_bytes(order.to_bytes(size_in_bytes, 'big'), curve)

	# On the other hand, serialization of `order - 1` is valid
	order -= 1
	_bn = CurveBN.from_bytes(order.to_bytes(size_in_bytes, 'big'), curve)
github nucypher / pyUmbral / tests / functional / test_vectors.py View on Github external
def test_curvebn_hash():

    vector_file = os.path.join('vectors', 'vectors_curvebn_hash.json')
    try:
        with open(vector_file) as f:
            vector_suite = json.load(f)
    except OSError:
        raise

    params = default_params()

    for vector in vector_suite['vectors']:
        hash_input = [bytes.fromhex(item['bytes']) for item in vector['input']]
        expected = CurveBN.from_bytes(bytes.fromhex(vector['output']))
        assert hash_to_curvebn(*hash_input, params=params) == expected
github nucypher / pyUmbral / tests / functional / test_vectors.py View on Github external
def test_point_operations():

    vector_file = os.path.join('vectors', 'vectors_point_operations.json')
    try:
        with open(vector_file) as f:
            vector_suite = json.load(f)
    except OSError:
        raise

    point1 = Point.from_bytes(bytes.fromhex(vector_suite['first Point operand']))
    point2 = Point.from_bytes(bytes.fromhex(vector_suite['second Point operand']))
    bn1 = CurveBN.from_bytes(bytes.fromhex(vector_suite['CurveBN operand']))

    expected = dict()
    for op_result in vector_suite['vectors']:
        expected[op_result['operation']] = bytes.fromhex(op_result['result'])

    test = [('Addition', point1 + point2),
            ('Subtraction', point1 - point2),
            ('Multiplication', bn1 * point1),
            ('Inversion', -point1),
            ]

    for (operation, result) in test:
        assert result == Point.from_bytes(expected[operation]), 'Error in {}'.format(operation)

    test = [('To_affine.X', point1.to_affine()[0]),
            ('To_affine.Y', point1.to_affine()[1]),
github nucypher / pyUmbral / tests / unit / test_primitives / test_curvebn_serializers.py View on Github external
def test_serialization_rotations_of_1(curve):

    size_in_bytes = CurveBN.expected_bytes_length(curve)
    for i in range(size_in_bytes):
        lonely_one = 1 << i
        bn = CurveBN.from_int(lonely_one, curve)
        lonely_one_in_bytes = lonely_one.to_bytes(size_in_bytes, 'big')

        # Check serialization
        assert bn.to_bytes() == lonely_one_in_bytes

        # Check deserialization
        assert CurveBN.from_bytes(lonely_one_in_bytes, curve) == bn
github nucypher / pyUmbral / tests / unit / test_primitives / test_point_arithmetic.py View on Github external
def test_point_curve_multiplication_regression():
    k256_point_bytes = b'\x03\xe0{\x1bQ\xbf@\x1f\x95\x8d\xe1\x17\xa7\xbe\x9e-G`T\xbf\xd7\x9e\xa7\x10\xc8uA\xc0z$\xc0\x92\x8a'
    k256_bn_bytes = b'4u\xd70-\xa0h\xdeG\xf0\x143\x06!\x91\x05{\xe4jC\n\xf1h\xed7a\xf8\x9d\xec^\x19\x8c'

    k256_point = Point.from_bytes(k256_point_bytes)
    k256_bn = CurveBN.from_bytes(k256_bn_bytes)

    product_with_star_operator = k256_point * k256_bn

    # Make sure we have instantiated a new, unequal point in the same curve and group
    assert isinstance(product_with_star_operator, Point), "Point.__mul__ did not return a point instance"
    assert k256_point != product_with_star_operator
    assert k256_point.curve == product_with_star_operator.curve

    product_bytes = b'\x03\xc9\xda\xa2\x88\xe2\xa0+\xb1N\xb6\xe6\x1c\xa5(\xe6\xe0p\xf6\xf4\xa9\xfc\xb1\xfaUV\xd3\xb3\x0e4\x94\xbe\x12'
    product_point = Point.from_bytes(product_bytes)
    assert product_with_star_operator.to_bytes() == product_bytes
    assert product_point == product_with_star_operator

    # Repeating the operation, should return the same result.
    product_with_star_operator_again = k256_point * k256_bn
    assert product_with_star_operator == product_with_star_operator_again
github nucypher / pyUmbral / tests / unit / test_serialization_property_based.py View on Github external
def test_bn_roundtrip(bn):
    assert(bn == CurveBN.from_bytes(bn.to_bytes()))
github nucypher / pyUmbral / tests / unit / test_primitives / test_curvebn_serializers.py View on Github external
# All-zeros bytestring are invalid (i.e., 0 < bn < order of the curve)
	zero_bytes = bytes(size_in_bytes)
	with pytest.raises(ValueError):
		_bn = CurveBN.from_bytes(zero_bytes, curve)

	# All-ones bytestring is invalid too (since it's greater than order)
	lots_of_ones = 2**(8*size_in_bytes) - 1
	lots_of_ones = lots_of_ones.to_bytes(size_in_bytes, 'big')
	with pytest.raises(ValueError):
		_bn = CurveBN.from_bytes(lots_of_ones, curve)

	# Serialization of `order` is invalid since it's not strictly lower than 
	# the order of the curve
	order = default_backend()._bn_to_int(curve.order)
	with pytest.raises(ValueError):
		_bn = CurveBN.from_bytes(order.to_bytes(size_in_bytes, 'big'), curve)

	# On the other hand, serialization of `order - 1` is valid
	order -= 1
	_bn = CurveBN.from_bytes(order.to_bytes(size_in_bytes, 'big'), curve)