types.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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. /// Heap types in bincode & vm
  19. #[derive(Clone, Debug)]
  20. #[repr(u8)]
  21. pub enum HeapType {
  22. Var = 0x00,
  23. Lit = 0x01,
  24. }
  25. impl HeapType {
  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. /// Variable 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. /// Elliptic curve nonidentity point
  49. EcNiPoint = 0x05,
  50. /// Base field element
  51. Base = 0x10,
  52. /// Base field element array
  53. BaseArray = 0x11,
  54. /// Scalar field element
  55. Scalar = 0x12,
  56. /// Scalar field element array
  57. ScalarArray = 0x13,
  58. /// Merkle tree path
  59. MerklePath = 0x20,
  60. /// Sparse merkle tree path
  61. SparseMerklePath = 0x21,
  62. /// Unsigned 32-bit integer
  63. Uint32 = 0x30,
  64. /// Unsigned 64-bit integer
  65. Uint64 = 0x31,
  66. /// Catch-all for any type
  67. Any = 0xff,
  68. }
  69. impl VarType {
  70. pub fn from_repr(b: u8) -> Option<Self> {
  71. match b {
  72. 0x01 => Some(Self::EcPoint),
  73. 0x02 => Some(Self::EcFixedPoint),
  74. 0x03 => Some(Self::EcFixedPointShort),
  75. 0x04 => Some(Self::EcFixedPointBase),
  76. 0x05 => Some(Self::EcNiPoint),
  77. 0x10 => Some(Self::Base),
  78. 0x11 => Some(Self::BaseArray),
  79. 0x12 => Some(Self::Scalar),
  80. 0x13 => Some(Self::ScalarArray),
  81. 0x20 => Some(Self::MerklePath),
  82. 0x21 => Some(Self::SparseMerklePath),
  83. 0x30 => Some(Self::Uint32),
  84. 0x31 => Some(Self::Uint64),
  85. 0xff => Some(Self::Any),
  86. _ => None,
  87. }
  88. }
  89. pub fn name(&self) -> &'static str {
  90. match self {
  91. Self::Dummy => "Dummy",
  92. Self::EcPoint => "EcPoint",
  93. Self::EcFixedPoint => "EcFixedPoint",
  94. Self::EcFixedPointShort => "EcFixedPointShort",
  95. Self::EcFixedPointBase => "EcFixedPointBase",
  96. Self::EcNiPoint => "EcNiPoint",
  97. Self::Base => "Base",
  98. Self::BaseArray => "BaseArray",
  99. Self::Scalar => "Scalar",
  100. Self::ScalarArray => "ScalarArray",
  101. Self::MerklePath => "MerklePath",
  102. Self::SparseMerklePath => "SparseMerklePath",
  103. Self::Uint32 => "Uint32",
  104. Self::Uint64 => "Uint64",
  105. Self::Any => "Any",
  106. }
  107. }
  108. }
  109. /// Literal types supported by the zkas VM
  110. #[derive(Copy, Clone, PartialEq, Eq, Debug)]
  111. #[repr(u8)]
  112. pub enum LitType {
  113. /// Dummy intermediate type
  114. Dummy = 0x00,
  115. /// Unsigned 64-bit integer
  116. Uint64 = 0x01,
  117. }
  118. impl LitType {
  119. pub fn from_repr(b: u8) -> Option<Self> {
  120. match b {
  121. 0x01 => Some(Self::Uint64),
  122. _ => None,
  123. }
  124. }
  125. pub fn to_vartype(&self) -> VarType {
  126. match self {
  127. Self::Dummy => VarType::Dummy,
  128. Self::Uint64 => VarType::Uint64,
  129. }
  130. }
  131. }