opcode.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use crate::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 given a position, Merkle path, and an element
  21. CalculateMerkleRoot = 0x20,
  22. /// Constrain a Base field element to a circuit's public input
  23. ConstrainInstance = 0xf0,
  24. /// Intermediate opcode for the compiler, should never appear in the result
  25. Noop = 0xff,
  26. }
  27. impl Opcode {
  28. /// Return a tuple of vectors of types that are accepted by a specific opcode
  29. /// `r.0` is the return type(s) and `r.1` is the argument type(s).
  30. pub fn arg_types(&self) -> (Vec<Type>, Vec<Type>) {
  31. match self {
  32. // (return_type, opcode_arg_types)
  33. Opcode::EcAdd => (vec![Type::EcPoint], vec![Type::EcPoint, Type::EcPoint]),
  34. Opcode::EcMul => (vec![Type::EcPoint], vec![Type::Scalar, Type::EcFixedPoint]),
  35. Opcode::EcMulBase => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPoint]),
  36. Opcode::EcMulShort => (vec![Type::EcPoint], vec![Type::Base, Type::EcFixedPoint]),
  37. Opcode::EcGetX => (vec![Type::Base], vec![Type::EcPoint]),
  38. Opcode::EcGetY => (vec![Type::Base], vec![Type::EcPoint]),
  39. Opcode::PoseidonHash => (vec![Type::Base], vec![Type::BaseArray]),
  40. Opcode::CalculateMerkleRoot => {
  41. (vec![Type::Base], vec![Type::Uint32, Type::MerklePath, Type::Base])
  42. }
  43. Opcode::ConstrainInstance => (vec![], vec![Type::Base]),
  44. Opcode::Noop => (vec![], vec![]),
  45. }
  46. }
  47. }