You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

654 lines
39 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # Binary format for R1CS
  2. ---
  3. eip:
  4. title: r1cs binary format
  5. author: Jordi Baylina <jordi@baylina.cat>
  6. discussions-to:
  7. status: draft
  8. type: Standards Track
  9. category: ERC
  10. created: 2019-09-24
  11. requires:
  12. ---
  13. ## Simple Summary
  14. This standard defines a standard format for a binery representation of a r1cs constraint system.
  15. ## Abstract
  16. ## Motivation
  17. The zero knowledge primitives, requires the definition of a statment that wants to be proved. This statment can be expressed as a deterministric program or an algebraic circuit. Lots of primitives like zkSnarks, bulletProofs or aurora, requires to convert this statment to a rank-one constraint system.
  18. This standard specifies a format for a r1cs and allows the to connect a set of tools that compiles a program or a circuit to r1cs that can be used for the zksnarks or bulletproofs primitives.
  19. ## Specification
  20. ### General considerations
  21. The standard extension is `.r1cs`
  22. A deterministic program (or circuit) is a program that generates a set of deterministic values given an input. All those values are labeled from l_{0} to l_{n_labels}
  23. This file defines a map beween l_{i} -> w_{j} and defines a series a R1CS of the form
  24. $$
  25. \left\{ \begin{array}{rclclcl}
  26. (a_{0,0}w_0 + a_{0,1}w_1 + ... + a_{0,n}w_{n}) &\cdot& (b_{0,0} w_0 + b_{0,1} w_1 + ... + b_{0,n} w_{n}) &-& (c_{0,0} w_0 + c_{0,1} w_1 + ... + c_{0,n}w_{n}) &=& 0 \\
  27. (a_{1,0}w_0 + a_{1,1}w_1 + ... + a_{1,n}w_{n}) &\cdot& (b_{1,0} w_0 + b_{1,1} w_1 + ... + b_{1,n} w_{n}) &-& (c_{1,0} w_0 + c_{1,1}w_1 + ... + c_{1,n}w_{n}) &=& 0 \\
  28. ...\\
  29. (a_{m-1,0}w_0 + a_{m-1,1}w_1 + ... + a_{m-1,n}w_{n}) &\cdot& (b_{m-1,0} w_0 + b_{m-1,1} w_1 + ... + b_{m-1,n} w_{n}) &-& (c_{m-1,0} w_0 + c_{m-1,1}w_1 + ... + c_{m-1,n}w_{n}) &=& 0
  30. \end{array} \right.
  31. $$
  32. Wire 0 must be always mapped to label 0 and it's an input forced to value "1" implicitly
  33. ### Format of the file
  34. ````
  35. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  36. ┃ 4 │ 72 31 63 73 ┃ Magic "r1cs"
  37. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  38. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  39. ┃ 4 │ 01 00 00 00 ┃ Version 1
  40. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  41. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  42. ┃ 4 │ 03 00 00 00 ┃ Number of Sections
  43. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  44. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
  45. ┃ 4 │ sectionType ┃ 8 │ SectionSize ┃
  46. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛
  47. ┏━━━━━━━━━━━━━━━━━━━━━┓
  48. ┃ ┃
  49. ┃ ┃
  50. ┃ ┃
  51. ┃ Section Content ┃
  52. ┃ ┃
  53. ┃ ┃
  54. ┃ ┃
  55. ┗━━━━━━━━━━━━━━━━━━━━━┛
  56. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
  57. ┃ 4 │ sectionType ┃ 8 │ SectionSize ┃
  58. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛
  59. ┏━━━━━━━━━━━━━━━━━━━━━┓
  60. ┃ ┃
  61. ┃ ┃
  62. ┃ ┃
  63. ┃ Section Content ┃
  64. ┃ ┃
  65. ┃ ┃
  66. ┃ ┃
  67. ┗━━━━━━━━━━━━━━━━━━━━━┛
  68. ...
  69. ...
  70. ...
  71. ````
  72. #### Magic Number
  73. Size: 4 bytes
  74. The file start with a constant 4 bytes (magic number) "r1cs"
  75. ```
  76. 0x72 0x31 0x63 0x73
  77. ```
  78. #### Version
  79. Size: 4 bytes
  80. Format: Little-Endian
  81. For this standard it's fixed to
  82. ```
  83. 0x01 0x00 0x00 0x00
  84. ```
  85. #### Number of Sections
  86. Size: 4 bytes
  87. Format: Little-Endian
  88. Number of sections contained in the file
  89. #### SectionType
  90. Size: 4 bytes
  91. Format: Little-Endian
  92. Type of the section.
  93. Currently there are 3 types of sections defined:
  94. * 0x00000001 : Header Section
  95. * 0x00000002 : Constraint Section
  96. * 0x00000003 : Wire2LabelId Map Section
  97. If the file contain other types, the format is valid, but they MUST be ignored.
  98. Any order of the section must be accepted.
  99. Example:
  100. ```
  101. 0x01 0x00 0x00 0x00
  102. ```
  103. #### SectionSize
  104. Size: `ws` bytes
  105. Format: Little-Endian
  106. Size in bytes of the section
  107. ### Header Section
  108. Section Type: 0x01
  109. ````
  110. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  111. ┃ 4 │ 20 00 00 00 ┃ Field Size in bytes (fs)
  112. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  113. ┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  114. ┃ fs │ 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430 ┃ Prime size
  115. ┗━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  116. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  117. ┃ 32 │ 01 00 00 00 ┃ nWires
  118. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  119. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  120. ┃ 32 │ 01 00 00 00 ┃ nPubOut
  121. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  122. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  123. ┃ 32 │ 01 00 00 00 ┃ nPubIn
  124. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  125. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  126. ┃ 32 │ 01 00 00 00 ┃ nPrvIn
  127. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  128. ┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  129. ┃ 64 │ 01 00 00 00 00 00 00 00 ┃ nLabels
  130. ┗━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  131. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  132. ┃ 32 │ 01 00 00 00 ┃ mConstraints
  133. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  134. ````
  135. #### field Size (fs)
  136. Size: 4 bytes
  137. Format: Little-Endian
  138. Size in bytes of a field element. Mast be a multiple of 8.
  139. Example:
  140. ```
  141. 0x00 0x0 0x00 0x00
  142. ```
  143. #### Prime
  144. Prime Number of the field
  145. Example:
  146. ```
  147. 0x010000f0_93f5e143_9170b979_48e83328_5d588181_b64550b8_29a031e1_724e6430
  148. ```
  149. #### Number of wires
  150. Size: 4 bytes
  151. Format: Little-Endian
  152. Total Number of wires including ONE signal (Index 0).
  153. #### Number of public outputs
  154. Size: 4 bytes
  155. Format: Little-Endian
  156. Total Number of wires public output wires. They should be starting at idx 1
  157. #### Number of public inputs
  158. Size: 4 bytes
  159. Format: Little-Endian
  160. Total Number of wires public input wires. They should be starting just after the public output
  161. #### Number of private inputs
  162. Size: 4 bytes
  163. Format: Little-Endian
  164. Total Number of wires private input wires. They should be starting just after the public inputs
  165. #### Number of Labels
  166. Size: 8 bytes
  167. Format: Little-Endian
  168. Total Number of wires private input wires. They should be starting just after the public inputs
  169. #### Number of constraints (m)
  170. Size: 4 bytes
  171. Format: Little-Endian
  172. Total Number of constraints
  173. ### Constraints section
  174. Section Type: 0x02
  175. ````
  176. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ ╲
  177. ┃ 32 │ nA ┃ ╲
  178. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ╲
  179. ┃ 32 │ wireId_1 ┃ fs │ a_{0,wireId_1} ┃ │
  180. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  181. ┃ 32 │ wireId_2 ┃ fs │ a_{0,wireId_2} ┃ │
  182. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  183. ... ... │
  184. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  185. ┃ 32 │ wireId_nA ┃ fs │ a_{0,wireId_nA} ┃ │
  186. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  187. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  188. ┃ 32 │ nB ┃ │
  189. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  190. ┃ 32 │ wireId_1 ┃ fs │ b_{0,wireId_1} ┃ │
  191. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ ╲
  192. ┃ 32 │ wireId_2 ┃ fs │ b_{0,wireId_2} ┃ ╲
  193. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱ Constraint_0
  194. ... ... ╱
  195. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  196. ┃ 32 │ wireId_nB ┃ fs │ b_{0,wireId_nB} ┃ │
  197. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  198. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  199. ┃ 32 │ nC ┃ │
  200. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  201. ┃ 32 │ wireId_1 ┃ fs │ c_{0,wireId_1} ┃ │
  202. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  203. ┃ 32 │ wireId_2 ┃ fs │ c_{0,wireId_2} ┃ │
  204. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  205. ... ... │
  206. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  207. ┃ 32 │ wireId_nC ┃ fs │ c_{0,wireId_nC} ┃ ╱
  208. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱
  209. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ ╲
  210. ┃ 32 │ nA ┃ ╲
  211. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ╲
  212. ┃ 32 │ wireId_1 ┃ fs │ a_{1,wireId_1} ┃ │
  213. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  214. ┃ 32 │ wireId_2 ┃ fs │ a_{1,wireId_2} ┃ │
  215. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  216. ... ... │
  217. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  218. ┃ 32 │ wireId_nA ┃ fs │ a_{1,wireId_nA} ┃ │
  219. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  220. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  221. ┃ 32 │ nB ┃ │
  222. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  223. ┃ 32 │ wireId_1 ┃ fs │ b_{1,wireId_1} ┃ │
  224. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ ╲
  225. ┃ 32 │ wireId_2 ┃ fs │ b_{1,wireId_2} ┃ ╲
  226. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱ Constraint_1
  227. ... ... ╱
  228. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  229. ┃ 32 │ wireId_nB ┃ fs │ b_{1,wireId_nB} ┃ │
  230. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  231. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  232. ┃ 32 │ nC ┃ │
  233. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  234. ┃ 32 │ wireId_1 ┃ fs │ c_{1,wireId_1} ┃ │
  235. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  236. ┃ 32 │ wireId_2 ┃ fs │ c_{1,wireId_2} ┃ │
  237. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  238. ... ... │
  239. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  240. ┃ 32 │ wireId_nC ┃ fs │ c_{1,wireId_nC} ┃ ╱
  241. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱
  242. ...
  243. ...
  244. ...
  245. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ ╲
  246. ┃ 32 │ nA ┃ ╲
  247. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ╲
  248. ┃ 32 │ wireId_1 ┃ fs │ a_{m-1,wireId_1} ┃ │
  249. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  250. ┃ 32 │ wireId_2 ┃ fs │ a_{m-1,wireId_2} ┃ │
  251. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  252. ... ... │
  253. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  254. ┃ 32 │ wireId_nA ┃ fs │ a_{m-1,wireId_nA} ┃ │
  255. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  256. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  257. ┃ 32 │ nB ┃ │
  258. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  259. ┃ 32 │ wireId_1 ┃ fs │ b_{m-1,wireId_1} ┃ │
  260. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ ╲
  261. ┃ 32 │ wireId_2 ┃ fs │ b_{m-1,wireId_2} ┃ ╲
  262. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱ Constraint_{m-1}
  263. ... ... ╱
  264. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  265. ┃ 32 │ wireId_nB ┃ fs │ b_{m-1,wireId_nB} ┃ │
  266. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  267. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  268. ┃ 32 │ nC ┃ │
  269. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  270. ┃ 32 │ wireId_1 ┃ fs │ c_{m-1,wireId_1} ┃ │
  271. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  272. ┃ 32 │ wireId_2 ┃ fs │ c_{m-1,wireId_2} ┃ │
  273. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  274. ... ... │
  275. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  276. ┃ 32 │ wireId_nC ┃ fs │ c_{m-1,wireId_nC} ┃ ╱
  277. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱
  278. ````
  279. #### Constraints
  280. Each constraint contains 3 linear combinations A, B, C.
  281. The constraint is such that:
  282. ```
  283. A*B-C = 0
  284. ```
  285. #### Linear combination
  286. Each linear combination is of the form:
  287. $$
  288. a_{j,0}w_0 + a_{j,1}w_1 + ... + a_{j,n}w_{n}
  289. $$
  290. #### Number of nonZero Factors
  291. Size: 4 bytes
  292. Format: Little-Endian
  293. Total number of non Zero factors in the linear compination.
  294. The factors MUST be sorted in ascending order.
  295. #### Factor
  296. For each factor we have the index of the factor and the value of the factor.
  297. #### WireId of the factor
  298. Size: 4 bytes
  299. Format: Little-Endian
  300. WireId of the nonZero Factor
  301. #### Value of the factor
  302. This is the factor that multiplies the associated wire in the linear convination.
  303. For example, to represent the linear combination:
  304. $$
  305. 5w_4 +8w_5 + 260w_{886}
  306. $$
  307. The linear combination would be represented as:
  308. ````
  309. ┏━━━━━━━━━━━━━━━━━┓
  310. ┃ 03 00 00 00 ┃
  311. ┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  312. ┃ 04 00 00 00 ┃ 05000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  313. ┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  314. ┃ 05 00 00 00 ┃ 08000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  315. ┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  316. ┃ 76 03 00 00 ┃ 04010000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  317. ┗━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  318. ````
  319. ### WireId2LabelId Map Section
  320. Section Type: 0x03
  321. ````
  322. ┏━━┳━━━━━━━━━━━━━━━━━━━┳━━┳━━━━━━━━━━━━━━━━━━━┓ ┏━━┳━━━━━━━━━━━━━━━━━━━┓
  323. ┃64│ labelId of Wire_0 ┃64│ labelId of Wire_1 ┃ ... ┃64│ labelId of Wire_n ┃
  324. ┗━━┻━━━━━━━━━━━━━━━━━━━┻━━┻━━━━━━━━━━━━━━━━━━━┛ ┗━━┻━━━━━━━━━━━━━━━━━━━┛
  325. ````
  326. ## Rationale
  327. Variable size for field elements allows to shrink the size of the file and allows to work with any field.
  328. Version allows to update the format.
  329. Have a very good comprasion ratio for sparse r1cs as it's the normal case.
  330. The motivation of having a map between l and w is that this allows optimizers to calculate equivalent r1cs systems but keeping the original values geneated by the circuit.
  331. ## Backward Compatibility
  332. N.A.
  333. ## Test Cases
  334. ### Example
  335. Given this r1cs in a 256 bit Field:
  336. $$
  337. \left\{ \begin{array}{rclclcl}
  338. (3w_5 + 8w_6) &\cdot& (2w_0 + 20w_2 + 12w_3) &-& (5w_0 + 7w_2) &=& 0 \\
  339. (4w_1 + 8w_4 + 3w_5) &\cdot& (6w_6 + 44w_3) && &=& 0 \\
  340. (4w_6) &\cdot& (6w_0 + 5w_3 + 11s_2) &-& (600w_6) &=& 0
  341. \end{array} \right.
  342. $$
  343. And a Wire to label map.
  344. $$
  345. w_0 := l_0 \\
  346. w_1 := l_3 \\
  347. w_2 := l_{10} \\
  348. w_3 := l_{11} \\
  349. w_4 := l_{12} \\
  350. w_5 := l_{15} \\
  351. w_6 := l_{324} \\
  352. $$
  353. The format will be:
  354. ````
  355. ┏━━━━━━━━━━┓
  356. ┃ 72316377 ┃ Magic
  357. ┣━━━━━━━━━━┫
  358. ┃ 01000000 ┃ Version
  359. ┣━━━━━━━━━━┫
  360. ┃ 03000000 ┃ nSections
  361. ┗━━━━━━━━━━┛
  362. ┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
  363. ┃ 01000000 ┃ 40000000 00000000 ┃ SectionType: Header
  364. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━┛
  365. ┏━━━━━━━━━━┓
  366. ┃ 20000000 ┃ Field Size
  367. ┣━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  368. ┃ 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430 ┃
  369. ┣━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  370. ┃ 07000000 ┃ # of wires
  371. ┣━━━━━━━━━━┫
  372. ┃ 01000000 ┃ # Public Outs
  373. ┣━━━━━━━━━━┫
  374. ┃ 02000000 ┃ # Public Ins
  375. ┣━━━━━━━━━━┫
  376. ┃ 03000000 ┃ # Private Ins
  377. ┣━━━━━━━━━━┻━━━━━━━━┓
  378. ┃ e8030000 00000000 ┃ # Labels
  379. ┣━━━━━━━━━━┳━━━━━━━━┛
  380. ┃ 03000000 ┃ # Constraints
  381. ┗━━━━━━━━━━┛
  382. ┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
  383. ┃ 02000000 ┃ 88200000 00000000 ┃ SectionType: Constraints
  384. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━┛
  385. ┏━━━━━━━━━━┓ Constraint 0: (3w_5 + 8w_6) * (2w_0 + 20w_2 + 12w_3) - (5w_0 + 7w_2) = 0
  386. ┃ 02000000 ┃
  387. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  388. ┃ 05000000 ┃ 03000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  389. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  390. ┃ 06000000 ┃ 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  391. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  392. ┏━━━━━━━━━━┓
  393. ┃ 03000000 ┃
  394. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  395. ┃ 00000000 ┃ 02000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  396. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  397. ┃ 02000000 ┃ 01140000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  398. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  399. ┃ 03000000 ┃ 0C000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  400. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  401. ┏━━━━━━━━━━┓
  402. ┃ 02000000 ┃
  403. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  404. ┃ 00000000 ┃ 05000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  405. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  406. ┃ 02000000 ┃ 07000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  407. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  408. ┏━━━━━━━━━━┓ Constraint 1: (4w_1 + 8w_4 + 3w_5) * (6w_6 + 44w_3) = 0
  409. ┃ 03000000 ┃
  410. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  411. ┃ 01000000 ┃ 04000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  412. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  413. ┃ 04000000 ┃ 08000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  414. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  415. ┃ 05000000 ┃ 03000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  416. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  417. ┏━━━━━━━━━━┓
  418. ┃ 02000000 ┃
  419. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  420. ┃ 03000000 ┃ 2C000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  421. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  422. ┃ 06000000 ┃ 06000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  423. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  424. ┏━━━━━━━━━━┓
  425. ┃ 00000000 ┃
  426. ┗━━━━━━━━━━┛
  427. ┏━━━━━━━━━━┓ Constraint 2: (4w_6) * (6w_0 + 5w_3 + 11w_2) - (600w_6) = 0
  428. ┃ 01000000 ┃
  429. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  430. ┃ 06000000 ┃ 04000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  431. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  432. ┏━━━━━━━━━━┓
  433. ┃ 03000000 ┃
  434. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  435. ┃ 00000000 ┃ 06000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  436. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  437. ┃ 02000000 ┃ 0B000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  438. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
  439. ┃ 03000000 ┃ 05000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  440. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  441. ┏━━━━━━━━━━┓
  442. ┃ 01000000 ┃
  443. ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  444. ┃ 06000000 ┃ 58020000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ┃
  445. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  446. ┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
  447. ┃ 03000000 ┃ 38000000 00000000 ┃ Wire to Label Map
  448. ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━┛
  449. ┏━━━━━━━━━━━━━━━━━━━┓
  450. ┃ 00000000 00000000 ┃
  451. ┣━━━━━━━━━━━━━━━━━━━┫
  452. ┃ 03000000 00000000 ┃
  453. ┣━━━━━━━━━━━━━━━━━━━┫
  454. ┃ 0a000000 00000000 ┃
  455. ┣━━━━━━━━━━━━━━━━━━━┫
  456. ┃ 0b000000 00000000 ┃
  457. ┣━━━━━━━━━━━━━━━━━━━┫
  458. ┃ 0c000000 00000000 ┃
  459. ┣━━━━━━━━━━━━━━━━━━━┫
  460. ┃ 0f000000 00000000 ┃
  461. ┣━━━━━━━━━━━━━━━━━━━┫
  462. ┃ 44010000 00000000 ┃
  463. ┗━━━━━━━━━━━━━━━━━━━┛
  464. ````
  465. And the binary representation in Hex:
  466. ````
  467. 72316377
  468. 01000000
  469. 03000000
  470. 01000000 40000000 00000000
  471. 20000000
  472. 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430
  473. 07000000
  474. 01000000
  475. 02000000
  476. 03000000
  477. e8030000 00000000
  478. 03000000
  479. 02000000 88200000 00000000
  480. 02000000
  481. 05000000 03000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  482. 06000000 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  483. 03000000
  484. 00000000 02000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  485. 02000000 01140000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  486. 03000000 0C000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  487. 02000000
  488. 00000000 05000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  489. 02000000 07000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  490. 03000000
  491. 01000000 04000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  492. 04000000 08000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  493. 05000000 03000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  494. 02000000
  495. 03000000 2C000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  496. 06000000 06000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  497. 00000000
  498. 01000000
  499. 06000000 04000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  500. 03000000
  501. 00000000 06000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  502. 02000000 0B000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  503. 03000000 05000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  504. 01000000
  505. 06000000 58020000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  506. 03000000 38000000 00000000
  507. 00000000 00000000
  508. 03000000 00000000
  509. 0a000000 00000000
  510. 0b000000 00000000
  511. 0c000000 00000000
  512. 0f000000 00000000
  513. 44010000 00000000
  514. ````
  515. ## Implementation
  516. circom will output this format.
  517. ## Copyright
  518. Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).