// 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 rand::rngs::StdRng; use rand::SeedableRng; use zero_crypto::common::*; use zero_plonk::prelude::*; #[test] fn decomposition_works() { let mut rng = StdRng::seed_from_u64(8349u64); let n = 1 << 10; let label = b"demo"; let pp = PublicParameters::setup(n, &mut rng).expect("failed to create pp"); pub struct DummyCircuit { a: BlsScalar, bits: [BlsScalar; N], } impl DummyCircuit { pub fn new(a: BlsScalar) -> Self { let mut bits = [BlsScalar::zero(); N]; bits.iter_mut() .zip(a.to_bits().iter().rev()) .for_each(|(b, v)| *b = BlsScalar::from(*v as u64)); Self { a, bits } } } impl Default for DummyCircuit { fn default() -> Self { Self::new(BlsScalar::from(23u64)) } } impl Circuit for DummyCircuit { fn circuit(&self, composer: &mut C) -> Result<(), Error> where C: Composer, { let w_a = composer.append_witness(self.a); let mut w_bits: [Witness; N] = [C::ZERO; N]; w_bits .iter_mut() .zip(self.bits.iter()) .for_each(|(w, b)| *w = composer.append_witness(*b)); let w_x: [Witness; N] = composer.component_decomposition(w_a); w_bits.iter().zip(w_x.iter()).for_each(|(w, b)| { composer.assert_equal(*w, *b); }); Ok(()) } } let (prover, verifier) = Compiler::compile::>(&pp, label) .expect("failed to compile circuit"); // default works { let a = BlsScalar::random(&mut rng); let (proof, public_inputs) = prover .prove(&mut rng, &DummyCircuit::<256>::new(a)) .expect("failed to prove"); verifier .verify(&proof, &public_inputs) .expect("failed to verify proof"); } // negative works { let a = BlsScalar::random(&mut rng); let mut circuit = DummyCircuit::<256>::new(a); circuit.bits[10] = circuit.bits[10] ^ BlsScalar::one(); prover.prove(&mut rng, &circuit).expect_err("invalid proof"); } }