dyn_circuit.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::zk::assign_free_advice;
  19. use halo2_proofs::{
  20. arithmetic::Field,
  21. circuit::{
  22. //floor_planner::V1,
  23. Layouter,
  24. SimpleFloorPlanner,
  25. Value,
  26. },
  27. dev::{CircuitLayout, MockProver},
  28. pasta::Fp,
  29. plonk::{self, Advice, Circuit, Column, ConstraintSystem, Instance as InstanceColumn},
  30. };
  31. use plotters::prelude::*;
  32. use rand::rngs::OsRng;
  33. #[derive(Clone)]
  34. struct DynConfig {
  35. primary: Column<InstanceColumn>,
  36. advices: Vec<Column<Advice>>,
  37. }
  38. struct DynCircuit {
  39. pub witnesses: Vec<Value<Fp>>,
  40. }
  41. impl Circuit<Fp> for DynCircuit {
  42. type Config = DynConfig;
  43. //type FloorPlanner = V1;
  44. type FloorPlanner = SimpleFloorPlanner;
  45. type Params = usize;
  46. fn without_witnesses(&self) -> Self {
  47. let mut witnesses = Vec::with_capacity(self.witnesses.len());
  48. for _ in &self.witnesses {
  49. witnesses.push(Value::unknown());
  50. }
  51. Self { witnesses }
  52. }
  53. fn params(&self) -> Self::Params {
  54. self.witnesses.len()
  55. }
  56. fn configure_with_params(
  57. meta: &mut ConstraintSystem<Fp>,
  58. params: Self::Params,
  59. ) -> Self::Config {
  60. // NOTE: `let advices = vec![meta.advice_column(); params];` does not work as expected.
  61. let mut advices = vec![];
  62. for _ in 1..params + 1 {
  63. advices.push(meta.advice_column());
  64. }
  65. for advice in advices.iter() {
  66. meta.enable_equality(*advice);
  67. }
  68. let primary = meta.instance_column();
  69. meta.enable_equality(primary);
  70. DynConfig { primary, advices }
  71. }
  72. fn configure(_meta: &mut ConstraintSystem<Fp>) -> Self::Config {
  73. unreachable!();
  74. }
  75. fn synthesize(
  76. &self,
  77. config: Self::Config,
  78. mut layouter: impl Layouter<Fp>,
  79. ) -> Result<(), plonk::Error> {
  80. for (i, witness) in self.witnesses.iter().enumerate() {
  81. let w = assign_free_advice(
  82. layouter.namespace(|| "witness element"),
  83. config.advices[i],
  84. *witness,
  85. )?;
  86. layouter.constrain_instance(w.cell(), config.primary, i)?;
  87. }
  88. Ok(())
  89. }
  90. }
  91. #[test]
  92. fn dyn_circuit() {
  93. const ITERS: usize = 10;
  94. const K: u32 = 4;
  95. for i in 1..ITERS + 1 {
  96. let public_inputs = vec![Fp::random(&mut OsRng); i];
  97. let witnesses = public_inputs.iter().map(|x| Value::known(*x)).collect();
  98. let circuit = DynCircuit { witnesses };
  99. let prover = MockProver::run(K, &circuit, vec![public_inputs]).unwrap();
  100. prover.assert_satisfied();
  101. let title = format!("target/dynamic_circuit_{:0>2}.png", i);
  102. let root = BitMapBackend::new(&title, (800, 600)).into_drawing_area();
  103. CircuitLayout::default().render(K, &circuit, &root).unwrap();
  104. }
  105. }