1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::commitment::{Commitment, CommitmentOpening};
use mpz_core::{
    commit::{Decommitment, HashCommit, Nonce},
    hash::Hash,
};
use mpz_garble_core::{encoding_state, encoding_state::Full, EncodedValue};
use serde::{Deserialize, Serialize};

/// A Blake3 commitment to the encodings of the substrings of a [`Transcript`](crate::Transcript).
#[derive(Clone, Copy, Serialize, Deserialize)]
pub struct Blake3Commitment {
    hash: Hash,
    nonce: Nonce,
}

opaque_debug::implement!(Blake3Commitment);

impl Blake3Commitment {
    /// Creates a new Blake3 commitment
    pub fn new(encodings: &[EncodedValue<encoding_state::Active>]) -> Self {
        let (decommitment, hash) = encodings.hash_commit();

        Self {
            hash,
            nonce: *decommitment.nonce(),
        }
    }

    /// Returns the hash of this commitment
    pub fn hash(&self) -> &Hash {
        &self.hash
    }

    /// Returns the nonce of this commitment
    pub fn nonce(&self) -> &Nonce {
        &self.nonce
    }

    /// Opens this commitment
    pub fn open(&self, data: Vec<u8>) -> Blake3Opening {
        Blake3Opening::new(data, self.nonce)
    }
}

impl From<Blake3Commitment> for Commitment {
    fn from(value: Blake3Commitment) -> Self {
        Self::Blake3(value)
    }
}

/// A substring opening using Blake3
#[derive(Serialize, Deserialize, Clone)]
pub struct Blake3Opening {
    data: Vec<u8>,
    nonce: Nonce,
}

impl Blake3Opening {
    pub(crate) fn new(data: Vec<u8>, nonce: Nonce) -> Self {
        Self { data, nonce }
    }

    /// Recovers the expected commitment from this opening.
    ///
    /// # Panics
    ///
    /// - If the number of encodings does not match the number of bytes in the opening.
    /// - If an encoding is not for a u8.
    pub fn recover(&self, encodings: &[EncodedValue<Full>]) -> Blake3Commitment {
        assert_eq!(
            encodings.len(),
            self.data.len(),
            "encodings and data must have the same length"
        );

        let encodings = encodings
            .iter()
            .zip(&self.data)
            .map(|(encoding, data)| encoding.select(*data).expect("encoding is for a u8"))
            .collect::<Vec<_>>();

        let hash = Decommitment::new_with_nonce(encodings, self.nonce).commit();

        Blake3Commitment {
            hash,
            nonce: self.nonce,
        }
    }

    /// Returns the transcript data corresponding to this opening
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Returns the transcript data corresponding to this opening
    pub fn into_data(self) -> Vec<u8> {
        self.data
    }
}

impl From<Blake3Opening> for CommitmentOpening {
    fn from(value: Blake3Opening) -> Self {
        Self::Blake3(value)
    }
}