2

I am very new to this, but I am trying to add two Integers in modular format using Crypto++ Library.

My program is very simple,

AutoSeededRandomPool prng;
Integer r0, m;

m = Integer( prng, 64);
r0 = Integer( prng, 64);

cout << "m: " << std::hex << m << endl;
cout << "r0:" << std::hex << r0 << endl;

Integer n1(r0 + m);

But this simply didn't work. It complied fine, but it crashed when I was trying to run it.

Could anyone give a sample code for addition/subtraction using Crypto++ please

jww
  • 97,681
  • 90
  • 411
  • 885
Mona
  • 69
  • 1
  • 5
  • *"Could anyone give a sample code for addition/subtraction using Crypto++ ..."* - have you looked at [Integer](http://www.cryptopp.com/wiki/Integer) on the Crypto++ wiki? The wiki has lots of examples and sample code. – jww Apr 04 '16 at 14:32
  • I just looked at it and it only gives examples of the Integer initialization, but not the addition/subtraction of two Integers – Mona Apr 04 '16 at 15:47
  • I've been working on this for the last 24 hours, and as I mentioned an addition like r0+m did not work for me. So that is why I was looking for an example for using the Add function in ModularArithmetic in modarith.h – Mona Apr 04 '16 at 16:19
  • This is what I did,CryptoPP::Integer r0, m, sum; m= CryptoPP::Integer( prng, 64); r0= CryptoPP::Integer( prng, 64); sum = r0 +m; It complied, but crashed when it ran – Mona Apr 04 '16 at 16:23
  • wait, the page you referenced is being updated.... I am trying something else now – Mona Apr 04 '16 at 16:26
  • *"... the page you referenced is being updated..."* - yes, I'm updating it as we we speak. *"...It complied, but crashed when it ran...'* - that's a different problem, and it lacks detail, so I'm guessing you stated it for completeness. I'm addressing the question *"Could anyone give a sample code for addition/subtraction using Crypto++"*. – jww Apr 04 '16 at 16:33
  • Yes, Thank you very much for your help.... I just tried a new program similar to what you have in the page, and it works fine. There must have been something else wrong in my old program. – Mona Apr 04 '16 at 16:40
  • Thank you also for updating the page. I spent all day yesterday searching for a simple examples on addition and I couldn't find it. I found RSA, DH...etc, but nothing simple. Thanks again – Mona Apr 04 '16 at 16:41
  • Thanks again, I must go to my lab now, but how do I close this question? – Mona Apr 04 '16 at 16:45
  • *"How do I close this question..."* - Lower left-hand corner of the question, then select "Close → Off-Topic → No longer reproducible". But I recommend you leave it open because of the unexpected results from [`ModularArithmetic` class](http://www.cryptopp.com/docs/ref/class_modular_arithmetic.html). – jww Apr 04 '16 at 22:13

1 Answers1

1

Modular Arithmetic (addition/subtraction) using Crypto++

We have closed some of the missing documentation gaps based on this question, so I won't address the sample code. The improved docs are available at Integer Class Reference and Integer on the Crypto++ wiki.

However, there may be a bug or (at least) unexpected results from using the ModularArithmetic class. The class describes itself as "Ring of congruence classes modulo n". Mathematically, a Ring is a group with closure and two well defined operations.

The disconnect is, which two operations are the ones included with ModularArithmetic<Integer>. Based on some sample code, it looks like its Multiply and Exponentiate, which is mostly expected (though it could have been Add and Multiply).

I don't think the mathematical definition of Ring gives ModularArithmetic a license to produce unexpected results. However, ModularArithmetic is kind of unique, and it may be accumulating intermediate results that one must then reduce using Multiply and Exponentiate. (It does accumulate results to speed up operations).

The open question for me is, what do we do... I'm trying to solicit some feedback at the moment on the issue.


Here's the test program:

int main(int argc, char* argv[])
{
  Integer m("4294967295"), n("0x1000000000000000000000000000000"), j;
  j = 1999;

  ModularArithmetic ma(j);

  cout << "n+m mod j: " << ma.Add(n, m) << endl;
  cout << "  cross-check: " << (n+m) % j << endl;
  cout << "n-m mod j: " << ma.Subtract(n, m) << endl;
  cout << "  cross-check: " << (n-m) % j << endl;
  cout << "n*m mod j: " << ma.Multiply(n, m) << endl;
  cout << "  cross-check: " << (n*m) % j << endl;
  cout << "n/m mod j: " << ma.Divide(n, m) << endl;
  cout << "  cross-check: " << (n/m) % j << endl;
  cout << "n%m mod j: " << ma.Reduce(n, m) << endl;
  cout << "  cross-check: " << (n%m) % j << endl;
  cout << "n^m mod j: " << ma.Exponentiate(n, m) << endl;
  cout << "  cross-check: " << a_exp_b_mod_c(n,m,j) << endl;

  return 0;
}

Here are the results:

$ ./test.exe 
n+m mod j: 1329227995784915872903807064575309872.
  cross-check: 1755.
n-m mod j: 1329227995784915872903807055985377281.
  cross-check: 50.
n*m mod j: 266.
  cross-check: 266.
n/m mod j: 599.
  cross-check: 1997.
n%m mod j: 1329227995784915872903807055985377281.
  cross-check: 1608.
n^m mod j: 1326.
  cross-check: 1326.

EDIT 1

The disconnect is, which two operations are the ones included with ModularArithmetic<Integer>...

So I had a chance to go though the source code and add more missing documentation. Of particular interest is AbstractRing< T > Class Template Reference, which ModularArithmetic inherits from. It confirms that multiply and exponentiation are the operations (and it gives rise to helpers, like Square).

What I am not clear about is why ModularArithmetic is providing Add, Subtract and friends but arriving at unexpected results. It could well be that its effectively accumulating the results and waiting to be reduced with a Multiply or Exponentiate, but I don't see any comments in the source code.


EDIT 2

The reason ModularArithmetic appears to produce incorrect results for Add, Subtract and friends is the class is meant to be fast for specific problems, and it does not perform a full reduction using the Euclidean extended algorithm. Rather, it performs at most one subtraction. That means the accumulated value n to be reduced by the modulus p must be in the range [0, 2p).

jww
  • 97,681
  • 90
  • 411
  • 885