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.

341 lines
6.6 KiB

  1. package gocircomprover
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "math/big"
  8. "strconv"
  9. "strings"
  10. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  11. )
  12. type ProvingKeyString struct {
  13. A [][]string `json:"A"`
  14. B2 [][][]string `json:"B2"`
  15. B1 [][]string `json:"B1"`
  16. C [][]string `json:"C"`
  17. NVars int `json:"nVars"`
  18. NPublic int `json:"nPublic"`
  19. VkAlpha1 []string `json:"vk_alfa_1"`
  20. VkDelta1 []string `json:"vk_delta_1"`
  21. VkBeta1 []string `json:"vk_beta_1"`
  22. VkBeta2 [][]string `json:"vk_beta_2"`
  23. VkDelta2 [][]string `json:"vk_delta_2"`
  24. HExps [][]string `json:"hExps"`
  25. DomainSize int `json:"domainSize"`
  26. PolsA []map[string]string `json:"polsA"`
  27. PolsB []map[string]string `json:"polsB"`
  28. PolsC []map[string]string `json:"polsC"`
  29. }
  30. type WitnessString []string
  31. func ParseWitness(wJson []byte) (Witness, error) {
  32. var ws WitnessString
  33. err := json.Unmarshal(wJson, &ws)
  34. if err != nil {
  35. return nil, err
  36. }
  37. var w Witness
  38. for i := 0; i < len(ws); i++ {
  39. bi, err := stringToBigInt(ws[i])
  40. if err != nil {
  41. return nil, err
  42. }
  43. w = append(w, bi)
  44. }
  45. return w, nil
  46. }
  47. func ParseProvingKey(pkJson []byte) (*ProvingKey, error) {
  48. var pkStr ProvingKeyString
  49. err := json.Unmarshal(pkJson, &pkStr)
  50. if err != nil {
  51. return nil, err
  52. }
  53. pk, err := provingKeyStringToProvingKey(pkStr)
  54. return pk, err
  55. }
  56. func provingKeyStringToProvingKey(ps ProvingKeyString) (*ProvingKey, error) {
  57. var p ProvingKey
  58. var err error
  59. p.A, err = arrayStringToG1(ps.A)
  60. if err != nil {
  61. return nil, err
  62. }
  63. p.B2, err = arrayStringToG2(ps.B2)
  64. if err != nil {
  65. return nil, err
  66. }
  67. p.B1, err = arrayStringToG1(ps.B1)
  68. if err != nil {
  69. return nil, err
  70. }
  71. p.C, err = arrayStringToG1(ps.C)
  72. if err != nil {
  73. return nil, err
  74. }
  75. p.NVars = ps.NVars
  76. p.NPublic = ps.NPublic
  77. p.VkAlpha1, err = stringToG1(ps.VkAlpha1)
  78. if err != nil {
  79. return nil, err
  80. }
  81. p.VkDelta1, err = stringToG1(ps.VkDelta1)
  82. if err != nil {
  83. return nil, err
  84. }
  85. p.VkBeta1, err = stringToG1(ps.VkBeta1)
  86. if err != nil {
  87. return nil, err
  88. }
  89. p.VkBeta2, err = stringToG2(ps.VkBeta2)
  90. if err != nil {
  91. return nil, err
  92. }
  93. p.VkDelta2, err = stringToG2(ps.VkDelta2)
  94. if err != nil {
  95. return nil, err
  96. }
  97. p.HExps, err = arrayStringToG1(ps.HExps)
  98. if err != nil {
  99. return nil, err
  100. }
  101. p.DomainSize = ps.DomainSize
  102. p.PolsA, err = polsStringToBigInt(ps.PolsA)
  103. if err != nil {
  104. return nil, err
  105. }
  106. p.PolsB, err = polsStringToBigInt(ps.PolsB)
  107. if err != nil {
  108. return nil, err
  109. }
  110. p.PolsC, err = polsStringToBigInt(ps.PolsC)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &p, nil
  115. }
  116. // polsStringToBigInt is for taking string polynomials and converting it to *big.Int polynomials
  117. func polsStringToBigInt(s []map[string]string) ([]map[int]*big.Int, error) {
  118. var o []map[int]*big.Int
  119. for i := 0; i < len(s); i++ {
  120. // var oi map[int]*big.Int
  121. oi := make(map[int]*big.Int)
  122. for j, v := range s[i] {
  123. si, err := stringToBigInt(v)
  124. if err != nil {
  125. return o, err
  126. }
  127. // oi = append(oi, si)
  128. jInt, err := strconv.Atoi(j)
  129. if err != nil {
  130. fmt.Println(j)
  131. return o, err
  132. }
  133. fmt.Println(jInt, si)
  134. oi[jInt] = si
  135. }
  136. o = append(o, oi)
  137. }
  138. return o, nil
  139. }
  140. func arrayStringToBigInt(s []string) ([]*big.Int, error) {
  141. var o []*big.Int
  142. for i := 0; i < len(s); i++ {
  143. si, err := stringToBigInt(s[i])
  144. if err != nil {
  145. return o, nil
  146. }
  147. o = append(o, si)
  148. }
  149. return o, nil
  150. }
  151. func stringToBigInt(s string) (*big.Int, error) {
  152. base := 10
  153. if bytes.HasPrefix([]byte(s), []byte("0x")) {
  154. base = 16
  155. s = strings.TrimPrefix(s, "0x")
  156. }
  157. n, ok := new(big.Int).SetString(s, base)
  158. if !ok {
  159. return nil, fmt.Errorf("Can not parse string to *big.Int: %s", s)
  160. }
  161. return n, nil
  162. }
  163. func addZPadding(b []byte) []byte {
  164. var z [32]byte
  165. var r []byte
  166. r = append(r, z[len(b):]...) // add padding on the left
  167. r = append(r, b...)
  168. return r[:32]
  169. }
  170. func stringToBytes(s string) ([]byte, error) {
  171. if s == "1" {
  172. s = "0"
  173. }
  174. bi, ok := new(big.Int).SetString(s, 10)
  175. if !ok {
  176. return nil, fmt.Errorf("error parsing bigint stringToBytes")
  177. }
  178. b := bi.Bytes()
  179. if len(b) != 32 {
  180. b = addZPadding(b)
  181. }
  182. return b, nil
  183. }
  184. func arrayStringToG1(h [][]string) ([]*bn256.G1, error) {
  185. var o []*bn256.G1
  186. for i := 0; i < len(h); i++ {
  187. hi, err := stringToG1(h[i])
  188. if err != nil {
  189. return o, err
  190. }
  191. o = append(o, hi)
  192. }
  193. return o, nil
  194. }
  195. func arrayStringToG2(h [][][]string) ([]*bn256.G2, error) {
  196. var o []*bn256.G2
  197. for i := 0; i < len(h); i++ {
  198. hi, err := stringToG2(h[i])
  199. if err != nil {
  200. return o, err
  201. }
  202. o = append(o, hi)
  203. }
  204. return o, nil
  205. }
  206. func stringToG1(h []string) (*bn256.G1, error) {
  207. if len(h) <= 2 {
  208. return nil, fmt.Errorf("not enought data for stringToG1")
  209. }
  210. h = h[:2]
  211. hexa := false
  212. if len(h[0]) > 1 {
  213. if "0x" == h[0][:2] {
  214. hexa = true
  215. }
  216. }
  217. in := ""
  218. var b []byte
  219. var err error
  220. if hexa {
  221. for i := range h {
  222. in += strings.TrimPrefix(h[i], "0x")
  223. }
  224. b, err = hex.DecodeString(in)
  225. if err != nil {
  226. return nil, err
  227. }
  228. } else {
  229. // TODO TMP
  230. // TODO use stringToBytes()
  231. if h[0] == "1" {
  232. h[0] = "0"
  233. }
  234. if h[1] == "1" {
  235. h[1] = "0"
  236. }
  237. bi0, ok := new(big.Int).SetString(h[0], 10)
  238. if !ok {
  239. return nil, fmt.Errorf("error parsing stringToG1")
  240. }
  241. bi1, ok := new(big.Int).SetString(h[1], 10)
  242. if !ok {
  243. return nil, fmt.Errorf("error parsing stringToG1")
  244. }
  245. b0 := bi0.Bytes()
  246. b1 := bi1.Bytes()
  247. if len(b0) != 32 {
  248. b0 = addZPadding(b0)
  249. }
  250. if len(b1) != 32 {
  251. b1 = addZPadding(b1)
  252. }
  253. b = append(b, b0...)
  254. b = append(b, b1...)
  255. }
  256. p := new(bn256.G1)
  257. _, err = p.Unmarshal(b)
  258. return p, err
  259. }
  260. func stringToG2(h [][]string) (*bn256.G2, error) {
  261. if len(h) <= 2 {
  262. return nil, fmt.Errorf("not enought data for stringToG2")
  263. }
  264. h = h[:2]
  265. hexa := false
  266. if len(h[0][0]) > 1 {
  267. if "0x" == h[0][0][:2] {
  268. hexa = true
  269. }
  270. }
  271. in := ""
  272. var b []byte
  273. var err error
  274. if hexa {
  275. for i := 0; i < len(h); i++ {
  276. for j := 0; j < len(h[i]); j++ {
  277. in += strings.TrimPrefix(h[i][j], "0x")
  278. }
  279. }
  280. b, err = hex.DecodeString(in)
  281. if err != nil {
  282. return nil, err
  283. }
  284. } else {
  285. // TODO TMP
  286. bH, err := stringToBytes(h[0][1])
  287. if err != nil {
  288. return nil, err
  289. }
  290. b = append(b, bH...)
  291. bH, err = stringToBytes(h[0][0])
  292. if err != nil {
  293. return nil, err
  294. }
  295. b = append(b, bH...)
  296. bH, err = stringToBytes(h[1][1])
  297. if err != nil {
  298. return nil, err
  299. }
  300. b = append(b, bH...)
  301. bH, err = stringToBytes(h[1][0])
  302. if err != nil {
  303. return nil, err
  304. }
  305. b = append(b, bH...)
  306. }
  307. p := new(bn256.G2)
  308. _, err = p.Unmarshal(b)
  309. return p, err
  310. }