Skip to content

Commit 038f04f

Browse files
test: add unit tests for crypto_utils (Issue hiero-ledger#993)
Signed-off-by: adityashirsatrao007 <adityashirsatrao007@gmail.com>
1 parent e9fea3e commit 038f04f

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Added
10+
1011
- Modularized `transfer_transaction_fungible` example by introducing `account_balance_query()` & `transfer_transaction()`.Renamed `transfer_tokens()``main()`
1112
- Phase 2 of the inactivity-unassign bot:Automatically detects stale open pull requests (no commit activity for 21+ days), comments with a helpful InactivityBot message, closes the stale PR, and unassigns the contributor from the linked issue.
1213
- Added **str**() to CustomFixedFee and updated examples and tests accordingly.
14+
- Added unit tests for `crypto_utils` (#993)
1315
- Added a github template for good first issues
1416
- Added `.github/workflows/bot-assignment-check.yml` to limit non-maintainers to 2 concurrent issue assignments.
1517
- Added all missing fields to **str**() method and updated `test_tokem_info.py`
@@ -19,7 +21,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
1921
- Made custom fraction fee end to end
2022
- feat: AccountCreateTransaction now supports both PrivateKey and PublicKey [#939](https://github.com/hiero-ledger/hiero-sdk-python/issues/939)
2123
- Added Acceptance Criteria section to Good First Issue template for better contributor guidance (#997)
22-
- Added __str__() to CustomRoyaltyFee and updated examples and tests accordingly (#986)
24+
- Added **str**() to CustomRoyaltyFee and updated examples and tests accordingly (#986)
2325
- Restore bug and feature request issue templates (#996)(https://github.com/hiero-ledger/hiero-sdk-python/issues/996)
2426
- Support selecting specific node account ID(s) for queries and transactions and added `Network._get_node()` with updated execution flow (#362)
2527
- Add TLS support with two-stage control (`set_transport_security()` and `set_verify_certificates()`) for encrypted connections to Hedera networks. TLS is enabled by default for hosted networks (mainnet, testnet, previewnet) and disabled for local networks (solo, localhost) (#855)

tests/unit/crypto_utils_test.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Unit tests for crypto_utils module."""
2+
from cryptography.hazmat.primitives.asymmetric import ec
3+
4+
from hiero_sdk_python.utils.crypto_utils import (
5+
compress_point_unchecked,
6+
compress_with_cryptography,
7+
decompress_point,
8+
keccak256,
9+
)
10+
11+
12+
def test_keccak256():
13+
"""Test keccak256 hashing."""
14+
# Known vector: empty string -> c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
15+
assert keccak256(b"").hex() == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
16+
17+
# "hello" -> 1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
18+
assert keccak256(b"hello").hex() == "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
19+
20+
21+
def test_compress_point_unchecked():
22+
"""Test point compression logic."""
23+
# Use cryptography to generate a valid point
24+
priv = ec.generate_private_key(ec.SECP256K1())
25+
pub = priv.public_key()
26+
nums = pub.public_numbers()
27+
x = nums.x
28+
y = nums.y
29+
30+
compressed = compress_point_unchecked(x, y)
31+
assert len(compressed) == 33
32+
33+
# Verify expected prefix
34+
expected_prefix = 0x03 if y % 2 else 0x02
35+
assert compressed[0] == expected_prefix
36+
assert int.from_bytes(compressed[1:], "big") == x
37+
38+
39+
def test_decompress_point():
40+
"""Test point decompression logic."""
41+
priv = ec.generate_private_key(ec.SECP256K1())
42+
pub = priv.public_key()
43+
nums = pub.public_numbers()
44+
45+
# Create valid compressed point
46+
compressed = compress_point_unchecked(nums.x, nums.y)
47+
48+
# Test decompression
49+
x, y = decompress_point(compressed)
50+
assert x == nums.x
51+
assert y == nums.y
52+
53+
# Test uncompressed 65-byte format (0x04 + x + y)
54+
uncompressed = b'\x04' + nums.x.to_bytes(32, 'big') + nums.y.to_bytes(32, 'big')
55+
x2, y2 = decompress_point(uncompressed)
56+
assert x2 == nums.x
57+
assert y2 == nums.y
58+
59+
60+
def test_compress_with_cryptography():
61+
"""Test compression using cryptography library."""
62+
priv = ec.generate_private_key(ec.SECP256K1())
63+
pub = priv.public_key()
64+
nums = pub.public_numbers()
65+
66+
# Create uncompressed
67+
uncompressed = b'\x04' + nums.x.to_bytes(32, 'big') + nums.y.to_bytes(32, 'big')
68+
69+
compressed_via_lib = compress_with_cryptography(uncompressed)
70+
compressed_manual = compress_point_unchecked(nums.x, nums.y)
71+
72+
assert compressed_via_lib == compressed_manual

0 commit comments

Comments
 (0)