#elliptic-curve #secp256k1 #p256 #ecvrf #k163

vrf

Fast and extensible Verifiable Random Function (VRF) library; currently supporting secp256k1, secp256r1 and sect163k1 curves

8 releases

0.2.4 Apr 7, 2022
0.2.3 Nov 3, 2021
0.2.2 Oct 24, 2019
0.2.1 Aug 26, 2019
0.1.1 May 22, 2019

#249 in Cryptography

Download history 183/week @ 2024-03-13 172/week @ 2024-03-20 164/week @ 2024-03-27 184/week @ 2024-04-03 124/week @ 2024-04-10 104/week @ 2024-04-17 112/week @ 2024-04-24 137/week @ 2024-05-01 134/week @ 2024-05-08 104/week @ 2024-05-15 114/week @ 2024-05-22 267/week @ 2024-05-29 262/week @ 2024-06-05 103/week @ 2024-06-12 125/week @ 2024-06-19 145/week @ 2024-06-26

705 downloads per month
Used in 33 crates (6 directly)

MIT license

55KB
733 lines

vrf-rs

vrf-rs is an open source implementation of Verifiable Random Functions (VRFs) written in Rust.

DISCLAIMER: This is experimental software. Be careful!

The library can be built using cargo and the examples can be executed with:

cargo build
cargo run --example <example_name>

Elliptic Curve VRF

This module uses the OpenSSL library to offer Elliptic Curve Verifiable Random Function (VRF) functionality.

It follows the algorithms described in:

Currently the supported cipher suites are:

  • P256_SHA256_TAI: the aforementioned algorithms with SHA256 and the secp256r1 curve (aka NIST P-256).
  • K163_SHA256_TAI: the aforementioned algorithms with SHA256 and the sect163k1 curve (aka NIST K-163).
  • SECP256K1_SHA256_TAI: the aforementioned algorithms with SHA256 and the secp256k1 curve.

Example

Create and verify a VRF proof by using the cipher suite SECP256K1_SHA256_TAI:

use vrf::openssl::{CipherSuite, ECVRF};
use vrf::VRF;

fn main() {
    // Initialization of VRF context by providing a curve
    let mut vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_TAI).unwrap();
    // Inputs: Secret Key, Public Key (derived) & Message
    let secret_key =
        hex::decode("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721").unwrap();
    let public_key = vrf.derive_public_key(&secret_key).unwrap();
    let message: &[u8] = b"sample";
    
    // VRF proof and hash output
    let pi = vrf.prove(&secret_key, &message).unwrap();
    let hash = vrf.proof_to_hash(&pi).unwrap();

    // VRF proof verification (returns VRF hash output)
    let beta = vrf.verify(&public_key, &pi, &message);
}

A complete example can be found in examples/basic.rs. It can be executed with:

cargo run --example basic

Adding unsupported cipher suites

This library defines a VRF trait which can be extended in order to use different curves and algorithms.

pub trait VRF<PublicKey, SecretKey> {
    type Error;

    fn prove(&mut self, x: SecretKey, alpha: &[u8]) -> Result<Vec<u8>, Self::Error>;

    fn verify(&mut self, y: PublicKey, pi: &[u8], alpha: &[u8]) -> Result<Vec<u8>, Self::Error>;
}

License

vrf-rs is published under the MIT license.

Dependencies

~1.9–3MB
~71K SLoC