metadata.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::{
  19. crypto::{schnorr::Signature, Keypair, PublicKey},
  20. pasta::pallas,
  21. };
  22. use darkfi_serial::{SerialDecodable, SerialEncodable};
  23. use log::error;
  24. use super::leadcoin::LeadCoin;
  25. use crate::{
  26. crypto::proof::{Proof, ProvingKey, VerifyingKey},
  27. Result,
  28. };
  29. /// This struct represents [`Block`](super::Block) information used by the consensus protocol.
  30. #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  31. pub struct Metadata {
  32. /// Block owner signature
  33. pub signature: Signature,
  34. /// Block owner public_key
  35. pub public_key: PublicKey, // TODO: remove this(to be derived by proof)
  36. /// Block owner slot competing coins public inputs
  37. pub public_inputs: Vec<pallas::Base>,
  38. /// Response of global random oracle, or it's emulation.
  39. pub eta: [u8; 32],
  40. /// Leader NIZK proof
  41. pub proof: LeadProof,
  42. }
  43. impl Default for Metadata {
  44. /// Default Metadata used in genesis block generation
  45. fn default() -> Self {
  46. let keypair = Keypair::default();
  47. let signature = Signature::dummy();
  48. let public_inputs = vec![];
  49. let eta: [u8; 32] = *blake3::hash(b"let there be dark!").as_bytes();
  50. let proof = LeadProof::default();
  51. Self { signature, public_key: keypair.public, public_inputs, eta, proof }
  52. }
  53. }
  54. impl Metadata {
  55. pub fn new(
  56. signature: Signature,
  57. public_key: PublicKey,
  58. public_inputs: Vec<pallas::Base>,
  59. eta: [u8; 32],
  60. proof: LeadProof,
  61. ) -> Self {
  62. Self { signature, public_key, public_inputs, eta, proof }
  63. }
  64. }
  65. /// Wrapper over the Proof, for future additions.
  66. #[derive(Default, Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  67. pub struct LeadProof {
  68. /// Leadership proof
  69. pub proof: Proof,
  70. }
  71. impl LeadProof {
  72. pub fn new(pk: &ProvingKey, coin: LeadCoin) -> Self {
  73. let proof = coin.create_lead_proof(pk).unwrap();
  74. Self { proof }
  75. }
  76. pub fn verify(&self, vk: &VerifyingKey, public_inputs: &[pallas::Base]) -> Result<()> {
  77. if let Err(e) = self.proof.verify(vk, public_inputs) {
  78. error!("Verification of consensus lead proof failed: {}", e);
  79. return Err(e.into())
  80. }
  81. Ok(())
  82. }
  83. }
  84. impl From<Proof> for LeadProof {
  85. fn from(proof: Proof) -> Self {
  86. Self { proof }
  87. }
  88. }