Skip to content

Commit 08e922c

Browse files
committed
Expose BP++ Apis
1 parent 4d59ef5 commit 08e922c

File tree

2 files changed

+197
-7
lines changed

2 files changed

+197
-7
lines changed

include/secp256k1_bulletproofs.h

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ extern "C" {
99

1010
#include <stdint.h>
1111

12+
#include "include/secp256k1_generator.h"
13+
1214
/** Opaque structure representing a large number of NUMS generators */
1315
typedef struct secp256k1_bulletproofs_generators secp256k1_bulletproofs_generators;
1416

1517
/** Opaque structure representing a prover context used in bulletproofs++ prover */
1618
typedef struct secp256k1_bulletproofs_pp_rangeproof_prover_context secp256k1_bulletproofs_pp_rangeproof_prover_context;
1719

18-
/** Returns a list of generators, or NULL if allocation failed.
20+
/** Allocates and initializes a list of NUMS generators
21+
* Returns a list of generators, or NULL if allocation failed.
1922
* Args: ctx: pointer to a context object
2023
* n: number of NUMS generators to produce. Should be 16 + 7 = 23
2124
* for a 64 bit range proof with base 16. In general, n = max(num_digits, base) + 7
2225
* where num_digits is the number of digits in base `base` representation of `n_bits`
2326
* base 2 number.
2427
*
25-
* TODO: For the first version of PR, this is would still require 16 + 8 = 24 NUMS
26-
* points. We will later use G = H0(required for compatibility with pedersen_commitment DS)
27-
* in a separate commit to make review easier.
2828
*/
2929
SECP256K1_API secp256k1_bulletproofs_generators *secp256k1_bulletproofs_generators_create(
3030
const secp256k1_context* ctx,
@@ -52,9 +52,6 @@ SECP256K1_API secp256k1_bulletproofs_generators* secp256k1_bulletproofs_generato
5252
* least 33 times the number of generators plus one(33 * (num_gens + 1));
5353
* will be ser to 33 times the number of generators plus one
5454
* on successful return
55-
*
56-
* TODO: For ease of review, this setting G = H0 is not included in this commit. We will
57-
* add it in a separate commit.
5855
*/
5956
SECP256K1_API int secp256k1_bulletproofs_generators_serialize(
6057
const secp256k1_context* ctx,
@@ -73,6 +70,97 @@ SECP256K1_API void secp256k1_bulletproofs_generators_destroy(
7370
secp256k1_bulletproofs_generators* gen
7471
) SECP256K1_ARG_NONNULL(1);
7572

73+
/** Returns the serialized size of an bulletproofs plus plus proof of a given number
74+
* of bits and the base. Both base and n_bits must be a power of two. The number
75+
* of digits required to represent number of bits in the given base must also be
76+
* a power of two. Specifically, all of n_bits, base and num_digits = (n_bits / log2(base))
77+
* must all be a power of two.
78+
* Args: ctx: pointer to a context object
79+
* Out: len: 0 if the parameters and num_digits (n_bits/log2(base)) are not a power of two
80+
* length of the serialized proof otherwise
81+
* In: n_bits: number of bits to prove (max 64, should usually be 64)
82+
* base: base representation to be used in proof construction (max 256, recommended 16)
83+
*/
84+
SECP256K1_API size_t secp256k1_bulletproofs_pp_rangeproof_proof_length(
85+
const secp256k1_context* ctx,
86+
size_t n_bits,
87+
size_t base
88+
) SECP256K1_ARG_NONNULL(1);
89+
90+
/** Produces a Bulletproofs++ rangeproof. Returns 1 on success, 0 on failure.
91+
* Proof creation can only fail if the arguments are invalid. The documentation
92+
* below specifies the constraints on inputs and arguments under which this API
93+
* can fail.
94+
* Args: ctx: pointer to a context object
95+
* scratch: pointer to a scratch space
96+
* gens: pointer to the generator set to use, which must have exactly
97+
* `n = max(num_digits, base) + 7` generators, where num_digits is the number.
98+
* asset_gen: pointer to the asset generator for the Pedersen/CT commitment
99+
* Out: proof: pointer to a byte array to output the proof into
100+
* In/Out: plen: pointer to the size of the above array; will be set to the actual size of
101+
* the serialized proof. To learn this value in advance, to allocate a sufficient
102+
* buffer, call `secp256k1_bulletproofs_pp_rangeproof_proof_length`
103+
* In: n_bits: size of range being proven, in bits. Must be a power of two,
104+
* and at most 64.
105+
* base: base representation to be used in proof construction. Must be a power of two,
106+
* value: value committed in the Pedersen commitment. Must be less
107+
* than 2^n_bits.
108+
* min_value: minimum value of the range being proven. Must be less than value
109+
* commit: the Pedersen commitment being proven
110+
* blind: blinding factor for the Pedersen commitment. Must be a 32 byte
111+
* valid scalar within secp curve order.
112+
* nonce: seed for the RNG used to generate random data during proving
113+
* extra_commit: arbitrary extra data that the proof commits to (may be NULL if extra_commit_len is 0)
114+
* extra_commit_len: length of the arbitrary extra data
115+
*/
116+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_bulletproofs_pp_rangeproof_prove(
117+
const secp256k1_context* ctx,
118+
secp256k1_scratch_space *scratch,
119+
const secp256k1_bulletproofs_generators* gens,
120+
const secp256k1_generator* asset_gen,
121+
unsigned char* proof,
122+
size_t* plen,
123+
const size_t n_bits,
124+
const size_t base,
125+
const uint64_t value,
126+
const uint64_t min_value,
127+
const secp256k1_pedersen_commitment* commit,
128+
const unsigned char* blind,
129+
const unsigned char* nonce,
130+
const unsigned char* extra_commit,
131+
size_t extra_commit_len
132+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(11) SECP256K1_ARG_NONNULL(12) SECP256K1_ARG_NONNULL(13);
133+
134+
/** Verifies an Bulletproofs++ rangeproof. Returns 1 on success, 0 on failure.
135+
* Args: ctx: pointer to a context object
136+
* scratch: pointer to a scratch space
137+
* gens: pointer to the generator set to use, which must have at least 2*n_bits generators
138+
* asset_gen: pointer to the asset generator for the CT commitment
139+
* In: proof: pointer to a byte array containing the serialized proof
140+
* plen: length of the serialized proof
141+
* n_bits: size of range being proven, in bits. Must be a power of two,
142+
* and at most 64.
143+
* base: base representation to be used in proof construction. Must be a power of two,
144+
* min_value: minimum value of the range being proven
145+
* commit: the Pedersen commitment being proven
146+
* extra_commit: arbitrary extra data that the proof commits to (may be NULL if extra_commit_len is 0)
147+
* extra_commit_len: length of the arbitrary extra data
148+
*/
149+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_bulletproofs_pp_rangeproof_verify(
150+
const secp256k1_context* ctx,
151+
secp256k1_scratch_space *scratch,
152+
const secp256k1_bulletproofs_generators* gens,
153+
const secp256k1_generator* asset_gen,
154+
const unsigned char* proof,
155+
const size_t plen,
156+
const uint64_t n_bits,
157+
const uint64_t base,
158+
const uint64_t min_value,
159+
const secp256k1_pedersen_commitment* commit,
160+
const unsigned char* extra_commit,
161+
size_t extra_commit_len
162+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(10);
163+
76164
# ifdef __cplusplus
77165
}
78166
# endif

src/modules/bulletproofs/main_impl.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,106 @@ size_t secp256k1_bulletproofs_pp_rangeproof_proof_length(
170170
return 33 * 4 + 65*n_rounds + 64;
171171
}
172172

173+
int secp256k1_bulletproofs_pp_rangeproof_prove(
174+
const secp256k1_context* ctx,
175+
secp256k1_scratch_space *scratch,
176+
const secp256k1_bulletproofs_generators* gens,
177+
const secp256k1_generator* asset_gen,
178+
unsigned char* proof,
179+
size_t* plen,
180+
const size_t n_bits,
181+
const size_t base,
182+
const uint64_t value,
183+
const uint64_t min_value,
184+
const secp256k1_pedersen_commitment* commit,
185+
const unsigned char* blind,
186+
const unsigned char* nonce,
187+
const unsigned char* extra_commit,
188+
size_t extra_commit_len
189+
) {
190+
secp256k1_ge commitp, asset_genp;
191+
secp256k1_scalar blinds;
192+
int overflow;
193+
194+
VERIFY_CHECK(ctx != NULL);
195+
VERIFY_CHECK(scratch != NULL);
196+
ARG_CHECK(gens != NULL);
197+
ARG_CHECK(asset_gen != NULL);
198+
ARG_CHECK(proof != NULL);
199+
ARG_CHECK(plen != NULL);
200+
ARG_CHECK(commit != NULL);
201+
ARG_CHECK(blind != NULL);
202+
ARG_CHECK(nonce != NULL);
203+
ARG_CHECK(extra_commit != NULL || extra_commit_len == 0);
204+
205+
secp256k1_scalar_set_b32(&blinds, blind, &overflow);
206+
if (overflow) {
207+
return 0;
208+
}
209+
210+
secp256k1_pedersen_commitment_load(&commitp, commit);
211+
secp256k1_generator_load(&asset_genp, asset_gen);
212+
213+
return secp256k1_bulletproofs_pp_rangeproof_prove_impl(
214+
ctx,
215+
scratch,
216+
gens,
217+
&asset_genp,
218+
proof,
219+
plen,
220+
n_bits,
221+
base,
222+
value,
223+
min_value,
224+
&commitp,
225+
&blinds,
226+
nonce,
227+
extra_commit,
228+
extra_commit_len
229+
);
230+
}
231+
232+
int secp256k1_bulletproofs_pp_rangeproof_verify(
233+
const secp256k1_context* ctx,
234+
secp256k1_scratch_space *scratch,
235+
const secp256k1_bulletproofs_generators* gens,
236+
const secp256k1_generator* asset_gen,
237+
const unsigned char* proof,
238+
const size_t plen,
239+
const uint64_t n_bits,
240+
const uint64_t base,
241+
const uint64_t min_value,
242+
const secp256k1_pedersen_commitment* commit,
243+
const unsigned char* extra_commit,
244+
size_t extra_commit_len
245+
) {
246+
secp256k1_ge commitp, asset_genp;
247+
248+
VERIFY_CHECK(ctx != NULL);
249+
VERIFY_CHECK(scratch != NULL);
250+
ARG_CHECK(gens != NULL);
251+
ARG_CHECK(asset_gen != NULL);
252+
ARG_CHECK(proof != NULL);
253+
ARG_CHECK(commit != NULL);
254+
ARG_CHECK(extra_commit != NULL || extra_commit_len == 0);
255+
256+
secp256k1_pedersen_commitment_load(&commitp, commit);
257+
secp256k1_generator_load(&asset_genp, asset_gen);
258+
259+
return secp256k1_bulletproofs_pp_rangeproof_verify_impl(
260+
ctx,
261+
scratch,
262+
gens,
263+
&asset_genp,
264+
proof,
265+
plen,
266+
n_bits,
267+
base,
268+
min_value,
269+
&commitp,
270+
extra_commit,
271+
extra_commit_len
272+
);
273+
}
274+
173275
#endif

0 commit comments

Comments
 (0)