@femto-apps/random-number
v1.0.0
Published
A library to coordinate random number generation.
Downloads
2
Readme
Random Numbers
A library which allows the selection of a random number between a group of people that do not trust each other. Uses the following algorithm (where n
is the highest number to select, 0
is always the lowest):
- All users select a random number between 0 and
2 ^ ceil(log2(n))
. The results are hashed with a nonce using a secure hashing algorithm and shared with the group:
| User | Generated Number | Nonce | Hash | |-------|------------------|-------|--------------| | Alice | 2 | 5h4wh | 5663653j356j | | Bob | 7 | w45hh | 56j35j35j535 | | Eve | 5 | wzghk | zdfg34t4353y |
Once a user receives the hash of every other participant, they reveal their randomly generated number and their nonce.
All generated numbers are then xor'd together and normalised to
Usage:
const { RandInt } = require('@femto-host/randomNumber')
// pick a random integer from 1 to 20 (inclusive)
const generator = new RandInt(1, 20)
const myPart = generator.gen()
/* implement some part sharing method
1. share `myPart.hash` with others
2. share `myPart` with others after everyone has shown you their hash
*/
generator.finalise(parts)