7 #ifndef SECP256K1_MODULE_BULLETPROOF_PARSER_IMPL
8 #define SECP256K1_MODULE_BULLETPROOF_PARSER_IMPL
15 static size_t secp256k1_bulletproof_encoding_width(
size_t n) {
16 if (n < 0x100)
return 1;
17 if (n < 0x10000)
return 2;
18 if (n < 0x100000000)
return 4;
22 static void secp256k1_encode(
unsigned char *buf,
size_t width,
size_t n) {
24 for (i = 0; i < width; i++) {
30 static size_t secp256k1_decode(
const unsigned char *buf,
size_t width) {
33 ret = ret * 0x100 + buf[width];
41 size_t high = secp256k1_scalar_is_high(&tmp);
44 secp256k1_scalar_negate(&tmp, &tmp);
47 while (!secp256k1_scalar_is_zero(&tmp)) {
48 buf[1 + i] = secp256k1_scalar_shr_int(&tmp, 8);
52 buf[0] = i ^ (
high << 7);
58 unsigned char rbuf[32] = {0};
59 const size_t neg = buf[0] & 0x80;
60 const size_t len = buf[0] & 0x3f;
64 for (i = 0; i < len; i++) {
65 rbuf[31 - i] = buf[i + 1];
67 secp256k1_scalar_set_b32(&
r->scal, rbuf, &overflow);
71 secp256k1_scalar_set_int(&two, 2);
72 if (secp256k1_scalar_is_one(&
r->scal)) {
73 r->special = neg ? -1 : 1;
74 }
else if (secp256k1_scalar_eq(&
r->scal, &two)) {
75 r->special = neg ? -2 : 2;
76 }
else if (secp256k1_scalar_is_zero(&
r->scal)) {
82 secp256k1_scalar_negate(&
r->scal, &
r->scal);
89 unsigned char buf[41];
90 const size_t row_width = secp256k1_bulletproof_encoding_width(n_rows);
91 for (i = 0; i < n_rows; i++) {
94 secp256k1_encode(buf, row_width, w[i].size);
95 if (fwrite(buf, row_width, 1, fh) != 1) {
98 for (j = 0; j < w[i].
size; j++) {
99 secp256k1_encode(buf, row_width, w[i].entry[j].idx);
100 scalar_width = secp256k1_scalar_encode(buf + row_width, &w[i].entry[j].scal);
101 if (fwrite(buf, row_width + scalar_width, 1, fh) != 1) {
111 unsigned char buf[0x3f];
113 for (i = 0; i < n_rows; i++) {
115 if (fread(buf, row_width, 1, fh) != 1) {
118 w[i].
size = secp256k1_decode(buf, row_width);
119 w[i].
entry = &entries[*n_entries];
120 for (j = 0; j < w[i].
size; j++) {
121 if (fread(buf, row_width, 1, fh) != 1) {
124 w[i].
entry[j].
idx = secp256k1_decode(buf, row_width);
125 if (fread(buf, 1, 1, fh) != 1 ||
126 fread(buf + 1, buf[0] & 0x3f, 1, fh) != 1 ||
127 secp256k1_scalar_decode(&w[i].entry[j].scal, buf) != 1) {
131 *n_entries += w[i].
size;
138 unsigned char buf[32];
143 size_t n_constraints;
148 if (fread(buf, 32, 1, fh) != 1) {
152 version = secp256k1_decode(&buf[0], 4);
156 n_commits = secp256k1_decode(&buf[4], 4);
157 n_gates = secp256k1_decode(&buf[8], 8);
158 n_bits = secp256k1_decode(&buf[16], 8);
159 n_constraints = secp256k1_decode(&buf[24], 8);
160 if (n_bits > n_gates) {
166 w = secp256k1_bulletproof_encoding_width(n_gates);
167 for (i = 0; i < 3 * n_gates + n_commits; i++) {
169 if (i == 3 * n_gates) {
170 w = secp256k1_bulletproof_encoding_width(n_commits);
172 if (fread(buf, w, 1, fh) != 1) {
175 n = secp256k1_decode(buf, w);
178 if (fseek(fh, w, SEEK_CUR)) {
181 if (fread(buf, 1, 1, fh) != 1) {
184 if (fseek(fh, buf[0] & 0x3f, SEEK_CUR)) {
190 total_mem =
sizeof(**ret) + (3 * n_gates + n_commits) *
sizeof(*(*ret)->wl) + n_constraints *
sizeof(*(*ret)->c) + n_entries *
sizeof(*(*ret)->entries);
196 if (fseek(fh, 32, SEEK_SET) != 0) {
209 if ((*ret)->wl == NULL || (*ret)->c == NULL || (*ret)->entries == NULL) {
212 free((*ret)->entries);
219 (*ret)->n_gates = n_gates;
220 (*ret)->n_bits = n_bits;
221 (*ret)->n_constraints = n_constraints;
222 (*ret)->wr = &(*ret)->wl[1 * n_gates];
223 (*ret)->wo = &(*ret)->wl[2 * n_gates];
224 (*ret)->wv = &(*ret)->wl[3 * n_gates];
233 while (isspace(*c)) {
239 if (*c ==
'-' || *c ==
'+') {
242 while (isspace(*c)) {
245 secp256k1_scalar_clear(&
r->scal);
246 while (isdigit(*c)) {
248 secp256k1_scalar_set_int(&digit, 10);
249 secp256k1_scalar_mul(&
r->scal, &
r->scal, &digit);
250 secp256k1_scalar_set_int(&digit, *c -
'0');
251 secp256k1_scalar_add(&
r->scal, &
r->scal, &digit);
257 secp256k1_scalar_set_int(&
r->scal, 1);
259 while (*c && *c !=
',' && *c !=
';' && *c !=
'=' && *c !=
'L' && *c !=
'R' && *c !=
'O' && *c !=
'V') {
262 if (secp256k1_scalar_is_one(&
r->scal)) {
263 r->special = neg ? -1 : 1;
264 }
else if (secp256k1_scalar_is_zero(&
r->scal)) {
270 secp256k1_scalar_negate(&
r->scal, &
r->scal);
290 size_t entry_idx = 0;
293 if (sscanf(c,
"%d,%d,%d,%d; %n", &n_gates, &n_commits, &n_bits, &n_constraints, &chars_read) != 4) {
304 ret->
wr = &ret->
wl[1 * n_gates];
305 ret->
wo = &ret->
wl[2 * n_gates];
306 ret->
wv = &ret->
wl[3 * n_gates];
310 memset(ret->
wl, 0, ret->
n_gates *
sizeof(*ret->
wl));
311 memset(ret->
wr, 0, ret->
n_gates *
sizeof(*ret->
wr));
312 memset(ret->
wo, 0, ret->
n_gates *
sizeof(*ret->
wo));
322 while (*c && *c !=
'=') {
327 secp256k1_parse_scalar(&mul, c, &c);
346 if (sscanf(c,
"%d %n", &index, &chars_read) != 1) {
350 if ((w != ret->
wv && index >= n_gates) || (w == ret->
wv && index >= n_commits)) {
364 secp256k1_parse_scalar(&ret->
c[i], c, &c);
379 for (i = 0; i < ret->
n_gates; i++) {
381 entry_idx += ret->
wl[i].
size;
384 entry_idx += ret->
wr[i].
size;
387 entry_idx += ret->
wo[i].
size;
392 entry_idx += ret->
wv[i].
size;
401 while (*c && *c !=
'=') {
407 secp256k1_parse_scalar(&mul, c, &c);
420 secp256k1_scalar_negate(&mul.
scal, &mul.
scal);
429 sscanf(c,
"%d %n", &index, &chars_read);
441 secp256k1_parse_scalar(&ret->
c[i], c, &c);