c++ - OpenSSL BN_sub() 不能做有符号减法
问题描述
我正在尝试使用 OpenSSL 的BN
函数执行减法。我期望有符号整数作为差异,但我得到的是无符号整数,或者整数溢出(或者如果我都没有得到想要的结果)。
我使用了错误的BN
功能吗?
int big_num_subtract_example(vector<int> *arr, unsigned int mod, int seed_num) {
BN_CTX *ctx;
ctx = BN_CTX_new();
BIGNUM *product = BN_new();
BIGNUM *tmp = BN_new();
BIGNUM *modulo = BN_new();
BIGNUM *mod_result = BN_new();
BN_set_word(product, seed_num);
BN_set_word(modulo, mod);
for (auto elem : arr) {
BN_CTX_start(ctx);
BN_set_word(tmp, elem);
BN_mul(product, product, tmp, ctx);
//Start of example
BIGNUM *sub_one = BN_new();
BIGNUM *sub_two = BN_new();
BIGNUM *num_one = BN_new();
BIGNUM *num_two = BN_new();
BN_set_word(num_one, -7);
BN_set_word(num_two, 11);
BN_sub(sub_one, num_one, num_two);
BN_sub(sub_two, num_two, num_one);
cout << "sub_one: " << BN_get_word(sub_one) << "\n"; //18446744073709551598 expected -4
cout << "sub_two: " << BN_get_word(sub_two) << "\n"; //18446744073709551598 expected 18
BN_set_word(num_one, 7);
BN_set_word(num_two, 11);
BN_sub(sub_one, num_one, num_two);
BN_sub(sub_two, num_two, num_one);
cout << "sub_one: " << BN_get_word(sub_one) << "\n"; //4 expected -11
cout << "sub_two: " << BN_get_word(sub_two) << "\n"; //4
//end of example
//...
//Code using the product from start of loop and result of subtraction...
//...
BN_CTX_end(ctx);
}
BN_clear_free(product);
BN_clear_free(tmp);
BN_clear_free(modulo);
BN_clear_free(mod_result);
BN_CTX_free(ctx);
return 0;
}
文档状态:
BN_sub() 从 a 中减去 b 并将结果放入 r (r=ab)。r 可以是与 a 或 b 相同的 BIGNUM。
解决方案
推荐阅读
- nginx - Kubernetes 上的 Nginx 代理
- c# - 如何使用 Migration on Id (primary) 启用(ON)自动增量(1,1)?C#.NET(MVC)
- c++ - 就地合并类似 std::vector 中的元素
- javascript - 如何使用 Javascript 平滑平移内联 SVG
- xslt - 添加命名空间前缀的 XSLT 转换不会复制属性
- node.js - 如何保护调用我的 API 的桌面应用程序?
- java - 无法在 Selenium 中启动多个边缘浏览器/实例?Selenium 只允许边缘的单个实例?
- kubernetes - 节点断电,但仍显示 daemonset pod 正在运行
- c# - 在 C# 中对布尔变量进行序列化反序列化的最简单方法是什么?
- bash - 为什么我的 bash 终端在使用像 server:start 这样的 Symfony CLI 命令时显示“?[30;43m”而不是格式正确的文本?