Files
voprf-vx/src/lib.rs
T

601 lines
22 KiB
Rust
Raw Normal View History

2021-09-09 01:56:54 -07:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
2021-09-27 18:53:06 -07:00
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree and the Apache
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
// of this source tree.
2021-09-09 01:56:54 -07:00
2021-09-15 17:49:31 -07:00
//! An implementation of a verifiable oblivious pseudorandom function (VOPRF)
//!
//! Note: This implementation is in sync with
2022-07-09 13:30:20 +02:00
//! [draft-irtf-cfrg-voprf-11](https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-11.html),
2021-09-20 00:17:53 -07:00
//! but this specification is subject to change, until the final version
//! published by the IETF.
2021-09-15 17:49:31 -07:00
//!
2021-09-20 00:17:53 -07:00
//! # Overview
2021-09-15 17:49:31 -07:00
//!
2021-12-23 01:17:03 +01:00
//! A verifiable oblivious pseudorandom function is a protocol that is evaluated
2022-04-01 17:30:28 -07:00
//! between a client and a server. They must first agree on a finite cyclic
//! group along with a point representation.
2021-09-20 00:17:53 -07:00
//!
2022-04-01 17:30:28 -07:00
//! We will use the following choice in this example:
2021-09-20 00:17:53 -07:00
//!
2021-12-23 07:50:48 +01:00
//! ```ignore
2022-01-21 22:52:09 +01:00
//! type CipherSuite = voprf::Ristretto255;
2021-09-20 00:17:53 -07:00
//! ```
//!
//! ## Modes of Operation
//!
2022-04-01 17:30:28 -07:00
//! VOPRF can be used in three modes:
2021-12-23 01:17:03 +01:00
//! - [Base Mode](#base-mode), which corresponds to a normal OPRF evaluation
//! with no support for the verification of the OPRF outputs
//! - [Verifiable Mode](#verifiable-mode), which corresponds to an OPRF
//! evaluation where the outputs can be verified against a server public key
2022-04-01 17:30:28 -07:00
//! (VOPRF)
//! - [Partially Oblivious Verifiable Mode](#metadata), which corresponds to a
//! VOPRF, where a public input can be supplied to the PRF computation
2021-09-20 00:17:53 -07:00
//!
2022-04-01 17:30:28 -07:00
//! In all of these modes, the protocol begins with a client blinding, followed
2022-07-09 13:30:20 +02:00
//! by a server evaluation, and finishes with a client finalization and server
//! evaluation.
2021-09-20 00:17:53 -07:00
//!
//! ## Base Mode
//!
2022-07-09 13:30:20 +02:00
//! In base mode, an [OprfClient] interacts with an [OprfServer] to compute the
//! output of the OPRF.
2021-09-20 00:17:53 -07:00
//!
//! ### Server Setup
//!
//! The protocol begins with a setup phase, in which the server must run
2022-07-09 13:30:20 +02:00
//! [OprfServer::new()] to produce an instance of itself. This instance must be
//! persisted on the server and used for online client evaluations.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2021-12-23 01:17:03 +01:00
//! use rand::rngs::OsRng;
//! use rand::RngCore;
2022-02-13 04:00:11 -08:00
//! use voprf::OprfServer;
2021-09-20 00:17:53 -07:00
//!
//! let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! let server = OprfServer::<CipherSuite>::new(&mut server_rng);
2021-09-20 00:17:53 -07:00
//! ```
//!
//! ### Client Blinding
//!
2022-07-09 13:30:20 +02:00
//! In the first step, the client chooses an input, and runs [OprfClient::blind]
//! to produce an [OprfClientBlindResult], which consists of a [BlindedElement]
//! to be sent to the server and an [OprfClient] which must be persisted on the
//! client for the final step of the VOPRF protocol.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2021-12-23 01:17:03 +01:00
//! use rand::rngs::OsRng;
//! use rand::RngCore;
2022-02-13 04:00:11 -08:00
//! use voprf::OprfClient;
2021-09-20 00:17:53 -07:00
//!
//! let mut client_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! let client_blind_result = OprfClient::<CipherSuite>::blind(b"input", &mut client_rng)
2021-12-23 21:03:38 +01:00
//! .expect("Unable to construct client");
2021-09-20 00:17:53 -07:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! ### Server Blind Evaluation
2021-09-20 00:17:53 -07:00
//!
//! In the second step, the server takes as input the message from
2022-02-13 04:00:11 -08:00
//! [OprfClient::blind] (a [BlindedElement]), and runs
2022-07-09 13:30:20 +02:00
//! [OprfServer::blind_evaluate] to produce [EvaluationElement] to be sent to
2022-01-28 01:38:17 +01:00
//! the client.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::OprfClient;
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = OprfClient::<CipherSuite>::blind(
2021-12-23 21:03:38 +01:00
//! # b"input",
2021-09-20 00:17:53 -07:00
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
2022-02-13 04:00:11 -08:00
//! # use voprf::OprfServer;
2021-09-20 00:17:53 -07:00
//! # let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = OprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
2022-07-09 13:30:20 +02:00
//! let server_evaluate_result = server.blind_evaluate(&client_blind_result.message);
2021-09-20 00:17:53 -07:00
//! ```
//!
//! ### Client Finalization
//!
2022-07-09 13:30:20 +02:00
//! In the final step on the client side, the client takes as input the message
//! from [OprfServer::evaluate] (an [EvaluationElement]), and runs
2022-02-13 04:00:11 -08:00
//! [OprfClient::finalize] to produce an output for the protocol.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::OprfClient;
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = OprfClient::<CipherSuite>::blind(
2021-12-23 21:03:38 +01:00
//! # b"input",
2021-09-20 00:17:53 -07:00
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
2022-02-13 04:00:11 -08:00
//! # use voprf::OprfServer;
2021-09-20 00:17:53 -07:00
//! # let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = OprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
2022-07-09 13:30:20 +02:00
//! # let message = server.blind_evaluate(&client_blind_result.message);
2021-12-23 01:17:03 +01:00
//! let client_finalize_result = client_blind_result
//! .state
2022-02-13 04:00:11 -08:00
//! .finalize(b"input", &message)
2021-12-23 01:17:03 +01:00
//! .expect("Unable to perform client finalization");
2021-09-20 00:17:53 -07:00
//!
2021-10-06 00:53:18 +02:00
//! println!("VOPRF output: {:?}", client_finalize_result.to_vec());
2021-09-20 00:17:53 -07:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! ### Server Evaluation
//!
//! Optionally, if the server has direct access to the PRF input, then it need
//! not perform the oblivious computation and can simply run
//! [OprfServer::evaluate] to generate an output which matches the output
//! produced by an execution of the oblivious protocol on the same input and
//! key.
//!
//! ```
//! # #[cfg(feature = "ristretto255")]
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
//! # use voprf::OprfClient;
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
//! # let client_blind_result = OprfClient::<CipherSuite>::blind(
//! # b"input",
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
//! # use voprf::OprfServer;
//! # let mut server_rng = OsRng;
//! # let server = OprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
//! # let message = server.blind_evaluate(&client_blind_result.message);
//! let client_finalize_result = client_blind_result
//! .state
//! .finalize(b"input", &message)
//! .expect("Unable to perform client finalization");
//!
//! let server_evaluate_result = server
//! .evaluate(b"input")
//! .expect("Unable to perform the server evaluation");
//!
//! assert_eq!(client_finalize_result, server_evaluate_result);
//! ```
//!
2021-09-20 00:17:53 -07:00
//! ## Verifiable Mode
//!
2022-07-09 13:30:20 +02:00
//! In verifiable mode, a [VoprfClient] interacts with a [VoprfServer] to
//! compute the output of the VOPRF. In order to verify the server's
2021-12-23 01:17:03 +01:00
//! computation, the client checks a server-generated proof against the server's
//! public key. If the proof fails to verify, then the client does not receive
//! an output.
2021-09-20 00:17:53 -07:00
//!
//! In batch mode, a single proof can be used for multiple VOPRF evaluations.
2021-12-23 01:17:03 +01:00
//! See [the batching section](#batching) for more details on how to perform
//! batch evaluations.
2021-09-20 00:17:53 -07:00
//!
//! ### Server Setup
//!
//! The protocol begins with a setup phase, in which the server must run
2022-07-09 13:30:20 +02:00
//! [VoprfServer::new()] to produce an instance of itself. This instance must be
//! persisted on the server and used for online client evaluations.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2021-12-23 01:17:03 +01:00
//! use rand::rngs::OsRng;
//! use rand::RngCore;
2022-02-13 04:00:11 -08:00
//! use voprf::VoprfServer;
2021-09-20 00:17:53 -07:00
//!
//! let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
2021-09-20 00:17:53 -07:00
//!
//! // To be sent to the client
//! println!("Server public key: {:?}", server.get_public_key());
//! ```
//!
2021-12-23 01:17:03 +01:00
//! The public key should be sent to the client, since the client will need it
//! in the final step of the protocol in order to complete the evaluation of the
//! VOPRF.
2021-09-20 00:17:53 -07:00
//!
//! ### Client Blinding
//!
//! In the first step, the client chooses an input, and runs
2022-07-09 13:30:20 +02:00
//! [VoprfClient::blind] to produce a [VoprfClientBlindResult], which consists
//! of a [BlindedElement] to be sent to the server and a [VoprfClient] which
//! must be persisted on the client for the final step of the VOPRF protocol.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2021-12-23 01:17:03 +01:00
//! use rand::rngs::OsRng;
//! use rand::RngCore;
2022-02-13 04:00:11 -08:00
//! use voprf::VoprfClient;
2021-09-20 00:17:53 -07:00
//!
//! let mut client_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! let client_blind_result = VoprfClient::<CipherSuite>::blind(b"input", &mut client_rng)
2021-12-23 21:03:38 +01:00
//! .expect("Unable to construct client");
2021-09-20 00:17:53 -07:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! ### Server Blind Evaluation
2021-09-20 00:17:53 -07:00
//!
//! In the second step, the server takes as input the message from
2022-02-13 04:00:11 -08:00
//! [VoprfClient::blind] (a [BlindedElement]), and runs
2022-07-09 13:30:20 +02:00
//! [VoprfServer::blind_evaluate] to produce a [VoprfServerEvaluateResult],
2021-12-23 01:17:03 +01:00
//! which consists of an [EvaluationElement] to be sent to the client along with
//! a proof.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::{VoprfServerEvaluateResult, VoprfClient};
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = VoprfClient::<CipherSuite>::blind(
2021-12-23 21:03:38 +01:00
//! # b"input",
2021-09-20 00:17:53 -07:00
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfServer;
2021-09-20 00:17:53 -07:00
//! # let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
//! let VoprfServerEvaluateResult { message, proof } =
2022-07-09 13:30:20 +02:00
//! server.blind_evaluate(&mut server_rng, &client_blind_result.message);
2021-09-20 00:17:53 -07:00
//! ```
//!
//! ### Client Finalization
//!
//! In the final step, the client takes as input the message from
2022-07-09 13:30:20 +02:00
//! [VoprfServer::blind_evaluate] (an [EvaluationElement]), the proof, and the
//! server's public key, and runs [VoprfClient::finalize] to produce an output
//! for the protocol.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfClient;
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = VoprfClient::<CipherSuite>::blind(
2021-12-23 21:03:38 +01:00
//! # b"input",
2021-09-20 00:17:53 -07:00
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfServer;
2021-09-20 00:17:53 -07:00
//! # let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
2022-07-09 13:30:20 +02:00
//! # let server_evaluate_result = server.blind_evaluate(
2021-09-20 00:17:53 -07:00
//! # &mut server_rng,
2021-12-21 20:17:02 +01:00
//! # &client_blind_result.message,
2022-02-13 04:00:11 -08:00
//! # );
2021-12-23 01:17:03 +01:00
//! let client_finalize_result = client_blind_result
//! .state
//! .finalize(
2021-12-23 21:03:38 +01:00
//! b"input",
2021-12-23 01:17:03 +01:00
//! &server_evaluate_result.message,
//! &server_evaluate_result.proof,
//! server.get_public_key(),
//! )
//! .expect("Unable to perform client finalization");
2021-09-20 00:17:53 -07:00
//!
2021-10-06 00:53:18 +02:00
//! println!("VOPRF output: {:?}", client_finalize_result.to_vec());
2021-09-20 00:17:53 -07:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! ### Server Evaluation
//!
//! Optionally, if the server has direct access to the PRF input, then it need
//! not perform the oblivious computation and can simply run
//! [VoprfServer::evaluate] to generate an output which matches the output
//! produced by an execution of the oblivious protocol on the same input and
//! key.
//!
//! ```
//! # #[cfg(feature = "ristretto255")]
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
//! # use voprf::VoprfClient;
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
//! # let client_blind_result = VoprfClient::<CipherSuite>::blind(
//! # b"input",
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
//! # use voprf::VoprfServer;
//! # let mut server_rng = OsRng;
//! # let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
//! # let server_evaluate_result = server.blind_evaluate(
//! # &mut server_rng,
//! # &client_blind_result.message,
//! # );
//! let client_finalize_result = client_blind_result
//! .state
//! .finalize(
//! b"input",
//! &server_evaluate_result.message,
//! &server_evaluate_result.proof,
//! server.get_public_key(),
//! )
//! .expect("Unable to perform client finalization");
//!
//! let server_evaluate_result = server
//! .evaluate(b"input")
//! .expect("Unable to perform the server evaluation");
//!
//! assert_eq!(client_finalize_result, server_evaluate_result);
//! ```
//!
2021-09-20 00:17:53 -07:00
//! # Advanced Usage
//!
//! There are two additional (and optional) extensions to the core VOPRF
//! protocol: support for batching of evaluations, and support for public
//! metadata.
//!
//! ## Batching
//!
2021-12-23 01:17:03 +01:00
//! It is sometimes desirable to generate only a single, constant-size proof for
//! an unbounded number of VOPRF evaluations (on arbitrary inputs).
2022-07-09 13:30:20 +02:00
//! [VoprfClient] and [VoprfServer] support a batch API for handling this case.
//! In the following example, we show how to use the batch API to produce a
//! single proof for 10 parallel VOPRF evaluations.
2021-09-20 00:17:53 -07:00
//!
2021-12-23 01:17:03 +01:00
//! First, the client produces 10 blindings, storing their resulting states and
//! messages:
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfClient;
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! let mut client_rng = OsRng;
//! let mut client_states = vec![];
//! let mut client_messages = vec![];
//! for _ in 0..10 {
2022-02-13 04:00:11 -08:00
//! let client_blind_result = VoprfClient::<CipherSuite>::blind(b"input", &mut client_rng)
2021-12-23 21:03:38 +01:00
//! .expect("Unable to construct client");
2021-09-20 00:17:53 -07:00
//! client_states.push(client_blind_result.state);
//! client_messages.push(client_blind_result.message);
//! }
2021-12-29 09:14:28 +01:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! Next, the server calls the [VoprfServer::batch_blind_evaluate_prepare] and
//! [VoprfServer::batch_blind_evaluate_finish] function on a set of client
2021-12-29 09:14:28 +01:00
//! messages, to produce a corresponding set of messages to be returned to the
//! client (returned in the same order), along with a single proof:
//!
//! ```
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::{VoprfServerBatchEvaluateFinishResult, VoprfClient};
2021-12-29 09:14:28 +01:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
//! # let mut client_states = vec![];
//! # let mut client_messages = vec![];
//! # for _ in 0..10 {
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = VoprfClient::<CipherSuite>::blind(
2021-12-29 09:14:28 +01:00
//! # b"input",
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
//! # client_states.push(client_blind_result.state);
//! # client_messages.push(client_blind_result.message);
2021-12-23 21:03:38 +01:00
//! # }
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfServer;
2021-12-29 09:14:28 +01:00
//! let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
2022-07-09 13:30:20 +02:00
//! let prepared_evaluation_elements = server.batch_blind_evaluate_prepare(client_messages.iter());
2021-12-29 09:14:28 +01:00
//! let prepared_elements: Vec<_> = prepared_evaluation_elements.collect();
2022-02-13 04:00:11 -08:00
//! let VoprfServerBatchEvaluateFinishResult { messages, proof } = server
2022-07-09 13:30:20 +02:00
//! .batch_blind_evaluate_finish(&mut server_rng, client_messages.iter(), &prepared_elements)
2021-12-29 09:14:28 +01:00
//! .expect("Unable to perform server batch evaluate");
//! let messages: Vec<_> = messages.collect();
2021-09-20 00:17:53 -07:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! If `alloc` is available, `VoprfServer::batch_blind_evaluate` can be called
2021-12-29 09:14:28 +01:00
//! to avoid having to collect output manually:
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 21:03:38 +01:00
//! # #[cfg(feature = "alloc")] {
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::{VoprfServerBatchEvaluateResult, VoprfClient};
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
//! # let mut client_states = vec![];
//! # let mut client_messages = vec![];
//! # for _ in 0..10 {
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = VoprfClient::<CipherSuite>::blind(
2021-12-23 21:03:38 +01:00
//! # b"input",
2021-09-20 00:17:53 -07:00
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
//! # client_states.push(client_blind_result.state);
//! # client_messages.push(client_blind_result.message);
//! # }
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfServer;
2021-09-20 00:17:53 -07:00
//! let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
//! let VoprfServerBatchEvaluateResult { messages, proof } = server
2022-07-09 13:30:20 +02:00
//! .batch_blind_evaluate(&mut server_rng, &client_messages)
2021-12-23 01:17:03 +01:00
//! .expect("Unable to perform server batch evaluate");
2021-12-23 21:03:38 +01:00
//! # }
2021-09-20 00:17:53 -07:00
//! ```
//!
2022-07-09 13:30:20 +02:00
//! Then, the client calls [VoprfClient::batch_finalize] on the client states
//! saved from the first step, along with the messages returned by the server,
//! along with the server's proof, in order to produce a vector of outputs if
//! the proof verifies correctly.
2021-09-20 00:17:53 -07:00
//!
//! ```
2021-12-23 21:03:38 +01:00
//! # #[cfg(feature = "alloc")] {
2021-12-23 07:50:48 +01:00
//! # #[cfg(feature = "ristretto255")]
2022-01-21 22:52:09 +01:00
//! # type CipherSuite = voprf::Ristretto255;
//! # #[cfg(not(feature = "ristretto255"))]
//! # type CipherSuite = p256::NistP256;
2022-02-13 04:00:11 -08:00
//! # use voprf::{VoprfServerBatchEvaluateResult, VoprfClient};
2021-09-20 00:17:53 -07:00
//! # use rand::{rngs::OsRng, RngCore};
//! #
//! # let mut client_rng = OsRng;
//! # let mut client_states = vec![];
//! # let mut client_messages = vec![];
//! # for _ in 0..10 {
2022-02-13 04:00:11 -08:00
//! # let client_blind_result = VoprfClient::<CipherSuite>::blind(
2021-12-23 21:03:38 +01:00
//! # b"input",
2021-09-20 00:17:53 -07:00
//! # &mut client_rng,
//! # ).expect("Unable to construct client");
//! # client_states.push(client_blind_result.state);
//! # client_messages.push(client_blind_result.message);
//! # }
2022-02-13 04:00:11 -08:00
//! # use voprf::VoprfServer;
2021-12-29 09:14:28 +01:00
//! # let mut server_rng = OsRng;
2022-02-13 04:00:11 -08:00
//! # let server = VoprfServer::<CipherSuite>::new(&mut server_rng).unwrap();
//! # let VoprfServerBatchEvaluateResult { messages, proof } = server
2022-07-09 13:30:20 +02:00
//! # .batch_blind_evaluate(&mut server_rng, &client_messages)
2021-12-29 09:14:28 +01:00
//! # .expect("Unable to perform server batch evaluate");
2022-02-13 04:00:11 -08:00
//! let client_batch_finalize_result = VoprfClient::batch_finalize(
2021-12-23 21:03:38 +01:00
//! &[b"input"; 10],
//! &client_states,
2021-12-29 09:14:28 +01:00
//! &messages,
//! &proof,
2021-09-20 00:17:53 -07:00
//! server.get_public_key(),
2021-12-23 01:17:03 +01:00
//! )
2021-12-23 21:03:38 +01:00
//! .expect("Unable to perform client batch finalization")
//! .collect::<Vec<_>>();
2021-09-20 00:17:53 -07:00
//!
2021-10-06 00:53:18 +02:00
//! println!("VOPRF batch outputs: {:?}", client_batch_finalize_result);
2021-12-23 21:03:38 +01:00
//! # }
2021-09-20 00:17:53 -07:00
//! ```
//!
//! ## Metadata
//!
2022-04-01 17:30:28 -07:00
//! The optional metadata parameter included in the POPRF mode allows clients
2022-07-09 13:30:20 +02:00
//! and servers to cryptographically bind additional data to the VOPRF output.
//! This metadata is known to both parties at the start of the protocol, and is
//! inserted under the server's blind evaluate step and the client's finalize
//! step. This metadata can be constructed with some type of higher-level domain
//! separation to avoid cross-protocol attacks or related issues.
2021-09-20 00:17:53 -07:00
//!
2022-04-01 17:30:28 -07:00
//! The API for POPRF mode is similar to VOPRF mode, except that a [PoprfServer]
//! and [PoprfClient] are used, and that each of the functions accept an
//! additional (and optional) info parameter which represents the public input.
2022-07-09 13:30:20 +02:00
//! See
//! <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-11.html#name-poprf-public-input>
2022-04-01 17:30:28 -07:00
//! for more detailed information on how this public input should be used.
2021-09-20 00:17:53 -07:00
//!
//! # Features
//!
//! - The `alloc` feature requires Rust's `alloc` crate and enables batching
2021-12-23 21:03:38 +01:00
//! VOPRF evaluations.
//!
2021-12-23 01:17:03 +01:00
//! - The `serde` feature, enabled by default, provides convenience functions
//! for serializing and deserializing with [serde](https://serde.rs/).
//!
//! - The `danger` feature, disabled by default, exposes functions for setting
//! and getting internal values not available in the default API. These
//! functions are intended for use in by higher-level cryptographic protocols
//! that need access to these raw values and are able to perform the necessary
//! validations on them (such as being valid group elements).
//!
2022-01-21 22:52:09 +01:00
//! - The `ristretto255-ciphersuite` features enables using [`Ristretto255`] as
//! a [`CipherSuite`].
//!
2022-01-18 12:34:28 +01:00
//! - The `ristretto255` feature enables using [`Ristretto255`] as the
//! underlying group for the [Group] choice. To select a specific backend see
//! the [curve25519-dalek] documentation.
2022-01-18 12:34:28 +01:00
//!
2022-07-09 13:30:20 +02:00
//! [curve25519-dalek]:
//! (https://docs.rs/curve25519-dalek/4.0.0-pre.5/curve25519_dalek/index.html#backends)
2021-09-15 17:49:31 -07:00
2021-12-21 20:17:02 +01:00
#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(test), deny(unsafe_code))]
2022-01-28 01:38:17 +01:00
#![warn(
clippy::cargo,
clippy::missing_errors_doc,
missing_debug_implementations,
missing_docs
)]
#![allow(clippy::multiple_crate_versions)]
2021-09-09 01:56:54 -07:00
2021-12-23 21:03:38 +01:00
#[cfg(any(feature = "alloc", test))]
2021-09-09 01:56:54 -07:00
extern crate alloc;
2021-12-21 20:17:02 +01:00
#[cfg(feature = "std")]
extern crate std;
2022-01-21 22:52:09 +01:00
mod ciphersuite;
2022-04-01 21:18:32 +02:00
mod common;
2021-12-25 22:54:27 +01:00
mod error;
mod group;
2022-02-13 04:00:11 -08:00
mod oprf;
mod poprf;
2022-01-21 22:52:09 +01:00
mod serialization;
2021-09-20 00:17:53 -07:00
mod voprf;
2021-09-09 01:56:54 -07:00
#[cfg(test)]
mod tests;
2021-09-20 00:17:53 -07:00
// Exports
2022-01-21 22:52:09 +01:00
pub use crate::ciphersuite::CipherSuite;
#[cfg(feature = "danger")]
pub use crate::common::derive_key;
2022-04-01 21:18:32 +02:00
pub use crate::common::{
BlindedElement, EvaluationElement, Mode, PreparedEvaluationElement, Proof,
};
2022-01-25 05:55:02 +01:00
pub use crate::error::{Error, InternalError, Result};
2021-12-25 22:54:27 +01:00
pub use crate::group::Group;
2022-01-21 22:52:09 +01:00
#[cfg(feature = "ristretto255")]
pub use crate::group::Ristretto255;
2022-02-13 04:00:11 -08:00
pub use crate::oprf::{OprfClient, OprfClientBlindResult, OprfServer};
#[cfg(feature = "alloc")]
pub use crate::poprf::PoprfServerBatchEvaluateResult;
pub use crate::poprf::{
PoprfClient, PoprfClientBatchFinalizeResult, PoprfPreparedTweak, PoprfServer,
PoprfServerBatchEvaluateFinishResult, PoprfServerBatchEvaluateFinishedMessages,
PoprfServerBatchEvaluatePrepareResult, PoprfServerBatchEvaluatePreparedEvaluationElements,
};
2022-01-21 22:52:09 +01:00
pub use crate::serialization::{
2022-02-13 04:00:11 -08:00
BlindedElementLen, EvaluationElementLen, OprfClientLen, OprfServerLen, PoprfClientLen,
PoprfServerLen, ProofLen, VoprfClientLen, VoprfServerLen,
2022-01-21 22:52:09 +01:00
};
2021-12-25 22:54:27 +01:00
#[cfg(feature = "alloc")]
2022-02-13 04:00:11 -08:00
pub use crate::voprf::VoprfServerBatchEvaluateResult;
2021-09-20 00:17:53 -07:00
pub use crate::voprf::{
2022-02-13 04:00:11 -08:00
VoprfClient, VoprfClientBatchFinalizeResult, VoprfClientBlindResult, VoprfServer,
VoprfServerBatchEvaluateFinishResult, VoprfServerBatchEvaluateFinishedMessages,
VoprfServerBatchEvaluatePreparedEvaluationElements, VoprfServerEvaluateResult,
2021-09-20 00:17:53 -07:00
};