purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
scratch_frames.c
Go to the documentation of this file.
1/*
2 * Legacy Bulletproof scratch-frame compatibility shim.
3 *
4 * This file owns the mutable side table used by the imported legacy
5 * Bulletproof code to emulate nested scratch frames on top of secp256k1's
6 * checkpoint API.
7 */
8
9#include <stdatomic.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include "third_party/secp256k1-zkp/src/scratch_impl.h"
14
21
22static atomic_flag purify_bulletproof_scratch_frames_lock = ATOMIC_FLAG_INIT;
24
26 while (atomic_flag_test_and_set_explicit(&purify_bulletproof_scratch_frames_lock, memory_order_acquire)) {
27 }
28}
29
31 atomic_flag_clear_explicit(&purify_bulletproof_scratch_frames_lock, memory_order_release);
32}
33
35 secp256k1_scratch *scratch,
37) {
40
41 while (cur != NULL && cur->scratch != scratch) {
42 prev = cur;
43 cur = cur->next;
44 }
45 if (prev_out != NULL) {
46 *prev_out = prev;
47 }
48 return cur;
49}
50
51int secp256k1_scratch_allocate_frame(secp256k1_scratch *scratch, size_t n, size_t objects) {
53 if (scratch == NULL) {
54 return 0;
55 }
56
59 if (frames == NULL) {
60 frames = (purify_bulletproof_scratch_frames *)malloc(sizeof(*frames));
61 if (frames == NULL) {
63 return 0;
64 }
65 memset(frames, 0, sizeof(*frames));
66 frames->scratch = scratch;
69 }
70 if (frames->depth >= sizeof(frames->checkpoints) / sizeof(frames->checkpoints[0])) {
72 return 0;
73 }
74 if (n > secp256k1_scratch_max_allocation(&default_error_callback, scratch, objects)) {
76 return 0;
77 }
78 frames->checkpoints[frames->depth++] = secp256k1_scratch_checkpoint(&default_error_callback, scratch);
80 return 1;
81}
82
86
87 VERIFY_CHECK(scratch != NULL);
90 VERIFY_CHECK(frames != NULL);
91 VERIFY_CHECK(frames->depth > 0);
92 secp256k1_scratch_apply_checkpoint(&default_error_callback, scratch, frames->checkpoints[--frames->depth]);
93 if (frames->depth == 0) {
94 if (prev == NULL) {
96 } else {
97 prev->next = frames->next;
98 }
99 free(frames);
100 }
102}
static purify_bulletproof_scratch_frames * purify_bulletproof_scratch_frames_head
static atomic_flag purify_bulletproof_scratch_frames_lock
static void purify_bulletproof_scratch_frames_release_lock(void)
int secp256k1_scratch_allocate_frame(secp256k1_scratch *scratch, size_t n, size_t objects)
static purify_bulletproof_scratch_frames * purify_bulletproof_scratch_frames_find(secp256k1_scratch *scratch, purify_bulletproof_scratch_frames **prev_out)
void secp256k1_scratch_deallocate_frame(secp256k1_scratch *scratch)
static void purify_bulletproof_scratch_frames_acquire_lock(void)
struct purify_bulletproof_scratch_frames * next