vm_stack.rs 3.8 KB

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