| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2022 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use darkfi_serial::{SerialDecodable, SerialEncodable};
- use halo2_proofs::{
- plonk,
- plonk::{Circuit, SingleVerifier},
- poly::commitment::Params,
- transcript::{Blake2bRead, Blake2bWrite},
- };
- use pasta_curves::{pallas, vesta};
- use rand::RngCore;
- #[derive(Clone, Debug)]
- pub struct VerifyingKey {
- pub params: Params<vesta::Affine>,
- pub vk: plonk::VerifyingKey<vesta::Affine>,
- }
- impl VerifyingKey {
- pub fn build(k: u32, c: &impl Circuit<pallas::Base>) -> Self {
- let params = Params::new(k);
- let vk = plonk::keygen_vk(¶ms, c).unwrap();
- VerifyingKey { params, vk }
- }
- }
- #[derive(Clone, Debug)]
- pub struct ProvingKey {
- pub params: Params<vesta::Affine>,
- pub pk: plonk::ProvingKey<vesta::Affine>,
- }
- impl ProvingKey {
- pub fn build(k: u32, c: &impl Circuit<pallas::Base>) -> Self {
- let params = Params::new(k);
- let vk = plonk::keygen_vk(¶ms, c).unwrap();
- let pk = plonk::keygen_pk(¶ms, vk, c).unwrap();
- ProvingKey { params, pk }
- }
- }
- #[derive(Clone, Default, PartialEq, Eq, SerialEncodable, SerialDecodable)]
- pub struct Proof(Vec<u8>);
- impl AsRef<[u8]> for Proof {
- fn as_ref(&self) -> &[u8] {
- &self.0
- }
- }
- impl core::fmt::Debug for Proof {
- fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
- write!(f, "Proof({:?})", self.0)
- }
- }
- impl Proof {
- pub fn create(
- pk: &ProvingKey,
- circuits: &[impl Circuit<pallas::Base>],
- instances: &[pallas::Base],
- mut rng: impl RngCore,
- ) -> std::result::Result<Self, plonk::Error> {
- let mut transcript = Blake2bWrite::<_, vesta::Affine, _>::init(vec![]);
- plonk::create_proof(
- &pk.params,
- &pk.pk,
- circuits,
- &[&[instances]],
- &mut rng,
- &mut transcript,
- )?;
- Ok(Proof(transcript.finalize()))
- }
- pub fn verify(
- &self,
- vk: &VerifyingKey,
- instances: &[pallas::Base],
- ) -> std::result::Result<(), plonk::Error> {
- let strategy = SingleVerifier::new(&vk.params);
- let mut transcript = Blake2bRead::init(&self.0[..]);
- plonk::verify_proof(&vk.params, &vk.vk, strategy, &[&[instances]], &mut transcript)
- }
- pub fn new(bytes: Vec<u8>) -> Self {
- Proof(bytes)
- }
- }
|