Kako se Provably Fair implementira u kodu?
Pod pretpostavkom da je igra završena i da imamo neheširani server seed, client seed i nonce, evo kako to funkcioniše.
Za generisanje rezultata igre potrebna su tri glavna koraka.
byteGenerator (generisanje nasumičnih bajtova)
generateFloats (pretvaranje bajtova u float vrednosti (cifre))
Float vrednosti u događaje u igri (pretvaranje float vrednosti u stvarne događaje u originalnim igrama)
Funkcija ByteGenerator kao generator nasumičnih bajtova Generator
Ova byteGenerator funkcija služi kao generator nasumičnih bajtova.
Koristi jedinstvene vrednosti clientSeed, serverSeed, nonce, i cursor za generisanje nasumične i jedinstvene heširane SHA-256 vrednosti pomoću kriptografske funkcije HMAC_SHA256.
Generisana SHA-256 vrednost ima veličinu od 32 bajtova. Kako bi se osigurala ravnoteža između dovoljno nasumičnog ishoda igre i računske zahtevnosti, 32 bajtova se deli na 8 delova od po 4 bajtova* za generisanje svakog rezultata igre.
U određenim igrama, gde je potrebno više od 8 rezultata igre, koristićemo cursor. Kursor u početku kreće od 0, a povećava se do 1,2,3,4 kako bi se ispunio zahtev za rezultatima,
Za igre u kojima nam nije potrebno više od 8 nasumičnih ishoda, vrednost kursora se ne povećava.
*4 bajtova podataka daje nam 2^32 (4,294,967,296) mogućih ishoda, što je dovoljno veliki skup za nasumičnost.
function* byteGenerator({ serverSeed, clientSeed, nonce, cursor }: ByteGeneratorInterface) {
// Setup cursor variables let currentRound = Math.floor(cursor / 32);
let currentRoundCursor = cursor;
currentRoundCursor -= currentRound * 32;
// Generate outputs until cursor requirement fullfilled
while (true) {
// HMAC function used to output provided inputs into bytes
const hmac = crypto.createHmac('sha256', serverSeed);
hmac.update(`${clientSeed}:${nonce}:${currentRound}`);
const buffer = hmac.digest();
// Update curser for next iteration of loop
while (currentRoundCursor < 32) {
yield Number(buffer[currentRoundCursor]);
currentRoundCursor += 1;
}
currentRoundCursor = 0;
currentRound += 1;
}
}GenerateFloats funkcija za pretvaranje bajtova u float vrednosti
Ova funkcija pretvara heksadecimalnu SHA-256 vrednost iz bajtova u float vrednosti za korišćenje u daljim proračunima događaja u igri.
U nastavku je prikazano kako se heksadecimalna SHA-256 vrednost pretvara iz bajta u float vrednost. Konačni rezultat, numArr, sadrži sve moguće ishode potrebne za igru. Ako je potreban samo 1 ishod, lista će sadržati samo jednu vrednost.
Vraća niz brojeva između 0-1, pri čemu count predstavlja broj elemenata u vraćenom nizu.
Kod:
// Convert the hash output from the rng byteGenerator to floats
export function generateFloats({
serverSeed,
clientSeed,
nonce,
cursor,
count,
}: GenerateFloatsInterface) {
// Random number generator function
const rng = byteGenerator({ serverSeed, clientSeed, nonce, cursor });
// Declare bytes as empty array
const bytes = [];
// Populate bytes array with sets of 4 from RNG output
while (bytes.length < count * 4) {
bytes.push(rng.next().value!);
}
// Return bytes as floats using lodash reduce function
const numArr = chunk(bytes, 4).map(bytesChunk =>
bytesChunk.reduce((result, value, i) => {
const divider = 256 ** (i + 1);
const partialResult = value / divider;
return result + partialResult;
}, 0),
);
return numArr;
}
Sve naše originalne igre koriste i funkcije ByteGenerator i GenerateFloats za generisanje nasumičnih float vrednosti između 0 i 1. Međutim, od ovog trenutka svaka igra koristi jedinstven postupak za određivanje događaja u igri na osnovu generisane float vrednosti.
Jedinstveni postupak će biti detaljno objašnjen u odeljku Događaji u igri.
Ilustrovani primer
Ovde ćemo prikazati kako se unosi koriste za generisanje događaja u igri kockica
Input Values: Given some random input values
serverSeed, clientSeed, nonce, cursor
Step 1: byteGenerator creates a SHA-256 byte
"a3f4e0ac7c7e8e9b5f16106c6b1d14e87c2c5a8d59b1d1c6a0b5f3e5a7d4c9a8”
Step 2: 256 bytes is split into 8 equal set of 32 bytes
"a3f4e0ac”, “7c7e8e9b”, “5f16106c”, “6b1d14e8”,
“7c2c5a8d”, “59b1d1c6”, “a0b5f3e5”, “a7d4c9a8”
Step 3: Each set is broken down into 2 bytes each
(only first 2 sets is shown)
set 1: a3-f4-e0-ac
set 2: 7c-7e-8e-9b
......
Step 4: Each of the 2 bytes represent a number from 0 to 255
set 1: [163, 244, 224, 172]
set 2: [124, 126, 142, 155]
......
Step 5: Using the formula in the code to generate numArr
Float 1 = (163 / (256^1)) + (244 / (256^2)) + (224 / (256^3))
+ (172 / (256^4)) = 0.64045528601
Float 2 = (124 / (256^1)) + (126 / (256^2)) + (142 / (256^3))
+ (155 / (256^4)) = 0.48630610737 Float 3 ......
Step 6: Output the floats in a list
numArr = [0.64045528601, 0.48630610737, ...... ]
*** Game Event Generation ***
Step 7: The numArr output will be used to generate game events,
depending on the game requirement
Example 7: Using dice as an example.
The game event is generated using the first numArr value.
const resultValue = floats.map(val => Math.floor(floats * 10001) / 100);
resultValue = Math.floor(0.64045528601 * 10001) / 100
= 64.05
Napomena: Kada je igra aktivna, server seed je heširan, pa igrač i operater ne mogu da vide pojedinačni rezultat ovog procesa tokom igre.
Pošto se isti algoritam i funkcije koriste tokom i nakon igre, korisnici uvek mogu da dokažu ishod uz iste unose.
