我以前发过类似的问题,所以我提前道歉,但我只是无法找到我要去哪里错了。沙米尔秘密共享和拉格朗日插值(OpenSSL BIGNUM)
我实现使用OpenSSL的BIGNUM库C.
我做了一轮拉格朗日插值后沙米尔秘密分享,我乘key * numerator,然后我需要用分母去除。
因为没有BN_mod_div功能,我不是在分母使用BN_mod_inverse(),然后繁衍,像这样:
(key * numerator) * (inverse of denominator)
我所注意到的是,如果我使用BN_mod_inverse(denom, denom, q, ctx);则值应反转仍然是相同的:
Round Key: 2E
Numerator: 14
Denominator: 6 **
Multiply key with numerator: 398 (POSITIVE)
Invert Denominator: 6 (POSITIVE) **
(Key*Numerator)*inv.Denom: 3FC (POSITIVE)
Round Key: 562
Numerator: A
Denominator: -2
Multiply key with numerator: 118 (POSITIVE)
Invert Denominator: -2 (NEGATIVE)
(Key*Numerator)*inv.Denom: 3AC (POSITIVE)
Round Key: 5D1
Numerator: 8
Denominator: 3
Multiply key with numerator: 584 (POSITIVE)
Invert Denominator: 3 (POSITIVE)
(Key*Numerator)*inv.Denom: 4D4 (POSITIVE)
Recovered Key: C4 (POSITIVE)
Key should = 4D2
如果我改变,要BN_mod_inverse(newBN, denom, q, ctx);它只是变成了零:
Round Key: 2E
Numerator: 14
Denominator: 6 **
Multiply key with numerator: 398 (POSITIVE)
Invert Denominator: 0 (NEGATIVE) **
(Key*Numerator)*inv.Denom: 0 (NEGATIVE)
Round Key: 562
Numerator: A
Denominator: -2
Multiply key with numerator: 118 (POSITIVE)
Invert Denominator: 0 (NEGATIVE)
(Key*Numerator)*inv.Denom: 0 (NEGATIVE)
Round Key: 5D1
Numerator: 8
Denominator: 3
Multiply key with numerator: 584 (POSITIVE)
Invert Denominator: 0 (NEGATIVE)
(Key*Numerator)*inv.Denom: 0 (NEGATIVE)
Recovered Key: 0 (NEGATIVE)
Key should = 4D2
在任何情况下,组合键都是错误的。这里发生了什么?有没有解决方法?
这里是我的代码:
BIGNUM *int2BN(int i)
{
BIGNUM *tmp = BN_new();
BN_zero(tmp);
int g;
if(i < 0) { //If 'i' is negative
for (g = 0; g > i; g--) {
BN_sub(tmp, tmp, one);
}
} else { //If 'i' is positive
for (g = 0; g < i; g++) {
BN_add(tmp, tmp, one);
}
}
return(tmp);
}
static void
blah() {
int denomTmp, numTmp, numAccum, denomAccum;
int s, j;
BIGNUM *accum[3], *bnNum, *bnDenom;
bnNum = BN_new();
bnDenom = BN_new();
/* Lagrange Interpolation */
for (s = 0; s < 3; s++) {
numAccum = 1;
denomAccum = 1;
for (j = 0; j < 3; j++) {
if(s == j) continue;
else {
/* 0 - i[k] = numTmp */
numTmp = 0 - key[j].keynum;
/* share - i[k] = denomTmp */
denomTmp = key[s].keynum - key[j].keynum;
/* Numerator accumulation: */
numAccum *= numTmp;
/* Denominator accumulation: */
denomAccum *= denomTmp;
}
}
accum[s] = BN_new();
bnNum = int2BN(numAccum);
bnDenom = int2BN(denomAccum);
/* Multiply result by share */
BN_mod_mul(accum[s], key[s].key, bnNum, q, ctx);
/* Invert denominator */
BN_mod_inverse(bnDenom, bnDenom, q, ctx);
/* Multiply by inverted denominator */
BN_mod_mul(accum[s], accum[s], bnDenom, q, ctx);
}
int a;
BIGNUM *total = BN_new();
BN_zero(total);
for(a = 0; a < 3; a++) {
BN_mod_add(total, total, accum[a], q, ctx);
}
}
2013-02-14
Chris C
+0
这就好像您向我们展示程序的输出 - 但不是程序 - 并向我们询问有关它的问题。但那是不可能的,不是吗?我所能做的就是建议你检查[BN_mod_inverse]的文档(http://www.openssl.org/docs/crypto/BN_mod_inverse.html) –
2013-02-15 15:00:15
+0
我的问题是一个更大的问题,“可以mod_inverse处理小的和/或负的值?” (文档中没有涉及),但我没有真正说清楚。我把我的来源。 –
2013-02-15 15:48:49
+0
你在哪里设置你的模数'q'?该值是否正确?你有没有解决这个问题呢? –
2013-05-06 09:24:45