tracer.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 std::cell::RefCell;
  19. use darkfi_sdk::{crypto::constants::OrchardFixedBases, pasta::pallas};
  20. use halo2_gadgets::ecc as ecc_gadget;
  21. use halo2_proofs::{arithmetic::Field, circuit::AssignedCell};
  22. #[derive(Clone, Debug)]
  23. pub enum DebugOpValue {
  24. EcPoint(pallas::Base, pallas::Base),
  25. Base(pallas::Base),
  26. Void,
  27. }
  28. #[derive(Clone)]
  29. pub struct ZkTracer {
  30. pub opvalues: RefCell<Option<Vec<DebugOpValue>>>,
  31. init_allowed: bool,
  32. is_enabled: bool,
  33. }
  34. impl ZkTracer {
  35. pub(crate) fn new(init_allowed: bool) -> Self {
  36. Self { opvalues: RefCell::new(None), init_allowed, is_enabled: false }
  37. }
  38. pub(crate) fn init(&mut self) {
  39. if !self.init_allowed {
  40. panic!("Cannot initialize tracer for verifier circuit!");
  41. }
  42. self.is_enabled = true;
  43. *self.opvalues.borrow_mut() = Some(Vec::new());
  44. }
  45. pub(crate) fn clear(&self) {
  46. if !self.is_enabled {
  47. return
  48. }
  49. self.opvalues.borrow_mut().as_mut().unwrap().clear();
  50. }
  51. fn push(&self, value: DebugOpValue) {
  52. let mut binding = self.opvalues.borrow_mut();
  53. let opvalues = binding.as_mut().unwrap();
  54. opvalues.push(value);
  55. }
  56. pub(crate) fn push_ecpoint(
  57. &self,
  58. point: &ecc_gadget::Point<pallas::Affine, ecc_gadget::chip::EccChip<OrchardFixedBases>>,
  59. ) {
  60. if !self.is_enabled {
  61. return
  62. }
  63. let (mut x, mut y) = (pallas::Base::ZERO, pallas::Base::ZERO);
  64. point.inner().x().value().map(|rx| x = *rx);
  65. point.inner().y().value().map(|ry| y = *ry);
  66. self.push(DebugOpValue::EcPoint(x, y));
  67. }
  68. pub(crate) fn push_base(&self, value: &AssignedCell<pallas::Base, pallas::Base>) {
  69. if !self.is_enabled {
  70. return
  71. }
  72. let mut x = pallas::Base::ZERO;
  73. value.value().map(|rx| x = *rx);
  74. self.push(DebugOpValue::Base(x));
  75. }
  76. pub(crate) fn push_void(&self) {
  77. if !self.is_enabled {
  78. return
  79. }
  80. self.push(DebugOpValue::Void);
  81. }
  82. pub(crate) fn assert_correct(&self, opcodes_len: usize) {
  83. if !self.is_enabled {
  84. return
  85. }
  86. let opvalues_len = self.opvalues.borrow().as_ref().map_or(0, |v| v.len());
  87. assert_eq!(opvalues_len, opcodes_len);
  88. }
  89. }