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
1
vote
1 answer

Why X mod 2pow(256) should return a fixed-size output of 256 bits?

This is written on page 26 of "Princeton Bitcoin book". I think that I understand why 2pow(256) return 256 bit - length value,but why a remainder of the division of any number to 2pow(256) returns value of fixed length of 256 bits?
1
vote
2 answers

Javacard big numbers and modular arithmetic

I am new in the Javacard ecosystem and I was wondering what's the consensus regarding (modular) computations with big numbers in Javacard. More specifically, I am looking for a lib which supports modular exponentiation and in general modular…
1
vote
1 answer

Extended Euclidean algorithm JAVA RSA

I'm trying to implement the EEA. I found this pattern which I use also. extended_euclid(a,b) 1 if b = 0 2 than return (a,1,0) 3 (d',s',t') <-- extended_euclid(b, a mod b) 4 (d,s,t) <--- (d',t',s' - (a div b)t') 5 return (d,s,t) And my code…
Paul
  • 571
  • 3
  • 17
1
vote
0 answers

Finding nCr%m in C++ efficiently for very large N

I have to find the value nCr%m where 0 < r < 10^6 and 0 < n < 10^18 and m is a prime (for sake of convenience 10^9+7) I have read various articles where we can find nCr%m in O(1) time if n<10^6 by pre-calculating the factorial and multiplicative…
nil96
  • 313
  • 1
  • 3
  • 12
1
vote
2 answers

Getting different results for the same equation in a function and the shell

I've implemented Pollard's Rho for logarithms using Sage, as the following program stored in pollardrho.py. def pollardrho(g, h, G): k, m = 1, 0 t = g**k * h**m i, j = 1, 0 r = g**i * h**j def step(t, k, m): if lift(t) %…
Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
1
vote
1 answer

how - 5%3 is equal to - 2?

I'm learning C basics right now. I have a question which is confusing me little bit. My question is how the below program's output is - 2 ? #include int main() { printf("%d", -5%3); return 0 ; }
user3788135
1
vote
2 answers

Calculate and Store Power of very large Number

I am finding pow(2,i) where i can range: 0<=i<=100000. Apart i have MOD=1000000007 powers[100000]; powers[0]=1; for (i = 1; i <=100000; ++i) { powers[i]=(powers[i-1]*2)%MOD; } for i=100000 won't power value become greater than MOD ? How do I…
Kabhi
  • 135
  • 1
  • 12
1
vote
1 answer

How to find m in c = m^e (mod n) if c, e, n are known

Suppose I have known java BigIntegers c, e, and n, is there a way to quickly calculate the BigInteger m, where: c = m^e (mod n)
1
vote
2 answers

Repeating Cycle of Modulus

Is there a efficient method to find the value of 1111..nmod M? One can always use repeated squaring to find 100mod M + 101mod M + 102mod M + 103mod M + ...10nmod M Is there any faster method than this?
1
vote
0 answers

Arithmetic with Large Integers using Chinese Remainder Theorem

This is done by Python Suppose we represent the sum of a collection of exponentiation operations as a list of tuples, where each tuple contains two integers: the base and the exponent. For example, the list [(2,4),(3,5),(-6,3)] represents the sum of…
1
vote
1 answer

Using modualr arithmetic for vector indices in matlab

I have a vector n values and i want to split it into groups n groups of 3 adjacent values if it is considered to have a ring topology. what i am trying to do is this: vector = [some values]; groups = {}; for i = 1:size(vector)(2) groups{i} =…
guskenny83
  • 1,321
  • 14
  • 27
1
vote
0 answers

Performing modular arithmetic in Java to implement Diffie-Hellman

I have a question, I am trying to implement the encryption protocol between server and client based on Diffie-Hellman problem. The problem is when I tried to ((t^RsRc) mod p)^(1/Rc mod q) it is not giving me (t^Rs) mod p. I have checked even if I…
1
vote
1 answer

Can Montgomery multiplication be used to speed up the computation of (large number)! % (some prime)

This question originates in a comment I almost wrote below this question, where Zack is computing the factorial of a large number modulo a large number (that we will assume to be prime for the sake of this question). Zack is using the traditional…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
1
vote
0 answers

NTT w/ Montgomery Multiplication

For the last few days I've been trying to help Mr. Spektre who, due to compatibility issues, had to write his own Number Theoretic Transform for FFT Multiplication. Modular arithmetics and NTT (finite field DFT) optimizations He has one that works…
1
vote
2 answers

Adding hexadecimal strings in JavaScript efficiently

In JavaScript, I have two variables that contain a hexadecimal number as a string, each. E.g.: var a = 'a3bc', b = '1d0f'; Now I want to add them (so, the result should be 'c0cb'). To make things a little bit easier, let's put some constraints…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425