-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use dusk_plonk::prelude::*; | ||
use rand::rngs::StdRng; | ||
use rand::SeedableRng; | ||
|
||
const CAPACITY: usize = 1 << 4; | ||
const LABEL: &[u8] = b"check_public_inputs"; | ||
|
||
#[derive(Default)] | ||
pub struct TestPI { | ||
public: Vec<BlsScalar>, | ||
sum: BlsScalar, | ||
} | ||
|
||
impl TestPI { | ||
pub fn new(public: Vec<BlsScalar>, sum: BlsScalar) -> Self { | ||
Self { public, sum } | ||
} | ||
} | ||
|
||
impl Circuit for TestPI { | ||
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> { | ||
// this circuit will always have the same amount of gates but different | ||
// amount of public inputs, depending on the struct data | ||
let mut sum = Composer::ZERO; | ||
for i in 0..2 { | ||
let constraint = match i < self.public.len() { | ||
true => Constraint::new() | ||
.left(1) | ||
.a(sum) | ||
.right(1) | ||
.b(Composer::ONE) | ||
.public(self.public[i]), | ||
false => { | ||
Constraint::new().left(1).a(sum).right(1).b(Composer::ONE) | ||
} | ||
}; | ||
sum = composer.gate_add(constraint); | ||
} | ||
let expected_sum = composer.append_witness(self.sum); | ||
composer.assert_equal(sum, expected_sum); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn public_inputs() { | ||
let rng = &mut StdRng::seed_from_u64(0x10b); | ||
let pp = PublicParameters::setup(CAPACITY, rng) | ||
.expect("Creation of public parameter shouldn't fail"); | ||
|
||
// compiling the default version of TestPI, which has no pi | ||
let (prover, _verifier) = Compiler::compile::<TestPI>(&pp, LABEL) | ||
.expect("It should be possible to compile the prover and verifier"); | ||
|
||
// Create circuit with public inputs | ||
let pi: Vec<BlsScalar> = [BlsScalar::one(); 2].into(); | ||
let sum = BlsScalar::from(4); | ||
let circuit = TestPI::new(pi, sum); | ||
let result = prover.prove(rng, &circuit); | ||
assert_eq!( | ||
result.unwrap_err(), | ||
Error::PublicInputNotFound { index: 0 }, | ||
"proof creation for different sized circuit shouldn't be possible" | ||
); | ||
} |