vm_serial.rs 7.3 KB

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