k98kurz

tapehash.py

Jun 20th, 2022 (edited)
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.37 KB | None | 0 0
  1. from hashlib import (
  2.     sha256, md5,
  3.     shake_128, shake_256,
  4.     sha3_256, sha3_512,
  5.     blake2b, blake2s
  6. )
  7.  
  8.  
  9. """New randomized proof-of-work hashing algorithms inspired roughly by
  10.    XMR's RandomX but much simpler and shoddier.
  11.    Mirrors:
  12.        https://pastebin.com/xY3B3K8S
  13.        https://github.com/k98kurz/tapehash
  14. """
  15.  
  16. def tapehash1(preimage: bytes, code_size: int = 20) -> bytes:
  17.     """Runs the tapehash 1 algorithm on the preimage and returns a
  18.        32-byte hash. Computational complexity is tuneable via the
  19.        `code_size` parameter.
  20.    """
  21.     if type(preimage) is not bytes:
  22.         raise TypeError('preimage must be bytes')
  23.     if type(code_size) is not int:
  24.         raise TypeError('code_size must be an int between 1 and 65,536')
  25.     if code_size <= 0 or code_size > 65_536:
  26.         raise ValueError('code_size must be an int between 1 and 65,536')
  27.  
  28.     # generate the code and the tape
  29.     code = shake_256(preimage).digest(code_size)
  30.     tape = bytearray(blake2b(preimage).digest())
  31.  
  32.     # run the program
  33.     for i in range(0, len(code)):
  34.         opcode = code[i] >> 4
  35.         #pointer = ((code[i] << 4) % 256) >> 4
  36.         pointer = code[i] & 0b00001111
  37.         double_pointer = int.from_bytes(code[i:i+2], 'big') % 2
  38.  
  39.         tape = execute_opcode(opcode, pointer, tape)
  40.         tape = execute_opcode(opcode, pointer + 16, tape)
  41.         tape = execute_opcode(opcode, pointer + 32, tape)
  42.         tape = execute_opcode(opcode, pointer + 48, tape)
  43.  
  44.     return sha256(tape).digest()
  45.  
  46.  
  47. def tapehash2(preimage: bytes, tape_size_multiplier: int = 2) -> bytes:
  48.     """Runs the tapehash2 algorithm on the preimage and returns a
  49.        32-byte hash. Memory complexity can be tuned via the
  50.        `tape_size_multiplier` parameter.
  51.    """
  52.     if type(preimage) is not bytes:
  53.         raise TypeError('preimage must be bytes')
  54.     if type(tape_size_multiplier) is not int:
  55.         raise TypeError('tape_size_multiplier must be an int between 1 and 65,536')
  56.     if tape_size_multiplier <= 0 or tape_size_multiplier > 65_536:
  57.         raise ValueError('tape_size_multiplier must be an int between 1 and 65,536')
  58.  
  59.     # generate the code and the tape
  60.     code = blake2b(preimage).digest()
  61.     tape = bytearray(shake_256(preimage).digest(tape_size_multiplier * 32))
  62.  
  63.     # run the program
  64.     for i in range(0, len(code), 2):
  65.         opcode = code[i] >> 4
  66.         #pointer = ((code[i] << 4) % 256) >> 4
  67.         pointer = code[i] & 0b00001111
  68.         double_pointer = int.from_bytes(code[i:i+2], 'big') % tape_size_multiplier
  69.  
  70.         tape = execute_opcode(opcode, pointer + double_pointer * 32, tape)
  71.         tape = execute_opcode(opcode, pointer + 16 + double_pointer * 32, tape)
  72.  
  73.     return sha256(tape).digest()
  74.  
  75.  
  76. def tapehash3(
  77.     preimage: bytes, code_size: int = 64, tape_size_multiplier: int = 2
  78. ) -> bytes:
  79.     """Runs the tapehash3 algorithm on the preimage and returns a
  80.        32-byte hash. Computational complexity is tuneable via the
  81.        `code_size` parameter. Memory complexity is tuneable via the
  82.        `tape_size_multiplier` parameter.
  83.    """
  84.     if type(preimage) is not bytes:
  85.         raise TypeError('preimage must be bytes')
  86.     if type(code_size) is not int:
  87.         raise TypeError('code_size must be an int between 1 and 65,536')
  88.     if code_size <= 0 or code_size > 65_536:
  89.         raise ValueError('code_size must be an int between 1 and 65,536')
  90.     if type(tape_size_multiplier) is not int:
  91.         raise TypeError('tape_size_multiplier must be an int between 1 and 65,536')
  92.     if tape_size_multiplier <= 0 or tape_size_multiplier > 65_536:
  93.         raise ValueError('tape_size_multiplier must be an int between 1 and 65,536')
  94.  
  95.     # generate the code and the tape
  96.     code = shake_256(preimage).digest(code_size)
  97.     tape = bytearray(shake_256(preimage).digest(tape_size_multiplier * 32))
  98.  
  99.     # run the program
  100.     for i in range(0, len(code), 2):
  101.         opcode = code[i] >> 4
  102.         #pointer = ((code[i] << 4) % 256) >> 4
  103.         pointer = code[i] & 0b00001111
  104.         double_pointer = int.from_bytes(code[i:i+2], 'big') % tape_size_multiplier
  105.  
  106.         tape = execute_opcode(opcode, pointer + double_pointer * 32, tape)
  107.         tape = execute_opcode(opcode, pointer + 16 + double_pointer * 32, tape)
  108.  
  109.     return sha256(tape).digest()
  110.  
  111.  
  112.  
  113. def rotate_tape(tape: bytearray, pointer: int) -> bytes:
  114.     """Rotates the tape so that the pointer-indexed byte is first."""
  115.     if not isinstance(tape, bytearray):
  116.         raise TypeError('tape must be bytearray')
  117.     if not isinstance(pointer, int):
  118.         raise TypeError('pointer must be int')
  119.     if len(tape) < 1:
  120.         raise ValueError('tape must not be empty')
  121.     if pointer < 0 or pointer >= len(tape):
  122.         raise ValueError('pointer must be a valid index of tape')
  123.  
  124.     new_tape = tape[pointer:]
  125.     new_tape.extend(tape[:pointer])
  126.     return bytes(new_tape)
  127.  
  128.  
  129. def execute_opcode(opcode: int, pointer: int, tape: bytearray) -> bytearray:
  130.     """Execute a single opcode."""
  131.     if type(opcode) is not int:
  132.         raise TypeError('opcode must be an int')
  133.     if type(pointer) is not int:
  134.         raise TypeError('pointer must be an int')
  135.     if type(tape) is not bytearray:
  136.         raise TypeError('tape must be bytearray')
  137.     if opcode < 0 or opcode > 15:
  138.         raise ValueError('opcode must be between 0 and 15')
  139.     if pointer < 0 or pointer >= len(tape):
  140.         raise ValueError('pointer must be a valid index of tape')
  141.  
  142.     operations = {
  143.         0: lambda data: data, # no op
  144.         1: lambda data: (data + 1) % 256,
  145.         2: lambda data: data - 1 if data > 0 else 255,
  146.         3: lambda data: data >> 1,
  147.         4: lambda data: (data << 1) % 256,
  148.         5: lambda data: data ^ 255,
  149.         6: lambda data: (data * 2) % 256,
  150.         7: lambda data: (data ** 2) % 256,
  151.         8: lambda data: (data // 2) % 256,
  152.         9: lambda data: ((data << 4) % 256) | (data >> 4),
  153.         10: lambda data: sha256(data).digest()[data[0] % 32],
  154.         11: lambda data: md5(data).digest()[data[0] % 16],
  155.         12: lambda data: shake_128(data).digest(data[0] + 1)[data[0]],
  156.         13: lambda data: sha3_256(data).digest()[data[0] % 32],
  157.         14: lambda data: sha3_512(data).digest()[data[0] % 64],
  158.         15: lambda data: blake2s(data).digest()[data[0] % 32]
  159.     }
  160.  
  161.     if opcode < 10:
  162.         tape[pointer] = operations[opcode](tape[pointer])
  163.     else:
  164.         tape[pointer] = operations[opcode](rotate_tape(tape, pointer))
  165.     return tape
  166.  
  167.  
  168. def license():
  169.     """Copyright (c) 2026 Jonathan Voss (k98kurz)
  170.  
  171.        Permission to use, copy, modify, and/or distribute this software
  172.        for any purpose with or without fee is hereby granted, provided
  173.        that the above copyright notice and this permission notice appear in
  174.        all copies.
  175.  
  176.        THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  177.        WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  178.        WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  179.        AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  180.        CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  181.        OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  182.        NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  183.        CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  184.    """
  185.     return license.__doc__
  186.  
Advertisement
Add Comment
Please, Sign In to add comment