|
10 | 10 |
|
11 | 11 | use ff::{Field, PrimeField}; |
12 | 12 | use group::{Group, GroupEncoding}; |
| 13 | +use sha3::Digest; |
| 14 | +use sha3::Sha3_256; |
13 | 15 |
|
14 | 16 | use crate::{ |
15 | 17 | errors::Error, |
@@ -273,6 +275,57 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> { |
273 | 275 | serialize_scalars::<G>(&[*challenge]) |
274 | 276 | } |
275 | 277 |
|
| 278 | + fn instance_label(&self) -> impl AsRef<[u8]> { |
| 279 | + match self { |
| 280 | + Protocol::Simple(p) => { |
| 281 | + let label = p.instance_label(); |
| 282 | + label.as_ref().to_vec() |
| 283 | + } |
| 284 | + Protocol::And(ps) => { |
| 285 | + let mut bytes = Vec::new(); |
| 286 | + for p in ps { |
| 287 | + bytes.extend(p.instance_label().as_ref()); |
| 288 | + } |
| 289 | + bytes |
| 290 | + } |
| 291 | + Protocol::Or(ps) => { |
| 292 | + let mut bytes = Vec::new(); |
| 293 | + for p in ps { |
| 294 | + bytes.extend(p.instance_label().as_ref()); |
| 295 | + } |
| 296 | + bytes |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + fn protocol_identifier(&self) -> impl AsRef<[u8]> { |
| 302 | + let mut hasher = Sha3_256::new(); |
| 303 | + |
| 304 | + match self { |
| 305 | + Protocol::Simple(p) => { |
| 306 | + // take the digest of the simple protocol id |
| 307 | + hasher.update([0u8; 32]); |
| 308 | + hasher.update(p.protocol_identifier()); |
| 309 | + } |
| 310 | + Protocol::And(protocols) => { |
| 311 | + let mut hasher = Sha3_256::new(); |
| 312 | + hasher.update([1u8; 32]); |
| 313 | + for p in protocols { |
| 314 | + hasher.update(p.protocol_identifier()); |
| 315 | + } |
| 316 | + } |
| 317 | + Protocol::Or(protocols) => { |
| 318 | + let mut hasher = Sha3_256::new(); |
| 319 | + hasher.update([2u8; 32]); |
| 320 | + for p in protocols { |
| 321 | + hasher.update(p.protocol_identifier()); |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + hasher.finalize() |
| 327 | + } |
| 328 | + |
276 | 329 | fn serialize_response(&self, response: &Self::Response) -> Vec<u8> { |
277 | 330 | match (self, response) { |
278 | 331 | (Protocol::Simple(p), ProtocolResponse::Simple(r)) => p.serialize_response(r), |
|
0 commit comments