Switch to secp256k1_pedersen_commitment by Andrew Poelstra. Switch to quadratic residue based disambiguation by Pieter Wuille.
194 lines
7.7 KiB
C
194 lines
7.7 KiB
C
/**********************************************************************
|
|
* Copyright (c) 2014-2015 Gregory Maxwell *
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
|
|
**********************************************************************/
|
|
|
|
#ifndef SECP256K1_MODULE_RANGEPROOF_MAIN
|
|
#define SECP256K1_MODULE_RANGEPROOF_MAIN
|
|
|
|
#include "group.h"
|
|
|
|
#include "modules/rangeproof/pedersen_impl.h"
|
|
#include "modules/rangeproof/borromean_impl.h"
|
|
#include "modules/rangeproof/rangeproof_impl.h"
|
|
|
|
static void secp256k1_pedersen_commitment_load(secp256k1_ge* ge, const secp256k1_pedersen_commitment* commit) {
|
|
secp256k1_fe fe;
|
|
secp256k1_fe_set_b32(&fe, &commit->data[1]);
|
|
secp256k1_ge_set_xquad(ge, &fe);
|
|
if (commit->data[0] & 1) {
|
|
secp256k1_ge_neg(ge, ge);
|
|
}
|
|
}
|
|
|
|
static void secp256k1_pedersen_commitment_save(secp256k1_pedersen_commitment* commit, secp256k1_ge* ge) {
|
|
secp256k1_fe_normalize(&ge->x);
|
|
secp256k1_fe_get_b32(&commit->data[1], &ge->x);
|
|
commit->data[0] = 9 ^ secp256k1_fe_is_quad_var(&ge->y);
|
|
}
|
|
|
|
int secp256k1_pedersen_commitment_parse(const secp256k1_context* ctx, secp256k1_pedersen_commitment* commit, const unsigned char *input) {
|
|
VERIFY_CHECK(ctx != NULL);
|
|
ARG_CHECK(commit != NULL);
|
|
ARG_CHECK(input != NULL);
|
|
memcpy(commit->data, input, sizeof(commit->data));
|
|
return 1;
|
|
}
|
|
|
|
int secp256k1_pedersen_commitment_serialize(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pedersen_commitment* commit) {
|
|
VERIFY_CHECK(ctx != NULL);
|
|
ARG_CHECK(output != NULL);
|
|
ARG_CHECK(commit != NULL);
|
|
memcpy(output, commit->data, sizeof(commit->data));
|
|
return 1;
|
|
}
|
|
|
|
/* Generates a pedersen commitment: *commit = blind * G + value * G2. The blinding factor is 32 bytes.*/
|
|
int secp256k1_pedersen_commit(const secp256k1_context* ctx, secp256k1_pedersen_commitment *commit, const unsigned char *blind, uint64_t value) {
|
|
secp256k1_gej rj;
|
|
secp256k1_ge r;
|
|
secp256k1_scalar sec;
|
|
int overflow;
|
|
int ret = 0;
|
|
ARG_CHECK(ctx != NULL);
|
|
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
|
ARG_CHECK(commit != NULL);
|
|
ARG_CHECK(blind != NULL);
|
|
secp256k1_scalar_set_b32(&sec, blind, &overflow);
|
|
if (!overflow) {
|
|
secp256k1_pedersen_ecmult(&ctx->ecmult_gen_ctx, &rj, &sec, value);
|
|
if (!secp256k1_gej_is_infinity(&rj)) {
|
|
secp256k1_ge_set_gej(&r, &rj);
|
|
secp256k1_pedersen_commitment_save(commit, &r);
|
|
ret = 1;
|
|
}
|
|
secp256k1_gej_clear(&rj);
|
|
secp256k1_ge_clear(&r);
|
|
}
|
|
secp256k1_scalar_clear(&sec);
|
|
return ret;
|
|
}
|
|
|
|
/** Takes a list of n pointers to 32 byte blinding values, the first negs of which are treated with positive sign and the rest
|
|
* negative, then calculates an additional blinding value that adds to zero.
|
|
*/
|
|
int secp256k1_pedersen_blind_sum(const secp256k1_context* ctx, unsigned char *blind_out, const unsigned char * const *blinds, size_t n, size_t npositive) {
|
|
secp256k1_scalar acc;
|
|
secp256k1_scalar x;
|
|
size_t i;
|
|
int overflow;
|
|
ARG_CHECK(ctx != NULL);
|
|
ARG_CHECK(blind_out != NULL);
|
|
ARG_CHECK(blinds != NULL);
|
|
secp256k1_scalar_set_int(&acc, 0);
|
|
for (i = 0; i < n; i++) {
|
|
secp256k1_scalar_set_b32(&x, blinds[i], &overflow);
|
|
if (overflow) {
|
|
return 0;
|
|
}
|
|
if (i >= npositive) {
|
|
secp256k1_scalar_negate(&x, &x);
|
|
}
|
|
secp256k1_scalar_add(&acc, &acc, &x);
|
|
}
|
|
secp256k1_scalar_get_b32(blind_out, &acc);
|
|
secp256k1_scalar_clear(&acc);
|
|
secp256k1_scalar_clear(&x);
|
|
return 1;
|
|
}
|
|
|
|
/* Takes two lists of commitments and sums the first set and subtracts the second and verifies that they sum to excess. */
|
|
int secp256k1_pedersen_verify_tally(const secp256k1_context* ctx, const secp256k1_pedersen_commitment * const* commits, size_t pcnt, const secp256k1_pedersen_commitment * const* ncommits, size_t ncnt, int64_t excess) {
|
|
secp256k1_gej accj;
|
|
secp256k1_ge add;
|
|
size_t i;
|
|
ARG_CHECK(ctx != NULL);
|
|
ARG_CHECK(!pcnt || (commits != NULL));
|
|
ARG_CHECK(!ncnt || (ncommits != NULL));
|
|
secp256k1_gej_set_infinity(&accj);
|
|
if (excess) {
|
|
uint64_t ex;
|
|
int neg;
|
|
/* Take the absolute value, and negate the result if the input was negative. */
|
|
neg = secp256k1_sign_and_abs64(&ex, excess);
|
|
secp256k1_pedersen_ecmult_small(&accj, ex);
|
|
if (neg) {
|
|
secp256k1_gej_neg(&accj, &accj);
|
|
}
|
|
}
|
|
for (i = 0; i < ncnt; i++) {
|
|
secp256k1_pedersen_commitment_load(&add, ncommits[i]);
|
|
secp256k1_gej_add_ge_var(&accj, &accj, &add, NULL);
|
|
}
|
|
secp256k1_gej_neg(&accj, &accj);
|
|
for (i = 0; i < pcnt; i++) {
|
|
secp256k1_pedersen_commitment_load(&add, commits[i]);
|
|
secp256k1_gej_add_ge_var(&accj, &accj, &add, NULL);
|
|
}
|
|
return secp256k1_gej_is_infinity(&accj);
|
|
}
|
|
|
|
int secp256k1_rangeproof_info(const secp256k1_context* ctx, int *exp, int *mantissa,
|
|
uint64_t *min_value, uint64_t *max_value, const unsigned char *proof, size_t plen) {
|
|
size_t offset;
|
|
uint64_t scale;
|
|
ARG_CHECK(exp != NULL);
|
|
ARG_CHECK(mantissa != NULL);
|
|
ARG_CHECK(min_value != NULL);
|
|
ARG_CHECK(max_value != NULL);
|
|
offset = 0;
|
|
scale = 1;
|
|
(void)ctx;
|
|
return secp256k1_rangeproof_getheader_impl(&offset, exp, mantissa, &scale, min_value, max_value, proof, plen);
|
|
}
|
|
|
|
int secp256k1_rangeproof_rewind(const secp256k1_context* ctx,
|
|
unsigned char *blind_out, uint64_t *value_out, unsigned char *message_out, size_t *outlen, const unsigned char *nonce,
|
|
uint64_t *min_value, uint64_t *max_value,
|
|
const secp256k1_pedersen_commitment *commit, const unsigned char *proof, size_t plen) {
|
|
secp256k1_ge commitp;
|
|
ARG_CHECK(ctx != NULL);
|
|
ARG_CHECK(commit != NULL);
|
|
ARG_CHECK(proof != NULL);
|
|
ARG_CHECK(min_value != NULL);
|
|
ARG_CHECK(max_value != NULL);
|
|
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
|
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
|
secp256k1_pedersen_commitment_load(&commitp, commit);
|
|
return secp256k1_rangeproof_verify_impl(&ctx->ecmult_ctx, &ctx->ecmult_gen_ctx,
|
|
blind_out, value_out, message_out, outlen, nonce, min_value, max_value, &commitp, proof, plen);
|
|
}
|
|
|
|
int secp256k1_rangeproof_verify(const secp256k1_context* ctx, uint64_t *min_value, uint64_t *max_value,
|
|
const secp256k1_pedersen_commitment *commit, const unsigned char *proof, size_t plen) {
|
|
secp256k1_ge commitp;
|
|
ARG_CHECK(ctx != NULL);
|
|
ARG_CHECK(commit != NULL);
|
|
ARG_CHECK(proof != NULL);
|
|
ARG_CHECK(min_value != NULL);
|
|
ARG_CHECK(max_value != NULL);
|
|
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
|
secp256k1_pedersen_commitment_load(&commitp, commit);
|
|
return secp256k1_rangeproof_verify_impl(&ctx->ecmult_ctx, NULL,
|
|
NULL, NULL, NULL, NULL, NULL, min_value, max_value, &commitp, proof, plen);
|
|
}
|
|
|
|
int secp256k1_rangeproof_sign(const secp256k1_context* ctx, unsigned char *proof, size_t *plen, uint64_t min_value,
|
|
const secp256k1_pedersen_commitment *commit, const unsigned char *blind, const unsigned char *nonce, int exp, int min_bits, uint64_t value){
|
|
secp256k1_ge commitp;
|
|
ARG_CHECK(ctx != NULL);
|
|
ARG_CHECK(proof != NULL);
|
|
ARG_CHECK(plen != NULL);
|
|
ARG_CHECK(commit != NULL);
|
|
ARG_CHECK(blind != NULL);
|
|
ARG_CHECK(nonce != NULL);
|
|
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
|
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
|
secp256k1_pedersen_commitment_load(&commitp, commit);
|
|
return secp256k1_rangeproof_sign_impl(&ctx->ecmult_ctx, &ctx->ecmult_gen_ctx,
|
|
proof, plen, min_value, &commitp, blind, nonce, exp, min_bits, value);
|
|
}
|
|
|
|
#endif
|