types.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /// Stack types in bincode & vm
  19. #[derive(Clone, Debug)]
  20. #[repr(u8)]
  21. pub enum StackType {
  22. Var = 0x00,
  23. Lit = 0x01,
  24. }
  25. impl StackType {
  26. pub fn from_repr(b: u8) -> Option<Self> {
  27. match b {
  28. 0x00 => Some(Self::Var),
  29. 0x01 => Some(Self::Lit),
  30. _ => None,
  31. }
  32. }
  33. }
  34. /// Varable types supported by the zkas VM
  35. #[derive(Copy, Clone, PartialEq, Eq, Debug)]
  36. #[repr(u8)]
  37. pub enum VarType {
  38. /// Dummy intermediate type
  39. Dummy = 0x00,
  40. /// Elliptic curve point
  41. EcPoint = 0x01,
  42. /// Elliptic curve fixed point (a constant)
  43. EcFixedPoint = 0x02,
  44. /// Elliptic curve fixed point short
  45. EcFixedPointShort = 0x03,
  46. /// Elliptic curve fixed point in base field
  47. EcFixedPointBase = 0x04,
  48. /// Base field element
  49. Base = 0x10,
  50. /// Base field element array
  51. BaseArray = 0x11,
  52. /// Scalar field element
  53. Scalar = 0x12,
  54. /// Scalar field element array
  55. ScalarArray = 0x13,
  56. /// A Merkle tree path
  57. MerklePath = 0x20,
  58. /// Unsigned 32-bit integer
  59. Uint32 = 0x30,
  60. /// Unsigned 64-bit integer
  61. Uint64 = 0x31,
  62. }
  63. impl VarType {
  64. pub fn from_repr(b: u8) -> Option<Self> {
  65. match b {
  66. 0x01 => Some(Self::EcPoint),
  67. 0x02 => Some(Self::EcFixedPoint),
  68. 0x03 => Some(Self::EcFixedPointShort),
  69. 0x04 => Some(Self::EcFixedPointBase),
  70. 0x10 => Some(Self::Base),
  71. 0x11 => Some(Self::BaseArray),
  72. 0x12 => Some(Self::Scalar),
  73. 0x13 => Some(Self::ScalarArray),
  74. 0x20 => Some(Self::MerklePath),
  75. 0x30 => Some(Self::Uint32),
  76. 0x31 => Some(Self::Uint64),
  77. _ => None,
  78. }
  79. }
  80. }
  81. /// Literal types supported by the zkas VM
  82. #[derive(Copy, Clone, PartialEq, Eq, Debug)]
  83. #[repr(u8)]
  84. pub enum LitType {
  85. /// Dummy intermediate type
  86. Dummy = 0x00,
  87. /// Unsigned 64-bit integer
  88. Uint64 = 0x01,
  89. }
  90. impl LitType {
  91. pub fn from_repr(b: u8) -> Option<Self> {
  92. match b {
  93. 0x01 => Some(Self::Uint64),
  94. _ => None,
  95. }
  96. }
  97. pub fn to_vartype(&self) -> VarType {
  98. match self {
  99. Self::Dummy => VarType::Dummy,
  100. Self::Uint64 => VarType::Uint64,
  101. }
  102. }
  103. }