Skip to content

Commit e85e3d1

Browse files
committed
Add internal BP++ commit API
1 parent 0240f36 commit e85e3d1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/modules/bulletproofs/bulletproofs_pp_norm_product_impl.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,66 @@ static void secp256k1_bulletproofs_powers_of_r(secp256k1_scalar *powers, const s
7777
secp256k1_scalar_sqr(&powers[i], &powers[i - 1]);
7878
}
7979
}
80+
81+
typedef struct ec_mult_bp_commit_cb_data {
82+
const secp256k1_scalar *n;
83+
const secp256k1_ge *g;
84+
const secp256k1_scalar *l;
85+
size_t g_len;
86+
} ec_mult_bp_commit_cb_data;
87+
88+
static int ec_mult_bp_commit_cb(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
89+
ec_mult_bp_commit_cb_data *data = (ec_mult_bp_commit_cb_data*) cbdata;
90+
*pt = data->g[idx];
91+
if (idx < data->g_len) {
92+
*sc = data->n[idx];
93+
} else {
94+
*sc = data->l[idx - data->g_len];
95+
}
96+
return 1;
97+
}
98+
99+
/* Create a commitment C = vG + [n_vec*G_vec]_q + l_vec*H_vec */
100+
static int secp256k1_bulletproofs_commit(
101+
const secp256k1_context* ctx,
102+
secp256k1_scratch_space* scratch,
103+
secp256k1_ge* commit,
104+
const secp256k1_bulletproofs_generators* g_vec,
105+
const secp256k1_scalar* n_vec,
106+
size_t n_vec_len,
107+
const secp256k1_scalar* l_vec,
108+
size_t l_vec_len,
109+
const secp256k1_scalar* c_vec,
110+
size_t c_vec_len,
111+
const secp256k1_scalar* q
112+
) {
113+
secp256k1_scalar v, l_c;
114+
/* First n_vec_len generators are Gs, rest are Hs*/
115+
if (g_vec->n != (n_vec_len + l_vec_len) || l_vec_len != c_vec_len) {
116+
return 0;
117+
}
118+
119+
if (!secp256k1_check_power_of_two(n_vec_len) || !secp256k1_check_power_of_two(c_vec_len)) {
120+
return 0;
121+
}
122+
/* Compute v = n_vec*n_vec*q + l_vec*c_vec */
123+
secp256k1_weighted_scalar_inner_product(&v, n_vec, 0 /*a offset */, n_vec, 0 /*b offset*/, 1 /*step*/, n_vec_len, q);
124+
secp256k1_scalar_inner_product(&l_c, l_vec, 0 /*a offset */, c_vec, 0 /*b offset*/, 1 /*step*/, l_vec_len);
125+
secp256k1_scalar_add(&v, &v, &l_c);
126+
127+
{
128+
ec_mult_bp_commit_cb_data data;
129+
secp256k1_gej commitj;
130+
data.g = g_vec->gens;
131+
data.n = n_vec;
132+
data.l = l_vec;
133+
data.g_len = n_vec_len;
134+
135+
if (!secp256k1_ecmult_multi_var(&ctx->error_callback, scratch, &commitj, &v, ec_mult_bp_commit_cb, (void*) &data, n_vec_len + l_vec_len)) {
136+
return 0;
137+
}
138+
secp256k1_ge_set_gej_var(commit, &commitj);
139+
}
140+
return 1;
141+
}
80142
#endif

0 commit comments

Comments
 (0)