Skip to content

Commit

Permalink
Inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TLA020 committed Oct 3, 2023
0 parents commit 42606be
Show file tree
Hide file tree
Showing 9 changed files with 625 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
cargo_term_color: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: build
uses: actions-rs/cargo@v1
with:
command: build
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
cargo_term_color: always

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: run tests
uses: actions-rs/cargo@v1
with:
command: test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
179 changes: 179 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "proof-fair"
version = "0.1.0"
edition = "2021"
authors = ["Tobias Lie-Atjam"]
description = "A provably fair random number generator (RNG) using HMAC-SHA512 hash with a server seed, a client seed, and a nonce."
license = "MIT"


[dependencies]
hex = "0.4.3"
hmac = "0.12.1"
sha2 = "0.10.6"
crypto-common = "0.1.6"
hex-literal = "0.4.1"
rand = "0.8"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 TLA020

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
🎲 Provably Fair RNG with HMAC-SHA512 🎲
===========================================================================
[![GitHub Actions Build Status](https://github.com/tla020/rust-proof-fair/actions/workflows/build.yml/badge.svg)](https://github.com/tla020/rust-proof-fair/actions)
[![GitHub Actions Test Status](https://github.com/tla020/rust-proof-fair/actions/workflows/test.yml/badge.svg)](https://github.com/tla020/rust-proof-fair/actions)

This code implements a provably fair random number generator (RNG) using HMAC-SHA512 hash with a server seed, a client seed, and a nonce.

πŸ“‹ Requirements
------------

This code requires the following dependencies to be installed:

* `rand` 🎲 \- a Rust crate for random number generation
* `sha2` πŸ”’ \- a Rust crate for SHA-256 and SHA-512 hash functions
* `hmac` πŸ” \- a Rust crate for Hash-based Message Authentication Code (HMAC)

Add the following lines to your Cargo.toml file to include the dependencies:

```
[dependencies]
rand = "0.8"
sha2 = "0.12"
hmac = "0.7"
```

πŸ“ Description
-----------

βš™οΈ This code generates random numbers based on the hash of the server seed, client seed, and a nonce value. The server seed is generated by the server and kept secret until the game is over, while the client seed is generated by the client and is also kept secret if there is more than one player for the game. The nonce is incremented by one for each game round, to ensure that each game round is unique and cannot be repeated or manipulated.

⚠️ It's important to note that the nonce should never be predictable or repeatable, and it should be kept secret from players to prevent any attempts to manipulate the game outcome. These combined are used to generate an HMAC-SHA512 hash.

The `Proof` struct represents the current state of the RNG, and it provides several methods to interact with the RNG, such as `new`, `log_state`, `roll`, `calculate`, and `verify`.

The `new` method creates a new instance of the `Proof` struct with the provided or random server seed and client seed, and a given nonce value.

The `log_state` method logs the current state of the `Proof` struct to the console, including the random number generated from the current state.

The `roll` method increments the nonce value and calculates the random number for the current state of the `Proof` struct.

The `calculate` method calculates the random number for the current state of the `Proof` struct, without modifying its state.

The `verify` method verifies that the provided random number is valid for the given client seed, server seed, and nonce values.

πŸš€ Example Usage
-----

To use this code, simply create a new instance of the `Proof` struct with the desired or random server seed, client seed, and nonce values. Then, use the `roll` or `calculate` methods to generate random numbers, and use the `verify` method to verify the validity of a given random number.


```rust
use hmac_rng::Proof;

// Create a new Proof instance with a random server seed and client seed, and a nonce value of 0
let mut proof = Proof::new(None, None, 0);

// Log the current state of the Proof instance
proof.log_state();

// Roll the Proof instance to generate a new random number
let result = proof.roll();
match result {
Ok(random_number) => println!("Random number: {}", random_number),
Err(err) => eprintln!("Error: {}", err),
}

// Verify the validity of a given random number
let client_seed = vec![1, 2, 3];
let server_seed = vec![4, 5, 6];
let nonce = 0;
let random_number = 0.42;
let result = Proof::verify(&client_seed, Some(&server_seed), nonce, random_number);
match result {
Ok(valid) => println!("Valid: {}", valid),
Err(err) => eprintln!("Error: {}", err),
}
```

πŸ”— [verifier](https://dicesites.com/primedice/verifier) << users can use this to verify
Loading

0 comments on commit 42606be

Please sign in to comment.