/* 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 .
*/
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,
pub vk: plonk::VerifyingKey,
}
impl VerifyingKey {
pub fn build(k: u32, c: &impl Circuit) -> 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,
pub pk: plonk::ProvingKey,
}
impl ProvingKey {
pub fn build(k: u32, c: &impl Circuit) -> 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);
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],
instances: &[pallas::Base],
mut rng: impl RngCore,
) -> std::result::Result {
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) -> Self {
Proof(bytes)
}
}