Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Pearson hashing

Reply to topic
Author Message
  • Joined: 05 Dec 2019
  • Posts: 56
  • Location: USA
Reply with quote
Pearson hashing
Post Posted: Fri Apr 24, 2020 5:35 am
A user of the Plutiedev Discord server mentioned an interesting hash function called Pearson hashing. It could be useful as a relatively strong error detection checksum of a saved game in battery SRAM. From Wikipedia's article:
Quote
Pearson hashing is a hash function designed for fast execution on processors with 8-bit registers. Given an input consisting of any number of bytes, it produces as output a single byte that is strongly dependent on every byte of the input. Its implementation requires only a few instructions, plus a 256-byte lookup table containing a permutation of the values 0 through 255.

In the past, I have used the S-box from AES as such a permutation.

I imagine it'd look like this on a Z80 (untested):
;;
; Calculates the Pearson hash of an array
; @param BC length of message
; @param HL pointer to start of message
pearson_hash:
  ld a, c      ; start hash at low byte of message length
  ld d, high(rijndaelS)  ; align this to 256 bytes!
  dec bc       ; Increment B if C is nonzero
  inc b
  inc c
@loop:
  ; here: A = hash
  xor [hl]     ; Combine data byte with current hash value
  inc hl
  ld e, a      ; Scramble through S-box
  ld a, [de]
  dec c        ; Count bytes
  jr nz, @loop
  djnz @loop
  ret
  View user's profile Send private message Visit poster's website
  • Joined: 29 Mar 2012
  • Posts: 879
  • Location: Spain
Reply with quote
Post Posted: Fri Apr 24, 2020 7:07 am
Thanks PinoBatch! It looks quite interesting for some integrity checking as you suggested! :-D
  View user's profile Send private message
  • Joined: 23 Aug 2009
  • Posts: 213
  • Location: Seattle, WA
Reply with quote
Post Posted: Fri Apr 24, 2020 3:21 pm
Here’s my implementation from a few months ago:

https://github.com/SavagePencil/SMSFramework/blob/master/Utils/hash_function.asm
  View user's profile Send private message
Reply to topic



Back to the top of this page

Back to SMS Power!