vm2_serial.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. use std::io;
  2. use crate::error::{Error, Result};
  3. use crate::impl_vec;
  4. use crate::serial::{Decodable, Encodable, ReadExt, VarInt};
  5. use crate::vm2::{ZkBinary, ZkContract, ZkFunctionCall, ZkType};
  6. impl_vec!((String, ZkType));
  7. impl_vec!(ZkFunctionCall);
  8. impl_vec!((String, ZkContract));
  9. impl Encodable for ZkType {
  10. fn encode<S: io::Write>(&self, _s: S) -> Result<usize> {
  11. unimplemented!();
  12. //Ok(0)
  13. }
  14. }
  15. impl Decodable for ZkType {
  16. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  17. let op_type = ReadExt::read_u8(&mut d)?;
  18. match op_type {
  19. 0 => Ok(Self::Base),
  20. 1 => Ok(Self::Scalar),
  21. 2 => Ok(Self::EcPoint),
  22. 3 => Ok(Self::EcFixedPoint),
  23. 4 => Ok(Self::MerklePath),
  24. _i => Err(Error::BadOperationType),
  25. }
  26. }
  27. }
  28. impl Encodable for ZkFunctionCall {
  29. fn encode<S: io::Write>(&self, _s: S) -> Result<usize> {
  30. unimplemented!();
  31. //Ok(0)
  32. }
  33. }
  34. impl Decodable for ZkFunctionCall {
  35. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  36. let func_id = ReadExt::read_u8(&mut d)?;
  37. match func_id {
  38. 0 => Ok(Self::PoseidonHash(
  39. ReadExt::read_u32(&mut d)? as usize,
  40. ReadExt::read_u32(&mut d)? as usize,
  41. )),
  42. 1 => Ok(Self::Add(
  43. ReadExt::read_u32(&mut d)? as usize,
  44. ReadExt::read_u32(&mut d)? as usize,
  45. )),
  46. 2 => Ok(Self::ConstrainInstance(ReadExt::read_u32(&mut d)? as usize)),
  47. 3 => Ok(Self::EcMulShort(
  48. ReadExt::read_u32(&mut d)? as usize,
  49. ReadExt::read_u32(&mut d)? as usize,
  50. )),
  51. 4 => Ok(Self::EcMul(
  52. ReadExt::read_u32(&mut d)? as usize,
  53. ReadExt::read_u32(&mut d)? as usize,
  54. )),
  55. 5 => Ok(Self::EcAdd(
  56. ReadExt::read_u32(&mut d)? as usize,
  57. ReadExt::read_u32(&mut d)? as usize,
  58. )),
  59. 6 => Ok(Self::EcGetX(ReadExt::read_u32(&mut d)? as usize)),
  60. 7 => Ok(Self::EcGetY(ReadExt::read_u32(&mut d)? as usize)),
  61. 8 => Ok(Self::CalculateMerkleRoot(
  62. ReadExt::read_u32(&mut d)? as usize,
  63. ReadExt::read_u32(&mut d)? as usize,
  64. )),
  65. _i => Err(Error::BadOperationType),
  66. }
  67. }
  68. }
  69. impl Encodable for ZkBinary {
  70. fn encode<S: io::Write>(&self, _s: S) -> Result<usize> {
  71. unimplemented!();
  72. //Ok(0)
  73. }
  74. }
  75. impl Decodable for ZkBinary {
  76. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  77. Ok(Self {
  78. constants: Decodable::decode(&mut d)?,
  79. contracts: Vec::<(String, ZkContract)>::decode(&mut d)?
  80. .into_iter()
  81. .collect(),
  82. })
  83. }
  84. }
  85. impl Encodable for ZkContract {
  86. fn encode<S: io::Write>(&self, _s: S) -> Result<usize> {
  87. unimplemented!();
  88. //Ok(0)
  89. }
  90. }
  91. impl Decodable for ZkContract {
  92. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  93. Ok(Self {
  94. witness: Decodable::decode(&mut d)?,
  95. code: Decodable::decode(&mut d)?,
  96. })
  97. }
  98. }