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.

683 lines
33 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
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
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 │ FieldDefSize ┃ 4 ┃FieldDef ┃ field Id ┃
  112. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━┻━━━━━━━━━┻━━━━━━━━━━━━━━━━┛
  113. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  114. ┃ 4 │ 00 00 00 00 ┃ bigInt Format
  115. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  116. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  117. ┃ 4 │ is ┃ Id size ( Normally 4 (32bits))
  118. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  119. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  120. ┃ is │ 01 00 00 00 ┃ nWires
  121. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  122. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  123. ┃ is │ 01 00 00 00 ┃ nPubOut
  124. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  125. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  126. ┃ is │ 01 00 00 00 ┃ nPubIn
  127. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  128. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  129. ┃ is │ 01 00 00 00 ┃ nPrvIn
  130. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  131. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  132. ┃ is │ 01 00 00 00 ┃ nLabels
  133. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  134. ┏━━━━┳━━━━━━━━━━━━━━━━━┓
  135. ┃ is │ 01 00 00 00 ┃ mConstraints
  136. ┗━━━━┻━━━━━━━━━━━━━━━━━┛
  137. ````
  138. #### fieldDefSize
  139. Size: 4 bytes
  140. Format: Little-Endian
  141. Size of the field Definition
  142. Example:
  143. ```
  144. 0x00 0x0 0x00 0x00
  145. ```
  146. #### fieldDef
  147. Field dfinition the first 4 bytes are the type in LE. 0x0000001 Ar prime fields.
  148. For the prime fields, the next bytes are the prime in variable length LE base 256 format.
  149. NOTE: This number is independent of the bigInt Format defined next
  150. #### bigInt Format
  151. Size: 4 bytes
  152. Format: Little-Endian
  153. 0 Means that the Big Int are variable size LE.
  154. That is the First byte indicates the size and the remaining bytes are the number in little enfian (LSB first) base 256.
  155. Numbers from 1 to 16383 are fixed size Litle endian format base 256.
  156. Example:
  157. ```
  158. 0x00 0x00 0x00 0x00
  159. ```
  160. #### Id Size (is)
  161. Size: 4 bytes
  162. Format: Little-Endian
  163. Size of the identifiers for wires, labels and constraints. In small circuits this is going to be 4 (32 bits)
  164. but can be increaset to 8 for bigger circiuits.
  165. The only possible numbers are 4 or 8
  166. #### Number of wires
  167. Size: `is` bytes
  168. Format: Little-Endian
  169. Total Number of wires including ONE signal (Index 0).
  170. #### Number of public outputs
  171. Size: `is` bytes
  172. Format: Little-Endian
  173. Total Number of wires public output wires. They should be starting at idx 1
  174. #### Number of public inputs
  175. Size: `is` bytes
  176. Format: Little-Endian
  177. Total Number of wires public input wires. They should be starting just after the public output
  178. #### Number of private inputs
  179. Size: `is` bytes
  180. Format: Little-Endian
  181. Total Number of wires private input wires. They should be starting just after the public inputs
  182. #### Number of constraints (m)
  183. Size: `ìs` bytes
  184. Format: Little-Endian
  185. Total Number of constraints
  186. ### Constraints section
  187. Section Type: 0x02
  188. ````
  189. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ ╲
  190. ┃ is │ nA ┃ ╲
  191. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ╲
  192. ┃ is │ wireId_1 ┃ V │ a_{0,wireId_1} ┃ │
  193. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  194. ┃ is │ wireId_2 ┃ V │ a_{0,wireId_2} ┃ │
  195. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  196. ... ... │
  197. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  198. ┃ is │ wireId_nA ┃ V │ a_{0,wireId_nA} ┃ │
  199. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  200. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  201. ┃ is │ nB ┃ │
  202. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  203. ┃ is │ wireId_1 ┃ V │ b_{0,wireId_1} ┃ │
  204. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ ╲
  205. ┃ is │ wireId_2 ┃ V │ b_{0,wireId_2} ┃ ╲
  206. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱ Constraint_0
  207. ... ... ╱
  208. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  209. ┃ is │ wireId_nB ┃ V │ b_{0,wireId_nB} ┃ │
  210. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  211. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  212. ┃ is │ nC ┃ │
  213. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  214. ┃ is │ wireId_1 ┃ V │ c_{0,wireId_1} ┃ │
  215. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  216. ┃ is │ wireId_2 ┃ V │ c_{0,wireId_2} ┃ │
  217. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  218. ... ... │
  219. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  220. ┃ is │ wireId_nC ┃ V │ c_{0,wireId_nC} ┃ ╱
  221. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱
  222. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ ╲
  223. ┃ is │ nA ┃ ╲
  224. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ╲
  225. ┃ is │ wireId_1 ┃ V │ a_{1,wireId_1} ┃ │
  226. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  227. ┃ is │ wireId_2 ┃ V │ a_{1,wireId_2} ┃ │
  228. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  229. ... ... │
  230. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  231. ┃ is │ wireId_nA ┃ V │ a_{1,wireId_nA} ┃ │
  232. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  233. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  234. ┃ is │ nB ┃ │
  235. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  236. ┃ is │ wireId_1 ┃ V │ b_{1,wireId_1} ┃ │
  237. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ ╲
  238. ┃ is │ wireId_2 ┃ V │ b_{1,wireId_2} ┃ ╲
  239. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱ Constraint_1
  240. ... ... ╱
  241. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  242. ┃ is │ wireId_nB ┃ V │ b_{1,wireId_nB} ┃ │
  243. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  244. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  245. ┃ is │ nC ┃ │
  246. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  247. ┃ is │ wireId_1 ┃ V │ c_{1,wireId_1} ┃ │
  248. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  249. ┃ is │ wireId_2 ┃ V │ c_{1,wireId_2} ┃ │
  250. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  251. ... ... │
  252. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  253. ┃ is │ wireId_nC ┃ V │ c_{1,wireId_nC} ┃ ╱
  254. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱
  255. ...
  256. ...
  257. ...
  258. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ ╲
  259. ┃ is │ nA ┃ ╲
  260. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ╲
  261. ┃ is │ wireId_1 ┃ V │ a_{m-1,wireId_1} ┃ │
  262. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  263. ┃ is │ wireId_2 ┃ V │ a_{m-1,wireId_2} ┃ │
  264. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  265. ... ... │
  266. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  267. ┃ is │ wireId_nA ┃ V │ a_{m-1,wireId_nA} ┃ │
  268. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  269. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  270. ┃ is │ nB ┃ │
  271. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  272. ┃ is │ wireId_1 ┃ V │ b_{m-1,wireId_1} ┃ │
  273. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ ╲
  274. ┃ is │ wireId_2 ┃ V │ b_{m-1,wireId_2} ┃ ╲
  275. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱ Constraint_{m-1}
  276. ... ... ╱
  277. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  278. ┃ is │ wireId_nB ┃ V │ b_{m-1,wireId_nB} ┃ │
  279. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  280. ┏━━━━┳━━━━━━━━━━━━━━━━━┓ │
  281. ┃ is │ nC ┃ │
  282. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  283. ┃ is │ wireId_1 ┃ V │ c_{m-1,wireId_1} ┃ │
  284. ┣━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━┫ │
  285. ┃ is │ wireId_2 ┃ V │ c_{m-1,wireId_2} ┃ │
  286. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ │
  287. ... ... │
  288. ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
  289. ┃ is │ wireId_nC ┃ V │ c_{m-1,wireId_nC} ┃ ╱
  290. ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛ ╱
  291. ╱ ╱
  292. ````
  293. #### Constraints
  294. Each constraint contains 3 linear combinations A, B, C.
  295. The constraint is such that:
  296. ```
  297. A*B-C = 0
  298. ```
  299. #### Linear combination
  300. Each linear combination is of the form:
  301. $$
  302. a_{j,0}w_0 + a_{j,1}w_1 + ... + a_{j,n}w_{n}
  303. $$
  304. #### Number of nonZero Factors
  305. Size: `ìs` bytes
  306. Format: Little-Endian
  307. Total number of non Zero factors in the linear compination.
  308. The factors MUST be sorted in ascending order.
  309. #### Factor
  310. For each factor we have the index of the factor and the value of the factor.
  311. #### WireId of the factor
  312. Size: `is` bytes
  313. Format: Little-Endian
  314. WireId of the nonZero Factor
  315. #### Value of the factor
  316. The first byte indicate the length N in bytes of the number in the upcoming bytes.
  317. The next N bytes represent the value in Little Endian format.
  318. For example, to represent the linear combination:
  319. $$
  320. 5w_4 +8w_5 + 260w_{886}
  321. $$
  322. The linear combination would be represented as:
  323. ````
  324. ┏━━━━━━━━━━━━━━━━━┓
  325. ┃ 03 00 00 00 ┃
  326. ┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━┓
  327. ┃ 04 00 00 00 ┃ 01 05 ┃
  328. ┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━┫
  329. ┃ 05 00 00 00 ┃ 01 08 ┃
  330. ┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━┫
  331. ┃ 76 03 00 00 ┃ 02 04 01 ┃
  332. ┗━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━┛
  333. ````
  334. ### WireId2LabelId Map Section
  335. Section Type: 0x03
  336. ````
  337. ┏━━┳━━━━━━━━━━━━━━━━━━━┳━━┳━━━━━━━━━━━━━━━━━━━┓ ┏━━┳━━━━━━━━━━━━━━━━━━━┓
  338. ┃is│ labelId of Wire_0 ┃is│ labelId of Wire_1 ┃ ... ┃is│ labelId of Wire_n ┃
  339. ┗━━┻━━━━━━━━━━━━━━━━━━━┻━━┻━━━━━━━━━━━━━━━━━━━┛ ┗━━┻━━━━━━━━━━━━━━━━━━━┛
  340. ````
  341. ## Rationale
  342. Variable size for field elements allows to shrink the size of the file and allows to work with any field.
  343. Version allows to update the format.
  344. Have a very good comprasion ratio for sparse r1cs as it's the normal case.
  345. 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.
  346. ## Backward Compatibility
  347. N.A.
  348. ## Test Cases
  349. ### Example
  350. Given this r1cs in a 256 bit Field:
  351. $$
  352. \left\{ \begin{array}{rclclcl}
  353. (3w_5 + 8w_6) &\cdot& (2w_0 + 20w_2 + 12w_3) &-& (5w_0 + 7w_2) &=& 0 \\
  354. (4w_1 + 8w_4 + 3w_5) &\cdot& (6w_6 + 44w_3) && &=& 0 \\
  355. (4w_6) &\cdot& (6w_0 + 5w_3 + 11s_2) &-& (600w_6) &=& 0
  356. \end{array} \right.
  357. $$
  358. And a Wire to label map.
  359. $$
  360. w_0 := l_0 \\
  361. w_1 := l_3 \\
  362. w_2 := l_{10} \\
  363. w_3 := l_{11} \\
  364. w_4 := l_{12} \\
  365. w_5 := l_{15} \\
  366. w_6 := l_{324} \\
  367. $$
  368. The format will be:
  369. ````
  370. ┏━━━━━━━━━━━━━━┓
  371. ┃ 72 31 63 73 ┃ Magic
  372. ┣━━━━━━━━━━━━━━┫
  373. ┃ 01 00 00 00 ┃ Version
  374. ┣━━━━━━━━━━━━━━┫
  375. ┃ 03 00 00 00 ┃ nSections
  376. ┗━━━━━━━━━━━━━━┛
  377. ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
  378. ┃ 01 00 00 00 ┃ 49 00 00 00 ┃ SectionType: Header
  379. ┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
  380. ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
  381. ┃ 25 00 00 00 ┃ 10 00 00 00 ┃ FieldDefSize FieldDef
  382. ┣━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  383. ┃ 20 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430┃
  384. ┣━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  385. ┃ 00 00 00 00 ┃ Big Int format
  386. ┣━━━━━━━━━━━━━━┫
  387. ┃ 04 00 00 00 ┃ Id Size
  388. ┣━━━━━━━━━━━━━━┫
  389. ┃ 07 00 00 00 ┃ # of wires
  390. ┣━━━━━━━━━━━━━━┫
  391. ┃ 01 00 00 00 ┃ # Public Outs
  392. ┣━━━━━━━━━━━━━━┫
  393. ┃ 02 00 00 00 ┃ # Public Ins
  394. ┣━━━━━━━━━━━━━━┫
  395. ┃ 03 00 00 00 ┃ # Private Ins
  396. ┣━━━━━━━━━━━━━━┫
  397. ┃ e8 03 00 00 ┃ # Labels
  398. ┣━━━━━━━━━━━━━━┫
  399. ┃ 03 00 00 00 ┃ # Constraints
  400. ┗━━━━━━━━━━━━━━┛
  401. ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
  402. ┃ 02 00 00 00 ┃ 8b 00 00 00 ┃ SectionType: Constraints
  403. ┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
  404. ┏━━━━━━━━━━━━━━┓ Constraint 0: (3w_5 + 8w_6) * (2w_0 + 20w_2 + 12w_3) - (5w_0 + 7w_2) = 0
  405. ┃ 02 00 00 00 ┃
  406. ┣━━━━━━━━━━━━━━╋━━━━━━━━┓
  407. ┃ 05 00 00 00 ┃ 01 03 ┃
  408. ┣━━━━━━━━━━━━━━╋━━━━━━━━┫
  409. ┃ 06 00 00 00 ┃ 01 08 ┃
  410. ┗━━━━━━━━━━━━━━┻━━━━━━━━┛
  411. ┏━━━━━━━━━━━━━━┓
  412. ┃ 03 00 00 00 ┃
  413. ┣━━━━━━━━━━━━━━╋━━━━━━━━┓
  414. ┃ 00 00 00 00 ┃ 01 02 ┃
  415. ┣━━━━━━━━━━━━━━╋━━━━━━━━┫
  416. ┃ 02 00 00 00 ┃ 01 14 ┃
  417. ┣━━━━━━━━━━━━━━╋━━━━━━━━┫
  418. ┃ 03 00 00 00 ┃ 01 0C ┃
  419. ┗━━━━━━━━━━━━━━┻━━━━━━━━┛
  420. ┏━━━━━━━━━━━━━━┓
  421. ┃ 02 00 00 00 ┃
  422. ┣━━━━━━━━━━━━━━╋━━━━━━━━┓
  423. ┃ 00 00 00 00 ┃ 01 05 ┃
  424. ┣━━━━━━━━━━━━━━╋━━━━━━━━┫
  425. ┃ 02 00 00 00 ┃ 01 07 ┃
  426. ┗━━━━━━━━━━━━━━┻━━━━━━━━┛
  427. ┏━━━━━━━━━━━━━━┓ Constraint 1: (4w_1 + 8w_4 + 3w_5) * (6w_6 + 44w_3) = 0
  428. ┃ 03 00 00 00 ┃
  429. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┓
  430. ┃ 01 00 00 00 ┃ 01 04 ┃
  431. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┫
  432. ┃ 04 00 00 00 ┃ 01 08 ┃
  433. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┫
  434. ┃ 05 00 00 00 ┃ 01 03 ┃
  435. ┗━━━━━━━━━━━━━━┻━━━━━━━━━┛
  436. ┏━━━━━━━━━━━━━━┓
  437. ┃ 02 00 00 00 ┃
  438. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┓
  439. ┃ 03 00 00 00 ┃ 01 2C ┃
  440. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┫
  441. ┃ 06 00 00 00 ┃ 01 06 ┃
  442. ┗━━━━━━━━━━━━━━┻━━━━━━━━━┛
  443. ┏━━━━━━━━━━━━━━┓
  444. ┃ 00 00 00 00 ┃
  445. ┗━━━━━━━━━━━━━━┛
  446. ┏━━━━━━━━━━━━━━┓ Constraint 2: (4w_6) * (6w_0 + 5w_3 + 11w_2) - (600w_6) = 0
  447. ┃ 01 00 00 00 ┃
  448. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┓
  449. ┃ 06 00 00 00 ┃ 01 04 ┃
  450. ┗━━━━━━━━━━━━━━┻━━━━━━━━━┛
  451. ┏━━━━━━━━━━━━━━┓
  452. ┃ 03 00 00 00 ┃
  453. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┓
  454. ┃ 00 00 00 00 ┃ 01 06 ┃
  455. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┫
  456. ┃ 02 00 00 00 ┃ 01 0B ┃
  457. ┣━━━━━━━━━━━━━━╋━━━━━━━━━┫
  458. ┃ 03 00 00 00 ┃ 01 05 ┃
  459. ┗━━━━━━━━━━━━━━┻━━━━━━━━━┛
  460. ┏━━━━━━━━━━━━━━┓
  461. ┃ 01 00 00 00 ┃
  462. ┣━━━━━━━━━━━━━━╋━━━━━━━━━━━━━┓
  463. ┃ 06 00 00 00 ┃ 02 58 02 ┃
  464. ┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
  465. ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
  466. ┃ 03 00 00 00 ┃ 1c 00 00 00 ┃ Wire to Label Map
  467. ┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
  468. ┏━━━━━━━━━━━━━━┓
  469. ┃ 00 00 00 00 ┃
  470. ┣━━━━━━━━━━━━━━┫
  471. ┃ 03 00 00 00 ┃
  472. ┣━━━━━━━━━━━━━━┫
  473. ┃ 0a 00 00 00 ┃
  474. ┣━━━━━━━━━━━━━━┫
  475. ┃ 0b 00 00 00 ┃
  476. ┣━━━━━━━━━━━━━━┫
  477. ┃ 0c 00 00 00 ┃
  478. ┣━━━━━━━━━━━━━━┫
  479. ┃ 0f 00 00 00 ┃
  480. ┣━━━━━━━━━━━━━━┫
  481. ┃ 44 01 00 00 ┃
  482. ┗━━━━━━━━━━━━━━┛
  483. ````
  484. And the binary representation in Hex:
  485. ````
  486. 72 31 63 77
  487. 01 00 00 00
  488. 03 00 00 00
  489. 01 00 00 00 49 00 00 00
  490. 25 00 00 00 10 00 00 00
  491. 20 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430
  492. 00 00 00 00
  493. 04 00 00 00
  494. 07 00 00 00
  495. 01 00 00 00
  496. 02 00 00 00
  497. 03 00 00 00
  498. e8 03 00 00
  499. 03 00 00 00
  500. 02 00 00 00 8b 00 00 00
  501. 02 00 00 00
  502. 05 00 00 00 01 03
  503. 06 00 00 00 01 08
  504. 03 00 00 00
  505. 00 00 00 00 01 02
  506. 02 00 00 00 01 14
  507. 03 00 00 00 01 0C
  508. 02 00 00 00
  509. 00 00 00 00 01 05
  510. 02 00 00 00 01 07
  511. 03 00 00 00
  512. 01 00 00 00 01 04
  513. 04 00 00 00 01 08
  514. 05 00 00 00 01 03
  515. 02 00 00 00
  516. 03 00 00 00 01 2C
  517. 06 00 00 00 01 06
  518. 00 00 00 00
  519. 01 00 00 00
  520. 06 00 00 00 01 04
  521. 03 00 00 00
  522. 00 00 00 00 01 06
  523. 02 00 00 00 01 0B
  524. 03 00 00 00 01 05
  525. 01 00 00 00
  526. 06 00 00 00 02 58 02
  527. 03 00 00 00 1c 00 00 00
  528. 00 00 00 00
  529. 03 00 00 00
  530. 0a 00 00 00
  531. 0b 00 00 00
  532. 0c 00 00 00
  533. 0f 00 00 00
  534. 44 01 00 00
  535. ````
  536. ## Implementation
  537. circom will output this format.
  538. ## Copyright
  539. Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).