vm_serial.rs 3.0 KB

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