Войти в систему

Home
    - Создать дневник
    - Написать в дневник
       - Подробный режим

LJ.Rossia.org
    - Новости сайта
    - Общие настройки
    - Sitemap
    - Оплата
    - ljr-fif

Редактировать...
    - Настройки
    - Список друзей
    - Дневник
    - Картинки
    - Пароль
    - Вид дневника

Сообщества

Настроить S2

Помощь
    - Забыли пароль?
    - FAQ
    - Тех. поддержка



Пишет nancygold ([info]nancygold)
@ 2024-06-22 00:37:00


Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Настроение: amused
Entry tags:computing

Have you ever seen this PRNG?
I've decided to look into the game's code out of curiosity.
Among the usual stuff, like EMS init, it seeds a random number generator.
Not a usual one, but with a 48-bit state.
Googled but could not find its origin.
Additionally, the game seeds that PRNG from the 40:6C at BDA.
Not something you usually see, since the Borland's C library provides a time access functions.

The game appears to be also using a custom EMS library, since it doesn't appear to match any of the free or shareware ones.

// ChatGPT claims it resembles the:
// 8-bit Maxim Integrated (formerly Dallas Semiconductor) one-wire CRC algorithm


//48-bit shift register
static uint8_t s[6]; //state

uint8_t rand8() { //1000:0730
  int i,c;
  uint8_t *src, *dst;
  //sum 1st, 4th and 5th bytes into the 0th byte
  c = s[1] + s[4] + 1;
  if (c&0x100) c = (c&0xFF) + 1;
  s[0] = (c + s[5])&0xFF;

  //shieft right by 8 bites
  src = s + 4;
  dst = s + 5;
  for (i = 5; i != 0; i = --i) *dst-- = *src--;

  return s[0];
}

void srand8(uint32_t seed) { //1000:0730
  int i;
  uint8_t *pseed;

  seed ^= 0x55555555;
  pseed = (uint8_t*)&seed;
  s[0] = 0;
  s[1] = pseed[0];
  s[2] = pseed[1];
  s[3] = pseed[2];
  s[4] = pseed[3];
  s[5] = 0;
  for (i = 0; i < 200; i++) rand8();
  return;
}



It is similar to the below code:
uint8_t rand48() {
    // Get the current state
    uint64_t s = get_state();

    // Apply bitwise transformations to update the state using only addition and shifting
    s = ((s << 13) | (s >> (48 - 13))) + 0xB;
    s &= ((1ULL << 48) - 1);

    // Store the updated state
    set_state(s);

    // Return the top 8 bits of the state
    return (s >> 40) & 0xFF;
}




(Добавить комментарий)


(Анонимно)
2024-06-22 02:07 (ссылка)
if you are such a prnd connoisseur, where is the analysis?

(Ответить) (Ветвь дискуссии)


[info]nancygold
2024-06-22 14:10 (ссылка)
I'm not good at cryptography, but I guess the function isn't particularly good either.

(Ответить) (Уровень выше) (Ветвь дискуссии)


[info]remedie
2024-06-23 00:00 (ссылка)
PRNGs are NOT cryptography, and should never be used in cryptorgraphy.
These are usually (esp. in old libraries) simple LFSR-based generators.

(Ответить) (Уровень выше) (Ветвь дискуссии)


[info]nancygold
2024-06-23 22:28 (ссылка)
Any hash function can be used a as PRGN, if you repeatedly apply it to itself.

(Ответить) (Уровень выше)


[info]lorde
2024-07-29 04:46 (ссылка)
Gboard provides dynamic suggestions for emoji combinations based on the first Emoji Kitchen selected, making it easy to explore different creative options.

(Ответить) (Уровень выше)


[info]remedie
2024-06-22 23:59 (ссылка)
// ChatGPT claims it resembles the:
// 8-bit Maxim Integrated (formerly Dallas Semiconductor) one-wire CRC algorithm

Ничего удивительного, потому что у CRC и PRNG такого рода (LFSR) одна и та же математическая база

(Ответить) (Ветвь дискуссии)


[info]nancygold
2024-06-23 11:12 (ссылка)
Yet Maxim CRC doesn't appear to be using the 0x55 constant or 48 bit state. Otherwise they are all basically do next_state = state*a + state*b + c

in practice addition is replaced by xor/or and multiplication by shifts.
so what differentiates them is the state size and the way these xors and shifts are arranged

I doubt some random trans girls with a background in C64 programming would be implementing her own PRNG,

(Ответить) (Уровень выше)