|
| 1 | +//! Hash commitment |
| 2 | +//! |
| 3 | +//! Hash commitment is a procedure that allows player $\P$ to commit some value (scalar, point, byte string, etc.), |
| 4 | +//! and reveal it later on. Player $\V$ can verify that commitment matches revealed value. |
| 5 | +//! |
| 6 | +//! ## Example |
| 7 | +//! |
| 8 | +//! 1. $\P$ commits some data (point, scalars, slices, whatever that can be represented as bytes): |
| 9 | +//! ```rust |
| 10 | +//! # use generic_ec::{Point, Scalar, Curve}; |
| 11 | +//! # use generic_ec_zkp::hash_commitment::HashCommit; |
| 12 | +//! # use sha2::Sha256; use rand::rngs::OsRng; |
| 13 | +//! # |
| 14 | +//! # fn doc_fn<E: Curve>() { |
| 15 | +//! let point: Point<E> = some_point(); |
| 16 | +//! let scalars: &[Scalar<E>] = some_scalars(); |
| 17 | +//! let arbitrary_data: &[&[u8]] = some_arbitrary_data(); |
| 18 | +//! |
| 19 | +//! let (commit, decommit) = HashCommit::<Sha256>::builder() |
| 20 | +//! .mix(point) |
| 21 | +//! .mix_many(scalars) |
| 22 | +//! .mix_many_bytes(arbitrary_data) |
| 23 | +//! .commit(&mut OsRng); |
| 24 | +//! # } |
| 25 | +//! # fn some_point<E: Curve>() -> Point<E> { unimplemented!() } |
| 26 | +//! # fn some_scalars<T>() -> T { unimplemented!() } |
| 27 | +//! # fn some_arbitrary_data<T>() -> T { unimplemented!() } |
| 28 | +//! ``` |
| 29 | +//! 2. $\P$ sends `commit` to $\V$ |
| 30 | +//! 3. At some point, $\P$ chooses to reveal committed data. It sends data + `decommit` to $\V$. |
| 31 | +//! |
| 32 | +//! $\V$ verifies that revealed data matches `commit`: |
| 33 | +//! |
| 34 | +//! ```rust |
| 35 | +//! # use generic_ec::{Point, Scalar, Curve}; |
| 36 | +//! # use generic_ec_zkp::hash_commitment::{HashCommit, DecommitNonce, MismatchedRevealedData}; |
| 37 | +//! # use sha2::Sha256; |
| 38 | +//! # |
| 39 | +//! # fn doc_fn<E: Curve>() -> Result<(), MismatchedRevealedData> { |
| 40 | +//! # let commit: HashCommit<Sha256> = unimplemented!(); |
| 41 | +//! # let (point, scalars, arbitrary_data): (Point<E>, &[Scalar<E>], &[&[u8]]) = unimplemented!(); |
| 42 | +//! # let decommit: DecommitNonce<Sha256> = unimplemented!(); |
| 43 | +//! HashCommit::<Sha256>::builder() |
| 44 | +//! .mix(point) |
| 45 | +//! .mix_many(scalars) |
| 46 | +//! .mix_many_bytes(arbitrary_data) |
| 47 | +//! .verify(&commit, &decommit)?; |
| 48 | +//! # } |
| 49 | +//! ``` |
| 50 | +//! |
| 51 | +//! ## Algorithm |
| 52 | +//! Underlying algorithm is based on hash function $\H$. To commit data, we sample a large random nonce, |
| 53 | +//! and hash it along with data. When we hash bytestrings, we prepend its length to it, in that way we |
| 54 | +//! ensure that there's only one set of inputs that can be decommitted. |
| 55 | +//! |
| 56 | +//! Roughly, algorithm is: |
| 57 | +//! |
| 58 | +//! 1. $commit(i_1, \dots, i_n) =$ \ |
| 59 | +//! 1. $\mathit{nonce} \gets \\{0,1\\}^k$ |
| 60 | +//! 2. $\text{return}\ \H(\dots \\| \text{u32\\_to\\_be}(\mathit{len}(i_j)) \\| i_j \\| \dots \\| \mathit{nonce}), \mathit{nonce}$ |
| 61 | +//! |
| 62 | +//! 2. $decommit(commit, nonce, i_1, \dots, i_n) =$ |
| 63 | +//! 1. $\text{return}\ \H(\dots \\| \text{u32\\_to\\_be}(\mathit{len}(i_j)) \\| i_j \\| \dots \\| nonce) \\? commit$ |
| 64 | +
|
| 65 | +use digest::{generic_array::GenericArray, Digest, Output}; |
| 66 | +use rand_core::RngCore; |
| 67 | +use subtle::ConstantTimeEq; |
| 68 | + |
| 69 | +use generic_ec::{Curve, Point, Scalar}; |
| 70 | + |
| 71 | +/// Builder for commitment/verification |
| 72 | +pub struct Builder<D: Digest>(D); |
| 73 | + |
| 74 | +impl<D: Digest> Builder<D> { |
| 75 | + /// Creates an instance of [`Builder`] |
| 76 | + pub fn new() -> Self { |
| 77 | + Self(D::new()) |
| 78 | + } |
| 79 | + |
| 80 | + /// Mixes value serialized to bytes into commitment |
| 81 | + /// |
| 82 | + /// You can use this method with [`Point<E>`](Point) or [`Scalar<E>`](Scalar). Also you can |
| 83 | + /// implement [`EncodesToBytes`] for your own types. |
| 84 | + /// |
| 85 | + /// ```rust |
| 86 | + /// use generic_ec::{Point, Scalar}; |
| 87 | + /// use generic_ec_zkp::hash_commitment::HashCommit; |
| 88 | + /// # use sha2::Sha256; |
| 89 | + /// # use rand::rngs::OsRng; |
| 90 | + /// |
| 91 | + /// # use generic_ec::Curve; |
| 92 | + /// # fn doc_fn<E: Curve>() { |
| 93 | + /// let point: Point<E> = some_point(); |
| 94 | + /// let scalar: Scalar<E> = some_scalar(); |
| 95 | + /// |
| 96 | + /// let (commit, decommit) = HashCommit::<Sha256>::builder() |
| 97 | + /// .mix(point) |
| 98 | + /// .mix(scalar) |
| 99 | + /// .commit(&mut OsRng); |
| 100 | + /// # } |
| 101 | + /// # fn some_point<E: Curve>() -> Point<E> { unimplemented!() } |
| 102 | + /// # fn some_scalar<E: Curve>() -> Scalar<E> { unimplemented!() } |
| 103 | + /// ``` |
| 104 | + /// |
| 105 | + /// ## Panics |
| 106 | + /// Panics if serialized value length exceeds `u32::MAX`. On most of modern systems, it's not |
| 107 | + /// possible to allocate such large chunk of memory. |
| 108 | + pub fn mix<T>(self, encodable: T) -> Self |
| 109 | + where |
| 110 | + T: EncodesToBytes, |
| 111 | + { |
| 112 | + self.mix_bytes(encodable.to_bytes()) |
| 113 | + } |
| 114 | + |
| 115 | + /// Mixes values serialized to bytes into commitment |
| 116 | + /// |
| 117 | + /// ```rust |
| 118 | + /// use generic_ec::Point; |
| 119 | + /// use generic_ec_zkp::hash_commitment::HashCommit; |
| 120 | + /// # use sha2::Sha256; |
| 121 | + /// # use rand::rngs::OsRng; |
| 122 | + /// |
| 123 | + /// # use generic_ec::Curve; |
| 124 | + /// # fn doc_fn<E: Curve>() { |
| 125 | + /// let points: Vec<Point<E>> = some_points(); |
| 126 | + /// |
| 127 | + /// let (commit, decommit) = HashCommit::<Sha256>::builder() |
| 128 | + /// .mix_many(&points) |
| 129 | + /// .commit(&mut OsRng); |
| 130 | + /// # } |
| 131 | + /// # fn some_points<E: Curve>() -> Vec<Point<E>> { unimplemented!() } |
| 132 | + /// ``` |
| 133 | + /// |
| 134 | + /// ## Panics |
| 135 | + /// Panics if number of values exceeds `u32::MAX` or if any of serialized values length exceeds |
| 136 | + /// `u32::MAX`. |
| 137 | + pub fn mix_many<T>(mut self, encodables: &[T]) -> Self |
| 138 | + where |
| 139 | + T: EncodesToBytes, |
| 140 | + { |
| 141 | + let len: u32 = encodables |
| 142 | + .len() |
| 143 | + .try_into() |
| 144 | + .expect("encodables len exceeds u32::MAX"); |
| 145 | + self = self.mix_bytes(len.to_be_bytes()); |
| 146 | + for encodable in encodables { |
| 147 | + self = self.mix(encodable); |
| 148 | + } |
| 149 | + self |
| 150 | + } |
| 151 | + |
| 152 | + /// Mixes bytes into commitment |
| 153 | + /// |
| 154 | + /// ```rust |
| 155 | + /// use generic_ec_zkp::hash_commitment::HashCommit; |
| 156 | + /// # use sha2::Sha256; |
| 157 | + /// # use rand::rngs::OsRng; |
| 158 | + /// |
| 159 | + /// # use generic_ec::Curve; |
| 160 | + /// # fn doc_fn<E: Curve>() { |
| 161 | + /// let (commit, decommit) = HashCommit::<Sha256>::builder() |
| 162 | + /// .mix_bytes(b"some message") |
| 163 | + /// .commit(&mut OsRng); |
| 164 | + /// # } |
| 165 | + /// ``` |
| 166 | + /// |
| 167 | + /// ## Panics |
| 168 | + /// Panics if `data` length exceeds `u32::MAX`. On most of modern systems, it's not possible |
| 169 | + /// to allocate such large chuck of memory. |
| 170 | + pub fn mix_bytes(self, data: impl AsRef<[u8]>) -> Self { |
| 171 | + let data_len: u32 = data |
| 172 | + .as_ref() |
| 173 | + .len() |
| 174 | + .try_into() |
| 175 | + .expect("data len exceeds u32::MAX"); |
| 176 | + Self( |
| 177 | + self.0 |
| 178 | + .chain_update(data_len.to_be_bytes()) |
| 179 | + .chain_update(data), |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + /// Mixes list of byte strings into commitment |
| 184 | + /// |
| 185 | + /// ```rust |
| 186 | + /// use generic_ec_zkp::hash_commitment::HashCommit; |
| 187 | + /// # use sha2::Sha256; |
| 188 | + /// # use rand::rngs::OsRng; |
| 189 | + /// |
| 190 | + /// # use generic_ec::Curve; |
| 191 | + /// # fn doc_fn<E: Curve>() { |
| 192 | + /// let (commit, decommit) = HashCommit::<Sha256>::builder() |
| 193 | + /// .mix_many_bytes(&[b"some message", b"another message"]) |
| 194 | + /// .commit(&mut OsRng); |
| 195 | + /// # } |
| 196 | + /// ``` |
| 197 | + /// |
| 198 | + /// ## Panics |
| 199 | + /// Panics if `list` length exceeds `u32::MAX` or if length of any item of the list exceeds |
| 200 | + /// `u32::MAX`. |
| 201 | + pub fn mix_many_bytes(mut self, list: &[&[u8]]) -> Self { |
| 202 | + let list_len: u32 = list.len().try_into().expect("list len exceeds u32::MAX"); |
| 203 | + self = self.mix_bytes(list_len.to_be_bytes()); |
| 204 | + for item in list { |
| 205 | + self = self.mix_bytes(item) |
| 206 | + } |
| 207 | + self |
| 208 | + } |
| 209 | + |
| 210 | + /// Performs commitment |
| 211 | + /// |
| 212 | + /// Decommitment nonce is generated from provided randomness source |
| 213 | + pub fn commit<R: RngCore>(self, rng: &mut R) -> (HashCommit<D>, DecommitNonce<D>) { |
| 214 | + let mut nonce = DecommitNonce::<D>::default(); |
| 215 | + rng.fill_bytes(&mut nonce.nonce); |
| 216 | + (self.commit_with_fixed_nonce(&nonce), nonce) |
| 217 | + } |
| 218 | + |
| 219 | + /// Performs commitment with specified decommitment nonce |
| 220 | + pub fn commit_with_fixed_nonce(mut self, nonce: &DecommitNonce<D>) -> HashCommit<D> { |
| 221 | + self = self.mix_bytes(&nonce.nonce); |
| 222 | + let resulting_hash = self.0.finalize(); |
| 223 | + HashCommit(resulting_hash) |
| 224 | + } |
| 225 | + |
| 226 | + /// Verifies that provided data matches commitment and decommitment |
| 227 | + pub fn verify( |
| 228 | + self, |
| 229 | + commit: &HashCommit<D>, |
| 230 | + nonce: &DecommitNonce<D>, |
| 231 | + ) -> Result<(), MismatchedRevealedData> { |
| 232 | + let should_be = self.commit_with_fixed_nonce(nonce); |
| 233 | + if commit.0.ct_eq(&should_be.0).into() { |
| 234 | + Ok(()) |
| 235 | + } else { |
| 236 | + Err(MismatchedRevealedData) |
| 237 | + } |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +/// Committed value |
| 242 | +#[derive(Clone)] |
| 243 | +pub struct HashCommit<D: Digest>(pub Output<D>); |
| 244 | + |
| 245 | +impl<D: Digest> HashCommit<D> { |
| 246 | + pub fn builder() -> Builder<D> { |
| 247 | + Builder::new() |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +/// Random nonce that was used to "blind" commitment |
| 252 | +#[derive(Clone)] |
| 253 | +pub struct DecommitNonce<D: Digest> { |
| 254 | + pub nonce: GenericArray<u8, D::OutputSize>, |
| 255 | +} |
| 256 | + |
| 257 | +impl<D: Digest> Default for DecommitNonce<D> { |
| 258 | + fn default() -> Self { |
| 259 | + Self { |
| 260 | + nonce: Default::default(), |
| 261 | + } |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | +/// Infallibly encodable to bytes |
| 266 | +/// |
| 267 | +/// Used in [`hash_commitment::Builder`](Builder) methods [`mix`](Builder::mix) and [`mix_many`](Builder::mix_many) to convert given value to bytes. |
| 268 | +pub trait EncodesToBytes { |
| 269 | + /// Value byte representation |
| 270 | + type Bytes: AsRef<[u8]>; |
| 271 | + /// Encodes value to bytes |
| 272 | + fn to_bytes(&self) -> Self::Bytes; |
| 273 | +} |
| 274 | + |
| 275 | +impl<T: EncodesToBytes> EncodesToBytes for &T { |
| 276 | + type Bytes = T::Bytes; |
| 277 | + fn to_bytes(&self) -> Self::Bytes { |
| 278 | + <T as EncodesToBytes>::to_bytes(*self) |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +impl<E: Curve> EncodesToBytes for Point<E> { |
| 283 | + type Bytes = generic_ec::EncodedPoint<E>; |
| 284 | + fn to_bytes(&self) -> Self::Bytes { |
| 285 | + self.to_bytes(true) |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +impl<E: Curve> EncodesToBytes for Scalar<E> { |
| 290 | + type Bytes = generic_ec::EncodedScalar<E>; |
| 291 | + fn to_bytes(&self) -> Self::Bytes { |
| 292 | + self.to_bytes() |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +impl EncodesToBytes for u16 { |
| 297 | + type Bytes = [u8; 2]; |
| 298 | + fn to_bytes(&self) -> Self::Bytes { |
| 299 | + self.to_be_bytes() |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | +/// Error indicating that revealed data doesn't match commitment |
| 304 | +pub struct MismatchedRevealedData; |
0 commit comments