vm_heap.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. //! VM heap type abstractions
  19. use darkfi_sdk::crypto::{constants::OrchardFixedBases, MerkleNode};
  20. use halo2_gadgets::ecc::{
  21. chip::EccChip, FixedPoint, FixedPointBaseField, FixedPointShort, NonIdentityPoint, Point,
  22. ScalarFixed,
  23. };
  24. use halo2_proofs::{
  25. circuit::{AssignedCell, Value},
  26. pasta::pallas,
  27. plonk,
  28. };
  29. use log::error;
  30. use crate::{
  31. zkas::{decoder::ZkBinary, types::VarType},
  32. Error::ZkasDecoderError,
  33. Result,
  34. };
  35. /// These represent the witness types outside of the circuit
  36. #[allow(clippy::large_enum_variant)]
  37. #[derive(Clone)]
  38. pub enum Witness {
  39. EcPoint(Value<pallas::Point>),
  40. EcNiPoint(Value<pallas::Point>),
  41. EcFixedPoint(Value<pallas::Point>),
  42. Base(Value<pallas::Base>),
  43. Scalar(Value<pallas::Scalar>),
  44. MerklePath(Value<[MerkleNode; 32]>),
  45. Uint32(Value<u32>),
  46. Uint64(Value<u64>),
  47. }
  48. impl Witness {
  49. pub fn name(&self) -> &str {
  50. match self {
  51. Self::EcPoint(_) => "EcPoint",
  52. Self::EcNiPoint(_) => "EcNiPoint",
  53. Self::EcFixedPoint(_) => "EcFixedPoint",
  54. Self::Base(_) => "Base",
  55. Self::Scalar(_) => "Scalar",
  56. Self::MerklePath(_) => "MerklePath",
  57. Self::Uint32(_) => "Uint32",
  58. Self::Uint64(_) => "Uint64",
  59. }
  60. }
  61. }
  62. pub enum Literal {
  63. Uint64(Value<u64>),
  64. }
  65. /// Helper function for verifiers to generate empty witnesses for
  66. /// a given decoded zkas binary
  67. pub fn empty_witnesses(zkbin: &ZkBinary) -> Result<Vec<Witness>> {
  68. let mut ret = Vec::with_capacity(zkbin.witnesses.len());
  69. for witness in &zkbin.witnesses {
  70. match witness {
  71. VarType::EcPoint => ret.push(Witness::EcPoint(Value::unknown())),
  72. VarType::EcNiPoint => ret.push(Witness::EcNiPoint(Value::unknown())),
  73. VarType::EcFixedPoint => ret.push(Witness::EcFixedPoint(Value::unknown())),
  74. VarType::Base => ret.push(Witness::Base(Value::unknown())),
  75. VarType::Scalar => ret.push(Witness::Scalar(Value::unknown())),
  76. VarType::MerklePath => ret.push(Witness::MerklePath(Value::unknown())),
  77. VarType::Uint32 => ret.push(Witness::Uint32(Value::unknown())),
  78. VarType::Uint64 => ret.push(Witness::Uint64(Value::unknown())),
  79. x => return Err(ZkasDecoderError(format!("Unsupported witness type: {:?}", x))),
  80. }
  81. }
  82. Ok(ret)
  83. }
  84. /// These represent the witness types inside the circuit
  85. #[allow(clippy::large_enum_variant)]
  86. #[derive(Debug, Clone)]
  87. pub enum HeapVar {
  88. EcPoint(Point<pallas::Affine, EccChip<OrchardFixedBases>>),
  89. EcNiPoint(NonIdentityPoint<pallas::Affine, EccChip<OrchardFixedBases>>),
  90. EcFixedPoint(FixedPoint<pallas::Affine, EccChip<OrchardFixedBases>>),
  91. EcFixedPointShort(FixedPointShort<pallas::Affine, EccChip<OrchardFixedBases>>),
  92. EcFixedPointBase(FixedPointBaseField<pallas::Affine, EccChip<OrchardFixedBases>>),
  93. Base(AssignedCell<pallas::Base, pallas::Base>),
  94. Scalar(ScalarFixed<pallas::Affine, EccChip<OrchardFixedBases>>),
  95. MerklePath(Value<[pallas::Base; 32]>),
  96. Uint32(Value<u32>),
  97. Uint64(Value<u64>),
  98. }
  99. macro_rules! impl_try_from {
  100. ($variant:ident, $fortype:ty) => {
  101. impl std::convert::TryFrom<HeapVar> for $fortype {
  102. type Error = plonk::Error;
  103. fn try_from(value: HeapVar) -> std::result::Result<Self, Self::Error> {
  104. match value {
  105. HeapVar::$variant(v) => Ok(v),
  106. x => {
  107. error!("Invalid TryFrom conversion {:?}", x);
  108. Err(plonk::Error::Synthesis)
  109. }
  110. }
  111. }
  112. }
  113. };
  114. }
  115. impl_try_from!(EcPoint, Point<pallas::Affine, EccChip<OrchardFixedBases>>);
  116. impl_try_from!(EcNiPoint, NonIdentityPoint<pallas::Affine, EccChip<OrchardFixedBases>>);
  117. impl_try_from!(EcFixedPoint, FixedPoint<pallas::Affine, EccChip<OrchardFixedBases>>);
  118. impl_try_from!(EcFixedPointShort, FixedPointShort<pallas::Affine, EccChip<OrchardFixedBases>>);
  119. impl_try_from!(EcFixedPointBase, FixedPointBaseField<pallas::Affine, EccChip<OrchardFixedBases>>);
  120. impl_try_from!(Scalar, ScalarFixed<pallas::Affine, EccChip<OrchardFixedBases>>);
  121. impl_try_from!(Base, AssignedCell<pallas::Base, pallas::Base>);
  122. impl_try_from!(Uint32, Value<u32>);
  123. impl_try_from!(MerklePath, Value<[pallas::Base; 32]>);