Questions tagged [modular-arithmetic]

Modular arithmetic is quite a useful tool in number theory. In particular, it can be used to obtain information about the solutions (or lack thereof) of a specific equation.

In mathematics, modular arithmetic (sometimes called clock arithmetic) is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801.

Time-keeping on this clock uses arithmetic modulo 12. A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 7:00 now, then 8 hours later it will be 3:00. Usual addition would suggest that the later time should be 7 + 8 = 15, but this is not the answer because clock time "wraps around" every 12 hours; in 12-hour time, there is no "15 o'clock". Likewise, if the clock starts at 12:00 (noon) and 21 hours elapse, then the time will be 9:00 the next day, rather than 33:00. Since the hour number starts over after it reaches 12, this is arithmetic modulo 12. 12 is congruent not only to 12 itself, but also to 0, so the time called "12:00" could also be called "0:00", since 0 ≡ 12 mod 12.

324 questions
2
votes
1 answer

Understanding Mod Operator in Math vs Programming

From what I understand, in mathematics, the mod operator is the result of the remainder of Euclidean division. Where 0 ≤ r < |b|, meaning that the result will always be positive. In programming however, there are operators in many languages which…
csguy
  • 1,354
  • 2
  • 17
  • 37
2
votes
1 answer

Is there a better way to do modulo in a finite field when directly working on polynomials rather than binary numbers?

So currently I am trying to implement finite fields using only polynomials. So like, I don't want to be operating on binary numbers using operations such as AND. Instead I want to make the whole thing with polynomials. I have got very far with this,…
Evelyn
  • 85
  • 8
2
votes
1 answer

Is there a function for integer fmod in C++?

http://en.cppreference.com/w/c/numeric/math/fmod Alright, cool. There's this function called fmod, but it appears to only work with float, double, and long double. Is there a version of this function that works with integers in C++?
Aaron Franke
  • 3,268
  • 4
  • 31
  • 51
2
votes
1 answer

Modulo computations in Sympy fail

I have a computation in Sympy, which I want to be modulo 2. However, if I just write from sympy.abc import x (((x + 1) % 2) + 1) % 2 Then this has the output Mod(Mod(x + 1, 2) + 1, 2), i.e. the two +1 do not vanish. Even calling .simplify() does…
Hennich
  • 682
  • 3
  • 18
2
votes
1 answer

Primality test taking longer than brute force method, how can I improve?

I am trying to calculate prime numbers on a single machine, around the size of 2^30-2^100. My algorithm is included below for anyone interested. I've optimized this Python code to be O(sqrt(n/2)) (I believe) for each number: it only accepts odd…
CS2016
  • 331
  • 1
  • 3
  • 15
2
votes
1 answer

Unidirectional ElGamal Proxy Re-Encryption implementation

I've implemented an ElGamal scheme in JavaScript (the code is awful, just wanted to test it quick) based on this explanation. var forge = require('node-forge'); var bigInt = require("big-integer"); var bits =…
mcansado
  • 2,026
  • 4
  • 25
  • 39
2
votes
2 answers

extended algorithm implementation in matlab

i have found following pseudo-code for extended euclidean algorithm i implemented following algorithm function [x1,y1,d1]=extend_eucledian(a,b) if b==0 x1=1; y1=0; d1=a; …
user466534
2
votes
2 answers

Fea program cryptology

I was just wondering if anyone can give me a hand with the code for a simple program. I've written the program but i seem to be strugling trying to calculate values with large values of e and n. But now when i try to calculate the following fea(2,…
Gibberish
  • 147
  • 9
2
votes
0 answers

basic example of elgamal algorithm in python 2.7

I'm trying to implement a basic example of El Gamal in python 2.7. There's some bug in the decryption which I'm unable to solve. It's the decryption step should be: e^-d * c mod p Any help appreciated. from __future__ import division from random…
Martin
  • 91
  • 1
  • 11
2
votes
1 answer

Modular Arithmetic addition and subtraction using Crypto++

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 <<…
Mona
  • 69
  • 1
  • 5
2
votes
1 answer

Find number coprime to modulus

I've been doing some research on the RSA encryption system, it's pretty simple to code one using small primes, as there aren't too many possibilities and performance is not a real issue. Here's how RSA Works: First, you generate a n value, product…
Juanco
  • 33
  • 1
  • 6
2
votes
1 answer

long mod opertaion returns an int Java

So I am using this modular exponentiation algorithm I saw on Wikipedia somewhere and it has been working fine for me for small numbers. But when I use larger numbers (E.g. 7000000000) it always returns a 0. public static void main(String[] args) { …
R Glover
  • 35
  • 4
2
votes
2 answers

Calculate (a^b)%c where 0<=a,b,c<=10^18

How can I calculate (a ^ b) % c, where 0 <= a, b, c <= 10^18. Here, (a ^ b) means a to the power b, not a xor b. My current code for the problem is: unsigned long long bigMod(unsigned long long b, unsigned long long p, …
Abu Hanifa
  • 2,857
  • 2
  • 22
  • 38
2
votes
2 answers

Modular arithmetic (a*b/e)%m?

Is there a modular expression possible for following : ((a*b)/e)%m For ex : (a*b*c)%m = ((a%m)*(b%m)*(c%m))%m
Akash Rana
  • 3,685
  • 3
  • 19
  • 28
2
votes
1 answer

What's the fastest running time for modular multiplication of large integers on multiple processors?

I am interested to learn about the time complexity for multiplying large integers modulo a (large) constant using multiple processors. What this boils down to is basically just integer multiplication, since division and remainder can also be…