【问题标题】:PBC on Windows - Unable to save elementWindows 上的 PBC - 无法保存元素
【发布时间】:2014-08-29 09:56:07
【问题描述】:

我在 Windows 上使用PBC,当我使用以下代码时,我无法通过字节方法或人类可读的字符串方法保存元素 gx(注意它是一个指数:g^x)。我该如何处理它,注意到上面的代码是 BLS 并且 BLS 公钥包括 gx,但不是 x(它是私有的)?

#include <stdlib.h>
#include <string.h>
#include <pbc.h>

// BLS structures

struct bls_sys_param_s {
    pairing_ptr pairing;
    element_t g;
    int signature_length;
};
typedef struct bls_sys_param_s bls_sys_param_t[1];
typedef struct bls_sys_param_s *bls_sys_param_ptr;

struct bls_private_key_s {
    bls_sys_param_ptr param;
    element_t x;
};
typedef struct bls_private_key_s bls_private_key_t[1];
typedef struct bls_private_key_s *bls_private_key_ptr;

struct bls_public_key_s {
    bls_sys_param_ptr param;
    element_t gx;
};
typedef struct bls_public_key_s bls_public_key_t[1];
typedef struct bls_public_key_s *bls_public_key_ptr;

void bls_sign(unsigned char *sig, unsigned int hashlen, unsigned char *hash,
    bls_private_key_t sk)
{
    element_t h;

    element_init_G1(h, sk->param->pairing);
    element_from_hash(h, hash, hashlen);
    element_pow_zn(h, h, sk->x);
    element_to_bytes_x_only(sig, h);

    element_clear(h);
}

int bls_verify(unsigned char *sig, unsigned int hashlen, unsigned char *hash,
    bls_public_key_t pk)
{
    //have to mess with internals since we are only given the x-coord
    element_t hx;
    element_t h;
    int res;

    pairing_ptr pairing = pk->param->pairing;

    element_init_G1(h, pairing);
    element_from_hash(h, hash, hashlen);
    element_init_G1(hx, pairing);
    element_from_bytes_x_only(hx, sig);

    res = is_almost_coddh(h, hx, pk->param->g, pk->gx, pk->param->pairing);

    element_clear(hx);
    element_clear(h);
    return res;
}

void bls_clear_sys_param(bls_sys_param_t param)
{
    element_clear(param->g);
}

void bls_clear_public_key(bls_public_key_t pk)
{
    element_clear(pk->gx);
}

void bls_clear_private_key(bls_private_key_t sk)
{
    element_clear(sk->x);
}

int main() {
    pairing_t pairing;
    bls_sys_param_t param;
    bls_public_key_t pk;
    bls_private_key_t sk;
    unsigned char *sig;
    const char p[] = "";
    int n;
    unsigned char *gen_x;

    printf("reading data...\n");
    // Pairing Initialization
    pairing_init_set_str(pairing, p);

    // Parameter Initialization
    param->pairing = pairing;
    param->signature_length = pairing_length_in_bytes_x_only_G1(pairing);
    element_init_G2(param->g, pairing);
    element_random(param->g);
    //element_out_str(stdout, 32, param->g);

    // Public/Private Key Initialization
    pk->param = sk->param = param;
    element_init_G2(pk->gx, param->pairing);
    element_init_Zr(sk->x, param->pairing);
    element_random(sk->x);
    //element_out_str(stdout, 32, sk->x);
    element_pow_zn(pk->gx, param->g, sk->x);
    //element_out_str(stdout, 32, pk->gx);

    n = element_length_in_bytes(pk->gx);
    gen_x = (unsigned char *)malloc(n*sizeof(unsigned char));
    printf("%d  %d\n", n, element_to_bytes(gen_x, pk->gx));

    sig = (unsigned char *)pbc_malloc(param->signature_length);

    printf("signing...\n");
    bls_sign(sig, 11, (unsigned char *) "hello world", sk);

    printf("verifying...\n");
    if (bls_verify(sig, 11, (unsigned char *) "hello world", pk)) {
        printf("signature verifies\n");
    }
    else {
        printf("signature does not verify\n");
    }

    pbc_free(sig);
    bls_clear_public_key(pk);
    bls_clear_private_key(sk);
    bls_clear_sys_param(param);
    pairing_clear(pairing);
    return 0;
}

对不起,我无法透露我正在使用的参数,但您可以执行以下操作:与“gengparam.exe >> gm.param”(或其他代,如“genaparam.exe”)生成配对然后将 gm.param 的内容复制到 p 常量表(注意用“\n”替换新行,之前或之后没有任何空格,只有那些已经存在的)。注意这里的g和x是随机元素...

我只是想以某种方式保存 gx,以便将其作为公钥常量的一部分提供,正如我已经提到的,x 不得成为公钥的一部分。

【问题讨论】:

    标签: c++ windows visual-studio-2013 cryptography


    【解决方案1】:

    也许你可以试试 element_printf(),例如

    element_printf("%B\n", pk->gx);
    

    如果您想序列化任何元素,请考虑使用 element_to_bytes() 和 element_from_bytes() 函数。更多详情here.

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2016-04-23
      • 2016-11-20
      • 2016-07-14
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多