darkfi/zkas/
types.rs

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
19/// Heap types in bincode & vm
20#[derive(Clone, Debug)]
21#[repr(u8)]
22pub enum HeapType {
23    Var = 0x00,
24    Lit = 0x01,
25}
26
27impl HeapType {
28    pub fn from_repr(b: u8) -> Option<Self> {
29        match b {
30            0x00 => Some(Self::Var),
31            0x01 => Some(Self::Lit),
32            _ => None,
33        }
34    }
35}
36
37/// Variable types supported by the zkas VM
38#[derive(Copy, Clone, PartialEq, Eq, Debug)]
39#[repr(u8)]
40pub enum VarType {
41    /// Dummy intermediate type
42    Dummy = 0x00,
43
44    /// Elliptic curve point
45    EcPoint = 0x01,
46
47    /// Elliptic curve fixed point (a constant)
48    EcFixedPoint = 0x02,
49
50    /// Elliptic curve fixed point short
51    EcFixedPointShort = 0x03,
52
53    /// Elliptic curve fixed point in base field
54    EcFixedPointBase = 0x04,
55
56    /// Elliptic curve nonidentity point
57    EcNiPoint = 0x05,
58
59    /// Base field element
60    Base = 0x10,
61
62    /// Base field element array
63    BaseArray = 0x11,
64
65    /// Scalar field element
66    Scalar = 0x12,
67
68    /// Scalar field element array
69    ScalarArray = 0x13,
70
71    /// Merkle tree path
72    MerklePath = 0x20,
73
74    /// Sparse merkle tree path
75    SparseMerklePath = 0x21,
76
77    /// Unsigned 32-bit integer
78    Uint32 = 0x30,
79
80    /// Unsigned 64-bit integer
81    Uint64 = 0x31,
82
83    /// Catch-all for any type
84    Any = 0xff,
85}
86
87impl VarType {
88    pub fn from_repr(b: u8) -> Option<Self> {
89        match b {
90            0x01 => Some(Self::EcPoint),
91            0x02 => Some(Self::EcFixedPoint),
92            0x03 => Some(Self::EcFixedPointShort),
93            0x04 => Some(Self::EcFixedPointBase),
94            0x05 => Some(Self::EcNiPoint),
95            0x10 => Some(Self::Base),
96            0x11 => Some(Self::BaseArray),
97            0x12 => Some(Self::Scalar),
98            0x13 => Some(Self::ScalarArray),
99            0x20 => Some(Self::MerklePath),
100            0x21 => Some(Self::SparseMerklePath),
101            0x30 => Some(Self::Uint32),
102            0x31 => Some(Self::Uint64),
103            0xff => Some(Self::Any),
104            _ => None,
105        }
106    }
107
108    pub fn name(&self) -> &'static str {
109        match self {
110            Self::Dummy => "Dummy",
111            Self::EcPoint => "EcPoint",
112            Self::EcFixedPoint => "EcFixedPoint",
113            Self::EcFixedPointShort => "EcFixedPointShort",
114            Self::EcFixedPointBase => "EcFixedPointBase",
115            Self::EcNiPoint => "EcNiPoint",
116            Self::Base => "Base",
117            Self::BaseArray => "BaseArray",
118            Self::Scalar => "Scalar",
119            Self::ScalarArray => "ScalarArray",
120            Self::MerklePath => "MerklePath",
121            Self::SparseMerklePath => "SparseMerklePath",
122            Self::Uint32 => "Uint32",
123            Self::Uint64 => "Uint64",
124            Self::Any => "Any",
125        }
126    }
127}
128
129/// Literal types supported by the zkas VM
130#[derive(Copy, Clone, PartialEq, Eq, Debug)]
131#[repr(u8)]
132pub enum LitType {
133    /// Dummy intermediate type
134    Dummy = 0x00,
135
136    /// Unsigned 64-bit integer
137    Uint64 = 0x01,
138}
139
140impl LitType {
141    pub fn from_repr(b: u8) -> Option<Self> {
142        match b {
143            0x01 => Some(Self::Uint64),
144            _ => None,
145        }
146    }
147
148    pub fn to_vartype(&self) -> VarType {
149        match self {
150            Self::Dummy => VarType::Dummy,
151            Self::Uint64 => VarType::Uint64,
152        }
153    }
154}