Windows上的PBC - 无法保存元素

时间:2014-08-29 09:56:07

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

我在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;
}

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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