ore_rs/term.rs
1//! What the schemes give back: a stored term and a query, each the
2//! serialised bytes of a ciphertext.
3//!
4//! The block count of a ciphertext is the plaintext's width, known only
5//! when the plaintext arrives, so a scheme cannot give back a ciphertext
6//! typed by its block count. It gives back the bytes, under a type that
7//! says which half they are: a [`Term`] is a full ciphertext, left and
8//! right halves, and is what is stored; a [`Query`] is the left half
9//! alone, made from the value searched for, and is what a comparison
10//! needs from the other side. The bytes carry their scheme and block
11//! count in the wire header, and the comparators parse and check them.
12//!
13//! The plaintext type is not in the bytes. vitaminc's `OrderTerm` and
14//! `QueryTerm` label a term with its target; here a `Term` is whatever
15//! the caller says it is, and the comparators refuse only what they can
16//! tell apart (another scheme; on a fixed-width scheme another block
17//! count; on the chained scheme another padding policy), as the scheme
18//! docs say.
19
20/// A full ciphertext, serialised: left and right halves, with the wire
21/// header. What a scheme stores. Compared with the scheme's
22/// `compare_raw_slices`, or against a [`Query`] with
23/// `compare_left_to_full`; never byte by byte, since a full ciphertext
24/// carries a random nonce and one value has many. So no `PartialEq`:
25/// `==` on two terms of one value would be `false`.
26#[derive(Debug, Clone)]
27pub struct Term(Vec<u8>);
28
29/// A left (query) ciphertext, serialised: the per-block permuted
30/// symbols and tags, with the wire header, and no nonce or right half.
31/// Made from the value searched for, and compared against a stored
32/// [`Term`] with the scheme's `compare_left_to_full`. Deterministic:
33/// one value under one key and salt gives one query.
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct Query(Vec<u8>);
36
37macro_rules! bytes_newtype {
38 ($type:ident) => {
39 impl $type {
40 /// The serialised bytes.
41 pub fn as_bytes(&self) -> &[u8] {
42 &self.0
43 }
44
45 /// The serialised bytes, owned.
46 pub fn into_bytes(self) -> Vec<u8> {
47 self.0
48 }
49
50 /// Bytes read back from storage, as this type. Nothing is
51 /// checked here: the comparators parse the bytes and return
52 /// `None` for ones that are not a ciphertext of their scheme.
53 pub fn from_bytes(bytes: Vec<u8>) -> Self {
54 Self(bytes)
55 }
56
57 pub(crate) fn new(bytes: Vec<u8>) -> Self {
58 Self(bytes)
59 }
60 }
61
62 impl AsRef<[u8]> for $type {
63 fn as_ref(&self) -> &[u8] {
64 &self.0
65 }
66 }
67
68 impl From<$type> for Vec<u8> {
69 fn from(value: $type) -> Vec<u8> {
70 value.0
71 }
72 }
73 };
74}
75
76bytes_newtype!(Term);
77bytes_newtype!(Query);