vm_stack.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 stack type abstractions
  19. use darkfi_sdk::crypto::{constants::OrchardFixedBases, MerkleNode};
  20. use halo2_gadgets::ecc::{
  21. chip::EccChip, FixedPoint, FixedPointBaseField, FixedPointShort, NonIdentityPoint, Point,
  22. };
  23. use halo2_proofs::{
  24. circuit::{AssignedCell, Value},
  25. pasta::pallas,
  26. };
  27. use crate::zkas::{decoder::ZkBinary, types::VarType};
  28. /// These represent the witness types outside of the circuit
  29. #[allow(clippy::large_enum_variant)]
  30. #[derive(Clone)]
  31. pub enum Witness {
  32. EcPoint(Value<pallas::Point>),
  33. EcNiPoint(Value<pallas::Point>),
  34. EcFixedPoint(Value<pallas::Point>),
  35. Base(Value<pallas::Base>),
  36. Scalar(Value<pallas::Scalar>),
  37. MerklePath(Value<[MerkleNode; 32]>),
  38. Uint32(Value<u32>),
  39. Uint64(Value<u64>),
  40. }
  41. pub enum Literal {
  42. Uint64(Value<u64>),
  43. }
  44. /// Helper function for verifiers to generate empty witnesses for
  45. /// a given decoded zkas binary
  46. pub fn empty_witnesses(zkbin: &ZkBinary) -> Vec<Witness> {
  47. let mut ret = Vec::with_capacity(zkbin.witnesses.len());
  48. for witness in &zkbin.witnesses {
  49. match witness {
  50. VarType::EcPoint => ret.push(Witness::EcPoint(Value::unknown())),
  51. VarType::EcNiPoint => ret.push(Witness::EcNiPoint(Value::unknown())),
  52. VarType::EcFixedPoint => ret.push(Witness::EcFixedPoint(Value::unknown())),
  53. VarType::Base => ret.push(Witness::Base(Value::unknown())),
  54. VarType::Scalar => ret.push(Witness::Scalar(Value::unknown())),
  55. VarType::MerklePath => ret.push(Witness::MerklePath(Value::unknown())),
  56. VarType::Uint32 => ret.push(Witness::Uint32(Value::unknown())),
  57. VarType::Uint64 => ret.push(Witness::Uint64(Value::unknown())),
  58. _ => todo!("Handle this gracefully"),
  59. }
  60. }
  61. ret
  62. }
  63. /// These represent the witness types inside the circuit
  64. #[allow(clippy::large_enum_variant)]
  65. #[derive(Debug, Clone)]
  66. pub enum StackVar {
  67. EcPoint(Point<pallas::Affine, EccChip<OrchardFixedBases>>),
  68. EcNiPoint(NonIdentityPoint<pallas::Affine, EccChip<OrchardFixedBases>>),
  69. EcFixedPoint(FixedPoint<pallas::Affine, EccChip<OrchardFixedBases>>),
  70. EcFixedPointShort(FixedPointShort<pallas::Affine, EccChip<OrchardFixedBases>>),
  71. EcFixedPointBase(FixedPointBaseField<pallas::Affine, EccChip<OrchardFixedBases>>),
  72. Base(AssignedCell<pallas::Base, pallas::Base>),
  73. Scalar(Value<pallas::Scalar>),
  74. MerklePath(Value<[pallas::Base; 32]>),
  75. Uint32(Value<u32>),
  76. Uint64(Value<u64>),
  77. }
  78. // TODO: Make this not panic (try_from)
  79. macro_rules! impl_from {
  80. ($variant:ident, $fortype:ty) => {
  81. impl From<StackVar> for $fortype {
  82. fn from(value: StackVar) -> Self {
  83. match value {
  84. StackVar::$variant(v) => v,
  85. _ => unreachable!(),
  86. }
  87. }
  88. }
  89. };
  90. }
  91. impl_from!(EcPoint, Point<pallas::Affine, EccChip<OrchardFixedBases>>);
  92. impl_from!(EcNiPoint, NonIdentityPoint<pallas::Affine, EccChip<OrchardFixedBases>>);
  93. impl_from!(EcFixedPoint, FixedPoint<pallas::Affine, EccChip<OrchardFixedBases>>);
  94. impl_from!(EcFixedPointShort, FixedPointShort<pallas::Affine, EccChip<OrchardFixedBases>>);
  95. impl_from!(EcFixedPointBase, FixedPointBaseField<pallas::Affine, EccChip<OrchardFixedBases>>);
  96. impl_from!(Scalar, Value<pallas::Scalar>);
  97. impl_from!(Base, AssignedCell<pallas::Base, pallas::Base>);
  98. impl_from!(Uint32, Value<u32>);
  99. impl_from!(MerklePath, Value<[pallas::Base; 32]>);