vm_serial.rs 7.3 KB

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