opcode.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. use super::types::Type;
  2. /// Opcodes supported by the VM
  3. #[derive(Copy, Clone, Debug)]
  4. #[repr(u8)]
  5. pub enum Opcode {
  6. /// Elliptic curve addition
  7. EcAdd = 0x00,
  8. /// Elliptic curve multiplication
  9. EcMul = 0x01,
  10. /// Elliptic curve multiplication with a Base field element
  11. EcMulBase = 0x02,
  12. /// Elliptic curve multiplication with a u64 wrapped in a Scalar element
  13. EcMulShort = 0x03,
  14. /// Get the x coordinate of an elliptic curve point
  15. EcGetX = 0x08,
  16. /// Get the y coordinate of an elliptic curve point
  17. EcGetY = 0x09,
  18. /// Poseidon hash of N elements
  19. PoseidonHash = 0x10,
  20. /// Calculate merkle root given a position, Merkle path, and an element
  21. CalculateMerkleRoot = 0x20,
  22. /// Base field element addition
  23. BaseAdd = 0x30,
  24. /// Base field element multiplication
  25. BaseMul = 0x31,
  26. /// Base field element subtraction
  27. BaseSub = 0x32,
  28. /// Base field greater than comparison
  29. GreaterThan = 0x33,
  30. /// Constrain a Base field element to a circuit's public input
  31. ConstrainInstance = 0xf0,
  32. /// Intermediate opcode for the compiler, should never appear in the result
  33. Noop = 0xff,
  34. }
  35. impl Opcode {
  36. /// Return a tuple of vectors of types that are accepted by a specific opcode
  37. /// `r.0` is the return type(s) and `r.1` is the argument type(s).
  38. pub fn arg_types(&self) -> (Vec<Type>, Vec<Type>) {
  39. match self {
  40. // (return_type, opcode_arg_types)
  41. Opcode::EcAdd => (vec![Type::EcPoint], vec![Type::EcPoint, Type::EcPoint]),
  42. Opcode::EcMul => (vec![Type::EcPoint], vec![Type::Scalar, Type::EcFixedPoint]),
  43. Opcode::EcMulBase => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPointBase]),
  44. Opcode::EcMulShort => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPointShort]),
  45. Opcode::EcGetX => (vec![Type::Base], vec![Type::EcPoint]),
  46. Opcode::EcGetY => (vec![Type::Base], vec![Type::EcPoint]),
  47. Opcode::PoseidonHash => (vec![Type::Base], vec![Type::BaseArray]),
  48. Opcode::CalculateMerkleRoot => {
  49. (vec![Type::Base], vec![Type::Uint32, Type::MerklePath, Type::Base])
  50. }
  51. Opcode::BaseAdd => (vec![Type::Base], vec![Type::Base, Type::Base]),
  52. Opcode::BaseMul => (vec![Type::Base], vec![Type::Base, Type::Base]),
  53. Opcode::BaseSub => (vec![Type::Base], vec![Type::Base, Type::Base]),
  54. Opcode::GreaterThan => (vec![Type::Base], vec![Type::Base, Type::Base]),
  55. Opcode::ConstrainInstance => (vec![], vec![Type::Base]),
  56. Opcode::Noop => (vec![], vec![]),
  57. }
  58. }
  59. pub fn from_repr(b: u8) -> Self {
  60. match b {
  61. 0x00 => Self::EcAdd,
  62. 0x01 => Self::EcMul,
  63. 0x02 => Self::EcMulBase,
  64. 0x03 => Self::EcMulShort,
  65. 0x08 => Self::EcGetX,
  66. 0x09 => Self::EcGetY,
  67. 0x10 => Self::PoseidonHash,
  68. 0x20 => Self::CalculateMerkleRoot,
  69. 0x30 => Self::BaseAdd,
  70. 0x31 => Self::BaseMul,
  71. 0x32 => Self::BaseSub,
  72. 0x33 => Self::GreaterThan,
  73. 0xf0 => Self::ConstrainInstance,
  74. _ => unimplemented!(),
  75. }
  76. }
  77. }