GithubHelp home page GithubHelp logo

mettaversesociety / pfrng Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andykswong/pfrng

0.0 0.0 0.0 381 KB

Provably Fair Random Number Generator

Home Page: https://andykswong.github.io/pfrng/

License: MIT License

JavaScript 2.16% TypeScript 97.84%

pfrng's Introduction

pfrng - Provably Fair Random Number Generator

NPM build

A library of provably fair random number generators, distributions and helpers.

Install

npm install pfrng

Usage

Example provably fair coin flipping game:

import {
  bitIter, CoinSide, commitment, flipCoin, keyedMessage, 
  nodeCryptoRandomBytes, nodeHash, randomBytesFromHash, sha512Iter
} from '.';

// 1. Server generates a seed and send serverCommitment to client
const serverRandomIter = sha512Iter(nodeCryptoRandomBytes());
const serverSeed = serverRandomIter.next().value;
const serverCommitment = commitment(nodeHash.sha3_512, serverSeed);

// 2. Client generates a seed and places a bet
const clientRandomIter = sha512Iter(nodeCryptoRandomBytes());
const clientSeed = clientRandomIter.next().value;
const clientNumHeadsBet = 3;

// 3. Server seeds the random generators with client + server seeds
const randomBytes = randomBytesFromHash(nodeHash.sha3_512, keyedMessage(clientSeed, serverSeed));
const randomBits = bitIter(randomBytes);

// 4. Server simulates the game
let isWinning = true;
for (let i = 0; i < clientNumHeadsBet; ++i) {
  const isHead = flipCoin(randomBits) === CoinSide.Head;
  isWinning = isWinning && isHead;
  console.log(`Coin flip ${i}: ${isHead ? 'head' : 'tail'}`);
}
console.log(`You ${isWinning ? 'win' : 'lose'}.`);

// 5. Server reveals serverSeed to client for verification

API

TSDoc: http://andykswong.github.io/pfrng

License

This repository and the code inside it is licensed under the MIT License. Read LICENSE for more information.

pfrng's People

Contributors

andykswong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.