purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
bppp_bridge.c
Go to the documentation of this file.
1// Copyright (c) 2026 Judica, Inc.
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit/.
4
9
10#define SECP256K1_BUILD
11#define ENABLE_MODULE_GENERATOR 1
12#define ENABLE_MODULE_BPPP 1
13#define ENABLE_MODULE_EXTRAKEYS 1
14#define ENABLE_MODULE_SCHNORRSIG 1
15
16#include "purify.h"
17#include "bppp_bridge.h"
18#include "purify/secp_bridge.h"
19
20#include <limits.h>
21#include <stdlib.h>
22#include <string.h>
23
24#if defined(PURIFY_VALGRIND_TESTING)
25#include <valgrind/memcheck.h>
26/* Proof points and terminal scalars are public from these protocol lines on. */
27#define PURIFY_BRIDGE_DECLASSIFY(data, size) \
28 VALGRIND_MAKE_MEM_DEFINED((data), (size))
29#else
30#define PURIFY_BRIDGE_DECLASSIFY(data, size) ((void)(data), (void)(size))
31#endif
32
33#include "third_party/secp256k1-zkp/include/secp256k1_extrakeys.h"
34#include "third_party/secp256k1-zkp/include/secp256k1_schnorrsig.h"
35#include "third_party/secp256k1-zkp/src/secp256k1.c"
36#include "third_party/secp256k1-zkp/src/precomputed_ecmult.c"
37#include "third_party/secp256k1-zkp/src/precomputed_ecmult_gen.c"
39
40#undef secp256k1_scratch_alloc
41
42_Static_assert(sizeof(purify_scalar) == sizeof(secp256k1_scalar), "purify_scalar size mismatch");
43_Static_assert(_Alignof(purify_scalar) >= _Alignof(secp256k1_scalar), "purify_scalar alignment mismatch");
44
46 secp256k1_context* ctx;
47};
48
49static void purify_bridge_secure_clear(void* data, size_t size);
50static int purify_bridge_memeq_const(const unsigned char* lhs,
51 const unsigned char* rhs,
52 size_t size);
53
54static secp256k1_context* purify_context_handle(const purify_secp_context* context) {
55 return context != NULL ? context->ctx : NULL;
56}
57
59 unsigned char seed32[32] = {0};
60 purify_secp_context* context =
61 (purify_secp_context*)calloc(1, sizeof(*context));
62 if (context == NULL) {
63 return NULL;
64 }
65 context->ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
66 if (context->ctx == NULL) {
67 free(context);
68 return NULL;
69 }
70 if (purify_fill_secure_random(seed32, sizeof(seed32)) != PURIFY_ERROR_OK
71 || !secp256k1_context_randomize(context->ctx, seed32)) {
72 purify_bridge_secure_clear(seed32, sizeof(seed32));
73 secp256k1_context_destroy(context->ctx);
74 free(context);
75 return NULL;
76 }
77 purify_bridge_secure_clear(seed32, sizeof(seed32));
78 return context;
79}
80
82 if (context == NULL) {
83 return;
84 }
85 if (context->ctx != NULL) {
86 secp256k1_context_destroy(context->ctx);
87 }
88 free(context);
89}
90
91static void purify_bridge_secure_clear(void* data, size_t size) {
92 if (data != NULL && size != 0u) {
93 secp256k1_memclear_explicit(data, size);
94 }
95}
96
97static void purify_bridge_secure_clear_scratch_tail(secp256k1_scratch_space* scratch,
98 size_t checkpoint) {
99 if (scratch != NULL && scratch->data != NULL && checkpoint <= scratch->max_size) {
100 purify_bridge_secure_clear((unsigned char*)scratch->data + checkpoint,
101 scratch->max_size - checkpoint);
102 }
103}
104
106 size_t n_gates;
108 secp256k1_context* ctx;
109 secp256k1_scratch_space* scratch;
111};
112
114 return resources != NULL && resources->scratch != NULL ? resources->scratch->max_size : 0;
115}
116
119 secp256k1_context* ctx;
120 secp256k1_scratch_space* scratch;
121 secp256k1_bppp_generators* gens;
122 secp256k1_bppp_generators gens_scratch;
124};
125
129
130static int purify_bridge_checked_mul_size(size_t lhs, size_t rhs, size_t* out) {
131 if (out == NULL) {
132 return 0;
133 }
134 if (lhs != 0 && rhs > SIZE_MAX / lhs) {
135 return 0;
136 }
137 *out = lhs * rhs;
138 return 1;
139}
140
141static int purify_bridge_checked_add_size(size_t lhs, size_t rhs, size_t* out) {
142 if (out == NULL) {
143 return 0;
144 }
145 if (rhs > SIZE_MAX - lhs) {
146 return 0;
147 }
148 *out = lhs + rhs;
149 return 1;
150}
151
152static void purify_bridge_secure_free_array(void* data, size_t count, size_t elem_size) {
153 size_t bytes = 0;
154
155 if (data != NULL && purify_bridge_checked_mul_size(count, elem_size, &bytes)) {
156 purify_bridge_secure_clear(data, bytes);
157 }
158 free(data);
159}
160
161/* Reserve this much work space for the batched multiscalar multiplication
162 * after the imported circuit and inner-product frames have been allocated.
163 */
164static const size_t purify_bulletproof_msm_scratch_size = (size_t)1u << 24;
165
166static int purify_bulletproof_add_scratch_frame(size_t* total, size_t bytes, size_t objects) {
167 size_t padding;
168 return purify_bridge_checked_mul_size(objects, ALIGNMENT - 1, &padding)
169 && purify_bridge_checked_add_size(bytes, padding, &bytes)
170 && purify_bridge_checked_add_size(*total, bytes, total);
171}
172
173static secp256k1_scratch_space* purify_bppp_create_scratch(
174 const secp256k1_context* ctx, size_t generators_count) {
175 size_t frame;
176 size_t capacity = purify_bulletproof_msm_scratch_size;
177 size_t allocation_size;
178
179 /* The norm prover keeps copies of n, l, c and all generators alive during
180 * its MSMs. With g = |n| and h = |l| = |c|, g + 2h <= 2(g + h).
181 * The verifier's four scalar arrays also fit this bound: their total is
182 * g + h + 2log2(g) <= 2(g + h) for the supported g >= h >= 1.
183 * Reserve the live frame separately from the existing MSM work space.
184 */
185 if (generators_count == 0
186 || !purify_bridge_checked_mul_size(generators_count,
187 sizeof(secp256k1_ge) + 2 * sizeof(secp256k1_scalar), &frame)
188 || !purify_bulletproof_add_scratch_frame(&capacity, frame, 4)
190 ROUND_TO_ALIGN(sizeof(secp256k1_scratch)), &allocation_size)) {
191 return NULL;
192 }
193 return secp256k1_scratch_space_create(ctx, capacity);
194}
195
197 const secp256k1_bulletproof_circuit* circuit, size_t* out) {
198 size_t scalars;
199 size_t bytes;
200 size_t points;
201 size_t prover = 0;
202 size_t verifier = 0;
203 size_t allocation_size;
204
205 /* The prover's compressed circuit stays live throughout the nested
206 * inner-product proof. Match both frames in the imported implementation.
207 */
208 if (!purify_bridge_checked_mul_size(5, circuit->n_gates, &scalars)
209 || !purify_bridge_checked_add_size(scalars, circuit->n_constraints, &scalars)
210 || !purify_bridge_checked_add_size(scalars, circuit->n_commits, &scalars)
211 || !purify_bridge_checked_mul_size(scalars, sizeof(secp256k1_scalar), &bytes)
213 || !purify_bulletproof_add_scratch_frame(&prover, bytes, 2)) {
214 return 0;
215 }
216 if (circuit->n_gates > 2) {
217 if (!purify_bridge_checked_mul_size(2, circuit->n_gates, &scalars)
218 || !purify_bridge_checked_mul_size(scalars, sizeof(secp256k1_scalar) + sizeof(secp256k1_ge), &bytes)
220 || !purify_bridge_checked_mul_size(points, sizeof(secp256k1_ge), &points)
221 || !purify_bridge_checked_add_size(bytes, points, &bytes)
222 || !purify_bulletproof_add_scratch_frame(&prover, bytes, 5)) {
223 return 0;
224 }
225 }
226
227 /* The bridge verifies one proof: outer verifier, compressed circuit, and
228 * inner-product verifier frames are simultaneously live during the MSM.
229 */
232 || !purify_bulletproof_add_scratch_frame(&verifier, bytes, 2)
233 || !purify_bridge_checked_mul_size(2, circuit->n_gates, &scalars)
234 || !purify_bridge_checked_add_size(scalars, circuit->n_constraints, &scalars)
235 || !purify_bridge_checked_add_size(scalars, circuit->n_commits, &scalars)
236 || !purify_bridge_checked_mul_size(scalars, sizeof(secp256k1_scalar), &bytes)
238 || !purify_bulletproof_add_scratch_frame(&verifier, bytes, 2)
239 || !purify_bridge_checked_add_size(sizeof(secp256k1_scalar),
241 || !purify_bulletproof_add_scratch_frame(&verifier, bytes, 2)
242 || !purify_bridge_checked_add_size(prover > verifier ? prover : verifier,
244 || !purify_bridge_checked_add_size(*out, ROUND_TO_ALIGN(sizeof(secp256k1_scratch)), &allocation_size)) {
245 return 0;
246 }
247 return 1;
248}
249
252 const secp256k1_bulletproof_circuit* circuit) {
253 size_t required;
254 secp256k1_scratch_space* replacement;
255
256 if (!purify_bulletproof_circuit_scratch_size(circuit, &required)) {
257 return 0;
258 }
259 if (required <= resources->scratch_capacity) {
260 return 1;
261 }
262 replacement = secp256k1_scratch_space_create(resources->ctx, required);
263 if (replacement == NULL) {
264 return 0;
265 }
267 secp256k1_scratch_space_destroy(resources->ctx, resources->scratch);
268 resources->scratch = replacement;
269 resources->scratch_capacity = required;
270 return 1;
271}
272
273static void* purify_malloc_array(size_t count, size_t elem_size) {
274 size_t bytes = 0;
275
276 if (count == 0) {
277 return NULL;
278 }
279 if (!purify_bridge_checked_mul_size(count, elem_size, &bytes)) {
280 return NULL;
281 }
282 return malloc(bytes);
283}
284
285static void* purify_calloc_array(size_t count, size_t elem_size) {
286 size_t bytes = 0;
287
288 if (count == 0) {
289 return NULL;
290 }
291 if (!purify_bridge_checked_mul_size(count, elem_size, &bytes)) {
292 return NULL;
293 }
294 return calloc(1, bytes);
295}
296
297static secp256k1_bppp_generators* purify_bppp_generators_clone(
298 const secp256k1_bppp_generators* generators) {
299 secp256k1_bppp_generators* clone;
300 size_t generator_bytes = 0;
301
302 if (generators == NULL || generators->gens == NULL || generators->n == 0 ||
303 !purify_bridge_checked_mul_size(generators->n, sizeof(*generators->gens), &generator_bytes)) {
304 return NULL;
305 }
306
307 clone = (secp256k1_bppp_generators*)calloc(1, sizeof(*clone));
308 if (clone == NULL) {
309 return NULL;
310 }
311 clone->n = generators->n;
312 clone->gens = (secp256k1_ge*)purify_malloc_array(generators->n, sizeof(*clone->gens));
313 if (clone->gens == NULL) {
314 free(clone);
315 return NULL;
316 }
317 memcpy(clone->gens, generators->gens, generator_bytes);
318 return clone;
319}
320
321/* BPPP proving folds the generator table in place, so cached resources keep one
322 pristine parsed copy and one resettable scratch copy for prove calls. */
324 size_t generator_bytes = 0;
325
326 if (resources == NULL || resources->gens == NULL || resources->gens->gens == NULL ||
327 resources->gens->n != resources->generators_count ||
328 resources->gens_scratch.gens == NULL || resources->gens_scratch.n != resources->generators_count ||
329 !purify_bridge_checked_mul_size(resources->generators_count, sizeof(*resources->gens->gens), &generator_bytes)) {
330 return 0;
331 }
332
333 memcpy(resources->gens_scratch.gens, resources->gens->gens, generator_bytes);
334 return 1;
335}
336
340 if (guard == NULL || resources == NULL || resources->gens_scratch_in_use) {
341 return NULL;
342 }
344 return NULL;
345 }
346 resources->gens_scratch_in_use = 1;
347 guard->resources = resources;
348 return &resources->gens_scratch;
349}
350
353 if (guard != NULL && guard->resources != NULL) {
354 guard->resources->gens_scratch_in_use = 0;
355 guard->resources = NULL;
356 }
357}
358
360 size_t n_gates) {
362 size_t generator_count = 0;
363 secp256k1_context* ctx = purify_context_handle(context);
364
365 if (ctx == NULL || n_gates == 0 || !secp256k1_is_power_of_two(n_gates)) {
366 return NULL;
367 }
368 if (!purify_bridge_checked_mul_size(2u, n_gates, &generator_count)) {
369 return NULL;
370 }
371
372 resources = (purify_bulletproof_backend_resources*)calloc(1, sizeof(*resources));
373 if (resources == NULL) {
374 return NULL;
375 }
376
377 resources->ctx = ctx;
378
380 resources->scratch = secp256k1_scratch_space_create(resources->ctx, resources->scratch_capacity);
381 if (resources->scratch == NULL) {
383 return NULL;
384 }
385
386 resources->gens = secp256k1_bulletproof_generators_create(resources->ctx, secp256k1_generator_h, generator_count, 1);
387 if (resources->gens == NULL || resources->gens->n < generator_count) {
389 return NULL;
390 }
391
392 resources->n_gates = n_gates;
393 return resources;
394}
395
397 const purify_bulletproof_backend_resources* resources) {
399 size_t generator_count = 0;
400
401 if (resources == NULL || resources->ctx == NULL || resources->n_gates == 0 ||
402 !purify_bridge_checked_mul_size(2u, resources->n_gates, &generator_count)) {
403 return NULL;
404 }
405
406 clone = (purify_bulletproof_backend_resources*)calloc(1, sizeof(*clone));
407 if (clone == NULL) {
408 return NULL;
409 }
410 clone->ctx = resources->ctx;
411 clone->n_gates = resources->n_gates;
412 clone->scratch_capacity = resources->scratch_capacity;
413 clone->scratch = secp256k1_scratch_space_create(clone->ctx, clone->scratch_capacity);
414 if (clone->scratch == NULL) {
416 return NULL;
417 }
419 clone->ctx, secp256k1_generator_h, generator_count, 1);
420 if (clone->gens == NULL || clone->gens->n < generator_count) {
422 return NULL;
423 }
424 return clone;
425}
426
428 if (resources == NULL) {
429 return;
430 }
431 if (resources->gens != NULL && resources->ctx != NULL) {
432 secp256k1_bulletproof_generators_destroy(resources->ctx, resources->gens);
433 }
434 if (resources->scratch != NULL && resources->ctx != NULL) {
436 secp256k1_scratch_space_destroy(resources->ctx, resources->scratch);
437 }
438 free(resources);
439}
440
442 const unsigned char* generators33,
443 size_t generators_count) {
445 size_t serialized_len = 0;
446 secp256k1_context* ctx = purify_context_handle(context);
447
448 if (ctx == NULL || generators33 == NULL || generators_count == 0) {
449 return NULL;
450 }
451 if (!purify_bridge_checked_mul_size(generators_count, 33u, &serialized_len)) {
452 return NULL;
453 }
454
455 resources = (purify_bppp_backend_resources*)calloc(1, sizeof(*resources));
456 if (resources == NULL) {
457 return NULL;
458 }
459
460 resources->ctx = ctx;
461
462 resources->scratch = purify_bppp_create_scratch(resources->ctx, generators_count);
463 if (resources->scratch == NULL) {
465 return NULL;
466 }
467
468 resources->gens = secp256k1_bppp_generators_parse(resources->ctx, generators33, serialized_len);
469 if (resources->gens == NULL || resources->gens->n != generators_count) {
471 return NULL;
472 }
473 resources->gens_scratch.n = generators_count;
474 resources->gens_scratch.gens =
475 (secp256k1_ge*)purify_malloc_array(generators_count, sizeof(*resources->gens_scratch.gens));
476 if (resources->gens_scratch.gens == NULL) {
478 return NULL;
479 }
480
481 resources->generators_count = generators_count;
482 return resources;
483}
484
486 const purify_bppp_backend_resources* resources) {
488 size_t generator_bytes = 0;
489
490 if (resources == NULL || resources->ctx == NULL || resources->generators_count == 0 ||
491 resources->gens == NULL || resources->gens->n != resources->generators_count ||
492 !purify_bridge_checked_mul_size(resources->generators_count, sizeof(*resources->gens->gens), &generator_bytes)) {
493 return NULL;
494 }
495
496 clone = (purify_bppp_backend_resources*)calloc(1, sizeof(*clone));
497 if (clone == NULL) {
498 return NULL;
499 }
500 clone->ctx = resources->ctx;
501 clone->generators_count = resources->generators_count;
503 if (clone->scratch == NULL) {
505 return NULL;
506 }
507 clone->gens = purify_bppp_generators_clone(resources->gens);
508 if (clone->gens == NULL) {
510 return NULL;
511 }
512 clone->gens_scratch.n = clone->generators_count;
513 clone->gens_scratch.gens =
514 (secp256k1_ge*)purify_malloc_array(clone->generators_count, sizeof(*clone->gens_scratch.gens));
515 if (clone->gens_scratch.gens == NULL) {
517 return NULL;
518 }
519 memcpy(clone->gens_scratch.gens, clone->gens->gens, generator_bytes);
520 return clone;
521}
522
524 if (resources == NULL) {
525 return;
526 }
527 if (resources->gens != NULL && resources->ctx != NULL) {
528 secp256k1_bppp_generators_destroy(resources->ctx, resources->gens);
529 }
530 if (resources->gens_scratch.gens != NULL) {
531 free(resources->gens_scratch.gens);
532 }
533 if (resources->scratch != NULL && resources->ctx != NULL) {
535 secp256k1_scratch_space_destroy(resources->ctx, resources->scratch);
536 }
537 free(resources);
538}
539
540static secp256k1_scalar* purify_scalar_cast(purify_scalar* scalar) {
541 return (secp256k1_scalar*)scalar;
542}
543
544static const secp256k1_scalar* purify_scalar_cast_const(const purify_scalar* scalar) {
545 return (const secp256k1_scalar*)scalar;
546}
547
548void purify_scalar_set_int(purify_scalar* out, unsigned int value) {
549 secp256k1_scalar_set_int(purify_scalar_cast(out), value);
550}
551
552void purify_scalar_set_u64(purify_scalar* out, uint64_t value) {
553 secp256k1_scalar_set_u64(purify_scalar_cast(out), value);
554}
555
556void purify_scalar_set_b32(purify_scalar* out, const unsigned char input32[32], int* overflow) {
557 secp256k1_scalar_set_b32(purify_scalar_cast(out), input32, overflow);
558}
559
560void purify_scalar_get_b32(unsigned char output32[32], const purify_scalar* value) {
561 secp256k1_scalar_get_b32(output32, purify_scalar_cast_const(value));
562}
563
565 return secp256k1_scalar_is_zero(purify_scalar_cast_const(value));
566}
567
569 return secp256k1_scalar_is_one(purify_scalar_cast_const(value));
570}
571
573 return secp256k1_scalar_is_even(purify_scalar_cast_const(value));
574}
575
576int purify_scalar_eq(const purify_scalar* lhs, const purify_scalar* rhs) {
577 return secp256k1_scalar_eq(purify_scalar_cast_const(lhs), purify_scalar_cast_const(rhs));
578}
579
581 secp256k1_scalar_negate(purify_scalar_cast(out), purify_scalar_cast_const(value));
582}
583
585 secp256k1_scalar_inverse(purify_scalar_cast(out), purify_scalar_cast_const(value));
586}
587
589 secp256k1_scalar_inverse_var(purify_scalar_cast(out), purify_scalar_cast_const(value));
590}
591
593 return secp256k1_scalar_add(purify_scalar_cast(out), purify_scalar_cast_const(lhs), purify_scalar_cast_const(rhs));
594}
595
596void purify_scalar_mul(purify_scalar* out, const purify_scalar* lhs, const purify_scalar* rhs) {
597 secp256k1_scalar_mul(purify_scalar_cast(out), purify_scalar_cast_const(lhs), purify_scalar_cast_const(rhs));
598}
599
600void purify_scalar_cmov(purify_scalar* dst, const purify_scalar* src, int flag) {
601 secp256k1_scalar_cmov(purify_scalar_cast(dst), purify_scalar_cast_const(src), flag);
602}
603
604void purify_sha256(unsigned char output32[32], const unsigned char *data,
605 size_t data_len) {
606 static const unsigned char zero = 0;
607 secp256k1_sha256 sha256;
608
609 if (data == NULL && data_len == 0) {
610 data = &zero;
611 }
612
613 secp256k1_sha256_initialize(&sha256);
614 if (data_len != 0) {
615 secp256k1_sha256_write(&sha256, data, data_len);
616 }
617 secp256k1_sha256_finalize(&sha256, output32);
618 secp256k1_sha256_clear(&sha256);
619}
620
621int purify_sha256_many(unsigned char output32[32],
622 const unsigned char *const *items,
623 const size_t *item_lens,
624 size_t items_count) {
625 secp256k1_sha256 sha256;
626 if (output32 == NULL) {
627 return 0;
628 }
629 if (items_count != 0 && (items == NULL || item_lens == NULL)) {
630 memset(output32, 0, 32);
631 return 0;
632 }
633 secp256k1_sha256_initialize(&sha256);
634 for (size_t i = 0; i < items_count; ++i) {
635 const unsigned char *item = items[i];
636 size_t item_len = item_lens[i];
637 if (item == NULL) {
638 if (item_len != 0) {
639 secp256k1_sha256_clear(&sha256);
640 memset(output32, 0, 32);
641 return 0;
642 }
643 continue;
644 }
645 if (item_len != 0) {
646 secp256k1_sha256_write(&sha256, item, item_len);
647 }
648 }
649 secp256k1_sha256_finalize(&sha256, output32);
650 secp256k1_sha256_clear(&sha256);
651 return 1;
652}
653
654void purify_hmac_sha256(unsigned char output32[32],
655 const unsigned char* key, size_t key_len,
656 const unsigned char* data, size_t data_len) {
657 static const unsigned char zero = 0;
658 secp256k1_hmac_sha256 hmac;
659
660 if (key == NULL && key_len == 0) {
661 key = &zero;
662 }
663 if (data == NULL && data_len == 0) {
664 data = &zero;
665 }
666
667 secp256k1_hmac_sha256_initialize(&hmac, key, key_len);
668 if (data_len != 0) {
669 secp256k1_hmac_sha256_write(&hmac, data, data_len);
670 }
671 secp256k1_hmac_sha256_finalize(&hmac, output32);
672 secp256k1_hmac_sha256_clear(&hmac);
673}
674
676 const secp256k1_context* ctx, unsigned char scalar32[32],
677 unsigned char xonly_pubkey32[32]) {
678 secp256k1_scalar scalar = {0};
679 secp256k1_ge point;
680 secp256k1_gej pointj;
681 int overflow = 0;
682 int ok = 0;
683
684 secp256k1_scalar_set_b32(&scalar, scalar32, &overflow);
685 if (overflow || secp256k1_scalar_is_zero(&scalar)) {
686 goto cleanup;
687 }
688 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pointj, &scalar);
689 secp256k1_ge_set_gej(&point, &pointj);
690 secp256k1_fe_normalize(&point.x);
691 secp256k1_fe_normalize(&point.y);
692 secp256k1_scalar_cond_negate(
693 &scalar, secp256k1_fe_is_odd(&point.y));
694 secp256k1_scalar_get_b32(scalar32, &scalar);
695 secp256k1_fe_get_b32(xonly_pubkey32, &point.x);
696 ok = 1;
697
698cleanup:
699 secp256k1_scalar_clear(&scalar);
700 purify_bridge_secure_clear(&point, sizeof(point));
701 purify_bridge_secure_clear(&pointj, sizeof(pointj));
702 return ok;
703}
704
706 unsigned char seckey32[32],
707 unsigned char xonly_pubkey32[32]) {
708 secp256k1_context* ctx = purify_context_handle(context);
709 int ok = 0;
710
711 if (xonly_pubkey32 != NULL) {
712 memset(xonly_pubkey32, 0, 32);
713 }
714
715 if (ctx == NULL || seckey32 == NULL || xonly_pubkey32 == NULL) {
716 return 0;
717 }
718
720 ctx, seckey32, xonly_pubkey32);
721 if (!ok) {
722 purify_bridge_secure_clear(seckey32, 32);
723 memset(xonly_pubkey32, 0, 32);
724 }
725 return ok;
726}
727
729 unsigned char scalar32[32],
730 unsigned char xonly_nonce32[32]) {
731 secp256k1_context* ctx = purify_context_handle(context);
732 int ok = 0;
733
734 if (xonly_nonce32 != NULL) {
735 memset(xonly_nonce32, 0, 32);
736 }
737
738 if (ctx == NULL || scalar32 == NULL || xonly_nonce32 == NULL) {
739 return 0;
740 }
741
743 ctx, scalar32, xonly_nonce32);
744 if (!ok) {
745 purify_bridge_secure_clear(scalar32, 32);
746 memset(xonly_nonce32, 0, 32);
747 }
748 return ok;
749}
750
752 const unsigned char point33[33],
753 unsigned char xonly32[32],
754 int* parity_out) {
755 secp256k1_context* ctx = purify_context_handle(context);
756 secp256k1_pubkey pubkey;
757 secp256k1_xonly_pubkey xonly;
758 int parity = 0;
759 int ok = 0;
760
761 memset(&pubkey, 0, sizeof(pubkey));
762 memset(&xonly, 0, sizeof(xonly));
763 if (xonly32 != NULL) {
764 memset(xonly32, 0, 32);
765 }
766 if (parity_out != NULL) {
767 *parity_out = 0;
768 }
769
770 if (ctx == NULL || point33 == NULL || xonly32 == NULL) {
771 return 0;
772 }
773
774 ok = secp256k1_ec_pubkey_parse(ctx, &pubkey, point33, 33);
775 if (ok) {
776 ok = secp256k1_xonly_pubkey_from_pubkey(ctx, &xonly, &parity, &pubkey);
777 }
778 if (ok) {
779 ok = secp256k1_xonly_pubkey_serialize(ctx, xonly32, &xonly);
780 }
781 if (ok && parity_out != NULL) {
782 *parity_out = parity;
783 }
784 if (!ok) {
785 memset(xonly32, 0, 32);
786 }
787
788 purify_bridge_secure_clear(&pubkey, sizeof(pubkey));
789 purify_bridge_secure_clear(&xonly, sizeof(xonly));
790 return ok;
791}
792
794 const unsigned char xonly_pubkey32[32]) {
795 secp256k1_context* ctx = purify_context_handle(context);
796 secp256k1_xonly_pubkey xonly;
797 int ok = 0;
798
799 memset(&xonly, 0, sizeof(xonly));
800 if (ctx == NULL || xonly_pubkey32 == NULL) {
801 return 0;
802 }
803
804 ok = secp256k1_xonly_pubkey_parse(ctx, &xonly, xonly_pubkey32);
805
806 purify_bridge_secure_clear(&xonly, sizeof(xonly));
807 return ok;
808}
809
811 const unsigned char sig64[64]) {
812 secp256k1_context* ctx = purify_context_handle(context);
813 secp256k1_xonly_pubkey rxonly;
814 secp256k1_scalar s;
815 int overflow = 0;
816 int ok = 0;
817
818 purify_bridge_secure_clear(&rxonly, sizeof(rxonly));
819 purify_bridge_secure_clear(&s, sizeof(s));
820 if (ctx == NULL || sig64 == NULL) {
821 return 0;
822 }
823
824 ok = secp256k1_xonly_pubkey_parse(ctx, &rxonly, sig64);
825 if (ok) {
826 secp256k1_scalar_set_b32(&s, sig64 + 32, &overflow);
827 ok = !overflow;
828 }
829
830 purify_bridge_secure_clear(&rxonly, sizeof(rxonly));
831 secp256k1_scalar_clear(&s);
832 return ok;
833}
834
835static int purify_fixed_nonce_function(unsigned char *nonce32,
836 const unsigned char *msg, size_t msglen,
837 const unsigned char *key32,
838 const unsigned char *xonly_pk32,
839 const unsigned char *algo, size_t algolen,
840 void *data) {
841 (void)msg;
842 (void)msglen;
843 (void)key32;
844 (void)xonly_pk32;
845 (void)algo;
846 (void)algolen;
847
848 if (nonce32 == NULL || data == NULL) {
849 return 0;
850 }
851 memcpy(nonce32, data, 32);
852 return 1;
853}
854
856 unsigned char sig64[64],
857 const unsigned char* msg, size_t msglen,
858 const unsigned char seckey32[32],
859 const unsigned char nonce32[32]) {
860 secp256k1_context* ctx = purify_context_handle(context);
861 secp256k1_keypair keypair;
862 secp256k1_scalar nonce_scalar;
863 secp256k1_schnorrsig_extraparams extraparams = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;
864 unsigned char canonical_nonce32[32];
865 unsigned char xonly_nonce32[32];
866 int ok = 0;
867 int overflow = 0;
868
869 purify_bridge_secure_clear(&keypair, sizeof(keypair));
870 purify_bridge_secure_clear(&nonce_scalar, sizeof(nonce_scalar));
871 purify_bridge_secure_clear(canonical_nonce32, sizeof(canonical_nonce32));
872 purify_bridge_secure_clear(xonly_nonce32, sizeof(xonly_nonce32));
873 if (sig64 != NULL) {
874 memset(sig64, 0, 64);
875 }
876
877 if (ctx == NULL || sig64 == NULL || seckey32 == NULL || nonce32 == NULL || (msg == NULL && msglen != 0)) {
878 return 0;
879 }
880
881 memcpy(canonical_nonce32, nonce32, 32);
882 ok = purify_bip340_nonce_from_scalar(context, canonical_nonce32, xonly_nonce32);
883 if (ok) {
885 canonical_nonce32, nonce32, sizeof(canonical_nonce32));
886 }
887 secp256k1_scalar_set_b32(&nonce_scalar, nonce32, &overflow);
888 if (ok && !overflow && !secp256k1_scalar_is_zero(&nonce_scalar)) {
889 ok = secp256k1_keypair_create(ctx, &keypair, seckey32);
890 if (ok) {
891 extraparams.noncefp = purify_fixed_nonce_function;
892 extraparams.ndata = (void*)nonce32;
893 ok = secp256k1_schnorrsig_sign_custom(ctx, sig64, msg, msglen, &keypair, &extraparams);
894 }
895 }
896
897 purify_bridge_secure_clear(&keypair, sizeof(keypair));
898 secp256k1_scalar_clear(&nonce_scalar);
899 purify_bridge_secure_clear(canonical_nonce32, sizeof(canonical_nonce32));
900 purify_bridge_secure_clear(xonly_nonce32, sizeof(xonly_nonce32));
901 if (!ok) {
902 memset(sig64, 0, 64);
903 }
904 return ok;
905}
906
908 const unsigned char sig64[64],
909 const unsigned char* msg, size_t msglen,
910 const unsigned char xonly_pubkey32[32]) {
911 secp256k1_context* ctx = purify_context_handle(context);
912 secp256k1_xonly_pubkey pubkey;
913 int ok = 0;
914
915 memset(&pubkey, 0, sizeof(pubkey));
916 if (ctx == NULL || sig64 == NULL || xonly_pubkey32 == NULL || (msg == NULL && msglen != 0)) {
917 return 0;
918 }
919
920 ok = secp256k1_xonly_pubkey_parse(ctx, &pubkey, xonly_pubkey32);
921 if (ok) {
922 ok = secp256k1_schnorrsig_verify(ctx, sig64, msg, msglen, &pubkey);
923 }
924
925 memset(&pubkey, 0, sizeof(pubkey));
926 return ok;
927}
928
929static int purify_parse_scalar(const unsigned char input32[32], secp256k1_scalar* scalar, int reject_zero) {
930 int overflow = 0;
931 secp256k1_scalar_set_b32(scalar, input32, &overflow);
932 if (overflow) {
933 return 0;
934 }
935 if (reject_zero && secp256k1_scalar_is_zero(scalar)) {
936 return 0;
937 }
938 return 1;
939}
940
941static int purify_parse_scalar_array(const unsigned char* input32, size_t count, secp256k1_scalar* out) {
942 size_t i;
943 for (i = 0; i < count; ++i) {
944 if (!purify_parse_scalar(input32 + 32 * i, &out[i], 0)) {
945 return 0;
946 }
947 }
948 return 1;
949}
950
951static int purify_serialize_point(unsigned char out33[33], secp256k1_ge* point) {
952 /* This helper also serializes prover commitments derived from secret
953 * witnesses. Keep normalization constant-time; serialization is the
954 * deliberate declassification boundary. */
955 secp256k1_fe_normalize(&point->x);
956 secp256k1_fe_normalize(&point->y);
957 return secp256k1_bppp_serialize_pt(out33, point);
958}
959
960static int purify_bridge_memeq_const(const unsigned char* lhs,
961 const unsigned char* rhs,
962 size_t size) {
963 volatile unsigned char difference = 0;
964 size_t i;
965
966 for (i = 0; i < size; ++i) {
967 difference |= lhs[i] ^ rhs[i];
968 }
969 return difference == 0;
970}
971
972static int purify_parse_fast_scalar(const unsigned char input32[32], secp256k1_fast_scalar* scalar) {
973 secp256k1_scalar one, two, neg_one, neg_two;
974
975 if (!purify_parse_scalar(input32, &scalar->scal, 0)) {
976 return 0;
977 }
978
979 secp256k1_scalar_set_int(&one, 1);
980 secp256k1_scalar_set_int(&two, 2);
981 secp256k1_scalar_negate(&neg_one, &one);
982 secp256k1_scalar_negate(&neg_two, &two);
983
984 scalar->special = 3;
985 if (secp256k1_scalar_is_zero(&scalar->scal)) {
986 scalar->special = 0;
987 } else if (secp256k1_scalar_eq(&scalar->scal, &one)) {
988 scalar->special = 1;
989 } else if (secp256k1_scalar_eq(&scalar->scal, &two)) {
990 scalar->special = 2;
991 } else if (secp256k1_scalar_eq(&scalar->scal, &neg_one)) {
992 scalar->special = -1;
993 } else if (secp256k1_scalar_eq(&scalar->scal, &neg_two)) {
994 scalar->special = -2;
995 }
996 return 1;
997}
998
999static int purify_parse_generator_as_ge(const secp256k1_context* ctx, const unsigned char generator33[33], secp256k1_ge* out) {
1000 secp256k1_generator generator;
1001 if (ctx == NULL || generator33 == NULL || out == NULL) {
1002 return 0;
1003 }
1004 if (!secp256k1_generator_parse(ctx, &generator, generator33)) {
1005 return 0;
1006 }
1007 secp256k1_generator_load(out, &generator);
1008 return 1;
1009}
1010
1011static int purify_parse_point_as_ge(const unsigned char point33[33], secp256k1_ge* out) {
1012 if (point33 == NULL || out == NULL) {
1013 return 0;
1014 }
1015 return secp256k1_eckey_pubkey_parse(out, point33, 33);
1016}
1017
1019 if (circuit == NULL) {
1020 return;
1021 }
1022 free(circuit->wl);
1023 free(circuit->wr);
1024 free(circuit->wo);
1025 free(circuit->wv);
1026 free(circuit->c);
1027 free(circuit->entries);
1028 memset(circuit, 0, sizeof(*circuit));
1029}
1030
1032 if (assignment == NULL) {
1033 return;
1034 }
1035 purify_bridge_secure_free_array(assignment->al, assignment->n_gates, sizeof(*assignment->al));
1036 purify_bridge_secure_free_array(assignment->ar, assignment->n_gates, sizeof(*assignment->ar));
1037 purify_bridge_secure_free_array(assignment->ao, assignment->n_gates, sizeof(*assignment->ao));
1038 purify_bridge_secure_free_array(assignment->v, assignment->n_commits, sizeof(*assignment->v));
1039 memset(assignment, 0, sizeof(*assignment));
1040}
1041
1042static size_t purify_total_row_entries(const purify_bulletproof_row_view* rows, size_t count, int* ok) {
1043 size_t total = 0;
1044 size_t i;
1045
1046 if (ok != NULL) {
1047 *ok = 0;
1048 }
1049 if (count == 0) {
1050 if (ok != NULL) {
1051 *ok = 1;
1052 }
1053 return 0;
1054 }
1055 if (rows == NULL) {
1056 return 0;
1057 }
1058 for (i = 0; i < count; ++i) {
1059 if (rows[i].size != 0 && (rows[i].indices == NULL || rows[i].scalars32 == NULL)) {
1060 return 0;
1061 }
1062 if (!purify_bridge_checked_add_size(total, rows[i].size, &total)) {
1063 return 0;
1064 }
1065 }
1066 if (ok != NULL) {
1067 *ok = 1;
1068 }
1069 return total;
1070}
1071
1073 size_t row_count,
1074 const purify_bulletproof_row_view* in_rows,
1075 size_t n_constraints,
1077 size_t* offset) {
1078 size_t i;
1079 for (i = 0; i < row_count; ++i) {
1080 size_t j;
1081 out_rows[i].size = in_rows[i].size;
1082 out_rows[i].entry = in_rows[i].size == 0 ? NULL : entries + *offset;
1083 for (j = 0; j < in_rows[i].size; ++j) {
1084 size_t entry_idx = *offset + j;
1085 if (in_rows[i].indices[j] >= n_constraints) {
1086 return 0;
1087 }
1088 out_rows[i].entry[j].idx = in_rows[i].indices[j];
1089 if (!purify_parse_fast_scalar(in_rows[i].scalars32 + 32 * j, &out_rows[i].entry[j].scal)) {
1090 return 0;
1091 }
1092 entries[entry_idx] = out_rows[i].entry[j];
1093 }
1094 *offset += in_rows[i].size;
1095 }
1096 return 1;
1097}
1098
1099static void purify_bulletproof_circuit_evaluate_sum_row(secp256k1_scalar* acc,
1101 const secp256k1_scalar* assn) {
1102 size_t j;
1103 for (j = 0; j < row->size; ++j) {
1104 secp256k1_scalar term;
1105 secp256k1_fast_scalar_mul(&term, &row->entry[j].scal, assn);
1106 secp256k1_scalar_add(&acc[row->entry[j].idx], &acc[row->entry[j].idx], &term);
1107 secp256k1_scalar_clear(&term);
1108 }
1109}
1110
1112 const secp256k1_bulletproof_circuit_assignment* assignment) {
1113 secp256k1_scalar* acc = NULL;
1114 size_t i;
1115 int ok = 0;
1116
1117 if (circuit == NULL || assignment == NULL) {
1118 return 0;
1119 }
1120 if (assignment->n_gates != circuit->n_gates || assignment->n_commits != circuit->n_commits) {
1121 return 0;
1122 }
1123
1124 acc = circuit->n_constraints == 0 ? NULL : (secp256k1_scalar*)calloc(circuit->n_constraints, sizeof(*acc));
1125 if (circuit->n_constraints != 0 && acc == NULL) {
1126 return 0;
1127 }
1128
1129 for (i = 0; i < assignment->n_gates; ++i) {
1130 secp256k1_scalar product;
1131 int matches;
1132 secp256k1_scalar_mul(&product, &assignment->al[i], &assignment->ar[i]);
1133 matches = secp256k1_scalar_eq(&product, &assignment->ao[i]);
1134 secp256k1_scalar_clear(&product);
1135 if (!matches) {
1136 goto done;
1137 }
1138 }
1139
1140 for (i = 0; i < circuit->n_constraints; ++i) {
1141 secp256k1_scalar_clear(&acc[i]);
1142 }
1143
1144 for (i = 0; i < circuit->n_gates; ++i) {
1145 purify_bulletproof_circuit_evaluate_sum_row(acc, &circuit->wl[i], &assignment->al[i]);
1146 purify_bulletproof_circuit_evaluate_sum_row(acc, &circuit->wr[i], &assignment->ar[i]);
1147 purify_bulletproof_circuit_evaluate_sum_row(acc, &circuit->wo[i], &assignment->ao[i]);
1148 }
1149 for (i = 0; i < circuit->n_commits; ++i) {
1150 secp256k1_scalar negated_v;
1151 secp256k1_scalar_negate(&negated_v, &assignment->v[i]);
1152 purify_bulletproof_circuit_evaluate_sum_row(acc, &circuit->wv[i], &negated_v);
1153 secp256k1_scalar_clear(&negated_v);
1154 }
1155 for (i = 0; i < circuit->n_constraints; ++i) {
1156 secp256k1_scalar constant_term;
1157 secp256k1_scalar one;
1158 int matches;
1159 secp256k1_scalar_set_int(&one, 1);
1160 secp256k1_fast_scalar_mul(&constant_term, &circuit->c[i], &one);
1161 matches = secp256k1_scalar_eq(&acc[i], &constant_term);
1162 secp256k1_scalar_clear(&constant_term);
1163 secp256k1_scalar_clear(&one);
1164 if (!matches) {
1165 goto done;
1166 }
1167 }
1168 ok = 1;
1169
1170done:
1171 purify_bridge_secure_free_array(acc, circuit->n_constraints, sizeof(*acc));
1172 return ok;
1173}
1174
1177 int ok = 0;
1178 size_t row_entries = 0;
1179 size_t total_entries = 0;
1180 size_t offset = 0;
1181 size_t i;
1182
1183 /* Bit/range-style circuits can have no explicit constraint rows, so the
1184 * constant vector is optional when n_constraints == 0.
1185 */
1186 if (view == NULL || out == NULL || (view->n_constraints != 0 && view->c32 == NULL)) {
1187 return 0;
1188 }
1189 if (!secp256k1_is_power_of_two(view->n_gates)) {
1190 return 0;
1191 }
1192 memset(out, 0, sizeof(*out));
1193
1194 row_entries = purify_total_row_entries(view->wl, view->n_gates, &ok);
1195 if (!ok || !purify_bridge_checked_add_size(total_entries, row_entries, &total_entries)) return 0;
1196 row_entries = purify_total_row_entries(view->wr, view->n_gates, &ok);
1197 if (!ok || !purify_bridge_checked_add_size(total_entries, row_entries, &total_entries)) return 0;
1198 row_entries = purify_total_row_entries(view->wo, view->n_gates, &ok);
1199 if (!ok || !purify_bridge_checked_add_size(total_entries, row_entries, &total_entries)) return 0;
1200 row_entries = purify_total_row_entries(view->wv, view->n_commits, &ok);
1201 if (!ok || !purify_bridge_checked_add_size(total_entries, row_entries, &total_entries)) return 0;
1202
1203 out->n_gates = view->n_gates;
1204 out->n_commits = view->n_commits;
1205 out->n_bits = view->n_bits;
1206 out->n_constraints = view->n_constraints;
1207 out->wl = (secp256k1_bulletproof_wmatrix_row*)purify_calloc_array(view->n_gates, sizeof(*out->wl));
1208 out->wr = (secp256k1_bulletproof_wmatrix_row*)purify_calloc_array(view->n_gates, sizeof(*out->wr));
1209 out->wo = (secp256k1_bulletproof_wmatrix_row*)purify_calloc_array(view->n_gates, sizeof(*out->wo));
1211 /* Mirror the legacy circuit representation: no explicit constraints means
1212 * no c vector allocation either.
1213 */
1214 out->c = (secp256k1_fast_scalar*)purify_malloc_array(view->n_constraints, sizeof(*out->c));
1215 out->entries = (secp256k1_bulletproof_wmatrix_entry*)purify_malloc_array(total_entries, sizeof(*out->entries));
1216 if ((view->n_gates != 0 && (out->wl == NULL || out->wr == NULL || out->wo == NULL)) ||
1217 (view->n_commits != 0 && out->wv == NULL) ||
1218 (view->n_constraints != 0 && out->c == NULL) ||
1219 (total_entries != 0 && out->entries == NULL)) {
1221 return 0;
1222 }
1223
1224 if (!purify_build_row_family(out->wl, view->n_gates, view->wl, view->n_constraints, out->entries, &offset) ||
1225 !purify_build_row_family(out->wr, view->n_gates, view->wr, view->n_constraints, out->entries, &offset) ||
1226 !purify_build_row_family(out->wo, view->n_gates, view->wo, view->n_constraints, out->entries, &offset) ||
1227 !purify_build_row_family(out->wv, view->n_commits, view->wv, view->n_constraints, out->entries, &offset)) {
1229 return 0;
1230 }
1231 for (i = 0; i < view->n_constraints; ++i) {
1232 if (!purify_parse_fast_scalar(view->c32 + 32 * i, &out->c[i])) {
1234 return 0;
1235 }
1236 }
1237 return 1;
1238}
1239
1242 if (view == NULL || out == NULL) {
1243 return 0;
1244 }
1245 if ((view->n_gates != 0 && (view->al32 == NULL || view->ar32 == NULL || view->ao32 == NULL)) ||
1246 (view->n_commits != 0 && view->v32 == NULL)) {
1247 return 0;
1248 }
1249 memset(out, 0, sizeof(*out));
1250 out->n_gates = view->n_gates;
1251 out->n_commits = view->n_commits;
1252 out->al = (secp256k1_scalar*)purify_malloc_array(view->n_gates, sizeof(*out->al));
1253 out->ar = (secp256k1_scalar*)purify_malloc_array(view->n_gates, sizeof(*out->ar));
1254 out->ao = (secp256k1_scalar*)purify_malloc_array(view->n_gates, sizeof(*out->ao));
1255 out->v = (secp256k1_scalar*)purify_malloc_array(view->n_commits, sizeof(*out->v));
1256 if ((view->n_gates != 0 && (out->al == NULL || out->ar == NULL || out->ao == NULL)) ||
1257 (view->n_commits != 0 && out->v == NULL)) {
1259 return 0;
1260 }
1261 if (!purify_parse_scalar_array(view->al32, view->n_gates, out->al) ||
1262 !purify_parse_scalar_array(view->ar32, view->n_gates, out->ar) ||
1263 !purify_parse_scalar_array(view->ao32, view->n_gates, out->ao) ||
1264 !purify_parse_scalar_array(view->v32, view->n_commits, out->v)) {
1266 return 0;
1267 }
1268 return 1;
1269}
1270
1272 size_t proof_size = 0;
1273
1274 if (n_gates == 0) {
1275 return 0;
1276 }
1278 return 0;
1279 }
1280 return proof_size;
1281}
1282
1284 const purify_bulletproof_assignment_view* assignment,
1285 const unsigned char* blinds32,
1286 const unsigned char value_gen33[33],
1287 const unsigned char nonce32[32],
1288 const unsigned char* extra_commit,
1289 size_t extra_commit_len,
1290 unsigned char* commitments_out33,
1291 unsigned char* proof_out,
1292 size_t* proof_len,
1293 int require_valid_assignment,
1294 purify_secp_context* context,
1296 secp256k1_context* ctx = NULL;
1297 secp256k1_scratch_space* scratch = NULL;
1301 secp256k1_ge value_gen;
1302 secp256k1_ge* commit_points = NULL;
1303 secp256k1_scalar* blinds = NULL;
1304 size_t n_commits = 0;
1305 size_t required_generators = 0;
1306 size_t commitment_bytes = 0;
1307 size_t scratch_checkpoint = 0;
1308 size_t i;
1309 int ok = 0;
1310 int owns_resources = 0;
1311 int scratch_contains_secrets = 0;
1312
1313 memset(&bp_circuit, 0, sizeof(bp_circuit));
1314 memset(&bp_assignment, 0, sizeof(bp_assignment));
1315
1316 if (circuit == NULL || assignment == NULL || value_gen33 == NULL || nonce32 == NULL || proof_out == NULL || proof_len == NULL) {
1317 return 0;
1318 }
1319 if (extra_commit == NULL && extra_commit_len != 0) {
1320 return 0;
1321 }
1322 if (!purify_build_bulletproof_circuit(circuit, &bp_circuit) ||
1323 !purify_build_bulletproof_assignment(assignment, &bp_assignment)) {
1324 goto done;
1325 }
1326 if (bp_assignment.n_gates != bp_circuit.n_gates || bp_assignment.n_commits != bp_circuit.n_commits) {
1327 goto done;
1328 }
1329 if (require_valid_assignment && !purify_bulletproof_circuit_evaluate(&bp_circuit, &bp_assignment)) {
1330 goto done;
1331 }
1332 if (resources == NULL) {
1333 resources = purify_bulletproof_backend_resources_create(context, bp_circuit.n_gates);
1334 if (resources == NULL) {
1335 goto done;
1336 }
1337 owns_resources = 1;
1338 }
1339 if (resources->n_gates < bp_circuit.n_gates || resources->ctx == NULL || resources->scratch == NULL || resources->gens == NULL) {
1340 goto done;
1341 }
1342 if (!purify_bulletproof_reserve_circuit_scratch(resources, &bp_circuit)) {
1343 goto done;
1344 }
1345 ctx = resources->ctx;
1346 scratch = resources->scratch;
1347 gens = resources->gens;
1348 if (!purify_bridge_checked_mul_size(2u, bp_circuit.n_gates, &required_generators) ||
1349 gens->n < required_generators) {
1350 goto done;
1351 }
1352 if (!purify_parse_generator_as_ge(ctx, value_gen33, &value_gen)) {
1353 goto done;
1354 }
1355
1356 n_commits = bp_circuit.n_commits;
1357 if (!purify_bridge_checked_mul_size(n_commits, 33u, &commitment_bytes)) {
1358 goto done;
1359 }
1360 if (n_commits != 0) {
1361 if (commitments_out33 == NULL) {
1362 goto done;
1363 }
1364 memset(commitments_out33, 0, commitment_bytes);
1365 commit_points = (secp256k1_ge*)purify_malloc_array(n_commits, sizeof(*commit_points));
1366 blinds = (secp256k1_scalar*)purify_calloc_array(n_commits, sizeof(*blinds));
1367 if (commit_points == NULL || blinds == NULL) {
1368 goto done;
1369 }
1370 }
1371 for (i = 0; i < n_commits; ++i) {
1372 secp256k1_gej commitj;
1373 if (blinds32 != NULL && !purify_parse_scalar(blinds32 + 32u * i, &blinds[i], 0)) {
1374 goto done;
1375 }
1376 secp256k1_pedersen_ecmult_scalar(&commitj, &blinds[i], &bp_assignment.v[i], &value_gen, gens->blinding_gen);
1377 if (secp256k1_gej_is_infinity(&commitj)) {
1378 goto done;
1379 }
1380 secp256k1_ge_set_gej(&commit_points[i], &commitj);
1381 if (!purify_serialize_point(commitments_out33 + 33u * i, &commit_points[i])) {
1382 goto done;
1383 }
1384 }
1385
1386 scratch_checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
1387 scratch_contains_secrets = 1;
1389 &ctx->error_callback,
1390 scratch,
1391 proof_out,
1392 proof_len,
1393 &bp_assignment,
1394 n_commits == 0 ? NULL : commit_points,
1395 n_commits == 0 ? NULL : blinds,
1396 n_commits,
1397 &value_gen,
1398 &bp_circuit,
1399 gens,
1400 nonce32,
1401 extra_commit,
1402 extra_commit_len
1403 );
1404
1405done:
1406 if (scratch_contains_secrets) {
1407 purify_bridge_secure_clear_scratch_tail(scratch, scratch_checkpoint);
1408 }
1409 if (blinds != NULL) {
1410 for (i = 0; i < n_commits; ++i) secp256k1_scalar_clear(&blinds[i]);
1411 }
1412 free(blinds);
1413 free(commit_points);
1414 purify_free_bulletproof_assignment(&bp_assignment);
1416 if (owns_resources) {
1418 }
1419 return ok;
1420}
1421
1423 const purify_bulletproof_circuit_view* circuit,
1424 const purify_bulletproof_assignment_view* assignment,
1425 const unsigned char* blinds32,
1426 const unsigned char value_gen33[33],
1427 const unsigned char nonce32[32],
1428 const unsigned char* extra_commit,
1429 size_t extra_commit_len,
1430 unsigned char* commitments_out33,
1431 unsigned char* proof_out,
1432 size_t* proof_len) {
1433 return purify_bulletproof_prove_circuit_impl(circuit, assignment, blinds32, value_gen33, nonce32,
1434 extra_commit, extra_commit_len, commitments_out33,
1435 proof_out, proof_len, 1, context, NULL);
1436}
1437
1439 const purify_bulletproof_circuit_view* circuit,
1440 const purify_bulletproof_assignment_view* assignment,
1441 const unsigned char* blinds32,
1442 const unsigned char value_gen33[33],
1443 const unsigned char nonce32[32],
1444 const unsigned char* extra_commit,
1445 size_t extra_commit_len,
1446 unsigned char* commitments_out33,
1447 unsigned char* proof_out,
1448 size_t* proof_len) {
1449 return purify_bulletproof_prove_circuit_impl(circuit, assignment, blinds32, value_gen33, nonce32,
1450 extra_commit, extra_commit_len, commitments_out33,
1451 proof_out, proof_len, 1, NULL, resources);
1452}
1453
1455 const purify_bulletproof_circuit_view* circuit,
1456 const purify_bulletproof_assignment_view* assignment,
1457 const unsigned char* blinds32,
1458 const unsigned char value_gen33[33],
1459 const unsigned char nonce32[32],
1460 const unsigned char* extra_commit,
1461 size_t extra_commit_len,
1462 unsigned char* commitments_out33,
1463 unsigned char* proof_out,
1464 size_t* proof_len) {
1465 return purify_bulletproof_prove_circuit_impl(circuit, assignment, blinds32, value_gen33, nonce32,
1466 extra_commit, extra_commit_len, commitments_out33,
1467 proof_out, proof_len, 0, context, NULL);
1468}
1469
1471 const purify_bulletproof_circuit_view* circuit,
1472 const purify_bulletproof_assignment_view* assignment,
1473 const unsigned char* blinds32,
1474 const unsigned char value_gen33[33],
1475 const unsigned char nonce32[32],
1476 const unsigned char* extra_commit,
1477 size_t extra_commit_len,
1478 unsigned char* commitments_out33,
1479 unsigned char* proof_out,
1480 size_t* proof_len) {
1481 return purify_bulletproof_prove_circuit_impl(circuit, assignment, blinds32, value_gen33, nonce32,
1482 extra_commit, extra_commit_len, commitments_out33,
1483 proof_out, proof_len, 0, NULL, resources);
1484}
1485
1488 const purify_bulletproof_circuit_view* circuit,
1489 const unsigned char* commitments33,
1490 const unsigned char value_gen33[33],
1491 const unsigned char* extra_commit,
1492 size_t extra_commit_len,
1493 const unsigned char* proof,
1494 size_t proof_len);
1495
1497 const purify_bulletproof_circuit_view* circuit,
1498 const unsigned char* commitments33,
1499 const unsigned char value_gen33[33],
1500 const unsigned char* extra_commit,
1501 size_t extra_commit_len,
1502 const unsigned char* proof,
1503 size_t proof_len) {
1504 return purify_bulletproof_verify_circuit_impl(context, NULL, circuit, commitments33, value_gen33,
1505 extra_commit, extra_commit_len, proof, proof_len);
1506}
1507
1510 const purify_bulletproof_circuit_view* circuit,
1511 const unsigned char* commitments33,
1512 const unsigned char value_gen33[33],
1513 const unsigned char* extra_commit,
1514 size_t extra_commit_len,
1515 const unsigned char* proof,
1516 size_t proof_len) {
1517 secp256k1_context* ctx = NULL;
1518 secp256k1_scratch_space* scratch = NULL;
1521 secp256k1_ge value_gen;
1522 secp256k1_ge* commit_points = NULL;
1523 const secp256k1_ge* commit_ptr = NULL;
1524 const secp256k1_bulletproof_circuit* circuit_ptr = NULL;
1525 const unsigned char* proof_ptr = NULL;
1526 const unsigned char* extra_commit_ptr = NULL;
1527 size_t n_commits = 0;
1528 size_t required_generators = 0;
1529 size_t commitment_bytes = 0;
1530 size_t i;
1531 int ok = 0;
1532 int owns_resources = 0;
1533
1534 memset(&bp_circuit, 0, sizeof(bp_circuit));
1535 if (circuit == NULL || value_gen33 == NULL || proof == NULL) {
1536 return 0;
1537 }
1538 if (extra_commit == NULL && extra_commit_len != 0) {
1539 return 0;
1540 }
1541 if (!purify_build_bulletproof_circuit(circuit, &bp_circuit)) {
1542 goto done;
1543 }
1544 if (resources == NULL) {
1545 resources = purify_bulletproof_backend_resources_create(context, bp_circuit.n_gates);
1546 if (resources == NULL) {
1547 goto done;
1548 }
1549 owns_resources = 1;
1550 }
1551 if (resources->n_gates < bp_circuit.n_gates || resources->ctx == NULL || resources->scratch == NULL || resources->gens == NULL) {
1552 goto done;
1553 }
1554 if (!purify_bulletproof_reserve_circuit_scratch(resources, &bp_circuit)) {
1555 goto done;
1556 }
1557 ctx = resources->ctx;
1558 scratch = resources->scratch;
1559 gens = resources->gens;
1560 if (!purify_bridge_checked_mul_size(2u, bp_circuit.n_gates, &required_generators) ||
1561 gens->n < required_generators) {
1562 goto done;
1563 }
1564 if (!purify_parse_generator_as_ge(ctx, value_gen33, &value_gen)) {
1565 goto done;
1566 }
1567
1568 n_commits = bp_circuit.n_commits;
1569 if (!purify_bridge_checked_mul_size(n_commits, 33u, &commitment_bytes)) {
1570 goto done;
1571 }
1572 if (n_commits != 0) {
1573 if (commitments33 == NULL) {
1574 goto done;
1575 }
1576 commit_points = (secp256k1_ge*)purify_malloc_array(n_commits, sizeof(*commit_points));
1577 if (commit_points == NULL) {
1578 goto done;
1579 }
1580 for (i = 0; i < n_commits; ++i) {
1581 if (!purify_parse_point_as_ge(commitments33 + 33u * i, &commit_points[i])) {
1582 goto done;
1583 }
1584 }
1585 commit_ptr = commit_points;
1586 }
1587
1588 circuit_ptr = &bp_circuit;
1589 proof_ptr = proof;
1590 extra_commit_ptr = extra_commit;
1592 &ctx->error_callback,
1593 scratch,
1594 &proof_ptr,
1595 1,
1596 proof_len,
1597 n_commits == 0 ? NULL : &commit_ptr,
1598 n_commits == 0 ? NULL : &n_commits,
1599 &value_gen,
1600 &circuit_ptr,
1601 gens,
1602 &extra_commit_ptr,
1603 &extra_commit_len
1604 );
1605
1606done:
1607 free(commit_points);
1609 if (owns_resources) {
1611 }
1612 return ok;
1613}
1614
1616 const purify_bulletproof_circuit_view* circuit,
1617 const unsigned char* commitments33,
1618 const unsigned char value_gen33[33],
1619 const unsigned char* extra_commit,
1620 size_t extra_commit_len,
1621 const unsigned char* proof,
1622 size_t proof_len) {
1623 return purify_bulletproof_verify_circuit_impl(NULL, resources, circuit, commitments33, value_gen33,
1624 extra_commit, extra_commit_len, proof, proof_len);
1625}
1626
1627static int purify_norm_arg_public_digest(unsigned char digest[32], const secp256k1_scalar* rho,
1628 const secp256k1_bppp_generators* gens_vec, size_t g_len,
1629 const secp256k1_scalar* c_vec, size_t c_vec_len) {
1630 static const unsigned char tag[] = "Purify/BPPP/NormArg/PublicDataV1";
1631 unsigned char ser_point[33], ser_scalar[32], ser_le64[8];
1632 secp256k1_sha256 public_data;
1633 size_t i;
1634
1635 secp256k1_sha256_initialize_tagged(&public_data, tag, sizeof(tag) - 1);
1636 secp256k1_scalar_get_b32(ser_scalar, rho);
1637 secp256k1_sha256_write(&public_data, ser_scalar, sizeof(ser_scalar));
1638 secp256k1_bppp_le64(ser_le64, g_len);
1639 secp256k1_sha256_write(&public_data, ser_le64, sizeof(ser_le64));
1640 secp256k1_bppp_le64(ser_le64, gens_vec->n);
1641 secp256k1_sha256_write(&public_data, ser_le64, sizeof(ser_le64));
1642 for (i = 0; i < gens_vec->n; ++i) {
1643 secp256k1_ge gen = gens_vec->gens[i];
1644 if (!purify_serialize_point(ser_point, &gen)) {
1645 secp256k1_sha256_clear(&public_data);
1646 return 0;
1647 }
1648 secp256k1_sha256_write(&public_data, ser_point, sizeof(ser_point));
1649 }
1650 secp256k1_bppp_le64(ser_le64, c_vec_len);
1651 secp256k1_sha256_write(&public_data, ser_le64, sizeof(ser_le64));
1652 for (i = 0; i < c_vec_len; ++i) {
1653 secp256k1_scalar_get_b32(ser_scalar, &c_vec[i]);
1654 secp256k1_sha256_write(&public_data, ser_scalar, sizeof(ser_scalar));
1655 }
1656 secp256k1_sha256_finalize(&public_data, digest);
1657 secp256k1_sha256_clear(&public_data);
1658 return 1;
1659}
1660
1661static int purify_norm_arg_commit_initial_data(secp256k1_sha256* transcript, const secp256k1_scalar* rho,
1662 const secp256k1_bppp_generators* gens_vec, size_t g_len,
1663 const secp256k1_scalar* c_vec, size_t c_vec_len,
1664 const secp256k1_ge* commit) {
1665 static const unsigned char tag[] = "Purify/BPPP/NormArg/TranscriptV1";
1666 unsigned char public_digest[32], ser_commit[33];
1667 secp256k1_ge comm = *commit;
1668
1669 /* Recompute the digest from the checked public statement; never accept a
1670 * caller-supplied digest as a replacement. A future parent verifier can
1671 * compute this public work outside its circuit and bind the digest inside.
1672 * Distinct tags separate this transcript from the old direct serialization.
1673 */
1674 if (!purify_serialize_point(ser_commit, &comm)
1675 || !purify_norm_arg_public_digest(public_digest, rho, gens_vec, g_len, c_vec, c_vec_len)) {
1676 return 0;
1677 }
1678 secp256k1_sha256_initialize_tagged(transcript, tag, sizeof(tag) - 1);
1679 secp256k1_sha256_write(transcript, public_digest, sizeof(public_digest));
1680 secp256k1_sha256_write(transcript, ser_commit, sizeof(ser_commit));
1681 return 1;
1682}
1683
1684static int purify_copy_vectors_into_scratch(const secp256k1_context* ctx, secp256k1_scratch_space* scratch,
1685 secp256k1_scalar** ns, secp256k1_scalar** ls, secp256k1_scalar** cs,
1686 secp256k1_ge** gs, const secp256k1_scalar* n_vec,
1687 const secp256k1_scalar* l_vec, const secp256k1_scalar* c_vec,
1688 const secp256k1_ge* gens_vec, size_t g_len, size_t h_len) {
1689 size_t scalar_g_bytes = 0;
1690 size_t scalar_h_bytes = 0;
1691 size_t ge_count = 0;
1692 size_t ge_bytes = 0;
1693
1694 if (!purify_bridge_checked_mul_size(g_len, sizeof(secp256k1_scalar), &scalar_g_bytes) ||
1695 !purify_bridge_checked_mul_size(h_len, sizeof(secp256k1_scalar), &scalar_h_bytes) ||
1696 !purify_bridge_checked_add_size(g_len, h_len, &ge_count) ||
1697 !purify_bridge_checked_mul_size(ge_count, sizeof(secp256k1_ge), &ge_bytes)) {
1698 return 0;
1699 }
1700
1701 *ns = (secp256k1_scalar*)secp256k1_scratch_alloc(&ctx->error_callback, scratch, scalar_g_bytes);
1702 *ls = (secp256k1_scalar*)secp256k1_scratch_alloc(&ctx->error_callback, scratch, scalar_h_bytes);
1703 *cs = (secp256k1_scalar*)secp256k1_scratch_alloc(&ctx->error_callback, scratch, scalar_h_bytes);
1704 *gs = (secp256k1_ge*)secp256k1_scratch_alloc(&ctx->error_callback, scratch, ge_bytes);
1705 if (*ns == NULL || *ls == NULL || *cs == NULL || *gs == NULL) {
1706 return 0;
1707 }
1708 memcpy(*ns, n_vec, scalar_g_bytes);
1709 memcpy(*ls, l_vec, scalar_h_bytes);
1710 memcpy(*cs, c_vec, scalar_h_bytes);
1711 memcpy(*gs, gens_vec, ge_bytes);
1712 return 1;
1713}
1714
1715/*
1716 * Secret-scalar MSM for the circuit prover boundary.
1717 *
1718 * libsecp256k1's ecmult_multi_var is intentionally variable time and is only
1719 * suitable when every scalar is public. Circuit witnesses are still secret
1720 * when their raw and norm commitments are formed, so use the constant-time
1721 * single-point and base-point multipliers here. The point list and its length
1722 * are public. A zero scalar produces infinity; replace that affine input with
1723 * G, perform the unified addition, then constant-time select the unchanged
1724 * accumulator. This avoids branching on a witness scalar.
1725 *
1726 * The final point is intentionally declassified by serialization into the
1727 * proof. Public verifier and accumulation MSMs remain variable time.
1728 */
1729static void purify_ge_cmov(secp256k1_ge* r, const secp256k1_ge* a, int flag) {
1730 secp256k1_fe_cmov(&r->x, &a->x, flag);
1731 secp256k1_fe_cmov(&r->y, &a->y, flag);
1732 r->infinity ^= (r->infinity ^ a->infinity) & flag;
1733}
1734
1735static void purify_gej_add_secret_scalar_term(secp256k1_gej* accumulator,
1736 const secp256k1_ge* point,
1737 const secp256k1_scalar* scalar) {
1738 secp256k1_gej termj, candidate;
1739 secp256k1_ge term;
1740 int term_is_infinity;
1741
1742 secp256k1_ecmult_const(&termj, point, scalar);
1743 secp256k1_ge_set_gej(&term, &termj);
1744 term_is_infinity = term.infinity;
1745 purify_ge_cmov(&term, &secp256k1_ge_const_g, term_is_infinity);
1746 candidate = *accumulator;
1747 secp256k1_gej_add_ge(&candidate, &candidate, &term);
1748 secp256k1_gej_cmov(accumulator, &candidate,
1749 term_is_infinity ^ 1);
1750
1751 purify_bridge_secure_clear(&termj, sizeof(termj));
1752 purify_bridge_secure_clear(&candidate, sizeof(candidate));
1753 purify_bridge_secure_clear(&term, sizeof(term));
1754}
1755
1756static int purify_ecmult_multi_secret(const secp256k1_context* ctx,
1757 secp256k1_gej* result,
1758 const secp256k1_scalar* base_scalar,
1759 secp256k1_ecmult_multi_callback callback,
1760 void* callback_data,
1761 size_t point_count) {
1762 size_t i;
1763
1764 if (base_scalar != NULL) {
1765 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, result, base_scalar);
1766 } else {
1767 secp256k1_gej_set_infinity(result);
1768 }
1769 for (i = 0; i < point_count; ++i) {
1770 secp256k1_scalar scalar = {0};
1771 secp256k1_ge point = {0};
1772 int callback_ok = callback(&scalar, &point, i, callback_data);
1773 if (!callback_ok) {
1774 secp256k1_scalar_clear(&scalar);
1775 purify_bridge_secure_clear(&point, sizeof(point));
1776 purify_bridge_secure_clear(result, sizeof(*result));
1777 return 0;
1778 }
1779 purify_gej_add_secret_scalar_term(result, &point, &scalar);
1780 secp256k1_scalar_clear(&scalar);
1781 purify_bridge_secure_clear(&point, sizeof(point));
1782 }
1783 return 1;
1784}
1785
1787 secp256k1_scalar* result,
1788 const secp256k1_scalar* lhs,
1789 size_t lhs_offset,
1790 const secp256k1_scalar* rhs,
1791 size_t rhs_offset,
1792 size_t step,
1793 size_t length) {
1794 size_t i;
1795
1796 secp256k1_scalar_set_int(result, 0);
1797 for (i = 0; i < length; ++i) {
1798 secp256k1_scalar term;
1799 secp256k1_scalar_mul(
1800 &term, &lhs[lhs_offset + step * i],
1801 &rhs[rhs_offset + step * i]);
1802 secp256k1_scalar_add(result, result, &term);
1803 secp256k1_scalar_clear(&term);
1804 }
1805}
1806
1808 secp256k1_scalar* result,
1809 const secp256k1_scalar* lhs,
1810 size_t lhs_offset,
1811 const secp256k1_scalar* rhs,
1812 size_t rhs_offset,
1813 size_t step,
1814 size_t length,
1815 const secp256k1_scalar* weight) {
1816 secp256k1_scalar weight_power = *weight;
1817 size_t i;
1818
1819 secp256k1_scalar_set_int(result, 0);
1820 for (i = 0; i < length; ++i) {
1821 secp256k1_scalar term;
1822 secp256k1_scalar_mul(
1823 &term, &lhs[lhs_offset + step * i],
1824 &rhs[rhs_offset + step * i]);
1825 secp256k1_scalar_mul(&term, &term, &weight_power);
1826 secp256k1_scalar_mul(&weight_power, &weight_power, weight);
1827 secp256k1_scalar_add(result, result, &term);
1828 secp256k1_scalar_clear(&term);
1829 }
1830 secp256k1_scalar_clear(&weight_power);
1831}
1832
1833static int purify_bppp_commit_secret(const secp256k1_context* ctx,
1834 secp256k1_ge* commit,
1835 const secp256k1_bppp_generators* generators,
1836 const secp256k1_scalar* n_vec,
1837 size_t n_vec_len,
1838 const secp256k1_scalar* l_vec,
1839 size_t l_vec_len,
1840 const secp256k1_scalar* c_vec,
1841 size_t c_vec_len,
1842 const secp256k1_scalar* mu) {
1843 secp256k1_scalar v, l_c;
1844 secp256k1_gej commitj;
1845 ecmult_bp_commit_cb_data data;
1846 int ok;
1847
1848 (void)c_vec_len;
1849 VERIFY_CHECK(generators->n == n_vec_len + l_vec_len);
1850 VERIFY_CHECK(l_vec_len == c_vec_len);
1851 VERIFY_CHECK(secp256k1_is_power_of_two(n_vec_len));
1852 VERIFY_CHECK(secp256k1_is_power_of_two(c_vec_len));
1853
1855 &v, n_vec, 0, n_vec, 0, 1, n_vec_len, mu);
1857 &l_c, l_vec, 0, c_vec, 0, 1, l_vec_len);
1858 secp256k1_scalar_add(&v, &v, &l_c);
1859
1860 data.g = generators->gens;
1861 data.n = n_vec;
1862 data.l = l_vec;
1863 data.g_len = n_vec_len;
1865 ctx, &commitj, &v, ecmult_bp_commit_cb, &data,
1866 n_vec_len + l_vec_len);
1867 if (ok) {
1868 secp256k1_ge_set_gej(commit, &commitj);
1869 PURIFY_BRIDGE_DECLASSIFY(commit, sizeof(*commit));
1870 }
1871
1872 secp256k1_scalar_clear(&v);
1873 secp256k1_scalar_clear(&l_c);
1874 purify_bridge_secure_clear(&commitj, sizeof(commitj));
1875 purify_bridge_secure_clear(&data, sizeof(data));
1876 return ok;
1877}
1878
1879/*
1880 * Constant-time adaptation of the upstream BPPP norm prover. The transcript,
1881 * folding challenges, vector lengths, and generator folds are public, so their
1882 * existing variable-time operations are retained. The X and R commitments
1883 * contain witness scalars and therefore use purify_ecmult_multi_secret.
1884 */
1886 const secp256k1_context* ctx,
1887 unsigned char* proof,
1888 size_t* proof_len,
1889 secp256k1_sha256* transcript,
1890 const secp256k1_scalar* rho,
1891 secp256k1_ge* g_vec,
1892 size_t g_vec_len,
1893 secp256k1_scalar* n_vec,
1894 size_t n_vec_len,
1895 secp256k1_scalar* l_vec,
1896 size_t l_vec_len,
1897 secp256k1_scalar* c_vec,
1898 size_t c_vec_len,
1899 purify_bppp_round_challenge_callback challenge_callback,
1900 void* callback_data) {
1901 secp256k1_scalar mu_f, rho_f = *rho;
1902 size_t proof_idx = 0;
1903 ecmult_x_cb_data x_cb_data;
1904 ecmult_r_cb_data r_cb_data;
1905 size_t g_len = n_vec_len, h_len = l_vec_len;
1906 const size_t G_GENS_LEN = g_len;
1907
1908#ifdef VERIFY
1909 {
1910 size_t log_g_len_ver, log_h_len_ver, num_rounds_ver;
1911 VERIFY_CHECK(g_len > 0 && h_len > 0);
1912 log_g_len_ver = secp256k1_bppp_log2(g_len);
1913 log_h_len_ver = secp256k1_bppp_log2(h_len);
1914 num_rounds_ver = log_g_len_ver > log_h_len_ver
1915 ? log_g_len_ver : log_h_len_ver;
1916 VERIFY_CHECK(*proof_len >= 65 * num_rounds_ver + 64);
1917 VERIFY_CHECK(g_vec_len == n_vec_len + l_vec_len);
1918 VERIFY_CHECK(l_vec_len == c_vec_len);
1919 VERIFY_CHECK(secp256k1_is_power_of_two(n_vec_len));
1920 VERIFY_CHECK(secp256k1_is_power_of_two(c_vec_len));
1921 }
1922#else
1923 (void)g_vec_len;
1924 (void)c_vec_len;
1925#endif
1926
1927 x_cb_data.n = n_vec;
1928 x_cb_data.g = g_vec;
1929 x_cb_data.l = l_vec;
1930 x_cb_data.G_GENS_LEN = G_GENS_LEN;
1931 r_cb_data.n1 = n_vec;
1932 r_cb_data.g1 = g_vec;
1933 r_cb_data.l1 = l_vec;
1934 r_cb_data.G_GENS_LEN = G_GENS_LEN;
1935 secp256k1_scalar_sqr(&mu_f, &rho_f);
1936
1937 while (g_len > 1 || h_len > 1) {
1938 size_t i, num_points;
1939 secp256k1_scalar mu_sq, rho_inv, c0_l1, c1_l0;
1940 secp256k1_scalar x_v, c1_l1, r_v, gamma;
1941 secp256k1_gej rj, xj;
1942 secp256k1_ge r_ge, x_ge;
1943 int ok;
1944
1945 /* rho_f is transcript-derived public data. */
1946 secp256k1_scalar_inverse_var(&rho_inv, &rho_f);
1947 secp256k1_scalar_sqr(&mu_sq, &mu_f);
1949 &c0_l1, c_vec, 0, l_vec, 1, 2, h_len / 2);
1951 &c1_l0, c_vec, 1, l_vec, 0, 2, h_len / 2);
1953 &x_v, n_vec, 0, n_vec, 1, 2, g_len / 2, &mu_sq);
1954 secp256k1_scalar_mul(&x_v, &x_v, &rho_inv);
1955 secp256k1_scalar_add(&x_v, &x_v, &x_v);
1956 secp256k1_scalar_add(&x_v, &x_v, &c0_l1);
1957 secp256k1_scalar_add(&x_v, &x_v, &c1_l0);
1958
1959 x_cb_data.rho = &rho_f;
1960 x_cb_data.rho_inv = &rho_inv;
1961 x_cb_data.n_len = g_len >= 2 ? g_len : 0;
1962 num_points = x_cb_data.n_len + (h_len >= 2 ? h_len : 0);
1964 ctx, &xj, &x_v, ecmult_x_cb, &x_cb_data, num_points);
1965 if (!ok) {
1966 goto round_failure;
1967 }
1968
1970 &r_v, n_vec, 1, n_vec, 1, 2, g_len / 2, &mu_sq);
1972 &c1_l1, c_vec, 1, l_vec, 1, 2, h_len / 2);
1973 secp256k1_scalar_add(&r_v, &r_v, &c1_l1);
1974
1975 r_cb_data.n_len = g_len / 2;
1976 num_points = r_cb_data.n_len + h_len / 2;
1978 ctx, &rj, &r_v, ecmult_r_cb, &r_cb_data, num_points);
1979 if (!ok) {
1980 goto round_failure;
1981 }
1982
1983 /* These points become public proof bytes at this line. */
1984 secp256k1_ge_set_gej(&x_ge, &xj);
1985 secp256k1_ge_set_gej(&r_ge, &rj);
1986 secp256k1_fe_normalize(&x_ge.x);
1987 secp256k1_fe_normalize(&x_ge.y);
1988 secp256k1_fe_normalize(&r_ge.x);
1989 secp256k1_fe_normalize(&r_ge.y);
1990 PURIFY_BRIDGE_DECLASSIFY(&x_ge, sizeof(x_ge));
1991 PURIFY_BRIDGE_DECLASSIFY(&r_ge, sizeof(r_ge));
1992 secp256k1_bppp_serialize_points(
1993 &proof[proof_idx], &x_ge, &r_ge);
1994 proof_idx += 65;
1995 if (challenge_callback != NULL) {
1996 unsigned char challenge32[32] = {0};
1997 const size_t round = proof_idx / 65u - 1u;
1998 ok = challenge_callback(callback_data, &proof[proof_idx - 65],
1999 round, challenge32);
2000 if (!ok || !purify_parse_scalar(challenge32, &gamma, 1)) {
2001 purify_bridge_secure_clear(challenge32,
2002 sizeof(challenge32));
2003 goto round_failure;
2004 }
2005 purify_bridge_secure_clear(challenge32, sizeof(challenge32));
2006 } else {
2007 secp256k1_sha256_write(
2008 transcript, &proof[proof_idx - 65], 65);
2009 secp256k1_bppp_challenge_scalar(&gamma, transcript, 0);
2010 }
2011
2012 if (g_len > 1) {
2013 for (i = 0; i < g_len; i += 2) {
2014 secp256k1_scalar nl, nr;
2015 secp256k1_gej gl, gr;
2016 secp256k1_scalar_mul(&nl, &n_vec[i], &rho_inv);
2017 secp256k1_scalar_mul(&nr, &n_vec[i + 1], &gamma);
2018 secp256k1_scalar_add(&n_vec[i / 2], &nl, &nr);
2019
2020 /* rho_f and gamma are public folding challenges. */
2021 secp256k1_gej_set_ge(&gl, &g_vec[i]);
2022 secp256k1_ecmult(&gl, &gl, &rho_f, NULL);
2023 secp256k1_gej_set_ge(&gr, &g_vec[i + 1]);
2024 secp256k1_ecmult(&gr, &gr, &gamma, NULL);
2025 secp256k1_gej_add_var(&gl, &gl, &gr, NULL);
2026 secp256k1_ge_set_gej_var(&g_vec[i / 2], &gl);
2027
2028 secp256k1_scalar_clear(&nl);
2029 secp256k1_scalar_clear(&nr);
2030 purify_bridge_secure_clear(&gl, sizeof(gl));
2031 purify_bridge_secure_clear(&gr, sizeof(gr));
2032 }
2033 }
2034 if (h_len > 1) {
2035 for (i = 0; i < h_len; i += 2) {
2036 secp256k1_scalar temporary;
2037 secp256k1_gej folded_generator;
2038 secp256k1_scalar_mul(
2039 &temporary, &c_vec[i + 1], &gamma);
2040 secp256k1_scalar_add(
2041 &c_vec[i / 2], &c_vec[i], &temporary);
2042 secp256k1_scalar_mul(
2043 &temporary, &l_vec[i + 1], &gamma);
2044 secp256k1_scalar_add(
2045 &l_vec[i / 2], &l_vec[i], &temporary);
2046
2047 secp256k1_gej_set_ge(
2048 &folded_generator, &g_vec[G_GENS_LEN + i + 1]);
2049 secp256k1_ecmult(
2050 &folded_generator, &folded_generator, &gamma, NULL);
2051 secp256k1_gej_add_ge_var(
2052 &folded_generator, &folded_generator,
2053 &g_vec[G_GENS_LEN + i], NULL);
2054 secp256k1_ge_set_gej_var(
2055 &g_vec[G_GENS_LEN + i / 2], &folded_generator);
2056
2057 secp256k1_scalar_clear(&temporary);
2059 &folded_generator, sizeof(folded_generator));
2060 }
2061 }
2062 g_len /= 2;
2063 h_len /= 2;
2064 rho_f = mu_f;
2065 mu_f = mu_sq;
2066
2067 secp256k1_scalar_clear(&mu_sq);
2068 secp256k1_scalar_clear(&rho_inv);
2069 secp256k1_scalar_clear(&c0_l1);
2070 secp256k1_scalar_clear(&c1_l0);
2071 secp256k1_scalar_clear(&x_v);
2072 secp256k1_scalar_clear(&c1_l1);
2073 secp256k1_scalar_clear(&r_v);
2074 secp256k1_scalar_clear(&gamma);
2075 purify_bridge_secure_clear(&rj, sizeof(rj));
2076 purify_bridge_secure_clear(&xj, sizeof(xj));
2077 purify_bridge_secure_clear(&r_ge, sizeof(r_ge));
2078 purify_bridge_secure_clear(&x_ge, sizeof(x_ge));
2079 continue;
2080
2081round_failure:
2082 secp256k1_scalar_clear(&mu_sq);
2083 secp256k1_scalar_clear(&rho_inv);
2084 secp256k1_scalar_clear(&c0_l1);
2085 secp256k1_scalar_clear(&c1_l0);
2086 secp256k1_scalar_clear(&x_v);
2087 secp256k1_scalar_clear(&c1_l1);
2088 secp256k1_scalar_clear(&r_v);
2089 secp256k1_scalar_clear(&gamma);
2090 purify_bridge_secure_clear(&rj, sizeof(rj));
2091 purify_bridge_secure_clear(&xj, sizeof(xj));
2092 purify_bridge_secure_clear(&r_ge, sizeof(r_ge));
2093 purify_bridge_secure_clear(&x_ge, sizeof(x_ge));
2094 secp256k1_scalar_clear(&mu_f);
2095 secp256k1_scalar_clear(&rho_f);
2096 purify_bridge_secure_clear(&x_cb_data, sizeof(x_cb_data));
2097 purify_bridge_secure_clear(&r_cb_data, sizeof(r_cb_data));
2098 return 0;
2099 }
2100
2101 secp256k1_scalar_get_b32(&proof[proof_idx], &n_vec[0]);
2102 secp256k1_scalar_get_b32(&proof[proof_idx + 32], &l_vec[0]);
2103 PURIFY_BRIDGE_DECLASSIFY(&proof[proof_idx], 64);
2104 proof_idx += 64;
2105 *proof_len = proof_idx;
2106 secp256k1_scalar_clear(&mu_f);
2107 secp256k1_scalar_clear(&rho_f);
2108 purify_bridge_secure_clear(&x_cb_data, sizeof(x_cb_data));
2109 purify_bridge_secure_clear(&r_cb_data, sizeof(r_cb_data));
2110 return 1;
2111}
2112
2114 const secp256k1_context* ctx, secp256k1_scratch_space* scratch,
2115 unsigned char* proof, size_t* proof_len,
2116 secp256k1_sha256* transcript, const secp256k1_scalar* rho,
2117 const secp256k1_ge* g_vec, size_t g_vec_len,
2118 const secp256k1_scalar* n_vec, size_t n_vec_len,
2119 const secp256k1_scalar* l_vec, size_t l_vec_len,
2120 const secp256k1_scalar* c_vec, size_t c_vec_len,
2121 size_t secure_clear_checkpoint,
2122 purify_bppp_round_challenge_callback challenge_callback,
2123 void* callback_data) {
2124 secp256k1_scalar *ns = NULL, *ls = NULL, *cs = NULL;
2125 secp256k1_ge *gs = NULL;
2126 size_t checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
2127 int result = 0;
2128
2129 if (purify_copy_vectors_into_scratch(ctx, scratch, &ns, &ls, &cs, &gs, n_vec, l_vec, c_vec, g_vec, n_vec_len, l_vec_len)) {
2131 ctx, proof, proof_len, transcript, rho, gs, g_vec_len,
2132 ns, n_vec_len, ls, l_vec_len, cs, c_vec_len,
2133 challenge_callback, callback_data);
2134 }
2135 /* The imported prover rewinds its private scratch frames before returning.
2136 * Clear the entire tail so those secret intermediates do not survive along
2137 * with the bridge-owned witness copies in a reusable scratch allocation.
2138 */
2139 purify_bridge_secure_clear_scratch_tail(scratch, secure_clear_checkpoint);
2140 secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, checkpoint);
2141 return result;
2142}
2143
2144size_t purify_bppp_required_proof_size(size_t n_vec_len, size_t c_vec_len) {
2145 size_t log_g_len, log_h_len, max_log_len, proof_size = 0;
2146 if (n_vec_len == 0 || c_vec_len == 0 || n_vec_len < c_vec_len) {
2147 return 0;
2148 }
2149 log_g_len = secp256k1_bppp_log2(n_vec_len);
2150 log_h_len = secp256k1_bppp_log2(c_vec_len);
2151 max_log_len = log_g_len > log_h_len ? log_g_len : log_h_len;
2152 if (!purify_bridge_checked_mul_size(65u, max_log_len, &proof_size) ||
2153 !purify_bridge_checked_add_size(proof_size, 64u, &proof_size)) {
2154 return 0;
2155 }
2156 return proof_size;
2157}
2158
2159int purify_bppp_base_generator(purify_secp_context* context, unsigned char out33[33]) {
2160 secp256k1_context* ctx = purify_context_handle(context);
2161 secp256k1_generator generator;
2162 secp256k1_ge ge = secp256k1_ge_const_g;
2163 int ok;
2164
2165 if (ctx == NULL || out33 == NULL) {
2166 return 0;
2167 }
2168 secp256k1_generator_save(&generator, &ge);
2169 ok = secp256k1_generator_serialize(ctx, out33, &generator);
2170 return ok;
2171}
2172
2173int purify_bppp_value_generator_h(purify_secp_context* context, unsigned char out33[33]) {
2174 secp256k1_context* ctx = purify_context_handle(context);
2175 int ok;
2176
2177 if (ctx == NULL || out33 == NULL) {
2178 return 0;
2179 }
2180 ok = secp256k1_generator_serialize(ctx, out33, secp256k1_generator_h);
2181 return ok;
2182}
2183
2184int purify_bppp_create_generators(purify_secp_context* context, size_t count, unsigned char* out, size_t* out_len) {
2185 secp256k1_context* ctx = purify_context_handle(context);
2186 secp256k1_bppp_generators* gens = NULL;
2187 size_t required = 0;
2188 int ok = 0;
2189
2190 if (ctx == NULL || out_len == NULL) {
2191 return 0;
2192 }
2193 if (!purify_bridge_checked_mul_size(count, 33u, &required)) {
2194 *out_len = 0;
2195 return 0;
2196 }
2197 if (*out_len < required || out == NULL) {
2198 *out_len = required;
2199 return 0;
2200 }
2201 gens = secp256k1_bppp_generators_create(ctx, count);
2202 if (gens == NULL) {
2203 return 0;
2204 }
2205 ok = secp256k1_bppp_generators_serialize(ctx, gens, out, out_len);
2206 secp256k1_bppp_generators_destroy(ctx, gens);
2207 return ok;
2208}
2209
2210/* Scale a serialized BPPP generator without changing its wire encoding. */
2212 const unsigned char generator33[33],
2213 const unsigned char scalar32[32],
2214 unsigned char out33[33]) {
2215 secp256k1_context* ctx = purify_context_handle(context);
2216 secp256k1_generator generator = {{0}}, result_generator = {{0}};
2217 secp256k1_scalar scalar = {0};
2218 secp256k1_ge generator_ge = {0}, result_ge = {0};
2219 secp256k1_gej result_j = {0};
2220 int ok = 0;
2221
2222 if (ctx == NULL || generator33 == NULL || scalar32 == NULL || out33 == NULL) {
2223 goto cleanup;
2224 }
2225 if (!secp256k1_generator_parse(ctx, &generator, generator33) ||
2226 !purify_parse_scalar(scalar32, &scalar, 0)) {
2227 goto cleanup;
2228 }
2229 secp256k1_generator_load(&generator_ge, &generator);
2230 secp256k1_ecmult_const(&result_j, &generator_ge, &scalar);
2231 if (secp256k1_gej_is_infinity(&result_j)) {
2232 goto cleanup;
2233 }
2234 secp256k1_ge_set_gej(&result_ge, &result_j);
2235 secp256k1_generator_save(&result_generator, &result_ge);
2236 ok = secp256k1_generator_serialize(ctx, out33, &result_generator);
2237
2238cleanup:
2239 if (!ok && out33 != NULL) {
2240 memset(out33, 0, 33);
2241 }
2242 secp256k1_scalar_clear(&scalar);
2243 secp256k1_ge_clear(&generator_ge);
2244 secp256k1_ge_clear(&result_ge);
2245 secp256k1_gej_clear(&result_j);
2246 secp256k1_memclear_explicit(&generator, sizeof(generator));
2247 secp256k1_memclear_explicit(&result_generator,
2248 sizeof(result_generator));
2249 return ok;
2250}
2251
2253 const unsigned char generator33[33],
2254 unsigned char out33[33]) {
2255 secp256k1_context* ctx = purify_context_handle(context);
2256 secp256k1_generator generator;
2257 secp256k1_ge generator_ge;
2258
2259 if (ctx == NULL || generator33 == NULL || out33 == NULL) {
2260 return 0;
2261 }
2262 if (!secp256k1_generator_parse(ctx, &generator, generator33)) {
2263 return 0;
2264 }
2265 secp256k1_generator_load(&generator_ge, &generator);
2266 return purify_serialize_point(out33, &generator_ge);
2267}
2268
2270 const unsigned char blind32[32], const unsigned char value32[32],
2271 const unsigned char value_gen33[33], const unsigned char blind_gen33[33],
2272 unsigned char commitment_out33[33]) {
2273 secp256k1_context* ctx = purify_context_handle(context);
2274 secp256k1_scalar blind_scalar = {0}, value_scalar = {0};
2275 secp256k1_generator value_generator, blind_generator;
2276 secp256k1_ge value_ge, blind_ge, commit_ge;
2277 secp256k1_gej total;
2278 int ok = 0;
2279
2280 if (ctx == NULL || blind32 == NULL || value32 == NULL || value_gen33 == NULL || blind_gen33 == NULL || commitment_out33 == NULL) {
2281 return 0;
2282 }
2283 purify_bridge_secure_clear(commitment_out33, 33);
2284 if (!purify_parse_scalar(blind32, &blind_scalar, 0) || !purify_parse_scalar(value32, &value_scalar, 0)) {
2285 goto cleanup;
2286 }
2287 if (!secp256k1_generator_parse(ctx, &value_generator, value_gen33) || !secp256k1_generator_parse(ctx, &blind_generator, blind_gen33)) {
2288 goto cleanup;
2289 }
2290 secp256k1_generator_load(&value_ge, &value_generator);
2291 secp256k1_generator_load(&blind_ge, &blind_generator);
2292 secp256k1_gej_set_infinity(&total);
2293 purify_gej_add_secret_scalar_term(&total, &blind_ge, &blind_scalar);
2294 purify_gej_add_secret_scalar_term(&total, &value_ge, &value_scalar);
2295 secp256k1_ge_set_gej(&commit_ge, &total);
2296 PURIFY_BRIDGE_DECLASSIFY(&commit_ge, sizeof(commit_ge));
2297 if (!commit_ge.infinity) {
2298 ok = purify_serialize_point(commitment_out33, &commit_ge);
2299 }
2300
2301cleanup:
2302 secp256k1_scalar_clear(&blind_scalar);
2303 secp256k1_scalar_clear(&value_scalar);
2304 purify_bridge_secure_clear(&value_ge, sizeof(value_ge));
2305 purify_bridge_secure_clear(&blind_ge, sizeof(blind_ge));
2306 purify_bridge_secure_clear(&commit_ge, sizeof(commit_ge));
2307 purify_bridge_secure_clear(&total, sizeof(total));
2308 return ok;
2309}
2310
2313 const unsigned char rho32[32], const unsigned char* generators33,
2314 size_t generators_count, const unsigned char* n_vec32, size_t n_vec_len,
2315 const unsigned char* l_vec32, size_t l_vec_len,
2316 const unsigned char* c_vec32, size_t c_vec_len,
2317 unsigned char commitment_out33[33]) {
2318 secp256k1_context* ctx = resources != NULL ? resources->ctx : purify_context_handle(context);
2319 secp256k1_scratch_space* scratch = resources != NULL ? resources->scratch : NULL;
2320 secp256k1_bppp_generators* gens = resources != NULL ? resources->gens : NULL;
2321 secp256k1_scalar rho = {0}, mu = {0};
2322 secp256k1_scalar *n_vec = NULL, *l_vec = NULL, *c_vec = NULL;
2323 secp256k1_ge commit;
2324 size_t expected_generators = 0;
2325 size_t serialized_generators_len = 0;
2326 size_t scratch_checkpoint = 0;
2327 int ok = 0;
2328 int scratch_contains_secrets = 0;
2329
2330 if (ctx == NULL || rho32 == NULL || (resources == NULL && generators33 == NULL) || n_vec32 == NULL || l_vec32 == NULL || c_vec32 == NULL ||
2331 commitment_out33 == NULL) {
2332 return 0;
2333 }
2334 if (n_vec_len == 0 || l_vec_len == 0 || c_vec_len == 0 || l_vec_len != c_vec_len) {
2335 return 0;
2336 }
2337 if (!secp256k1_is_power_of_two(n_vec_len) || !secp256k1_is_power_of_two(c_vec_len) ||
2338 !purify_bridge_checked_add_size(n_vec_len, l_vec_len, &expected_generators) ||
2339 generators_count != expected_generators ||
2340 !purify_bridge_checked_mul_size(generators_count, 33u, &serialized_generators_len)) {
2341 return 0;
2342 }
2343 if (resources != NULL && resources->generators_count != generators_count) {
2344 return 0;
2345 }
2346 if (!purify_parse_scalar(rho32, &rho, 1)) {
2347 goto cleanup;
2348 }
2349 if (resources == NULL) {
2350 scratch = purify_bppp_create_scratch(ctx, generators_count);
2351 gens = secp256k1_bppp_generators_parse(ctx, generators33, serialized_generators_len);
2352 }
2353 n_vec = (secp256k1_scalar*)purify_malloc_array(n_vec_len, sizeof(*n_vec));
2354 l_vec = (secp256k1_scalar*)purify_malloc_array(l_vec_len, sizeof(*l_vec));
2355 c_vec = (secp256k1_scalar*)purify_malloc_array(c_vec_len, sizeof(*c_vec));
2356 if (scratch == NULL || gens == NULL || n_vec == NULL || l_vec == NULL || c_vec == NULL) {
2357 goto cleanup;
2358 }
2359 if (!purify_parse_scalar_array(n_vec32, n_vec_len, n_vec) ||
2360 !purify_parse_scalar_array(l_vec32, l_vec_len, l_vec) ||
2361 !purify_parse_scalar_array(c_vec32, c_vec_len, c_vec)) {
2362 goto cleanup;
2363 }
2364 scratch_checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
2365 scratch_contains_secrets = 1;
2366 secp256k1_scalar_sqr(&mu, &rho);
2367 if (!purify_bppp_commit_secret(ctx, &commit, gens, n_vec, n_vec_len,
2368 l_vec, l_vec_len, c_vec, c_vec_len, &mu)) {
2369 goto cleanup;
2370 }
2371 ok = purify_serialize_point(commitment_out33, &commit);
2372
2373cleanup:
2374 purify_bridge_secure_free_array(n_vec, n_vec_len, sizeof(*n_vec));
2375 purify_bridge_secure_free_array(l_vec, l_vec_len, sizeof(*l_vec));
2376 purify_bridge_secure_free_array(c_vec, c_vec_len, sizeof(*c_vec));
2377 if (scratch_contains_secrets) {
2378 purify_bridge_secure_clear_scratch_tail(scratch, scratch_checkpoint);
2379 }
2380 secp256k1_scalar_clear(&rho);
2381 secp256k1_scalar_clear(&mu);
2382 purify_bridge_secure_clear(&commit, sizeof(commit));
2383 if (resources == NULL) {
2384 if (gens != NULL) secp256k1_bppp_generators_destroy(ctx, gens);
2385 if (scratch != NULL) secp256k1_scratch_space_destroy(ctx, scratch);
2386 }
2387 return ok;
2388}
2389
2391 const unsigned char rho32[32], const unsigned char* generators33, size_t generators_count,
2392 const unsigned char* n_vec32, size_t n_vec_len, const unsigned char* l_vec32,
2393 size_t l_vec_len, const unsigned char* c_vec32, size_t c_vec_len,
2394 unsigned char commitment_out33[33]) {
2395 return purify_bppp_commit_norm_arg_impl(context, NULL, rho32, generators33, generators_count, n_vec32, n_vec_len,
2396 l_vec32, l_vec_len, c_vec32, c_vec_len, commitment_out33);
2397}
2398
2400 const unsigned char rho32[32],
2401 const unsigned char* n_vec32, size_t n_vec_len,
2402 const unsigned char* l_vec32, size_t l_vec_len,
2403 const unsigned char* c_vec32, size_t c_vec_len,
2404 unsigned char commitment_out33[33]) {
2405 return purify_bppp_commit_norm_arg_impl(NULL, resources, rho32, NULL,
2406 resources != NULL ? resources->generators_count : 0,
2407 n_vec32, n_vec_len, l_vec32, l_vec_len, c_vec32, c_vec_len,
2408 commitment_out33);
2409}
2410
2413 const unsigned char* generators33, size_t generators_count,
2414 const unsigned char* n_vec32, size_t n_vec_len,
2415 const unsigned char* l_vec32, size_t l_vec_len,
2416 unsigned char commitment_out33[33]) {
2417 secp256k1_context* ctx = resources != NULL ? resources->ctx : purify_context_handle(context);
2418 secp256k1_scratch_space* scratch = resources != NULL ? resources->scratch : NULL;
2419 secp256k1_bppp_generators* gens = resources != NULL ? resources->gens : NULL;
2420 secp256k1_scalar zero = {0};
2421 secp256k1_scalar *n_vec = NULL, *l_vec = NULL;
2422 secp256k1_ge commit;
2423 secp256k1_gej commitj;
2424 ecmult_bp_commit_cb_data data;
2425 size_t expected_generators = 0;
2426 size_t serialized_generators_len = 0;
2427 size_t scratch_checkpoint = 0;
2428 int ok = 0;
2429 int scratch_contains_secrets = 0;
2430
2431 if (ctx == NULL || (resources == NULL && generators33 == NULL) || n_vec32 == NULL || l_vec32 == NULL || commitment_out33 == NULL) {
2432 return 0;
2433 }
2434 if (n_vec_len == 0 || l_vec_len == 0 ||
2435 !purify_bridge_checked_add_size(n_vec_len, l_vec_len, &expected_generators) ||
2436 generators_count != expected_generators ||
2437 !purify_bridge_checked_mul_size(generators_count, 33u, &serialized_generators_len)) {
2438 return 0;
2439 }
2440 if (resources != NULL && resources->generators_count != generators_count) {
2441 return 0;
2442 }
2443
2444 if (resources == NULL) {
2445 scratch = purify_bppp_create_scratch(ctx, generators_count);
2446 gens = secp256k1_bppp_generators_parse(ctx, generators33, serialized_generators_len);
2447 }
2448 n_vec = (secp256k1_scalar*)purify_malloc_array(n_vec_len, sizeof(*n_vec));
2449 l_vec = (secp256k1_scalar*)purify_malloc_array(l_vec_len, sizeof(*l_vec));
2450 if (scratch == NULL || gens == NULL || n_vec == NULL || l_vec == NULL) {
2451 goto cleanup;
2452 }
2453 if (!purify_parse_scalar_array(n_vec32, n_vec_len, n_vec) ||
2454 !purify_parse_scalar_array(l_vec32, l_vec_len, l_vec)) {
2455 goto cleanup;
2456 }
2457
2458 scratch_checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
2459 scratch_contains_secrets = 1;
2460 secp256k1_scalar_set_int(&zero, 0);
2461 data.g = gens->gens;
2462 data.n = n_vec;
2463 data.l = l_vec;
2464 data.g_len = n_vec_len;
2465 if (!purify_ecmult_multi_secret(ctx, &commitj, &zero,
2466 ecmult_bp_commit_cb, &data,
2467 expected_generators)) {
2468 goto cleanup;
2469 }
2470 PURIFY_BRIDGE_DECLASSIFY(&commitj.infinity,
2471 sizeof(commitj.infinity));
2472 if (secp256k1_gej_is_infinity(&commitj)) {
2473 goto cleanup;
2474 }
2475 secp256k1_ge_set_gej(&commit, &commitj);
2476 PURIFY_BRIDGE_DECLASSIFY(&commit, sizeof(commit));
2477 ok = purify_serialize_point(commitment_out33, &commit);
2478
2479cleanup:
2480 purify_bridge_secure_free_array(n_vec, n_vec_len, sizeof(*n_vec));
2481 purify_bridge_secure_free_array(l_vec, l_vec_len, sizeof(*l_vec));
2482 if (scratch_contains_secrets) {
2483 purify_bridge_secure_clear_scratch_tail(scratch, scratch_checkpoint);
2484 }
2485 secp256k1_scalar_clear(&zero);
2486 purify_bridge_secure_clear(&data, sizeof(data));
2487 purify_bridge_secure_clear(&commit, sizeof(commit));
2488 purify_bridge_secure_clear(&commitj, sizeof(commitj));
2489 if (resources == NULL) {
2490 if (gens != NULL) secp256k1_bppp_generators_destroy(ctx, gens);
2491 if (scratch != NULL) secp256k1_scratch_space_destroy(ctx, scratch);
2492 }
2493 return ok;
2494}
2495
2497 const unsigned char* generators33, size_t generators_count,
2498 const unsigned char* n_vec32, size_t n_vec_len, const unsigned char* l_vec32,
2499 size_t l_vec_len, unsigned char commitment_out33[33]) {
2500 return purify_bppp_commit_witness_only_impl(context, NULL, generators33, generators_count,
2501 n_vec32, n_vec_len, l_vec32, l_vec_len, commitment_out33);
2502}
2503
2505 const unsigned char* n_vec32, size_t n_vec_len,
2506 const unsigned char* l_vec32, size_t l_vec_len,
2507 unsigned char commitment_out33[33]) {
2508 return purify_bppp_commit_witness_only_impl(NULL, resources, NULL,
2509 resources != NULL ? resources->generators_count : 0,
2510 n_vec32, n_vec_len, l_vec32, l_vec_len, commitment_out33);
2511}
2512
2514 const unsigned char commitment33[33], const unsigned char scalar32[32],
2515 unsigned char commitment_out33[33]) {
2516 secp256k1_context* ctx = purify_context_handle(context);
2517 secp256k1_scalar scalar = {0};
2518 secp256k1_ge commitment_ge, result_ge;
2519 secp256k1_gej result_j;
2520 int ok = 0;
2521
2522 if (ctx == NULL || commitment33 == NULL || scalar32 == NULL || commitment_out33 == NULL) {
2523 return 0;
2524 }
2525 purify_bridge_secure_clear(commitment_out33, 33);
2526 if (!purify_parse_point_as_ge(commitment33, &commitment_ge) ||
2527 !purify_parse_scalar(scalar32, &scalar, 0)) {
2528 goto cleanup;
2529 }
2530
2531 secp256k1_gej_set_ge(&result_j, &commitment_ge);
2533 &result_j, &secp256k1_ge_const_g, &scalar);
2534 secp256k1_ge_set_gej(&result_ge, &result_j);
2535 PURIFY_BRIDGE_DECLASSIFY(&result_ge, sizeof(result_ge));
2536 if (!result_ge.infinity) {
2537 ok = purify_serialize_point(commitment_out33, &result_ge);
2538 }
2539
2540cleanup:
2541 secp256k1_scalar_clear(&scalar);
2542 purify_bridge_secure_clear(&commitment_ge, sizeof(commitment_ge));
2543 purify_bridge_secure_clear(&result_ge, sizeof(result_ge));
2544 purify_bridge_secure_clear(&result_j, sizeof(result_j));
2545 return ok;
2546}
2547
2549 const unsigned char point33[33], const unsigned char scalar32[32],
2550 unsigned char out33[33]) {
2551 secp256k1_context* ctx = purify_context_handle(context);
2552 secp256k1_scalar scalar = {0};
2553 secp256k1_ge point_ge, result_ge;
2554 secp256k1_gej result_j;
2555 int ok = 0;
2556
2557 if (ctx == NULL || point33 == NULL || scalar32 == NULL || out33 == NULL) {
2558 return 0;
2559 }
2560 purify_bridge_secure_clear(out33, 33);
2561 if (!purify_parse_point_as_ge(point33, &point_ge) ||
2562 !purify_parse_scalar(scalar32, &scalar, 0)) {
2563 goto cleanup;
2564 }
2565
2566 secp256k1_ecmult_const(&result_j, &point_ge, &scalar);
2567 secp256k1_ge_set_gej(&result_ge, &result_j);
2568 PURIFY_BRIDGE_DECLASSIFY(&result_ge, sizeof(result_ge));
2569 if (!result_ge.infinity) {
2570 ok = purify_serialize_point(out33, &result_ge);
2571 }
2572
2573cleanup:
2574 secp256k1_scalar_clear(&scalar);
2575 purify_bridge_secure_clear(&point_ge, sizeof(point_ge));
2576 purify_bridge_secure_clear(&result_ge, sizeof(result_ge));
2577 purify_bridge_secure_clear(&result_j, sizeof(result_j));
2578 return ok;
2579}
2580
2582 const unsigned char lhs33[33], const unsigned char rhs33[33],
2583 unsigned char out33[33]) {
2584 secp256k1_context* ctx = purify_context_handle(context);
2585 secp256k1_ge lhs_ge, rhs_ge, result_ge;
2586 secp256k1_gej lhs_j, result_j;
2587 int ok = 0;
2588
2589 if (ctx == NULL || lhs33 == NULL || rhs33 == NULL || out33 == NULL) {
2590 return 0;
2591 }
2592 if (!purify_parse_point_as_ge(lhs33, &lhs_ge) ||
2593 !purify_parse_point_as_ge(rhs33, &rhs_ge)) {
2594 return 0;
2595 }
2596
2597 secp256k1_gej_set_ge(&lhs_j, &lhs_ge);
2598 secp256k1_gej_add_ge_var(&result_j, &lhs_j, &rhs_ge, NULL);
2599 if (!secp256k1_gej_is_infinity(&result_j)) {
2600 secp256k1_ge_set_gej(&result_ge, &result_j);
2601 ok = purify_serialize_point(out33, &result_ge);
2602 }
2603 return ok;
2604}
2605
2607 purify_secp_context* context, const unsigned char lhs33[33],
2608 const unsigned char rhs33[33], unsigned char out33[33]) {
2609 secp256k1_context* ctx = purify_context_handle(context);
2610 secp256k1_ge lhs_ge, rhs_ge, result_ge;
2611 secp256k1_gej lhs_j, rhs_j, result_j;
2612 static const unsigned char infinity33[33] = {0};
2613 int lhs_infinity;
2614 int rhs_infinity;
2615 int ok = 0;
2616
2617 if (ctx == NULL || lhs33 == NULL || rhs33 == NULL || out33 == NULL) {
2618 return 0;
2619 }
2620 purify_bridge_secure_clear(out33, 33);
2621 lhs_infinity = purify_bridge_memeq_const(lhs33, infinity33, 33);
2622 rhs_infinity = purify_bridge_memeq_const(rhs33, infinity33, 33);
2623 if ((!lhs_infinity && !purify_parse_point_as_ge(lhs33, &lhs_ge)) ||
2624 (!rhs_infinity && !purify_parse_point_as_ge(rhs33, &rhs_ge))) {
2625 goto cleanup;
2626 }
2627 if (lhs_infinity) {
2628 secp256k1_gej_set_infinity(&lhs_j);
2629 } else {
2630 secp256k1_gej_set_ge(&lhs_j, &lhs_ge);
2631 }
2632 if (rhs_infinity) {
2633 secp256k1_gej_set_infinity(&rhs_j);
2634 } else {
2635 secp256k1_gej_set_ge(&rhs_j, &rhs_ge);
2636 }
2637 secp256k1_gej_add_var(&result_j, &lhs_j, &rhs_j, NULL);
2638 if (secp256k1_gej_is_infinity(&result_j)) {
2639 ok = 1;
2640 } else {
2641 secp256k1_ge_set_gej(&result_ge, &result_j);
2642 ok = purify_serialize_point(out33, &result_ge);
2643 }
2644
2645cleanup:
2646 purify_bridge_secure_clear(&lhs_ge, sizeof(lhs_ge));
2647 purify_bridge_secure_clear(&rhs_ge, sizeof(rhs_ge));
2648 purify_bridge_secure_clear(&result_ge, sizeof(result_ge));
2649 purify_bridge_secure_clear(&lhs_j, sizeof(lhs_j));
2650 purify_bridge_secure_clear(&rhs_j, sizeof(rhs_j));
2651 purify_bridge_secure_clear(&result_j, sizeof(result_j));
2652 return ok;
2653}
2654
2656 purify_secp_context* context, const unsigned char lhs33[33],
2657 const unsigned char rhs33[33], unsigned char out33[33]) {
2658 secp256k1_context* ctx = purify_context_handle(context);
2659 secp256k1_ge lhs_ge, rhs_ge, result_ge;
2660 secp256k1_gej lhs_j, result_j;
2661 int ok = 0;
2662
2663 if (ctx == NULL || lhs33 == NULL || rhs33 == NULL || out33 == NULL) {
2664 return 0;
2665 }
2666 purify_bridge_secure_clear(out33, 33);
2667 if (!purify_parse_point_as_ge(lhs33, &lhs_ge) ||
2668 !purify_parse_point_as_ge(rhs33, &rhs_ge)) {
2669 goto cleanup;
2670 }
2671
2672 secp256k1_ge_neg(&rhs_ge, &rhs_ge);
2673 secp256k1_gej_set_ge(&lhs_j, &lhs_ge);
2674 secp256k1_gej_add_ge_var(&result_j, &lhs_j, &rhs_ge, NULL);
2675 if (secp256k1_gej_is_infinity(&result_j)) {
2676 ok = 1;
2677 } else {
2678 secp256k1_ge_set_gej(&result_ge, &result_j);
2679 ok = purify_serialize_point(out33, &result_ge);
2680 }
2681
2682cleanup:
2683 purify_bridge_secure_clear(&lhs_ge, sizeof(lhs_ge));
2684 purify_bridge_secure_clear(&rhs_ge, sizeof(rhs_ge));
2685 purify_bridge_secure_clear(&result_ge, sizeof(result_ge));
2686 purify_bridge_secure_clear(&lhs_j, sizeof(lhs_j));
2687 purify_bridge_secure_clear(&result_j, sizeof(result_j));
2688 return ok;
2689}
2690
2693 const unsigned char rho32[32], const unsigned char* generators33,
2694 size_t generators_count, const unsigned char* n_vec32, size_t n_vec_len,
2695 const unsigned char* l_vec32, size_t l_vec_len,
2696 const unsigned char* c_vec32, size_t c_vec_len,
2697 unsigned char commitment_out33[33], unsigned char* proof_out,
2698 size_t* proof_len) {
2699 secp256k1_context* ctx = resources != NULL ? resources->ctx : purify_context_handle(context);
2700 secp256k1_scratch_space* scratch = resources != NULL ? resources->scratch : NULL;
2701 secp256k1_bppp_generators* gens = NULL;
2702 secp256k1_scalar rho = {0}, mu = {0};
2703 secp256k1_scalar *n_vec = NULL, *l_vec = NULL, *c_vec = NULL;
2704 secp256k1_ge commit;
2705 secp256k1_sha256 transcript;
2706 size_t required = purify_bppp_required_proof_size(n_vec_len, c_vec_len);
2707 size_t expected_generators = 0;
2708 size_t serialized_generators_len = 0;
2709 size_t scratch_checkpoint = 0;
2710 int ok = 0;
2711 int scratch_contains_secrets = 0;
2712 int scratch_tail_cleared = 0;
2713 purify_bppp_mutable_generators_guard gens_guard = {0};
2714
2715 if (ctx == NULL || rho32 == NULL || (resources == NULL && generators33 == NULL) || n_vec32 == NULL || l_vec32 == NULL || c_vec32 == NULL ||
2716 commitment_out33 == NULL || proof_out == NULL || proof_len == NULL) {
2717 return 0;
2718 }
2719 if (n_vec_len == 0 || l_vec_len == 0 || c_vec_len == 0 || l_vec_len != c_vec_len ||
2720 n_vec_len < c_vec_len) {
2721 return 0;
2722 }
2723 if (!secp256k1_is_power_of_two(n_vec_len) || !secp256k1_is_power_of_two(c_vec_len) ||
2724 !purify_bridge_checked_add_size(n_vec_len, l_vec_len, &expected_generators) ||
2725 generators_count != expected_generators ||
2726 !purify_bridge_checked_mul_size(generators_count, 33u, &serialized_generators_len)) {
2727 return 0;
2728 }
2729 if (resources != NULL && resources->generators_count != generators_count) {
2730 return 0;
2731 }
2732 if (*proof_len < required) {
2733 *proof_len = required;
2734 return 0;
2735 }
2736 if (!purify_parse_scalar(rho32, &rho, 1)) {
2737 goto cleanup;
2738 }
2739 if (resources != NULL) {
2740 gens = purify_bppp_backend_resources_acquire_scratch_gens(resources, &gens_guard);
2741 if (gens == NULL) {
2742 goto cleanup;
2743 }
2744 } else {
2745 scratch = purify_bppp_create_scratch(ctx, generators_count);
2746 gens = secp256k1_bppp_generators_parse(ctx, generators33, serialized_generators_len);
2747 }
2748 n_vec = (secp256k1_scalar*)purify_malloc_array(n_vec_len, sizeof(*n_vec));
2749 l_vec = (secp256k1_scalar*)purify_malloc_array(l_vec_len, sizeof(*l_vec));
2750 c_vec = (secp256k1_scalar*)purify_malloc_array(c_vec_len, sizeof(*c_vec));
2751 if (scratch == NULL || gens == NULL || n_vec == NULL || l_vec == NULL || c_vec == NULL) {
2752 goto cleanup;
2753 }
2754 if (!purify_parse_scalar_array(n_vec32, n_vec_len, n_vec) ||
2755 !purify_parse_scalar_array(l_vec32, l_vec_len, l_vec) ||
2756 !purify_parse_scalar_array(c_vec32, c_vec_len, c_vec)) {
2757 goto cleanup;
2758 }
2759 scratch_checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
2760 scratch_contains_secrets = 1;
2761 secp256k1_scalar_sqr(&mu, &rho);
2762 if (!purify_bppp_commit_secret(ctx, &commit, gens, n_vec, n_vec_len,
2763 l_vec, l_vec_len, c_vec, c_vec_len, &mu)) {
2764 goto cleanup;
2765 }
2766 if (!purify_serialize_point(commitment_out33, &commit)) {
2767 goto cleanup;
2768 }
2769 if (!purify_norm_arg_commit_initial_data(&transcript, &rho, gens, n_vec_len, c_vec, c_vec_len, &commit)) {
2770 goto cleanup;
2771 }
2773 ctx, scratch, proof_out, proof_len, &transcript, &rho, gens->gens,
2774 gens->n, n_vec, n_vec_len, l_vec, l_vec_len, c_vec, c_vec_len,
2775 scratch_checkpoint, NULL, NULL);
2776 scratch_tail_cleared = 1;
2777
2778cleanup:
2779 purify_bridge_secure_free_array(n_vec, n_vec_len, sizeof(*n_vec));
2780 purify_bridge_secure_free_array(l_vec, l_vec_len, sizeof(*l_vec));
2781 purify_bridge_secure_free_array(c_vec, c_vec_len, sizeof(*c_vec));
2782 if (scratch_contains_secrets && !scratch_tail_cleared) {
2783 purify_bridge_secure_clear_scratch_tail(scratch, scratch_checkpoint);
2784 }
2785 secp256k1_scalar_clear(&rho);
2786 secp256k1_scalar_clear(&mu);
2787 purify_bridge_secure_clear(&commit, sizeof(commit));
2788 purify_bridge_secure_clear(&transcript, sizeof(transcript));
2790 if (resources == NULL) {
2791 if (gens != NULL) secp256k1_bppp_generators_destroy(ctx, gens);
2792 if (scratch != NULL) secp256k1_scratch_space_destroy(ctx, scratch);
2793 }
2794 return ok;
2795}
2796
2798 const unsigned char rho32[32], const unsigned char* generators33, size_t generators_count,
2799 const unsigned char* n_vec32, size_t n_vec_len, const unsigned char* l_vec32,
2800 size_t l_vec_len, const unsigned char* c_vec32, size_t c_vec_len,
2801 unsigned char commitment_out33[33], unsigned char* proof_out, size_t* proof_len) {
2802 return purify_bppp_prove_norm_arg_impl(context, NULL, rho32, generators33, generators_count, n_vec32, n_vec_len,
2803 l_vec32, l_vec_len, c_vec32, c_vec_len, commitment_out33,
2804 proof_out, proof_len);
2805}
2806
2808 const unsigned char rho32[32],
2809 const unsigned char* n_vec32, size_t n_vec_len,
2810 const unsigned char* l_vec32, size_t l_vec_len,
2811 const unsigned char* c_vec32, size_t c_vec_len,
2812 unsigned char commitment_out33[33], unsigned char* proof_out,
2813 size_t* proof_len) {
2814 return purify_bppp_prove_norm_arg_impl(NULL, resources, rho32, NULL,
2815 resources != NULL ? resources->generators_count : 0,
2816 n_vec32, n_vec_len, l_vec32, l_vec_len, c_vec32, c_vec_len,
2817 commitment_out33, proof_out, proof_len);
2818}
2819
2822 const unsigned char rho32[32], const unsigned char* generators33,
2823 size_t generators_count, const unsigned char* n_vec32,
2824 size_t n_vec_len, const unsigned char* l_vec32,
2825 size_t l_vec_len, const unsigned char* c_vec32,
2826 size_t c_vec_len, const unsigned char commitment33[33],
2827 unsigned char* proof_out, size_t* proof_len,
2828 purify_bppp_round_challenge_callback challenge_callback,
2829 void* callback_data) {
2830 secp256k1_context* ctx = resources != NULL ? resources->ctx : purify_context_handle(context);
2831 secp256k1_scratch_space* scratch = resources != NULL ? resources->scratch : NULL;
2832 secp256k1_bppp_generators* gens = NULL;
2833 secp256k1_scalar rho = {0}, mu = {0};
2834 secp256k1_scalar *n_vec = NULL, *l_vec = NULL, *c_vec = NULL;
2835 secp256k1_ge expected_commit, commitment_ge;
2836 secp256k1_sha256 transcript;
2837 unsigned char expected_commitment33[33];
2838 size_t required = purify_bppp_required_proof_size(n_vec_len, c_vec_len);
2839 size_t expected_generators = 0;
2840 size_t serialized_generators_len = 0;
2841 size_t scratch_checkpoint = 0;
2842 int ok = 0;
2843 int scratch_contains_secrets = 0;
2844 int scratch_tail_cleared = 0;
2845 purify_bppp_mutable_generators_guard gens_guard = {0};
2846
2847 memset(expected_commitment33, 0, sizeof(expected_commitment33));
2848 if (ctx == NULL || rho32 == NULL || (resources == NULL && generators33 == NULL) || n_vec32 == NULL || l_vec32 == NULL || c_vec32 == NULL ||
2849 commitment33 == NULL || proof_out == NULL || proof_len == NULL) {
2850 return 0;
2851 }
2852 if (n_vec_len == 0 || l_vec_len == 0 || c_vec_len == 0 || l_vec_len != c_vec_len ||
2853 n_vec_len < c_vec_len) {
2854 return 0;
2855 }
2856 if (!secp256k1_is_power_of_two(n_vec_len) || !secp256k1_is_power_of_two(c_vec_len) ||
2857 !purify_bridge_checked_add_size(n_vec_len, l_vec_len, &expected_generators) ||
2858 generators_count != expected_generators ||
2859 !purify_bridge_checked_mul_size(generators_count, 33u, &serialized_generators_len)) {
2860 return 0;
2861 }
2862 if (resources != NULL && resources->generators_count != generators_count) {
2863 return 0;
2864 }
2865 if (*proof_len < required) {
2866 *proof_len = required;
2867 return 0;
2868 }
2869 if (!purify_parse_scalar(rho32, &rho, 1) || !purify_parse_point_as_ge(commitment33, &commitment_ge)) {
2870 goto cleanup;
2871 }
2872 if (resources != NULL) {
2873 gens = purify_bppp_backend_resources_acquire_scratch_gens(resources, &gens_guard);
2874 if (gens == NULL) {
2875 goto cleanup;
2876 }
2877 } else {
2878 scratch = purify_bppp_create_scratch(ctx, generators_count);
2879 gens = secp256k1_bppp_generators_parse(ctx, generators33, serialized_generators_len);
2880 }
2881 n_vec = (secp256k1_scalar*)purify_malloc_array(n_vec_len, sizeof(*n_vec));
2882 l_vec = (secp256k1_scalar*)purify_malloc_array(l_vec_len, sizeof(*l_vec));
2883 c_vec = (secp256k1_scalar*)purify_malloc_array(c_vec_len, sizeof(*c_vec));
2884 if (scratch == NULL || gens == NULL || n_vec == NULL || l_vec == NULL || c_vec == NULL) {
2885 goto cleanup;
2886 }
2887 if (!purify_parse_scalar_array(n_vec32, n_vec_len, n_vec) ||
2888 !purify_parse_scalar_array(l_vec32, l_vec_len, l_vec) ||
2889 !purify_parse_scalar_array(c_vec32, c_vec_len, c_vec)) {
2890 goto cleanup;
2891 }
2892 scratch_checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
2893 scratch_contains_secrets = 1;
2894 secp256k1_scalar_sqr(&mu, &rho);
2895 if (!purify_bppp_commit_secret(ctx, &expected_commit, gens, n_vec,
2896 n_vec_len, l_vec, l_vec_len, c_vec,
2897 c_vec_len, &mu)) {
2898 goto cleanup;
2899 }
2900 if (!purify_serialize_point(expected_commitment33, &expected_commit)) {
2901 goto cleanup;
2902 }
2903 if (!purify_bridge_memeq_const(expected_commitment33, commitment33,
2904 sizeof(expected_commitment33))) {
2905 goto cleanup;
2906 }
2907 if (!purify_norm_arg_commit_initial_data(&transcript, &rho, gens, n_vec_len, c_vec, c_vec_len, &commitment_ge)) {
2908 goto cleanup;
2909 }
2911 ctx, scratch, proof_out, proof_len, &transcript, &rho, gens->gens,
2912 gens->n, n_vec, n_vec_len, l_vec, l_vec_len, c_vec, c_vec_len,
2913 scratch_checkpoint, challenge_callback, callback_data);
2914 scratch_tail_cleared = 1;
2915
2916cleanup:
2917 purify_bridge_secure_free_array(n_vec, n_vec_len, sizeof(*n_vec));
2918 purify_bridge_secure_free_array(l_vec, l_vec_len, sizeof(*l_vec));
2919 purify_bridge_secure_free_array(c_vec, c_vec_len, sizeof(*c_vec));
2920 if (scratch_contains_secrets && !scratch_tail_cleared) {
2921 purify_bridge_secure_clear_scratch_tail(scratch, scratch_checkpoint);
2922 }
2923 secp256k1_scalar_clear(&rho);
2924 secp256k1_scalar_clear(&mu);
2925 purify_bridge_secure_clear(&expected_commit, sizeof(expected_commit));
2926 purify_bridge_secure_clear(&commitment_ge, sizeof(commitment_ge));
2927 purify_bridge_secure_clear(expected_commitment33,
2928 sizeof(expected_commitment33));
2929 purify_bridge_secure_clear(&transcript, sizeof(transcript));
2931 if (resources == NULL) {
2932 if (gens != NULL) secp256k1_bppp_generators_destroy(ctx, gens);
2933 if (scratch != NULL) secp256k1_scratch_space_destroy(ctx, scratch);
2934 }
2935 return ok;
2936}
2937
2939 const unsigned char rho32[32], const unsigned char* generators33, size_t generators_count,
2940 const unsigned char* n_vec32, size_t n_vec_len, const unsigned char* l_vec32,
2941 size_t l_vec_len, const unsigned char* c_vec32, size_t c_vec_len,
2942 const unsigned char commitment33[33], unsigned char* proof_out, size_t* proof_len) {
2943 return purify_bppp_prove_norm_arg_to_commitment_impl(context, NULL, rho32, generators33, generators_count,
2944 n_vec32, n_vec_len, l_vec32, l_vec_len, c_vec32, c_vec_len,
2945 commitment33, proof_out, proof_len, NULL, NULL);
2946}
2947
2949 const unsigned char rho32[32],
2950 const unsigned char* n_vec32, size_t n_vec_len,
2951 const unsigned char* l_vec32, size_t l_vec_len,
2952 const unsigned char* c_vec32, size_t c_vec_len,
2953 const unsigned char commitment33[33],
2954 unsigned char* proof_out, size_t* proof_len) {
2955 return purify_bppp_prove_norm_arg_to_commitment_impl(NULL, resources, rho32, NULL,
2956 resources != NULL ? resources->generators_count : 0,
2957 n_vec32, n_vec_len, l_vec32, l_vec_len, c_vec32, c_vec_len,
2958 commitment33, proof_out, proof_len, NULL, NULL);
2959}
2960
2962 purify_secp_context* context,
2963 const unsigned char rho32[32],
2964 const unsigned char* generators33,
2965 size_t generators_count,
2966 const unsigned char* n_vec32,
2967 size_t n_vec_len,
2968 const unsigned char* l_vec32,
2969 size_t l_vec_len,
2970 const unsigned char* c_vec32,
2971 size_t c_vec_len,
2972 const unsigned char commitment33[33],
2973 purify_bppp_round_challenge_callback challenge_callback,
2974 void* callback_data,
2975 unsigned char* proof_out,
2976 size_t* proof_len) {
2977 if (challenge_callback == NULL) {
2978 return 0;
2979 }
2981 context, NULL, rho32, generators33, generators_count,
2982 n_vec32, n_vec_len, l_vec32, l_vec_len, c_vec32, c_vec_len,
2983 commitment33, proof_out, proof_len, challenge_callback,
2984 callback_data);
2985}
2986
2988 const secp256k1_context* ctx,
2989 secp256k1_scratch_space* scratch,
2990 const unsigned char* proof,
2991 size_t proof_len,
2992 const secp256k1_scalar* rho,
2993 const secp256k1_bppp_generators* g_vec,
2994 size_t g_len,
2995 const secp256k1_scalar* c_vec,
2996 size_t c_vec_len,
2997 const secp256k1_ge* commit,
2998 const secp256k1_scalar* gammas,
2999 size_t gamma_count) {
3000 secp256k1_scalar rho_f, mu_f, v, n, l, rho_inv, h_c;
3001 secp256k1_scalar *s_g, *s_h, *rho_inv_pows;
3002 secp256k1_gej res1, res2;
3003 size_t i = 0, scratch_checkpoint;
3004 int overflow;
3005 size_t log_g_len, log_h_len, n_rounds;
3006 size_t h_len = c_vec_len;
3007 int ok = 0;
3008
3009 if (g_len == 0 || c_vec_len == 0) {
3010 return 0;
3011 }
3012 log_g_len = secp256k1_bppp_log2(g_len);
3013 log_h_len = secp256k1_bppp_log2(c_vec_len);
3014 n_rounds = log_g_len > log_h_len ? log_g_len : log_h_len;
3015 if (g_vec->n != h_len + g_len ||
3016 proof_len != 65u * n_rounds + 64u ||
3017 gamma_count != n_rounds ||
3018 (gamma_count != 0 && gammas == NULL) ||
3019 !secp256k1_is_power_of_two(g_len) ||
3020 !secp256k1_is_power_of_two(h_len)) {
3021 return 0;
3022 }
3023
3024 secp256k1_scalar_set_b32(&n, &proof[n_rounds * 65u], &overflow);
3025 if (overflow) {
3026 return 0;
3027 }
3028 secp256k1_scalar_set_b32(&l, &proof[n_rounds * 65u + 32u], &overflow);
3029 if (overflow || secp256k1_scalar_is_zero(rho)) {
3030 return 0;
3031 }
3032
3033 scratch_checkpoint =
3034 secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
3035 s_g = (secp256k1_scalar*)secp256k1_scratch_alloc(
3036 &ctx->error_callback, scratch, g_len * sizeof(*s_g));
3037 s_h = (secp256k1_scalar*)secp256k1_scratch_alloc(
3038 &ctx->error_callback, scratch, h_len * sizeof(*s_h));
3039 rho_inv_pows = (secp256k1_scalar*)secp256k1_scratch_alloc(
3040 &ctx->error_callback, scratch,
3041 log_g_len * sizeof(*rho_inv_pows));
3042 if (s_g == NULL || s_h == NULL || rho_inv_pows == NULL) {
3043 goto cleanup;
3044 }
3045
3046 secp256k1_scalar_inverse_var(&rho_inv, rho);
3047 secp256k1_bppp_powers_of_rho(rho_inv_pows, &rho_inv, log_g_len);
3048 rho_f = *rho;
3049 for (i = 0; i < log_g_len; ++i) {
3050 secp256k1_scalar_sqr(&rho_f, &rho_f);
3051 }
3052
3053 secp256k1_scalar_mul(&s_g[0], &n, &rho_f);
3054 secp256k1_scalar_mul(&s_g[0], &s_g[0], &rho_inv);
3055 for (i = 1; i < g_len; ++i) {
3056 const size_t log_i = secp256k1_bppp_log2(i);
3057 const size_t nearest_pow_of_two = (size_t)1 << log_i;
3058 secp256k1_scalar_mul(&s_g[i],
3059 &s_g[i - nearest_pow_of_two],
3060 &gammas[log_i]);
3061 secp256k1_scalar_mul(&s_g[i], &s_g[i], &rho_inv_pows[log_i]);
3062 }
3063 s_h[0] = l;
3064 secp256k1_scalar_set_int(&h_c, 0);
3065 for (i = 1; i < h_len; ++i) {
3066 const size_t log_i = secp256k1_bppp_log2(i);
3067 const size_t nearest_pow_of_two = (size_t)1 << log_i;
3068 secp256k1_scalar_mul(&s_h[i],
3069 &s_h[i - nearest_pow_of_two],
3070 &gammas[log_i]);
3071 }
3072 secp256k1_scalar_inner_product(&h_c, c_vec, 0, s_h, 0, 1, h_len);
3073 secp256k1_scalar_sqr(&mu_f, &rho_f);
3074 secp256k1_scalar_mul(&v, &n, &n);
3075 secp256k1_scalar_mul(&v, &v, &mu_f);
3076 secp256k1_scalar_add(&v, &v, &h_c);
3077
3078 {
3079 ec_mult_verify_cb_data1 data;
3080 data.proof = proof;
3081 data.commit = commit;
3082 data.gammas = gammas;
3083 if (!secp256k1_ecmult_multi_var(
3084 &ctx->error_callback, scratch, &res1, NULL,
3085 ec_mult_verify_cb1, &data, 2u * n_rounds + 1u)) {
3086 goto cleanup;
3087 }
3088 }
3089 {
3090 ec_mult_verify_cb_data2 data;
3091 data.g_vec = g_vec->gens;
3092 data.g_vec_len = g_len;
3093 data.s_g = s_g;
3094 data.s_h = s_h;
3095 if (!secp256k1_ecmult_multi_var(
3096 &ctx->error_callback, scratch, &res2, &v,
3097 ec_mult_verify_cb2, &data, g_len + h_len)) {
3098 goto cleanup;
3099 }
3100 }
3101 ok = secp256k1_gej_eq_var(&res1, &res2);
3102
3103cleanup:
3104 secp256k1_scratch_apply_checkpoint(
3105 &ctx->error_callback, scratch, scratch_checkpoint);
3106 return ok;
3107}
3108
3111 const unsigned char rho32[32], const unsigned char* generators33,
3112 size_t generators_count, const unsigned char* c_vec32, size_t c_vec_len,
3113 size_t n_vec_len, const unsigned char commitment33[33],
3114 const unsigned char* proof, size_t proof_len,
3115 const unsigned char* gammas32,
3116 size_t gamma_count) {
3117 secp256k1_context* ctx = resources != NULL ? resources->ctx : purify_context_handle(context);
3118 secp256k1_scratch_space* scratch = resources != NULL ? resources->scratch : NULL;
3119 secp256k1_bppp_generators* gens = resources != NULL ? resources->gens : NULL;
3120 secp256k1_scalar rho, *c_vec = NULL, *gammas = NULL;
3121 secp256k1_ge commit;
3122 secp256k1_sha256 transcript;
3123 size_t expected_generators = 0;
3124 size_t serialized_generators_len = 0;
3125 int ok = 0;
3126
3127 if (ctx == NULL || rho32 == NULL || (resources == NULL && generators33 == NULL) || c_vec32 == NULL || commitment33 == NULL || proof == NULL) {
3128 return 0;
3129 }
3130 /* Four-fork norm extraction requires a paired norm coordinate in every
3131 * nonterminal round. A linear-only tail does not preserve that relation. */
3132 if (n_vec_len == 0 || c_vec_len == 0 || n_vec_len < c_vec_len ||
3133 !purify_bridge_checked_add_size(n_vec_len, c_vec_len, &expected_generators) ||
3134 generators_count != expected_generators ||
3135 !purify_bridge_checked_mul_size(generators_count, 33u, &serialized_generators_len)) {
3136 return 0;
3137 }
3138 if (!secp256k1_is_power_of_two(n_vec_len) || !secp256k1_is_power_of_two(c_vec_len)) {
3139 return 0;
3140 }
3141 if (resources != NULL && resources->generators_count != generators_count) {
3142 return 0;
3143 }
3144 if (!purify_parse_scalar(rho32, &rho, 1) || !secp256k1_ge_parse_ext(&commit, commitment33)) {
3145 return 0;
3146 }
3147 if (resources == NULL) {
3148 scratch = purify_bppp_create_scratch(ctx, generators_count);
3149 gens = secp256k1_bppp_generators_parse(ctx, generators33, serialized_generators_len);
3150 }
3151 c_vec = (secp256k1_scalar*)purify_malloc_array(c_vec_len, sizeof(*c_vec));
3152 if (scratch == NULL || gens == NULL || c_vec == NULL) {
3153 goto cleanup;
3154 }
3155 if (!purify_parse_scalar_array(c_vec32, c_vec_len, c_vec)) {
3156 goto cleanup;
3157 }
3158 if (gammas32 != NULL) {
3159 const size_t rounds = secp256k1_bppp_log2(n_vec_len);
3160 if (gamma_count != rounds) {
3161 goto cleanup;
3162 }
3163 gammas = (secp256k1_scalar*)purify_malloc_array(
3164 gamma_count, sizeof(*gammas));
3165 if ((gamma_count != 0 && gammas == NULL) ||
3166 !purify_parse_scalar_array(gammas32, gamma_count, gammas)) {
3167 goto cleanup;
3168 }
3169 for (size_t i = 0; i < gamma_count; ++i) {
3170 if (secp256k1_scalar_is_zero(&gammas[i])) {
3171 goto cleanup;
3172 }
3173 }
3175 ctx, scratch, proof, proof_len, &rho, gens, n_vec_len,
3176 c_vec, c_vec_len, &commit, gammas, gamma_count);
3177 } else {
3178 if (gamma_count != 0 ||
3180 &transcript, &rho, gens, n_vec_len, c_vec, c_vec_len,
3181 &commit)) {
3182 goto cleanup;
3183 }
3184 ok = secp256k1_bppp_rangeproof_norm_product_verify(
3185 ctx, scratch, proof, proof_len, &transcript, &rho,
3186 gens, n_vec_len, c_vec, c_vec_len, &commit);
3187 }
3188
3189cleanup:
3190 free(gammas);
3191 if (c_vec != NULL) free(c_vec);
3192 if (resources == NULL) {
3193 if (gens != NULL) secp256k1_bppp_generators_destroy(ctx, gens);
3194 if (scratch != NULL) secp256k1_scratch_space_destroy(ctx, scratch);
3195 }
3196 return ok;
3197}
3198
3200 const unsigned char rho32[32], const unsigned char* generators33, size_t generators_count,
3201 const unsigned char* c_vec32, size_t c_vec_len, size_t n_vec_len,
3202 const unsigned char commitment33[33], const unsigned char* proof, size_t proof_len) {
3203 return purify_bppp_verify_norm_arg_impl(context, NULL, rho32, generators33, generators_count,
3204 c_vec32, c_vec_len, n_vec_len, commitment33, proof, proof_len,
3205 NULL, 0);
3206}
3207
3209 const unsigned char rho32[32],
3210 const unsigned char* c_vec32, size_t c_vec_len, size_t n_vec_len,
3211 const unsigned char commitment33[33], const unsigned char* proof,
3212 size_t proof_len) {
3213 return purify_bppp_verify_norm_arg_impl(NULL, resources, rho32, NULL,
3214 resources != NULL ? resources->generators_count : 0,
3215 c_vec32, c_vec_len, n_vec_len, commitment33, proof, proof_len,
3216 NULL, 0);
3217}
3218
3220 purify_secp_context* context,
3221 const unsigned char rho32[32],
3222 const unsigned char* generators33,
3223 size_t generators_count,
3224 const unsigned char* c_vec32,
3225 size_t c_vec_len,
3226 size_t n_vec_len,
3227 const unsigned char commitment33[33],
3228 const unsigned char* proof,
3229 size_t proof_len,
3230 const unsigned char* gammas32,
3231 size_t gamma_count) {
3232 if (gammas32 == NULL) {
3233 return 0;
3234 }
3236 context, NULL, rho32, generators33, generators_count,
3237 c_vec32, c_vec_len, n_vec_len, commitment33, proof, proof_len,
3238 gammas32, gamma_count);
3239}
3240
3242 purify_secp_context* context,
3243 const unsigned char rho32[32],
3244 const unsigned char* generators33,
3245 size_t generators_count,
3246 const unsigned char* c_vec32,
3247 size_t c_vec_len,
3248 size_t n_vec_len,
3249 const unsigned char commitment33[33],
3250 const unsigned char* proof,
3251 size_t proof_len,
3252 const unsigned char* supplied_gammas32,
3253 unsigned char* derived_gammas32,
3254 size_t gamma_count,
3255 unsigned char terminal_n32[32],
3256 unsigned char terminal_l32[32],
3257 unsigned char residual33[33]) {
3258 secp256k1_context* ctx = purify_context_handle(context);
3259 secp256k1_scratch_space* scratch = NULL;
3260 secp256k1_bppp_generators* gens = NULL;
3261 secp256k1_scalar rho, n, l, norm, *c_vec = NULL, *gammas = NULL;
3262 secp256k1_ge commit, residual;
3263 secp256k1_gej residual_j;
3264 secp256k1_sha256 transcript;
3265 ec_mult_verify_cb_data1 equation;
3266 size_t expected_generators, generator_bytes, rounds, i;
3267 int ok = 0;
3268
3269 if (ctx == NULL || rho32 == NULL || generators33 == NULL || c_vec32 == NULL ||
3270 commitment33 == NULL || proof == NULL || terminal_n32 == NULL ||
3271 terminal_l32 == NULL || residual33 == NULL ||
3272 (gamma_count != 0 && supplied_gammas32 == NULL &&
3273 derived_gammas32 == NULL)) {
3274 return 0;
3275 }
3276 /* Preserve the standalone norm verifier's supported relation exactly. */
3277 if (!secp256k1_is_power_of_two(n_vec_len) || !secp256k1_is_power_of_two(c_vec_len) ||
3278 n_vec_len < c_vec_len ||
3279 !purify_bridge_checked_add_size(n_vec_len, c_vec_len, &expected_generators) ||
3280 generators_count != expected_generators ||
3281 !purify_bridge_checked_mul_size(generators_count, 33u, &generator_bytes)) {
3282 return 0;
3283 }
3284 rounds = secp256k1_bppp_log2(n_vec_len);
3285 if (gamma_count != rounds || proof_len != 65u * rounds + 64u ||
3286 !purify_parse_scalar(rho32, &rho, 1) ||
3287 !purify_parse_scalar(proof + 65u * rounds, &n, 0) ||
3288 !purify_parse_scalar(proof + 65u * rounds + 32u, &l, 0) ||
3289 !purify_parse_point_as_ge(commitment33, &commit)) {
3290 return 0;
3291 }
3292 scratch = purify_bppp_create_scratch(ctx, generators_count);
3293 gens = secp256k1_bppp_generators_parse(ctx, generators33, generator_bytes);
3294 c_vec = (secp256k1_scalar*)purify_malloc_array(c_vec_len, sizeof(*c_vec));
3295 if (rounds != 0) {
3296 gammas = (secp256k1_scalar*)purify_malloc_array(rounds, sizeof(*gammas));
3297 }
3298 if (scratch == NULL || gens == NULL || c_vec == NULL ||
3299 (rounds != 0 && gammas == NULL) ||
3300 !purify_parse_scalar_array(c_vec32, c_vec_len, c_vec)) {
3301 goto cleanup;
3302 }
3303 if (supplied_gammas32 != NULL) {
3305 supplied_gammas32, rounds, gammas)) {
3306 goto cleanup;
3307 }
3308 for (i = 0; i < rounds; ++i) {
3309 if (secp256k1_scalar_is_zero(&gammas[i])) {
3310 goto cleanup;
3311 }
3312 }
3313 } else {
3315 &transcript, &rho, gens, n_vec_len, c_vec, c_vec_len,
3316 &commit)) {
3317 goto cleanup;
3318 }
3319 for (i = 0; i < rounds; ++i) {
3320 secp256k1_sha256_write(&transcript, proof + 65u * i, 65u);
3321 secp256k1_bppp_challenge_scalar(&gammas[i], &transcript, 0);
3322 }
3323 secp256k1_sha256_clear(&transcript);
3324 }
3325
3326 /* Remove only the weighted-norm base-generator contribution. This
3327 * computes the short side of the verification equation:
3328 * T = C + sum(gamma*X + (gamma^2-1)*R) - n^2*rho^(2*g)*G.
3329 * The linear contribution l*<c,w_gamma>*G still needs an authenticated
3330 * coefficient opening; equality to the fixed-basis MSM remains deferred.
3331 */
3332 norm = rho;
3333 for (i = 0; i <= rounds; ++i) {
3334 secp256k1_scalar_sqr(&norm, &norm);
3335 }
3336 secp256k1_scalar_mul(&norm, &norm, &n);
3337 secp256k1_scalar_mul(&norm, &norm, &n);
3338 secp256k1_scalar_negate(&norm, &norm);
3339 equation.proof = proof;
3340 equation.commit = &commit;
3341 equation.gammas = gammas;
3342 if (!secp256k1_ecmult_multi_var(&ctx->error_callback, scratch, &residual_j, &norm,
3343 ec_mult_verify_cb1, &equation, 2u * rounds + 1u)) {
3344 goto cleanup;
3345 }
3346 secp256k1_ge_set_gej_var(&residual, &residual_j);
3347 secp256k1_ge_serialize_ext(residual33, &residual);
3348 secp256k1_scalar_get_b32(terminal_n32, &n);
3349 secp256k1_scalar_get_b32(terminal_l32, &l);
3350 for (i = 0; derived_gammas32 != NULL && i < rounds; ++i) {
3351 secp256k1_scalar_get_b32(
3352 derived_gammas32 + 32u * i, &gammas[i]);
3353 }
3354 ok = 1;
3355
3356cleanup:
3357 free(gammas);
3358 free(c_vec);
3359 if (gens != NULL) secp256k1_bppp_generators_destroy(ctx, gens);
3360 if (scratch != NULL) secp256k1_scratch_space_destroy(ctx, scratch);
3361 return ok;
3362}
3363
3365 const unsigned char rho32[32],
3366 const unsigned char* generators33, size_t generators_count,
3367 const unsigned char* c_vec32, size_t c_vec_len, size_t n_vec_len,
3368 const unsigned char commitment33[33],
3369 const unsigned char* proof, size_t proof_len,
3370 unsigned char terminal_n32[32], unsigned char terminal_l32[32],
3371 unsigned char* gammas32, size_t gamma_count,
3372 unsigned char residual33[33]) {
3374 context, rho32, generators33, generators_count, c_vec32,
3375 c_vec_len, n_vec_len, commitment33, proof, proof_len, NULL,
3376 gammas32, gamma_count, terminal_n32, terminal_l32, residual33);
3377}
3378
3380 purify_secp_context* context,
3381 const unsigned char rho32[32],
3382 const unsigned char* generators33,
3383 size_t generators_count,
3384 const unsigned char* c_vec32,
3385 size_t c_vec_len,
3386 size_t n_vec_len,
3387 const unsigned char commitment33[33],
3388 const unsigned char* proof,
3389 size_t proof_len,
3390 const unsigned char* gammas32,
3391 size_t gamma_count,
3392 unsigned char terminal_n32[32],
3393 unsigned char terminal_l32[32],
3394 unsigned char residual33[33]) {
3395 if (gammas32 == NULL) {
3396 return 0;
3397 }
3399 context, rho32, generators33, generators_count, c_vec32,
3400 c_vec_len, n_vec_len, commitment33, proof, proof_len,
3401 gammas32, NULL, gamma_count, terminal_n32, terminal_l32,
3402 residual33);
3403}
3404
3406 const unsigned char* generators33, size_t generators_count,
3407 unsigned char* points_out33) {
3408 secp256k1_context* ctx = purify_context_handle(context);
3409 size_t bytes, i;
3410
3411 if (ctx == NULL || generators33 == NULL || points_out33 == NULL || generators_count == 0 ||
3412 !purify_bridge_checked_mul_size(generators_count, 33u, &bytes)) {
3413 return 0;
3414 }
3415 for (i = 0; i < generators_count; ++i) {
3416 secp256k1_ge point;
3417 if (!purify_parse_generator_as_ge(ctx, generators33 + 33u * i, &point) ||
3418 !purify_serialize_point(points_out33 + 33u * i, &point)) {
3419 return 0;
3420 }
3421 }
3422 return 1;
3423}
3424
3426 const unsigned char* points33;
3427 const unsigned char* scalars32;
3429
3430static int purify_bppp_public_msm_cb(secp256k1_scalar* scalar, secp256k1_ge* point,
3431 size_t index, void* data_ptr) {
3432 const purify_bppp_public_msm_data* data = (const purify_bppp_public_msm_data*)data_ptr;
3433 return purify_parse_scalar(data->scalars32 + 32u * index, scalar, 0)
3434 && secp256k1_ge_parse_ext(point, data->points33 + 33u * index);
3435}
3436
3438 const unsigned char* points33, const unsigned char* scalars32,
3439 size_t count, unsigned char out33[33]) {
3440 secp256k1_context* ctx = purify_context_handle(context);
3441 secp256k1_scratch_space* scratch = NULL;
3442 secp256k1_gej result_j;
3443 secp256k1_ge result;
3445 size_t point_bytes, scalar_bytes;
3446 int ok;
3447
3448 if (ctx == NULL || out33 == NULL || (count != 0 && (points33 == NULL || scalars32 == NULL)) ||
3449 !purify_bridge_checked_mul_size(count, 33u, &point_bytes) ||
3450 !purify_bridge_checked_mul_size(count, 32u, &scalar_bytes)) {
3451 return 0;
3452 }
3453 if (count == 0) {
3454 memset(out33, 0, 33);
3455 return 1;
3456 }
3457 memset(out33, 0, 33);
3458 data.points33 = points33;
3459 data.scalars32 = scalars32;
3460 if (count <= 8u) {
3461 ok = secp256k1_ecmult_multi_var(&ctx->error_callback, NULL,
3462 &result_j, NULL,
3464 count);
3465 } else {
3466 scratch = secp256k1_scratch_space_create(
3468 if (scratch == NULL) return 0;
3469 ok = secp256k1_ecmult_multi_var(
3470 &ctx->error_callback, scratch, &result_j, NULL,
3471 purify_bppp_public_msm_cb, &data, count);
3472 }
3473 if (ok) {
3474 secp256k1_ge_set_gej_var(&result, &result_j);
3475 secp256k1_ge_serialize_ext(out33, &result);
3476 }
3477 if (scratch != NULL) {
3478 secp256k1_scratch_space_destroy(ctx, scratch);
3479 }
3480 return ok;
3481}
3482
3484 const unsigned char point33[33]) {
3485 secp256k1_ge point;
3486 unsigned char canonical[33];
3487
3488 if (point33 == NULL ||
3489 !secp256k1_ge_parse_ext(&point, point33)) {
3490 return 0;
3491 }
3492 secp256k1_ge_serialize_ext(canonical, &point);
3493 return memcmp(point33, canonical, sizeof(canonical)) == 0;
3494}
3495
3497 const unsigned char* input, size_t input_len,
3498 unsigned char out33[33]) {
3499 static const unsigned char tag[] = "Purify/BPPP/PublicHashToPointV1";
3500 secp256k1_context* ctx = purify_context_handle(context);
3501 secp256k1_sha256 transcript;
3502 secp256k1_generator generator;
3503 secp256k1_ge point;
3504 unsigned char seed[32];
3505
3506 if (ctx == NULL || out33 == NULL || (input_len != 0 && input == NULL)) return 0;
3507 secp256k1_sha256_initialize_tagged(&transcript, tag, sizeof(tag) - 1);
3508 if (input_len != 0) secp256k1_sha256_write(&transcript, input, input_len);
3509 secp256k1_sha256_finalize(&transcript, seed);
3510 secp256k1_sha256_clear(&transcript);
3511 if (!secp256k1_generator_generate(ctx, &generator, seed)) return 0;
3512 secp256k1_generator_load(&point, &generator);
3513 return purify_serialize_point(out33, &point);
3514}
static secp256k1_scalar * purify_scalar_cast(purify_scalar *scalar)
int purify_bppp_verify_norm_arg_with_challenges(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len, const unsigned char *gammas32, size_t gamma_count)
static int purify_bppp_rangeproof_norm_product_prove_secret(const secp256k1_context *ctx, unsigned char *proof, size_t *proof_len, secp256k1_sha256 *transcript, const secp256k1_scalar *rho, secp256k1_ge *g_vec, size_t g_vec_len, secp256k1_scalar *n_vec, size_t n_vec_len, secp256k1_scalar *l_vec, size_t l_vec_len, secp256k1_scalar *c_vec, size_t c_vec_len, purify_bppp_round_challenge_callback challenge_callback, void *callback_data)
static int purify_bulletproof_add_scratch_frame(size_t *total, size_t bytes, size_t objects)
int purify_bulletproof_prove_circuit_with_resources(purify_bulletproof_backend_resources *resources, const purify_bulletproof_circuit_view *circuit, const purify_bulletproof_assignment_view *assignment, const unsigned char *blinds32, const unsigned char value_gen33[33], const unsigned char nonce32[32], const unsigned char *extra_commit, size_t extra_commit_len, unsigned char *commitments_out33, unsigned char *proof_out, size_t *proof_len)
static void purify_bulletproof_circuit_evaluate_sum_row(secp256k1_scalar *acc, const secp256k1_bulletproof_wmatrix_row *row, const secp256k1_scalar *assn)
void purify_scalar_mul(purify_scalar *out, const purify_scalar *lhs, const purify_scalar *rhs)
Multiplies two scalars modulo the backend field.
int purify_bppp_prove_norm_arg_to_commitment_with_resources(purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, const unsigned char commitment33[33], unsigned char *proof_out, size_t *proof_len)
static int purify_ecmult_multi_secret(const secp256k1_context *ctx, secp256k1_gej *result, const secp256k1_scalar *base_scalar, secp256k1_ecmult_multi_callback callback, void *callback_data, size_t point_count)
static int purify_parse_scalar_array(const unsigned char *input32, size_t count, secp256k1_scalar *out)
static void purify_ge_cmov(secp256k1_ge *r, const secp256k1_ge *a, int flag)
static int purify_bppp_backend_resources_reset_scratch_gens(purify_bppp_backend_resources *resources)
static int purify_build_row_family(secp256k1_bulletproof_wmatrix_row *out_rows, size_t row_count, const purify_bulletproof_row_view *in_rows, size_t n_constraints, secp256k1_bulletproof_wmatrix_entry *entries, size_t *offset)
static void purify_scalar_inner_product_secret(secp256k1_scalar *result, const secp256k1_scalar *lhs, size_t lhs_offset, const secp256k1_scalar *rhs, size_t rhs_offset, size_t step, size_t length)
static secp256k1_bppp_generators * purify_bppp_backend_resources_acquire_scratch_gens(purify_bppp_backend_resources *resources, purify_bppp_mutable_generators_guard *guard)
static int purify_parse_scalar(const unsigned char input32[32], secp256k1_scalar *scalar, int reject_zero)
static int purify_build_bulletproof_assignment(const purify_bulletproof_assignment_view *view, secp256k1_bulletproof_circuit_assignment *out)
int purify_bppp_commit_witness_only_with_resources(purify_bppp_backend_resources *resources, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, unsigned char commitment_out33[33])
int purify_scalar_is_zero(const purify_scalar *value)
Returns nonzero when the scalar is zero.
int purify_bip340_xonly_from_point(purify_secp_context *context, const unsigned char point33[33], unsigned char xonly32[32], int *parity_out)
Converts a compressed secp256k1 point into its x-only public key encoding.
#define PURIFY_BRIDGE_DECLASSIFY(data, size)
Definition bppp_bridge.c:30
static int purify_bulletproof_prove_circuit_impl(const purify_bulletproof_circuit_view *circuit, const purify_bulletproof_assignment_view *assignment, const unsigned char *blinds32, const unsigned char value_gen33[33], const unsigned char nonce32[32], const unsigned char *extra_commit, size_t extra_commit_len, unsigned char *commitments_out33, unsigned char *proof_out, size_t *proof_len, int require_valid_assignment, purify_secp_context *context, purify_bulletproof_backend_resources *resources)
int purify_point_subtract_allow_infinity(purify_secp_context *context, const unsigned char lhs33[33], const unsigned char rhs33[33], unsigned char out33[33])
int purify_bulletproof_verify_circuit(purify_secp_context *context, const purify_bulletproof_circuit_view *circuit, const unsigned char *commitments33, const unsigned char value_gen33[33], const unsigned char *extra_commit, size_t extra_commit_len, const unsigned char *proof, size_t proof_len)
int purify_bip340_key_from_seckey(purify_secp_context *context, unsigned char seckey32[32], unsigned char xonly_pubkey32[32])
Canonicalizes a valid secp256k1 secret key for BIP340 and derives its x-only public key.
static void purify_bridge_secure_free_array(void *data, size_t count, size_t elem_size)
int purify_scalar_add(purify_scalar *out, const purify_scalar *lhs, const purify_scalar *rhs)
Adds two scalars modulo the backend field.
int purify_bip340_nonce_from_scalar(purify_secp_context *context, unsigned char scalar32[32], unsigned char xonly_nonce32[32])
Canonicalizes a valid secp256k1 nonce scalar for BIP340 and derives its x-only public nonce.
int purify_point_scale(purify_secp_context *context, const unsigned char point33[33], const unsigned char scalar32[32], unsigned char out33[33])
void purify_scalar_set_int(purify_scalar *out, unsigned int value)
Initializes a scalar from an unsigned integer.
int purify_bulletproof_verify_circuit_with_resources(purify_bulletproof_backend_resources *resources, const purify_bulletproof_circuit_view *circuit, const unsigned char *commitments33, const unsigned char value_gen33[33], const unsigned char *extra_commit, size_t extra_commit_len, const unsigned char *proof, size_t proof_len)
void purify_scalar_cmov(purify_scalar *dst, const purify_scalar *src, int flag)
Conditionally assigns src into dst when flag is nonzero.
static int purify_bppp_rangeproof_norm_product_verify_challenges(const secp256k1_context *ctx, secp256k1_scratch_space *scratch, const unsigned char *proof, size_t proof_len, const secp256k1_scalar *rho, const secp256k1_bppp_generators *g_vec, size_t g_len, const secp256k1_scalar *c_vec, size_t c_vec_len, const secp256k1_ge *commit, const secp256k1_scalar *gammas, size_t gamma_count)
static int purify_copy_vectors_into_scratch(const secp256k1_context *ctx, secp256k1_scratch_space *scratch, secp256k1_scalar **ns, secp256k1_scalar **ls, secp256k1_scalar **cs, secp256k1_ge **gs, const secp256k1_scalar *n_vec, const secp256k1_scalar *l_vec, const secp256k1_scalar *c_vec, const secp256k1_ge *gens_vec, size_t g_len, size_t h_len)
static int purify_bridge_checked_mul_size(size_t lhs, size_t rhs, size_t *out)
static int purify_bulletproof_circuit_scratch_size(const secp256k1_bulletproof_circuit *circuit, size_t *out)
int purify_bppp_prove_norm_arg_to_commitment_with_callback(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, const unsigned char commitment33[33], purify_bppp_round_challenge_callback challenge_callback, void *callback_data, unsigned char *proof_out, size_t *proof_len)
int purify_bppp_prepare_norm_arg_claim_with_challenges(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len, const unsigned char *gammas32, size_t gamma_count, unsigned char terminal_n32[32], unsigned char terminal_l32[32], unsigned char residual33[33])
static int purify_parse_point_as_ge(const unsigned char point33[33], secp256k1_ge *out)
int purify_bip340_sign_with_fixed_nonce(purify_secp_context *context, unsigned char sig64[64], const unsigned char *msg, size_t msglen, const unsigned char seckey32[32], const unsigned char nonce32[32])
Signs a message with a caller-supplied BIP340 nonce scalar.
static int purify_bulletproof_verify_circuit_impl(purify_secp_context *context, purify_bulletproof_backend_resources *resources, const purify_bulletproof_circuit_view *circuit, const unsigned char *commitments33, const unsigned char value_gen33[33], const unsigned char *extra_commit, size_t extra_commit_len, const unsigned char *proof, size_t proof_len)
static int purify_bppp_prove_norm_arg_to_commitment_impl(purify_secp_context *context, purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, const unsigned char commitment33[33], unsigned char *proof_out, size_t *proof_len, purify_bppp_round_challenge_callback challenge_callback, void *callback_data)
int purify_bppp_scale_generator(purify_secp_context *context, const unsigned char generator33[33], const unsigned char scalar32[32], unsigned char out33[33])
static int purify_bulletproof_reserve_circuit_scratch(purify_bulletproof_backend_resources *resources, const secp256k1_bulletproof_circuit *circuit)
static int purify_bppp_rangeproof_norm_product_prove_with_copied_inputs(const secp256k1_context *ctx, secp256k1_scratch_space *scratch, unsigned char *proof, size_t *proof_len, secp256k1_sha256 *transcript, const secp256k1_scalar *rho, const secp256k1_ge *g_vec, size_t g_vec_len, const secp256k1_scalar *n_vec, size_t n_vec_len, const secp256k1_scalar *l_vec, size_t l_vec_len, const secp256k1_scalar *c_vec, size_t c_vec_len, size_t secure_clear_checkpoint, purify_bppp_round_challenge_callback challenge_callback, void *callback_data)
int purify_scalar_is_even(const purify_scalar *value)
Returns nonzero when the scalar is even.
static void purify_free_bulletproof_circuit(secp256k1_bulletproof_circuit *circuit)
int purify_bulletproof_prove_circuit(purify_secp_context *context, const purify_bulletproof_circuit_view *circuit, const purify_bulletproof_assignment_view *assignment, const unsigned char *blinds32, const unsigned char value_gen33[33], const unsigned char nonce32[32], const unsigned char *extra_commit, size_t extra_commit_len, unsigned char *commitments_out33, unsigned char *proof_out, size_t *proof_len)
static int purify_norm_arg_public_digest(unsigned char digest[32], const secp256k1_scalar *rho, const secp256k1_bppp_generators *gens_vec, size_t g_len, const secp256k1_scalar *c_vec, size_t c_vec_len)
int purify_bppp_generator_to_point(purify_secp_context *context, const unsigned char generator33[33], unsigned char out33[33])
static int purify_bppp_commit_norm_arg_impl(purify_secp_context *context, purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, unsigned char commitment_out33[33])
static int purify_fixed_nonce_function(unsigned char *nonce32, const unsigned char *msg, size_t msglen, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data)
int purify_bulletproof_prove_circuit_assume_valid(purify_secp_context *context, const purify_bulletproof_circuit_view *circuit, const purify_bulletproof_assignment_view *assignment, const unsigned char *blinds32, const unsigned char value_gen33[33], const unsigned char nonce32[32], const unsigned char *extra_commit, size_t extra_commit_len, unsigned char *commitments_out33, unsigned char *proof_out, size_t *proof_len)
int purify_sha256_many(unsigned char output32[32], const unsigned char *const *items, const size_t *item_lens, size_t items_count)
Computes SHA-256 over a set of byte strings.
int purify_bppp_prove_norm_arg(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, unsigned char commitment_out33[33], unsigned char *proof_out, size_t *proof_len)
Produces a standalone BPPP norm argument.
int purify_bppp_commit_witness_only(purify_secp_context *context, const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, unsigned char commitment_out33[33])
int purify_bppp_commit_norm_arg(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, unsigned char commitment_out33[33])
static void purify_free_bulletproof_assignment(secp256k1_bulletproof_circuit_assignment *assignment)
int purify_bppp_prepare_norm_arg_claim(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len, unsigned char terminal_n32[32], unsigned char terminal_l32[32], unsigned char *gammas32, size_t gamma_count, unsigned char residual33[33])
void purify_sha256(unsigned char output32[32], const unsigned char *data, size_t data_len)
Computes SHA-256 over a byte string.
int purify_scalar_is_one(const purify_scalar *value)
Returns nonzero when the scalar is one.
int purify_bppp_public_point_validate(const unsigned char point33[33])
static size_t purify_total_row_entries(const purify_bulletproof_row_view *rows, size_t count, int *ok)
int purify_bip340_validate_signature(purify_secp_context *context, const unsigned char sig64[64])
Returns nonzero when the 64-byte BIP340 signature has a syntactically valid encoding.
void purify_secp_context_destroy(purify_secp_context *context)
Destroys a context returned by purify_secp_context_create.
Definition bppp_bridge.c:81
void purify_bppp_backend_resources_destroy(purify_bppp_backend_resources *resources)
int purify_bppp_public_msm(purify_secp_context *context, const unsigned char *points33, const unsigned char *scalars32, size_t count, unsigned char out33[33])
int purify_bulletproof_prove_circuit_assume_valid_with_resources(purify_bulletproof_backend_resources *resources, const purify_bulletproof_circuit_view *circuit, const purify_bulletproof_assignment_view *assignment, const unsigned char *blinds32, const unsigned char value_gen33[33], const unsigned char nonce32[32], const unsigned char *extra_commit, size_t extra_commit_len, unsigned char *commitments_out33, unsigned char *proof_out, size_t *proof_len)
size_t purify_bulletproof_backend_resources_scratch_capacity(const purify_bulletproof_backend_resources *resources)
void purify_scalar_inverse(purify_scalar *out, const purify_scalar *value)
Computes the multiplicative inverse of a scalar in constant time.
static int purify_bppp_commit_secret(const secp256k1_context *ctx, secp256k1_ge *commit, const secp256k1_bppp_generators *generators, const secp256k1_scalar *n_vec, size_t n_vec_len, const secp256k1_scalar *l_vec, size_t l_vec_len, const secp256k1_scalar *c_vec, size_t c_vec_len, const secp256k1_scalar *mu)
int purify_bppp_value_generator_h(purify_secp_context *context, unsigned char out33[33])
Serializes the alternate value generator used by Pedersen commitments.
int purify_bppp_create_generators(purify_secp_context *context, size_t count, unsigned char *out, size_t *out_len)
Expands the generator list required by the BPPP prover and verifier.
purify_bppp_backend_resources * purify_bppp_backend_resources_clone(const purify_bppp_backend_resources *resources)
static const secp256k1_scalar * purify_scalar_cast_const(const purify_scalar *scalar)
static int purify_bulletproof_circuit_evaluate(const secp256k1_bulletproof_circuit *circuit, const secp256k1_bulletproof_circuit_assignment *assignment)
int purify_point_add(purify_secp_context *context, const unsigned char lhs33[33], const unsigned char rhs33[33], unsigned char out33[33])
int purify_bppp_verify_norm_arg(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len)
Verifies a standalone BPPP norm argument.
static const size_t purify_bulletproof_msm_scratch_size
int purify_pedersen_commit_char(purify_secp_context *context, const unsigned char blind32[32], const unsigned char value32[32], const unsigned char value_gen33[33], const unsigned char blind_gen33[33], unsigned char commitment_out33[33])
Computes a Pedersen commitment to an arbitrary 32-byte scalar value.
void purify_scalar_inverse_var(purify_scalar *out, const purify_scalar *value)
Computes the multiplicative inverse of a scalar.
int purify_bip340_validate_xonly_pubkey(purify_secp_context *context, const unsigned char xonly_pubkey32[32])
Returns nonzero when the x-only public key encoding parses successfully.
static secp256k1_context * purify_context_handle(const purify_secp_context *context)
Definition bppp_bridge.c:54
static int purify_bppp_prepare_norm_arg_claim_impl(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len, const unsigned char *supplied_gammas32, unsigned char *derived_gammas32, size_t gamma_count, unsigned char terminal_n32[32], unsigned char terminal_l32[32], unsigned char residual33[33])
static void purify_bridge_secure_clear(void *data, size_t size)
Definition bppp_bridge.c:91
int purify_bppp_prove_norm_arg_with_resources(purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, unsigned char commitment_out33[33], unsigned char *proof_out, size_t *proof_len)
static int purify_bip340_canonicalize_scalar(const secp256k1_context *ctx, unsigned char scalar32[32], unsigned char xonly_pubkey32[32])
int purify_scalar_eq(const purify_scalar *lhs, const purify_scalar *rhs)
Returns nonzero when two scalars are equal.
static int purify_parse_generator_as_ge(const secp256k1_context *ctx, const unsigned char generator33[33], secp256k1_ge *out)
int purify_bppp_offset_commitment(purify_secp_context *context, const unsigned char commitment33[33], const unsigned char scalar32[32], unsigned char commitment_out33[33])
static int purify_parse_fast_scalar(const unsigned char input32[32], secp256k1_fast_scalar *scalar)
void purify_scalar_negate(purify_scalar *out, const purify_scalar *value)
Computes the additive inverse of a scalar.
void purify_bulletproof_backend_resources_destroy(purify_bulletproof_backend_resources *resources)
int purify_bppp_public_hash_to_point(purify_secp_context *context, const unsigned char *input, size_t input_len, unsigned char out33[33])
purify_secp_context * purify_secp_context_create(void)
Creates one reusable secp256k1 context for the Purify bridge and public APIs.
Definition bppp_bridge.c:58
int purify_bppp_prove_norm_arg_to_commitment(purify_secp_context *context, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, const unsigned char commitment33[33], unsigned char *proof_out, size_t *proof_len)
static secp256k1_scratch_space * purify_bppp_create_scratch(const secp256k1_context *ctx, size_t generators_count)
int purify_bppp_generators_to_points(purify_secp_context *context, const unsigned char *generators33, size_t generators_count, unsigned char *points_out33)
static int purify_bppp_verify_norm_arg_impl(purify_secp_context *context, purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len, const unsigned char *gammas32, size_t gamma_count)
purify_bulletproof_backend_resources * purify_bulletproof_backend_resources_create(purify_secp_context *context, size_t n_gates)
int purify_point_add_allow_infinity(purify_secp_context *context, const unsigned char lhs33[33], const unsigned char rhs33[33], unsigned char out33[33])
static void purify_gej_add_secret_scalar_term(secp256k1_gej *accumulator, const secp256k1_ge *point, const secp256k1_scalar *scalar)
static int purify_bppp_public_msm_cb(secp256k1_scalar *scalar, secp256k1_ge *point, size_t index, void *data_ptr)
static int purify_norm_arg_commit_initial_data(secp256k1_sha256 *transcript, const secp256k1_scalar *rho, const secp256k1_bppp_generators *gens_vec, size_t g_len, const secp256k1_scalar *c_vec, size_t c_vec_len, const secp256k1_ge *commit)
purify_bulletproof_backend_resources * purify_bulletproof_backend_resources_clone(const purify_bulletproof_backend_resources *resources)
static void purify_bridge_secure_clear_scratch_tail(secp256k1_scratch_space *scratch, size_t checkpoint)
Definition bppp_bridge.c:97
int purify_bppp_base_generator(purify_secp_context *context, unsigned char out33[33])
Serializes the secp256k1 base generator into compressed form.
int purify_bppp_commit_norm_arg_with_resources(purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, unsigned char commitment_out33[33])
size_t purify_bppp_required_proof_size(size_t n_vec_len, size_t c_vec_len)
Computes the maximum serialized size of a BPPP norm proof.
static secp256k1_bppp_generators * purify_bppp_generators_clone(const secp256k1_bppp_generators *generators)
static int purify_bppp_prove_norm_arg_impl(purify_secp_context *context, purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, const unsigned char *c_vec32, size_t c_vec_len, unsigned char commitment_out33[33], unsigned char *proof_out, size_t *proof_len)
static void purify_weighted_scalar_inner_product_secret(secp256k1_scalar *result, const secp256k1_scalar *lhs, size_t lhs_offset, const secp256k1_scalar *rhs, size_t rhs_offset, size_t step, size_t length, const secp256k1_scalar *weight)
static void * purify_malloc_array(size_t count, size_t elem_size)
void purify_scalar_set_u64(purify_scalar *out, uint64_t value)
Initializes a scalar from a 64-bit unsigned integer.
void purify_hmac_sha256(unsigned char output32[32], const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len)
Computes HMAC-SHA256 over a byte string.
static void purify_bppp_backend_resources_release_scratch_gens(purify_bppp_mutable_generators_guard *guard)
void purify_scalar_get_b32(unsigned char output32[32], const purify_scalar *value)
Serializes a scalar as 32 big-endian bytes.
size_t purify_bulletproof_required_proof_size(size_t n_gates)
purify_bppp_backend_resources * purify_bppp_backend_resources_create(purify_secp_context *context, const unsigned char *generators33, size_t generators_count)
static int purify_bridge_checked_add_size(size_t lhs, size_t rhs, size_t *out)
static int purify_bridge_memeq_const(const unsigned char *lhs, const unsigned char *rhs, size_t size)
int purify_bip340_verify(purify_secp_context *context, const unsigned char sig64[64], const unsigned char *msg, size_t msglen, const unsigned char xonly_pubkey32[32])
Verifies a BIP340 signature against a serialized x-only public key.
static void * purify_calloc_array(size_t count, size_t elem_size)
static int purify_serialize_point(unsigned char out33[33], secp256k1_ge *point)
int purify_bppp_verify_norm_arg_with_resources(purify_bppp_backend_resources *resources, const unsigned char rho32[32], const unsigned char *c_vec32, size_t c_vec_len, size_t n_vec_len, const unsigned char commitment33[33], const unsigned char *proof, size_t proof_len)
void purify_scalar_set_b32(purify_scalar *out, const unsigned char input32[32], int *overflow)
Parses a big-endian 32-byte scalar.
static int purify_build_bulletproof_circuit(const purify_bulletproof_circuit_view *view, secp256k1_bulletproof_circuit *out)
static int purify_bppp_commit_witness_only_impl(purify_secp_context *context, purify_bppp_backend_resources *resources, const unsigned char *generators33, size_t generators_count, const unsigned char *n_vec32, size_t n_vec_len, const unsigned char *l_vec32, size_t l_vec_len, unsigned char commitment_out33[33])
C ABI bridging Purify C++ code to secp256k1-zkp BPPP functionality.
int(* purify_bppp_round_challenge_callback)(void *callback_data, const unsigned char point_pair65[65], size_t round, unsigned char challenge32[32])
Definition bppp_bridge.h:26
static void secp256k1_fast_scalar_mul(secp256k1_scalar *r, const secp256k1_fast_scalar *a, const secp256k1_scalar *b)
static int secp256k1_bulletproof_relation66_verify_impl(const secp256k1_ecmult_context *ecmult_ctx, secp256k1_scratch *scratch, const unsigned char *const *proof, size_t n_proofs, size_t plen, const secp256k1_ge *const *commitp, size_t *nc, const secp256k1_ge *value_gen, const secp256k1_bulletproof_circuit *const *circ, const secp256k1_bulletproof_generators *gens, const unsigned char **extra_commit, size_t *extra_commit_len)
static int secp256k1_bulletproof_relation66_prove_impl(const secp256k1_ecmult_context *ecmult_ctx, secp256k1_scratch *scratch, unsigned char *proof, size_t *plen, const secp256k1_bulletproof_circuit_assignment *assn, const secp256k1_ge *commitp, const secp256k1_scalar *blinds, size_t nc, const secp256k1_ge *value_gen, const secp256k1_bulletproof_circuit *circ, const secp256k1_bulletproof_generators *gens, const unsigned char *nonce, const unsigned char *extra_commit, size_t extra_commit_len)
size_t secp256k1_bulletproof_innerproduct_proof_length(size_t n)
static secp256k1_bulletproof_generators * secp256k1_bulletproof_generators_create(const secp256k1_context *ctx, const secp256k1_generator *blinding_gen, size_t n, size_t precomp_n)
Definition core.h:97
static void secp256k1_bulletproof_generators_destroy(const secp256k1_context *ctx, secp256k1_bulletproof_generators *gen)
Definition core.h:169
#define secp256k1_scratch_alloc(scratch, size)
Definition core.h:26
static SECP256K1_INLINE void secp256k1_pedersen_ecmult_scalar(secp256k1_gej *rj, const secp256k1_scalar *sec, const secp256k1_scalar *value, const secp256k1_ge *value_gen, const secp256k1_ge *blind_gen)
Definition core.h:220
Public C core for Purify key validation, key derivation, key generation, and evaluation.
purify_error_code purify_fill_secure_random(unsigned char *bytes, size_t bytes_len)
Fills a caller-owned buffer with secure operating-system randomness.
Definition core.c:266
@ PURIFY_ERROR_OK
Definition purify.h:29
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
secp256k1_scratch_space * scratch
secp256k1_context * ctx
secp256k1_bppp_generators * gens
secp256k1_bppp_generators gens_scratch
purify_bppp_backend_resources * resources
const unsigned char * scalars32
const unsigned char * points33
secp256k1_scratch_space * scratch
secp256k1_bulletproof_generators * gens
const purify_bulletproof_row_view * wr
const purify_bulletproof_row_view * wo
const purify_bulletproof_row_view * wl
const purify_bulletproof_row_view * wv
const unsigned char * c32
Opaque scalar storage compatible with secp256k1-zkp internal scalar storage.
Definition secp_bridge.h:22
secp256k1_context * ctx
Definition bppp_bridge.c:46
secp256k1_bulletproof_wmatrix_row * wl
Definition core.h:52
secp256k1_bulletproof_wmatrix_row * wv
Definition core.h:55
secp256k1_bulletproof_wmatrix_entry * entries
Definition core.h:57
secp256k1_bulletproof_wmatrix_row * wo
Definition core.h:54
secp256k1_bulletproof_wmatrix_row * wr
Definition core.h:53
secp256k1_fast_scalar * c
Definition core.h:56
secp256k1_ge * blinding_gen
Definition core.h:72
Definition core.h:37
secp256k1_fast_scalar scal
Definition core.h:39
size_t idx
Definition core.h:38
secp256k1_bulletproof_wmatrix_entry * entry
Definition core.h:44
secp256k1_scalar scal
Definition core.h:30
static SECP256K1_INLINE size_t secp256k1_floor_lg(size_t n)
Definition util.h:85