oprf/protocol/configuration.rs
1//! ## 3.1. Configuration
2
3use hacspec_lib::i2osp;
4
5/// Each of the three protocol variants are identified with a one-byte
6/// value (in hexadecimal):
7///
8/// | Mode | Value |
9/// |-----------|-------|
10/// | modeOPRF | 0x00 |
11/// | modeVOPRF | 0x01 |
12/// | modePOPRF | 0x02 |
13/// | | |
14/// | modecoPRF | 0x03 |
15///
16/// **Note:** `modecoPRF` is not part of the original draft document, but belongs to our Convertible OPRF extension.
17#[allow(non_camel_case_types)]
18pub enum ModeID {
19 modeOPRF = 0x00,
20 modeVOPRF = 0x01,
21 modePOPRF = 0x02,
22 modecoPRF = 0x03,
23}
24
25impl From<u32> for ModeID {
26 fn from(value: u32) -> Self {
27 match value {
28 0 => ModeID::modeOPRF,
29 1 => ModeID::modeVOPRF,
30 2 => ModeID::modePOPRF,
31 3 => ModeID::modecoPRF,
32 _ => panic!("Invalid ModeID."),
33 }
34 }
35}
36
37/// Additionally, each protocol variant is instantiated with a
38/// ciphersuite, or suite. Each ciphersuite is identified with an ASCII
39/// string identifier, referred to as identifier; see Section 4 for the
40/// set of initial ciphersuite values.
41///
42/// The mode and ciphersuite identifier values are combined to create a
43/// "context string" used throughout the protocol with the following
44/// function:
45///
46/// ```text
47/// def CreateContextString(mode, identifier):
48/// return "OPRFV1-" || I2OSP(mode, 1) || "-" || identifier
49/// ```
50pub fn create_context_string(mode: ModeID, identifier: &[u8]) -> Vec<u8> {
51 let mut res = b"OPRFV1-".to_vec(); // "OPRVV1-"
52 res.extend_from_slice(&i2osp(mode as usize, 1)); // || I2OSP(mode, 1)
53 res.extend_from_slice(b"-"); // || "-"
54 res.extend_from_slice(identifier); // || identifier
55
56 res
57}