opcode.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. use super::VarType;
  19. /// Opcodes supported by the zkas VM
  20. #[derive(Copy, Clone, Debug)]
  21. #[repr(u8)]
  22. pub enum Opcode {
  23. /// Intermediate opcode for the compiler, should never appear in the result
  24. Noop = 0x00,
  25. /// Elliptic curve addition
  26. EcAdd = 0x01,
  27. /// Elliptic curve multiplication
  28. EcMul = 0x02,
  29. /// Elliptic curve multiplication with a Base field element
  30. EcMulBase = 0x03,
  31. /// Elliptic curve multiplication with a Base field element of 64bit width
  32. EcMulShort = 0x04,
  33. /// Variable Elliptic curve multiplication with a Base field element
  34. EcMulVarBase = 0x05,
  35. /// Get the x coordinate of an elliptic curve point
  36. EcGetX = 0x08,
  37. /// Get the y coordinate of an elliptic curve point
  38. EcGetY = 0x09,
  39. /// Poseidon hash of N Base field elements
  40. PoseidonHash = 0x10,
  41. /// Calculate Merkle root, given a position, Merkle path, and an element
  42. MerkleRoot = 0x20,
  43. /// Base field element addition
  44. BaseAdd = 0x30,
  45. /// Base field element multiplication
  46. BaseMul = 0x31,
  47. /// Base field element subtraction
  48. BaseSub = 0x32,
  49. /// Witness an unsigned integer into a Base field element
  50. WitnessBase = 0x40,
  51. /// Range check a Base field element, given bit-width (up to 253)
  52. RangeCheck = 0x50,
  53. /// Strictly compare two Base field elements and see if a is less than b
  54. /// This enforces the sum of remaining bits to be zero.
  55. LessThanStrict = 0x51,
  56. /// Loosely two Base field elements and see if a is less than b
  57. /// This does not enforce the sum of remaining bits to be zero.
  58. LessThanLoose = 0x52,
  59. /// Check if a field element fits in a boolean (Either 0 or 1)
  60. BoolCheck = 0x53,
  61. /// Constrain equality of two Base field elements inside the circuit
  62. ConstrainEqualBase = 0xe0,
  63. /// Constrain equality of two EcPoint elements inside the circuit
  64. ConstrainEqualPoint = 0xe1,
  65. /// Constrain a Base field element to a circuit's public input
  66. ConstrainInstance = 0xf0,
  67. /// Debug a variable's value in the ZK circuit table.
  68. DebugPrint = 0xff,
  69. }
  70. impl Opcode {
  71. pub fn from_name(n: &str) -> Option<Self> {
  72. match n {
  73. "ec_add" => Some(Self::EcAdd),
  74. "ec_mul" => Some(Self::EcMul),
  75. "ec_mul_base" => Some(Self::EcMulBase),
  76. "ec_mul_short" => Some(Self::EcMulShort),
  77. "ec_mul_var_base" => Some(Self::EcMulVarBase),
  78. "ec_get_x" => Some(Self::EcGetX),
  79. "ec_get_y" => Some(Self::EcGetY),
  80. "poseidon_hash" => Some(Self::PoseidonHash),
  81. "merkle_root" => Some(Self::MerkleRoot),
  82. "base_add" => Some(Self::BaseAdd),
  83. "base_mul" => Some(Self::BaseMul),
  84. "base_sub" => Some(Self::BaseSub),
  85. "witness_base" => Some(Self::WitnessBase),
  86. "range_check" => Some(Self::RangeCheck),
  87. "less_than_strict" => Some(Self::LessThanStrict),
  88. "less_than_loose" => Some(Self::LessThanLoose),
  89. "bool_check" => Some(Self::BoolCheck),
  90. "constrain_equal_base" => Some(Self::ConstrainEqualBase),
  91. "constrain_equal_point" => Some(Self::ConstrainEqualPoint),
  92. "constrain_instance" => Some(Self::ConstrainInstance),
  93. "debug" => Some(Self::DebugPrint),
  94. _ => None,
  95. }
  96. }
  97. pub fn from_repr(b: u8) -> Option<Self> {
  98. match b {
  99. 0x01 => Some(Self::EcAdd),
  100. 0x02 => Some(Self::EcMul),
  101. 0x03 => Some(Self::EcMulBase),
  102. 0x04 => Some(Self::EcMulShort),
  103. 0x05 => Some(Self::EcMulVarBase),
  104. 0x08 => Some(Self::EcGetX),
  105. 0x09 => Some(Self::EcGetY),
  106. 0x10 => Some(Self::PoseidonHash),
  107. 0x20 => Some(Self::MerkleRoot),
  108. 0x30 => Some(Self::BaseAdd),
  109. 0x31 => Some(Self::BaseMul),
  110. 0x32 => Some(Self::BaseSub),
  111. 0x40 => Some(Self::WitnessBase),
  112. 0x50 => Some(Self::RangeCheck),
  113. 0x51 => Some(Self::LessThanStrict),
  114. 0x52 => Some(Self::LessThanLoose),
  115. 0x53 => Some(Self::BoolCheck),
  116. 0xe0 => Some(Self::ConstrainEqualBase),
  117. 0xe1 => Some(Self::ConstrainEqualPoint),
  118. 0xf0 => Some(Self::ConstrainInstance),
  119. 0xff => Some(Self::DebugPrint),
  120. _ => None,
  121. }
  122. }
  123. /// Return a tuple of vectors of types that are accepted by a specific opcode.
  124. /// `r.0` is the return type(s), and `r.1` is the argument type(s).
  125. pub fn arg_types(&self) -> (Vec<VarType>, Vec<VarType>) {
  126. match self {
  127. Opcode::Noop => (vec![], vec![]),
  128. Opcode::EcAdd => (vec![VarType::EcPoint], vec![VarType::EcPoint, VarType::EcPoint]),
  129. Opcode::EcMul => (vec![VarType::EcPoint], vec![VarType::Scalar, VarType::EcFixedPoint]),
  130. Opcode::EcMulBase => {
  131. (vec![VarType::EcPoint], vec![VarType::Base, VarType::EcFixedPointBase])
  132. }
  133. Opcode::EcMulShort => {
  134. (vec![VarType::EcPoint], vec![VarType::Base, VarType::EcFixedPointShort])
  135. }
  136. Opcode::EcMulVarBase => {
  137. (vec![VarType::EcPoint], vec![VarType::Base, VarType::EcNiPoint])
  138. }
  139. Opcode::EcGetX => (vec![VarType::Base], vec![VarType::EcPoint]),
  140. Opcode::EcGetY => (vec![VarType::Base], vec![VarType::EcPoint]),
  141. Opcode::PoseidonHash => (vec![VarType::Base], vec![VarType::BaseArray]),
  142. Opcode::MerkleRoot => {
  143. (vec![VarType::Base], vec![VarType::Uint32, VarType::MerklePath, VarType::Base])
  144. }
  145. Opcode::BaseAdd => (vec![VarType::Base], vec![VarType::Base, VarType::Base]),
  146. Opcode::BaseMul => (vec![VarType::Base], vec![VarType::Base, VarType::Base]),
  147. Opcode::BaseSub => (vec![VarType::Base], vec![VarType::Base, VarType::Base]),
  148. Opcode::WitnessBase => (vec![VarType::Base], vec![VarType::Uint64]),
  149. Opcode::RangeCheck => (vec![], vec![VarType::Uint64, VarType::Base]),
  150. Opcode::LessThanStrict => (vec![], vec![VarType::Base, VarType::Base]),
  151. Opcode::LessThanLoose => (vec![], vec![VarType::Base, VarType::Base]),
  152. Opcode::BoolCheck => (vec![], vec![VarType::Base]),
  153. Opcode::ConstrainEqualBase => (vec![], vec![VarType::Base, VarType::Base]),
  154. Opcode::ConstrainEqualPoint => (vec![], vec![VarType::EcPoint, VarType::EcPoint]),
  155. Opcode::ConstrainInstance => (vec![], vec![VarType::Base]),
  156. Opcode::DebugPrint => (vec![], vec![]),
  157. }
  158. }
  159. }