proof.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_serial::{SerialDecodable, SerialEncodable};
  19. use halo2_proofs::{
  20. plonk,
  21. plonk::{Circuit, SingleVerifier},
  22. poly::commitment::Params,
  23. transcript::{Blake2bRead, Blake2bWrite},
  24. };
  25. use pasta_curves::{pallas, vesta};
  26. use rand::RngCore;
  27. #[derive(Clone, Debug)]
  28. pub struct VerifyingKey {
  29. pub params: Params<vesta::Affine>,
  30. pub vk: plonk::VerifyingKey<vesta::Affine>,
  31. }
  32. impl VerifyingKey {
  33. pub fn build(k: u32, c: &impl Circuit<pallas::Base>) -> Self {
  34. let params = Params::new(k);
  35. let vk = plonk::keygen_vk(&params, c).unwrap();
  36. VerifyingKey { params, vk }
  37. }
  38. }
  39. #[derive(Clone, Debug)]
  40. pub struct ProvingKey {
  41. pub params: Params<vesta::Affine>,
  42. pub pk: plonk::ProvingKey<vesta::Affine>,
  43. }
  44. impl ProvingKey {
  45. pub fn build(k: u32, c: &impl Circuit<pallas::Base>) -> Self {
  46. let params = Params::new(k);
  47. let vk = plonk::keygen_vk(&params, c).unwrap();
  48. let pk = plonk::keygen_pk(&params, vk, c).unwrap();
  49. ProvingKey { params, pk }
  50. }
  51. }
  52. #[derive(Clone, Default, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  53. pub struct Proof(Vec<u8>);
  54. impl AsRef<[u8]> for Proof {
  55. fn as_ref(&self) -> &[u8] {
  56. &self.0
  57. }
  58. }
  59. impl core::fmt::Debug for Proof {
  60. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  61. write!(f, "Proof({:?})", self.0)
  62. }
  63. }
  64. impl Proof {
  65. pub fn create(
  66. pk: &ProvingKey,
  67. circuits: &[impl Circuit<pallas::Base>],
  68. instances: &[pallas::Base],
  69. mut rng: impl RngCore,
  70. ) -> std::result::Result<Self, plonk::Error> {
  71. let mut transcript = Blake2bWrite::<_, vesta::Affine, _>::init(vec![]);
  72. plonk::create_proof(
  73. &pk.params,
  74. &pk.pk,
  75. circuits,
  76. &[&[instances]],
  77. &mut rng,
  78. &mut transcript,
  79. )?;
  80. Ok(Proof(transcript.finalize()))
  81. }
  82. pub fn verify(
  83. &self,
  84. vk: &VerifyingKey,
  85. instances: &[pallas::Base],
  86. ) -> std::result::Result<(), plonk::Error> {
  87. let strategy = SingleVerifier::new(&vk.params);
  88. let mut transcript = Blake2bRead::init(&self.0[..]);
  89. plonk::verify_proof(&vk.params, &vk.vk, strategy, &[&[instances]], &mut transcript)
  90. }
  91. pub fn new(bytes: Vec<u8>) -> Self {
  92. Proof(bytes)
  93. }
  94. }