This repository contains an implementation of a Verifiable Random Function (VRF) in Rust, used for weighted random selection among a list of candidates. Each candidate has a unique Utility address and a corresponding power (weight), determining their likelihood of being selected.
- VRF Implementation: Utilizes Elliptic Curve Cryptography for generating verifiable random outputs.
- Weighted Random Selection: Candidates are selected based on their assigned powers, with higher power increasing the chance of selection.
- Utility Address Support: Candidates are identified by Utility addresses.
- Rust programming environment
- Cargo, Rust's package manager
Clone the repository to your local machine:
git clone git@github.com:utnet-org/uvrf.git
cd uvrf
Use Cargo to build the project:
cargo build
To run tests, use the following Cargo command:
cargo test
To run main function, use the following Cargo command:
cargo run
The main functionalities include key pair generation for VRF, computation of VRF, and the verification of the VRF output. Additionally, the repository provides functionality to read a list of candidates from a JSON file and select one based on their weighted power.
let (sk, pk) = generate_key_pair();
let vrf_output = compute_vrf(&sk, input);
let is_valid = verify_vrf(&pk, input, &vrf_output);
Candidates are stored in a JSON file in the following format:
[
{"address": "0x123...", "power": 2},
{"address": "0x456...", "power": 5},
"..."
]
To select a candidate:
let candidates = read_candidates_from_file("path/to/candidates.json").unwrap();
let selected_candidate = choose_candidate_vrf(&candidates, random_number);
To output the candidate with random number:
cargo test -- --nocapture test_vrf_random_selection_from_file
The --nocapture flag is used with cargo test to allow the print statements to be displayed on the console. By default, cargo test captures output from test functions, and this flag disables that behavior.
Here are the regression fit curves generated from the mock data:
Tesing in 10,000 times
Tesing in 100,000 times
Tesing in 1M times
URL: /get_candidates
Method: GET
Response Format: JSON
Response Fields:
public_key
: The public key associated with the selection process.vrf_input
: The input used to generate the VRF output.selected_candidate
: The candidate selected in the process.
Example Response:
{
"public_key": "Public key here",
"vrf_input": "VRF input here",
"selected_candidate": {
"address": "Candidate address here",
"power": "Candidate power here"
}
}
Contributions to this project are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.