Skip to content

use hashlib instead for scrypt#4

Open
VeNoMouS wants to merge 1 commit intoxKiian:mainfrom
VeNoMouS:main
Open

use hashlib instead for scrypt#4
VeNoMouS wants to merge 1 commit intoxKiian:mainfrom
VeNoMouS:main

Conversation

@VeNoMouS
Copy link
Copy Markdown

@VeNoMouS VeNoMouS commented Aug 5, 2025

Challenge Input: eyJ2ZXJzaW9uIjoxLCJ1YmlkIjoiMDk4Y2ZmNDQtMmRjOC00OT...
Checksum: B3CBDF0A
Difficulty: 4
Memory Cost: 128
Challenge Type: HashcashScrypt
==========================================================================================

1. Testing Current Production (pyscrypt)
----------------------------------------------------------------------
Description: What you currently use
Result: 38
Time: 5.9083 seconds
Verification: ✓ VALID

2. Testing Optimized pyscrypt
----------------------------------------------------------------------
Description: pyscrypt with optimizations
Result: 38
Time: 5.9010 seconds
Verification: ✓ VALID

3. Testing hashlib.scrypt (basic)
----------------------------------------------------------------------
Description: Built-in Python scrypt
Result: 38
Time: 0.0161 seconds
Verification: ✓ VALID

4. Testing hashlib.scrypt (optimized)
----------------------------------------------------------------------
Description: hashlib with optimizations
Result: 38
Time: 0.0163 seconds
Verification: ✓ VALID

==========================================================================================
SCRYPT PERFORMANCE COMPARISON
==========================================================================================
Implementation                 Time (s)     Result     Valid    Library
---------------------------------------------------------------------------
Current Production (pyscrypt)  5.9083       38pyscrypt
Optimized pyscrypt             5.9010       38pyscrypt
hashlib.scrypt (basic)         0.0161       38hashlib
hashlib.scrypt (optimized)     0.0163       38hashlib

==========================================================================================
KEY FINDINGS
==========================================================================================
🏆 FASTEST: hashlib.scrypt (basic)
   Time: 0.0161 seconds
   Result: 38
   Library: hashlib
   Speedup vs current: 366.1x faster
   Time saved: 5.8922 seconds per solve

--- COMPATIBILITY CHECK ---COMPATIBLE: Both libraries find the same nonce
   Both found: 38Safe to switch libraries

--- LIBRARY PERFORMANCE ---
📈 hashlib.scrypt is 366x FASTER
   pyscrypt: 5.9083s
   hashlib:  0.0161s

--- OPTIMIZATION EFFECTIVENESS ---
pyscrypt optimization: 1.00x speedup
hashlib optimization: 0.99x speedup

@ptraced
Copy link
Copy Markdown

ptraced commented Sep 8, 2025

@xKiian this looks like chatgpt edit verify if this even works and produces same result

@xKiian
Copy link
Copy Markdown
Owner

xKiian commented Sep 8, 2025

😭

@VeNoMouS
Copy link
Copy Markdown
Author

VeNoMouS commented Sep 8, 2025

Not exactly rocket science.... hashlib uses openssl shared library, where as pyscrypt is all in python

@Felixoid Felixoid mentioned this pull request Mar 24, 2026
@Felixoid
Copy link
Copy Markdown

Felixoid commented Mar 24, 2026

Since the library https://github.com/ricmoo/pyscrypt looks very outdated (12 years), slow and unpopular, this improvement would help a lot with adapting the library in pytr-org/pytr#319

I added unit tests on top of the @VeNoMouS changes:

$ git show --stat
commit 25d4c14575237ed3053f7c050e3be7aeb6a3dd41 (HEAD -> main)
Author: Mikhail f. Shiryaev <mr.felixoid@gmail.com>
Date:   Tue Mar 24 13:08:33 2026 +0100

    Add tests for hashlib

 python/tests/__init__.py    |   0
 python/tests/test_verify.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)
$ python3 -m venv venv && venv/bin/pip install --upgrade pip && venv/bin/pip install pyscrypt
Requirement already satisfied: pip in ./venv/lib/python3.14/site-packages (25.3)
Collecting pip
  Downloading pip-26.0.1-py3-none-any.whl.metadata (4.7 kB)
Downloading pip-26.0.1-py3-none-any.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 9.5 MB/s  0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 25.3
    Uninstalling pip-25.3:
      Successfully uninstalled pip-25.3
Successfully installed pip-26.0.1
Collecting pyscrypt
  Using cached pyscrypt-1.6.2-py3-none-any.whl
Installing collected packages: pyscrypt
Successfully installed pyscrypt-1.6.2
$ venv/bin/python -m unittest tests/test_verify.py -v
test_hashpow_key_maps_to_hash_pow (tests.test_verify.TestChallengeTypes.test_hashpow_key_maps_to_hash_pow) ... ok
test_mp_verify_key (tests.test_verify.TestChallengeTypes.test_mp_verify_key) ... ok
test_scrypt_key_maps_to_compute_scrypt_nonce (tests.test_verify.TestChallengeTypes.test_scrypt_key_maps_to_compute_scrypt_nonce) ... ok
test_all_zeros_meets_any_difficulty (tests.test_verify.TestCheck.test_all_zeros_meets_any_difficulty) ... ok
test_difficulty_zero_always_passes (tests.test_verify.TestCheck.test_difficulty_zero_always_passes) ... ok
test_exact_full_bytes (tests.test_verify.TestCheck.test_exact_full_bytes) ... ok
test_full_plus_partial (tests.test_verify.TestCheck.test_full_plus_partial) ... ok
test_no_leading_zeros_fails_difficulty_1 (tests.test_verify.TestCheck.test_no_leading_zeros_fails_difficulty_1) ... ok
test_partial_bits (tests.test_verify.TestCheck.test_partial_bits) ... ok
test_known_nonce (tests.test_verify.TestComputeScryptNonce.test_known_nonce) ... ok
test_result_is_valid (tests.test_verify.TestComputeScryptNonce.test_result_is_valid) ... ok
test_difficulty_1 (tests.test_verify.TestHashPow.test_difficulty_1) ... ok
test_known_nonce (tests.test_verify.TestHashPow.test_known_nonce) ... ok
test_result_is_valid (tests.test_verify.TestHashPow.test_result_is_valid) ... ok
test_deterministic (tests.test_verify.TestScryptFunc.test_deterministic) ... ok
test_different_inputs_differ (tests.test_verify.TestScryptFunc.test_different_inputs_differ) ... ok
test_known_output (tests.test_verify.TestScryptFunc.test_known_output) ... ok

----------------------------------------------------------------------
Ran 17 tests in 1.119s

OK
$ git checkout hashlib-tests
Switched to branch 'hashlib-tests'
Your branch is up to date with 'Felixoid/hashlib-tests'.
$ venv/bin/python -m unittest tests/test_verify.py -v
test_hashpow_key_maps_to_hash_pow (tests.test_verify.TestChallengeTypes.test_hashpow_key_maps_to_hash_pow) ... ok
test_mp_verify_key (tests.test_verify.TestChallengeTypes.test_mp_verify_key) ... ok
test_scrypt_key_maps_to_compute_scrypt_nonce (tests.test_verify.TestChallengeTypes.test_scrypt_key_maps_to_compute_scrypt_nonce) ... ok
test_all_zeros_meets_any_difficulty (tests.test_verify.TestCheck.test_all_zeros_meets_any_difficulty) ... ok
test_difficulty_zero_always_passes (tests.test_verify.TestCheck.test_difficulty_zero_always_passes) ... ok
test_exact_full_bytes (tests.test_verify.TestCheck.test_exact_full_bytes) ... ok
test_full_plus_partial (tests.test_verify.TestCheck.test_full_plus_partial) ... ok
test_no_leading_zeros_fails_difficulty_1 (tests.test_verify.TestCheck.test_no_leading_zeros_fails_difficulty_1) ... ok
test_partial_bits (tests.test_verify.TestCheck.test_partial_bits) ... ok
test_known_nonce (tests.test_verify.TestComputeScryptNonce.test_known_nonce) ... ok
test_result_is_valid (tests.test_verify.TestComputeScryptNonce.test_result_is_valid) ... ok
test_difficulty_1 (tests.test_verify.TestHashPow.test_difficulty_1) ... ok
test_known_nonce (tests.test_verify.TestHashPow.test_known_nonce) ... ok
test_result_is_valid (tests.test_verify.TestHashPow.test_result_is_valid) ... ok
test_deterministic (tests.test_verify.TestScryptFunc.test_deterministic) ... ok
test_different_inputs_differ (tests.test_verify.TestScryptFunc.test_different_inputs_differ) ... ok
test_known_output (tests.test_verify.TestScryptFunc.test_known_output) ... ok

----------------------------------------------------------------------
Ran 17 tests in 0.003s

OK

It's not only faster (1.119s with pyscrypt vs 0.003 with hashlib), but it also eliminates outdated dependencies in favor of the standard library.

I created #10 on top of these changes, so to be on the safe side, you can merge either of the PRs.

But please, consider it merged. Otherwise it would require some additional work to make a fix for pytr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants