Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
6704 Discussions

Big Number sign and a bug in the documentation sample

Ricardo_Costa
Beginner
456 Views
Hello,

I'm working with the Cryptography functions that handle Big Numbers with IPP 6.1 Update 2. I'd like to know the best way to change the sign of a big number. The ippsSet_BN function allows that, but you must also specifythe value, which I don't want to change, so it would be wasting time copying it.

Also, I'd like to report a bug in the BigNumber Class sample provided in the manual. One of the BigNumber constructors has a memory leak as shown below:

BigNumber::BigNumber(const BigNumber& bn)
{
IppsBigNumSGN sgn;
int length;
ippsGetSize_BN(bn.m_pBN, &length);
Ipp32u* pData = new Ipp32u[length];
ippsGet_BN(&sgn, &length, pData, bn.m_pBN);
//
create(pData, length, sgn);
//
delete pData; // <-- Here it should be delete[]
}


Thank you.
0 Kudos
1 Solution
Ying_H_Intel
Employee
456 Views
Quoting - Ricardo Costa

I think so, since there's no way ippsSet_BN canskip data copying without checking ifthe data in the pointer passed as parameteris the same as data in the BN structure (m_pBN).In my understanding, that would only be possible if itallowed the data pointerto be passed asNULL, which is currently not the case.

Hi Ricardo, Andrzej

Your doubts are right. I check again with our engineers. The ippsSet_BNcan skip data copying only if the data pointer passed as parameter is same as data dresses in BN structure.But the feature is implemented in next release (may be IPP 7.0), no in current version IPP 6.1 update 2.So yes, there is no way to skip data copying at current version.

And if unary - operation, I recommend to use thepairippsRef_BN() and ippsSet_BN().
Thecodemay be like
// change sign
IppsBigNumSGN sgn;
int bitSize;
Ipp32u* pData;
ippsRef_BN(&sgn, &bitSize, &pData, pBN);
sgn = IppsBigNumPOS==sgn? IppsBigNumNEG : IppsBigNumPOS;
ippsSet_BN(sgn,BITS_TO_DWORD(bitSize), pData, pBN);
//(will no data copy in future version if external and internal data have the same addresses)

Regards,
Ying

View solution in original post

0 Kudos
10 Replies
Ying_H_Intel
Employee
456 Views

Hello Ricardo,

Thank you for the bug reports. Yes, it should bedelete [] pData.

About your question aboutthe way to changethe sign of big number, please see the below our experts suggested,

Big Number in IPP supports signed numbers. the are several ways how to handle sign of number:
1) sign_hanged_number = 0-number
2) exchange operation (Add<->Sub) in the algebraic summation
3) use the pair ippsExtGet_BN() and ippsSet_BN(). There is no data copy in this case

Hope it helps.
Regards,
Ying
0 Kudos
Ricardo_Costa
Beginner
456 Views
Quoting - Ying H (Intel)

Hello Ricardo,

Thank you for the bug reports. Yes, it should bedelete [] pData.

About your question aboutthe way to changethe sign of big number, please see the below our experts suggested,

Big Number in IPP supports signed numbers. the are several ways how to handle sign of number:
1) sign_hanged_number = 0-number
2) exchange operation (Add<->Sub) in the algebraic summation
3) use the pair ippsExtGet_BN() and ippsSet_BN(). There is no data copy in this case

Hope it helps.
Regards,
Ying

Hi,

Thanks for the fast reply. Regarding solution #3, which I believe is the best for my problem, I'd like to do the following:

IppsBigNumSGN sign;
ippsExtGet_BN(&sign, 0, 0, m_pBN);
sign = (sign == IppsBigNumPOS) ? IppsBigNumNEG : IppsBigNumPOS;
ippsSet_BN(sign, 0, NULL, m_pBN); // not allowed

But ippsSet_BN doesn't allow passing NULL to the data parameter, so I always have to specify a data buffer. Since I only want to change the sign, not the value, it would imply getting the BN data and passing it to ippsSet_BN again, wouldn't it? In this case, I suggest modifying ippsSet_BN in order to allow a change of the sign only.

Regards,
Ricardo
0 Kudos
Ying_H_Intel
Employee
456 Views
Quoting - Ricardo Costa

Hi,

Thanks for the fast reply. Regarding solution #3, which I believe is the best for my problem, I'd like to do the following:

IppsBigNumSGN sign;
ippsExtGet_BN(&sign, 0, 0, m_pBN);
sign = (sign == IppsBigNumPOS) ? IppsBigNumNEG : IppsBigNumPOS;
ippsSet_BN(sign, 0, NULL, m_pBN); // not allowed

But ippsSet_BN doesn't allow passing NULL to the data parameter, so I always have to specify a data buffer. Since I only want to change the sign, not the value, it would imply getting the BN data and passing it to ippsSet_BN again, wouldn't it? In this case, I suggest modifying ippsSet_BN in order to allow a change of the sign only.

Regards,
Ricardo

Hi Ricardo,
Right,the ippsSet_BN don't accept Null to the data parameterincurrent version, seems the pData is required here.Do you likesubmit it as feature request?

Morover, considering the bestway (performance), the solution # 2 should efficent if youhave other operations following the operaion of change the sign.

Regards,
Ying
0 Kudos
achrzesz2
New Contributor I
456 Views
Hello Ricardo
Hello Ying

I'd like to mention that Intel's mkl-gmp library has specialized function

mpz_neg (negating big integers)

so I can understand Ricardo's final sugestion

On the other hand
as far as I understand the situation, changing the sign:

ippsSet_BN(sign, m_len, m_Data, m_pBN);

needs only the pointers m_len, m_Data to BN data
and no excessive copying is needed
Passing two pointers is'nt to costly
Is it?

Regards,
Andrzej Ch
0 Kudos
Ricardo_Costa
Beginner
456 Views
Quoting - Ying H (Intel)

Hi Ricardo,
Right,the ippsSet_BN don't accept Null to the data parameterincurrent version, seems the pData is required here.Do you likesubmit it as feature request?

Morover, considering the bestway (performance), the solution # 2 should efficent if youhave other operations following the operaion of change the sign.

Regards,
Ying

Ying,

Solution #2 is not always possible. The negate functionality is neededto implement the unary operator- of my BigNumber class,for example. I'd like to submit that as a feature request, if possible.


Quoting - achrzesz2
Hello Ricardo
Hello Ying

I'd like to mention that Intel's mkl-gmp library has specialized function

mpz_neg (negating big integers)

so I can understand Ricardo's final sugestion

On the other hand
as far as I understand the situation, changing the sign:

ippsSet_BN(sign, m_len, m_Data, m_pBN);

needs only the pointers m_len, m_Data to BN data
and no excessive copying is needed
Passing two pointers is'nt to costly
Is it?

Regards,
Andrzej Ch

Hello Andrzej,

The data buffer of a big number is stored internally in the IppsBigNumState structure and is not accessible from outside. You can get a copy of thatdata into your localbufferwith ippsGet_BN, but then the ippsSet_BN function would have to copy the data from the local buffer back into its internal buffer, meaning that two data copy operations would be performed.

Regards,
Ricardo
0 Kudos
achrzesz2
New Contributor I
456 Views
Quoting - Ricardo Costa



Hello Andrzej,

The data buffer of a big number is stored internally in the IppsBigNumState structure and is not accessible from outside. You can get a copy of thatdata into your localbufferwith ippsGet_BN, but then the ippsSet_BN function would have to copy the data from the local buffer back into its internal buffer, meaning that two data copy operations would be performed.

Regards,
Ricardo
Hello Ricardo
Thank you for replay
Now I'm slightly confused since your opinion is not in accordance with that of Intel's expert:
"3) use the pair ippsExtGet_BN() and ippsSet_BN(). ***There is no data copy in this case***"
Is that expert's mistake?
Regards
Andrzej

0 Kudos
Ricardo_Costa
Beginner
456 Views
Quoting - achrzesz2
Hello Ricardo
Thank you for replay
Now I'm slightly confused since your opinion is not in accordance with that of Intel's expert:
"3) use the pair ippsExtGet_BN() and ippsSet_BN(). ***There is no data copy in this case***"
Is that expert's mistake?
Regards
Andrzej


I think so, since there's no way ippsSet_BN canskip data copying without checking ifthe data in the pointer passed as parameteris the same as data in the BN structure (m_pBN).In my understanding, that would only be possible if itallowed the data pointerto be passed asNULL, which is currently not the case.
0 Kudos
Ying_H_Intel
Employee
457 Views
Quoting - Ricardo Costa

I think so, since there's no way ippsSet_BN canskip data copying without checking ifthe data in the pointer passed as parameteris the same as data in the BN structure (m_pBN).In my understanding, that would only be possible if itallowed the data pointerto be passed asNULL, which is currently not the case.

Hi Ricardo, Andrzej

Your doubts are right. I check again with our engineers. The ippsSet_BNcan skip data copying only if the data pointer passed as parameter is same as data dresses in BN structure.But the feature is implemented in next release (may be IPP 7.0), no in current version IPP 6.1 update 2.So yes, there is no way to skip data copying at current version.

And if unary - operation, I recommend to use thepairippsRef_BN() and ippsSet_BN().
Thecodemay be like
// change sign
IppsBigNumSGN sgn;
int bitSize;
Ipp32u* pData;
ippsRef_BN(&sgn, &bitSize, &pData, pBN);
sgn = IppsBigNumPOS==sgn? IppsBigNumNEG : IppsBigNumPOS;
ippsSet_BN(sgn,BITS_TO_DWORD(bitSize), pData, pBN);
//(will no data copy in future version if external and internal data have the same addresses)

Regards,
Ying
0 Kudos
Ricardo_Costa
Beginner
456 Views
Quoting - Ying H (Intel)

Hi Ricardo, Andrzej

Your doubts are right. I check again with our engineers. The ippsSet_BNcan skip data copying only if the data pointer passed as parameter is same as data dresses in BN structure.But the feature is implemented in next release (may be IPP 7.0), no in current version IPP 6.1 update 2.So yes, there is no way to skip data copying at current version.

And if unary - operation, I recommend to use thepairippsRef_BN() and ippsSet_BN().
Thecodemay be like
// change sign
IppsBigNumSGN sgn;
int bitSize;
Ipp32u* pData;
ippsRef_BN(&sgn, &bitSize, &pData, pBN);
sgn = IppsBigNumPOS==sgn? IppsBigNumNEG : IppsBigNumPOS;
ippsSet_BN(sgn,BITS_TO_DWORD(bitSize), pData, pBN);
//(will no data copy in future version if external and internal data have the same addresses)

Regards,
Ying

Hi Ying,

Thank you for the information. Glad to see this is resolved for the next release.I'll adopt this solution in the meantime.

Regards,
Ricardo
0 Kudos
Chao_Y_Intel
Moderator
456 Views


Hi Ricardo,

The fix was included in the IPP 7.0 beta. Check the post here on the Beta accouchement: http://software.intel.com/en-us/forums/showthread.php?t=75279

Thank you for your report.

Regards,
Chao


0 Kudos
Reply