Open the context
Choose the project folder and name the files or behavior involved.
Build a SHA-256 Merkle root from one to 64 byte leaves of at most 4096 bytes using distinct leaf and parent tags. Test an odd level by duplicating its last node and reject empty input without treating the root as proof of ownership.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
import hashlib
def merkle_root(leaves):
if type(leaves) is not list or not 1 <= len(leaves) <= 64 or any(type(x) is not bytes or len(x) > 4096 for x in leaves):
raise ValueError("merkle.leaves")
level = [hashlib.sha256(b"\x00"+x).digest() for x in leaves]
while len(level) > 1:
if len(level) % 2:
level.append(level[-1])
level = [hashlib.sha256(b"\x01"+level[i]+level[i+1]).digest() for i in range(0, len(level), 2)]
return level[0].hex()
The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.