vm_serial.rs 7.4 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, 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))
  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. let len = Encodable::encode(&((*self) as u64), s)?;
  100. Ok(len)
  101. }
  102. }
  103. impl Decodable for VariableIndex {
  104. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  105. let val: u64 = Decodable::decode(&mut d)?;
  106. Ok(val 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);