如何实现BN_num_bytes()(和BN_num_bits())在C#中? [英] How do I implement BN_num_bytes() (and BN_num_bits() ) in C#?
问题描述
我这条线从C ++移植到C#,我又不是一个有经验的C ++程序员:
I'm porting this line from C++ to C#, and I'm not an experienced C++ programmer:
unsigned int nSize = BN_num_bytes(this);
在.NET中,我使用System.Numerics.BigInteger
In .NET I'm using System.Numerics.BigInteger
BigInteger num = originalBigNumber;
byte[] numAsBytes = num.ToByteArray();
uint compactBitsRepresentation = 0;
uint size2 = (uint)numAsBytes.Length;
我觉得这是在他们内部是如何运作的,有根本的区别,因为来源单元测试结果如果使用BIGINT等于不匹配:
I think there is a fundamental difference in how they operate internally, since the sources' unit tests' results don't match if the BigInt equals:
- 0
- 在任何负数
- 在0x00123456处
我知道从字面上一无所知 BN_num_bytes
(*编辑:评论只是告诉我,这对BN_num_bits宏)*。
I know literally nothing about BN_num_bytes
(*edit: the comments just told me that it's a macro for BN_num_bits)*.
问题
您是否验证有关code这些猜测:
Would you verify these guesses about the code:
-
我需要移植
BN_num_bytes
这是((BN_num_bits(BN)+7)/ 8),$宏C $ C>(谢谢@WhozCraig)
I need to port
BN_num_bytes
which is a macro for((BN_num_bits(bn)+7)/8)
(Thank you @WhozCraig)
我需要移植 BN_num_bits
是地板(LOG2(W))+ 1
之后,如果可能性是存在的开头和结尾的字节数不计,那么是什么在大/小端机器会发生什么?有没有关系呢?
Then, if the possibility exists that leading and trailing bytes aren't counted, then what happens on Big/Little endian machines? Does it matter?
根据这些问题的答案在Security.StackExchange,和我的应用程序的性能并不十分重要,我可能使用默认的实现在.NET中,不使用可能已经实现了相当的替代方法的备用库。
Based on these answers on Security.StackExchange, and that my application isn't performance critical, I may use the default implementation in .NET and not use an alternate library that may already implement a comparable workaround.
编辑:到目前为止,我的实现看起来是这样的,但我不知道什么是LookupTable中是中提到的意见
so far my implementation looks something like this, but I'm not sure what the "LookupTable" is as mentioned in the comments.
private static int BN_num_bytes(byte[] numAsBytes)
{
int bits = BN_num_bits(numAsBytes);
return (bits + 7) / 8;
}
private static int BN_num_bits(byte[] numAsBytes)
{
var log2 = Math.Log(numAsBytes.Length, 2);
var floor = Math.Floor(log2);
return (uint)floor + 1;
}
编辑2:
Edit 2:
一些搜索后,我发现:
BN_num_bits不返回给定BIGNUM的显著比特数,但最显著1比特相当的位置,这并不一定是一样的东西
BN_num_bits does not return the number of significant bits of a given bignum, but rather the position of the most significant 1 bit, which is not necessarily the same thing
虽然我还是不知道它的来源的样子...
Though I still don't know what the source of it looks like...
推荐答案
在手册页(OpenSSL的BN_num_bits的项目)说,基本上,除了零,则返回地板(LOG2(W))+ 1
。
所以,这些都是 BN_num_bytes
和 BN_num_bits
对.NET的功能的BigInteger 的正确实施code>。
The man page (OpenSSL project) of BN_num_bits says that "Basically, except for a zero, it returns floor(log2(w))+1
.".
So these are the correct implementations of the BN_num_bytes
and BN_num_bits
functions for .Net's BigInteger
.
public static int BN_num_bytes(BigInteger number) {
if (number == 0) {
return 0;
}
return 1 + (int)Math.Floor(BigInteger.Log(BigInteger.Abs(number), 2)) / 8;
}
public static int BN_num_bits(BigInteger number) {
if (number == 0) {
return 0;
}
return 1 + (int)Math.Floor(BigInteger.Log(BigInteger.Abs(number), 2));
}
您也许应该改变这些为方便起见,扩展方法。
You should probably change these into extension methods for convenience.
您应该明白,这些功能测量比特/需要ex的preSS一个给定整数的字节的最小数目。声明为 INT
变量( System.Int32的
)取4个字节的内存,但你只需要1个字节(或3位)EX preSS的整数7.这是BN_num_bytes和BN_num_bits计算 - 所需的最小存储空间大小的具体数量
You should understand that these functions measure the minimum number of bits/bytes that are needed to express a given integer number. Variables declared as int
(System.Int32
) take 4 bytes of memory, but you only need 1 byte (or 3 bits) to express the integer number 7. This is what BN_num_bytes and BN_num_bits calculate - the minimum required storage size for a concrete number.
您可以找到的功能,在<一个原始的实现源$ C $ C href="http://git.openssl.org/gitweb/?p=openssl.git;a=blob_plain;f=crypto/bn/bn_lib.c;hb=HEAD">official OpenSSL的库。
You can find the source code of the original implementations of the functions in the official OpenSSL repository.
这篇关于如何实现BN_num_bytes()(和BN_num_bits())在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!