vm_serial.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. use crate::error::{Error, Result};
  2. use crate::serial::{Decodable, Encodable, ReadExt, VarInt};
  3. use crate::vm::{
  4. AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZKVMCircuit,
  5. ZKVirtualMachine,
  6. };
  7. use crate::{impl_vec, ZKContract};
  8. use std::collections::HashMap;
  9. use std::io;
  10. impl_vec!((String, VariableIndex));
  11. impl Encodable for ZKContract {
  12. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  13. unimplemented!();
  14. Ok(0)
  15. }
  16. }
  17. impl Decodable for ZKContract {
  18. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  19. Ok(Self {
  20. name: Decodable::decode(&mut d)?,
  21. vm: ZKVirtualMachine {
  22. constants: Decodable::decode(&mut d)?,
  23. alloc: Decodable::decode(&mut d)?,
  24. ops: Decodable::decode(&mut d)?,
  25. constraints: Decodable::decode(&mut d)?,
  26. aux: Vec::new(),
  27. params: None,
  28. verifying_key: None,
  29. },
  30. params_map: Vec::<(String, VariableIndex)>::decode(&mut d)?
  31. .into_iter()
  32. .collect(),
  33. public_map: Vec::<(String, VariableIndex)>::decode(&mut d)?
  34. .into_iter()
  35. .collect(),
  36. params: HashMap::new(),
  37. })
  38. }
  39. }
  40. impl Encodable for (AllocType, VariableIndex) {
  41. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  42. //let len = self.x.encode(&mut s)?;
  43. //Ok(len + self.y.encode(s)?)
  44. unimplemented!();
  45. Ok(0)
  46. }
  47. }
  48. impl Decodable for (AllocType, VariableIndex) {
  49. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  50. let type_val = ReadExt::read_u8(&mut d)?;
  51. assert!(type_val == 0 || type_val == 1);
  52. let alloc_type = if type_val == 0 {
  53. AllocType::Private
  54. } else {
  55. AllocType::Public
  56. };
  57. Ok((alloc_type, ReadExt::read_u32(&mut d)? as usize))
  58. }
  59. }
  60. impl_vec!((AllocType, VariableIndex));
  61. impl Encodable for VariableIndex {
  62. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  63. unimplemented!();
  64. Ok(0)
  65. }
  66. }
  67. impl Decodable for VariableIndex {
  68. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  69. Ok(ReadExt::read_u32(&mut d)? as Self)
  70. }
  71. }
  72. impl Encodable for VariableRef {
  73. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  74. unimplemented!();
  75. Ok(0)
  76. }
  77. }
  78. impl Decodable for VariableRef {
  79. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  80. let arg_type = ReadExt::read_u8(&mut d)?;
  81. match arg_type {
  82. 0 => Ok(Self::Aux(Decodable::decode(&mut d)?)),
  83. 1 => Ok(Self::Local(Decodable::decode(&mut d)?)),
  84. _ => Err(Error::BadVariableRefType),
  85. }
  86. }
  87. }
  88. impl Encodable for CryptoOperation {
  89. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  90. unimplemented!();
  91. Ok(0)
  92. }
  93. }
  94. impl Decodable for CryptoOperation {
  95. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  96. let op_type = ReadExt::read_u8(&mut d)?;
  97. match op_type {
  98. 0 => Ok(Self::Set(
  99. Decodable::decode(&mut d)?,
  100. Decodable::decode(&mut d)?,
  101. )),
  102. 1 => Ok(Self::Mul(
  103. Decodable::decode(&mut d)?,
  104. Decodable::decode(&mut d)?,
  105. )),
  106. 2 => Ok(Self::Add(
  107. Decodable::decode(&mut d)?,
  108. Decodable::decode(&mut d)?,
  109. )),
  110. 3 => Ok(Self::Sub(
  111. Decodable::decode(&mut d)?,
  112. Decodable::decode(&mut d)?,
  113. )),
  114. 4 => Ok(Self::Divide(
  115. Decodable::decode(&mut d)?,
  116. Decodable::decode(&mut d)?,
  117. )),
  118. 5 => Ok(Self::Double(Decodable::decode(&mut d)?)),
  119. 6 => Ok(Self::Square(Decodable::decode(&mut d)?)),
  120. 7 => Ok(Self::Invert(Decodable::decode(&mut d)?)),
  121. 8 => Ok(Self::UnpackBits(
  122. Decodable::decode(&mut d)?,
  123. Decodable::decode(&mut d)?,
  124. Decodable::decode(&mut d)?,
  125. )),
  126. 9 => Ok(Self::Local),
  127. 10 => Ok(Self::Load(
  128. Decodable::decode(&mut d)?,
  129. Decodable::decode(&mut d)?,
  130. )),
  131. 11 => Ok(Self::Debug(
  132. Decodable::decode(&mut d)?,
  133. Decodable::decode(&mut d)?,
  134. )),
  135. 12 => Ok(Self::DumpAlloc),
  136. 13 => Ok(Self::DumpLocal),
  137. i => Err(Error::BadOperationType),
  138. }
  139. }
  140. }
  141. impl_vec!(CryptoOperation);
  142. impl Encodable for ConstraintInstruction {
  143. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  144. unimplemented!();
  145. Ok(0)
  146. }
  147. }
  148. impl Decodable for ConstraintInstruction {
  149. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  150. let constraint_type = ReadExt::read_u8(&mut d)?;
  151. match constraint_type {
  152. 0 => Ok(Self::Lc0Add(Decodable::decode(&mut d)?)),
  153. 1 => Ok(Self::Lc1Add(Decodable::decode(&mut d)?)),
  154. 2 => Ok(Self::Lc2Add(Decodable::decode(&mut d)?)),
  155. 3 => Ok(Self::Lc0Sub(Decodable::decode(&mut d)?)),
  156. 4 => Ok(Self::Lc1Sub(Decodable::decode(&mut d)?)),
  157. 5 => Ok(Self::Lc2Sub(Decodable::decode(&mut d)?)),
  158. 6 => Ok(Self::Lc0AddOne),
  159. 7 => Ok(Self::Lc1AddOne),
  160. 8 => Ok(Self::Lc2AddOne),
  161. 9 => Ok(Self::Lc0SubOne),
  162. 10 => Ok(Self::Lc1SubOne),
  163. 11 => Ok(Self::Lc2SubOne),
  164. 12 => Ok(Self::Lc0AddCoeff(
  165. Decodable::decode(&mut d)?,
  166. Decodable::decode(&mut d)?,
  167. )),
  168. 13 => Ok(Self::Lc1AddCoeff(
  169. Decodable::decode(&mut d)?,
  170. Decodable::decode(&mut d)?,
  171. )),
  172. 14 => Ok(Self::Lc2AddCoeff(
  173. Decodable::decode(&mut d)?,
  174. Decodable::decode(&mut d)?,
  175. )),
  176. 15 => Ok(Self::Lc0AddOneCoeff(Decodable::decode(&mut d)?)),
  177. 16 => Ok(Self::Lc1AddOneCoeff(Decodable::decode(&mut d)?)),
  178. 17 => Ok(Self::Lc2AddOneCoeff(Decodable::decode(&mut d)?)),
  179. 18 => Ok(Self::Enforce),
  180. 19 => Ok(Self::LcCoeffReset),
  181. 20 => Ok(Self::LcCoeffDouble),
  182. _ => Err(Error::BadConstraintType),
  183. }
  184. }
  185. }
  186. impl_vec!(ConstraintInstruction);