relic-toolkit / relic

Code
Other
452 stars 179 forks source link

Alerts for errors #294

Closed Salterwater23 closed 4 months ago

Salterwater23 commented 4 months ago
#include <stdio.h>
#include <assert.h>
#include "relic.h"
// 定义一个函数,用于执行 Paillier 密码体制的操作
static int paillier(void) {
    int code = RLC_ERR; // 默认返回错误代码
    bn_t a, b, c, d, s, pub, prv; // 定义大数变量
    int result;
    // 初始化所有大数为 NULL
    bn_null(a);
    bn_null(b);
    bn_null(c);
    bn_null(d);
    bn_null(pub);
    bn_null(prv);
    // 尝试区块,用于捕获在此区块内可能发生的任何异常
    RLC_TRY{
        // 为所有大数分配空间
        bn_new(a);
        bn_new(b);
        bn_new(c);
        bn_new(d);
        bn_new(pub);
        bn_new(prv);
        printf("Big numbers initialized.\n"); // 输出初始化成功的信息
        // 生成2048位的公钥和私钥
        result = cp_ghpe_gen(pub, prv, 2048);
        if (result != RLC_OK) {
            printf("Error in cp_ghpe_gen(): %d\n", result);
            RLC_ERROR(end);
        }
        //assert(result == RLC_OK); // 断言生成成功
        printf("Public and private keys generated successfully.\n");
        // 打印生成的公钥和私钥
        printf("Public key:\n");
        bn_print(pub);
        printf("Private key:\n");
        bn_print(prv);
        // 进行两轮加解密和同态性测试
        for (int i = 1; i <= 2; i++) {
            printf("Testing generalized paillier for (s = %d)\n", i);
            // 加解密测试
            // 生成小于 n^s 的明文 a
            bn_rand(a, RLC_POS, i * (bn_bits(pub) - 1));
            //加密、解密并检查结果是否相同。
            result = cp_ghpe_enc(c, a, pub, i);
            printf("Encryption result: %d\n", result);
            assert(result == RLC_OK);
            result = cp_ghpe_dec(b, c, pub, prv, i);
            printf("Decryption result: %d\n", result);
            assert(result == RLC_OK);
            assert(bn_cmp(a, b) == RLC_EQ);
            // 同态性测试:Dec(c*d)=a+b
            // 重新生成新的明文 a 和 b
            bn_rand(a, RLC_POS, i * (bn_bits(pub) - 1));
            bn_rand(b, RLC_POS, i * (bn_bits(pub) - 1));
            /* Encrypt both plaintexts using the same public key. */
            assert(cp_ghpe_enc(c, a, pub, i) == RLC_OK);
            assert(cp_ghpe_enc(d, b, pub, i) == RLC_OK);
            // 计算 (d * c) mod n^(i + 1)
            bn_mul(c, c, d);
            bn_sqr(d, pub);
            if (i == 2) {
                bn_mul(d, d, pub);
            }
            bn_mod(c, c, d);
            // 解密 c,检查结果是否等于 (a + b) mod n^i
            assert(cp_ghpe_dec(c, c, pub, prv, i) == RLC_OK);
            bn_add(a, a, b);
            bn_mod(a, a, d);
            assert(bn_cmp(a, c) == RLC_EQ);
        }
    } RLC_CATCH_ANY{
        printf("An error occurred.\n");// 捕获到异常时输出错误信息
        RLC_ERROR(end);
    }
    code = RLC_OK; // 正常结束设置返回码为 RLC_OK
end:
    // 释放所有大数资源
    bn_free(a);
    bn_free(b);
    bn_free(c);
    bn_free(d);
    bn_free(prv);
    bn_free(pub);
    return code;// 返回执行结果码
}
int main() {
    if (core_init() != RLC_OK) {
        fprintf(stderr, "Failed to initialize RELIC library.\n");
        return 1; // RELIC 库初始化失败时返回错误
    }
    if (paillier() != RLC_OK) {
        fprintf(stderr, "Paillier test failed.\n");
        return 1;// 测试失败时返回错误
    }
    // 清理 RELIC 库资源
    core_clean();
    return 0;// 程序正常结束
}

The above is my code, which reports an error after running the ERROR in bn_make() at D:\evn\relic-main\src\bn\relic_bn_mem.c,81: (null). CAUGHT in bn_mul_comba() at D:\evn\relic-main\src\bn\relic_bn_mul.c,232. Encryption result: 1 Assertion failed: result == RLC_OK, file D:\vsproject\ProjectTestRelic\ProjectTestRelic\ProjectTestRelic.c, line 53 What is the cause of the following

dfaranha commented 4 months ago

If you're using 2048-bit Paillier, change the configuration option with BN_PRECI=2048.

Salterwater23 commented 4 months ago

As per your suggestion, I changed the configuration options, but it still reports the error

dfaranha commented 4 months ago

Did you rebuild the library and your binaries?

The error you receive, and which I cannot reproduce, is exactly because the code is trying to allocate integers larger than the build-time configuration.

dfaranha commented 4 months ago

Closing in hope that the issue is solved.