types.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use lazy_static::lazy_static;
  2. use std::collections::HashMap;
  3. #[derive(Debug, Clone, Copy, PartialEq)]
  4. pub enum TypeId {
  5. Base,
  6. Scalar,
  7. EcPoint,
  8. EcFixedPoint,
  9. LastId,
  10. }
  11. #[derive(Debug, Clone)]
  12. pub enum FuncId {
  13. PoseidonHash,
  14. Add,
  15. ConstrainInstance,
  16. EcMulShort,
  17. EcMul,
  18. EcAdd,
  19. EcGetX,
  20. EcGetY,
  21. }
  22. lazy_static! {
  23. pub static ref ALLOWED_TYPES: HashMap<&'static str, TypeId> = {
  24. let mut map = HashMap::new();
  25. map.insert("Base", TypeId::Base);
  26. map.insert("Scalar", TypeId::Scalar);
  27. map.insert("EcFixedPoint", TypeId::EcFixedPoint);
  28. map
  29. };
  30. }
  31. #[derive(Debug, Clone)]
  32. pub struct FuncFormat {
  33. pub func_id: FuncId,
  34. pub return_type_ids: Vec<TypeId>,
  35. pub param_types: Vec<TypeId>,
  36. }
  37. impl FuncFormat {
  38. pub fn new(func_id: FuncId, return_type_ids: &[TypeId], param_types: &[TypeId]) -> Self {
  39. FuncFormat {
  40. func_id,
  41. return_type_ids: return_type_ids.to_vec(),
  42. param_types: param_types.to_vec(),
  43. }
  44. }
  45. pub fn total_arguments(&self) -> usize {
  46. self.return_type_ids.len() + self.param_types.len()
  47. }
  48. }
  49. lazy_static! {
  50. pub static ref FUNCTION_FORMATS: HashMap<&'static str, FuncFormat> = {
  51. let mut map = HashMap::new();
  52. map.insert(
  53. "poseidon_hash",
  54. FuncFormat::new(
  55. FuncId::PoseidonHash,
  56. &[TypeId::Base],
  57. &[TypeId::Base, TypeId::Base],
  58. ),
  59. );
  60. map.insert(
  61. "add",
  62. FuncFormat::new(FuncId::Add, &[TypeId::Base], &[TypeId::Base, TypeId::Base]),
  63. );
  64. map.insert(
  65. "constrain_instance",
  66. FuncFormat::new(FuncId::ConstrainInstance, &[], &[TypeId::Base]),
  67. );
  68. map.insert(
  69. "ec_mul_short",
  70. FuncFormat::new(
  71. FuncId::EcMulShort,
  72. &[TypeId::EcPoint],
  73. &[TypeId::Base, TypeId::EcFixedPoint],
  74. ),
  75. );
  76. map.insert(
  77. "ec_mul",
  78. FuncFormat::new(
  79. FuncId::EcMul,
  80. &[TypeId::EcPoint],
  81. &[TypeId::Scalar, TypeId::EcFixedPoint],
  82. ),
  83. );
  84. map.insert(
  85. "ec_add",
  86. FuncFormat::new(
  87. FuncId::EcAdd,
  88. &[TypeId::EcPoint],
  89. &[TypeId::EcPoint, TypeId::EcPoint],
  90. ),
  91. );
  92. map.insert(
  93. "ec_get_x",
  94. FuncFormat::new(FuncId::EcGetX, &[TypeId::Base], &[TypeId::EcPoint]),
  95. );
  96. map.insert(
  97. "ec_get_y",
  98. FuncFormat::new(FuncId::EcGetY, &[TypeId::Base], &[TypeId::EcPoint]),
  99. );
  100. map
  101. };
  102. }