What are some basic modulo arithmetic rules? - modulo

Let a = 10^18 and b = 10 ^ 18. c = 1 to 100000
I want to find ( a + b ) % c or ( a * b ) % c
I need to prevent integer overflow. How can I do so?

(x * y) % k = ((x % k) * (y % k)) % k
(x + y) % k = ((x % k) + (y % k)) % k
(x - y) % k = ((((x % k + k) % k) - ((y % k + k) % k)) % k + k) % k
I need compute (9173501*9173502*9173504)%9173503 in C#;

Related

Maxima: eliminate variables from equations

Given N equations in K variables,
can Maxima produce N-J equations in K-J variables?
SOLVE and ELIMINATE seem unable, about to reach for pen and paper.
(%i1) elim ([a = x + y, b = y + z, c = z + x, a = b * c], [a, b, c]);
(%o1) elim([a = y + x, b = z + y, c = z + x, a = b c], [a, b, c])
(%i2) load (to_poly);
(%o2) ~/maxima-5.44.0/share/to_poly_solve/to_poly.lisp
(%i3) elim ([a = x + y, b = y + z, c = z + x, a = b * c], [a, b, c]);
2
(%o3) [[z + (y + x) z + (x - 1) y - x],
[b z - y + b x - x, z + x - c, y + x - a]]
(%i4) solve (first (%o3), x);
2
z + y z - y
(%o4) [x = - ------------]
z + y - 1

Substitute variable in Maxima

newbie Maxima question
I have a transfer function in Maxima
E1 : y = K_i*s/(s^2 + w^2);
I'd like to have the closed-form of the equation affter applying the bilinear transform
E2 : s = (2/Ts*(z-1)/(z+1));
I would like to get the transfer function for z, by substituing s by equation E2. How should I proceed?
Regards
Note that subst can apply one or more substitutions stated as equations. In this case, try subst(E2, E1).
That will probably create a messy result -- you can simplify it somewhat by applying ratsimp to the result.
Here's what I get from that.
(%i2) E1 : y = K_i*s/(s^2 + w^2);
K_i s
(%o2) y = -------
2 2
w + s
(%i3) E2 : s = (2/Ts*(z-1)/(z+1));
2 (z - 1)
(%o3) s = ----------
Ts (z + 1)
(%i4) subst (E2, E1);
2 K_i (z - 1)
(%o4) y = ------------------------------
2
4 (z - 1) 2
Ts (z + 1) (------------ + w )
2 2
Ts (z + 1)
(%i5) ratsimp (%);
2
2 K_i Ts z - 2 K_i Ts
(%o5) y = -----------------------------------------------
2 2 2 2 2 2 2
(Ts w + 4) z + (2 Ts w - 8) z + Ts w + 4

Maxima CAS - substitution

I am trying to simplify a differential equation via substitution in maxima. However, the substitution does not seem to be working.
Here's my code:
depends (\rho,[t, r, \theta, z]); depends (V, [t, r, \theta, z]);
f_contin : diff (\rho, t) + diff (\rho*r*V[r], r)*(1/r) = 0;
base : diff (V[b]*r*\rho, r) = 0;
V_sub : V[r] = V[b] + \epsilon*V[r];
subst (V_sub, f_contin);
subst (base, %o6);
The last substitution did not work. What am I doing wrong here?
For clarity I add a Screenshot here:
The problem is that subst(a=b, c) (or equivalently subst(b, a, c)) can only make substitutions when a is an exact subexpression of c.
ratsubst (which see) can handle some cases when a is not an exact subexepression but in this case it doesn't seem to work.
But I think you can get the result you want by just subtracting the one equation from the other. Note that (a=b) - (c=d) yields a - c = b - d. Note also that I've put in another step (in %i7) to apply the diff operator. Also I've multiplied %o7 by r to get something like base.
(%i1) depends (\rho,[t, r, \theta, z]); depends (V, [t, r, \theta, z]);
(%o1) [rho(t, r, theta, z)]
(%o2) [V(t, r, theta, z)]
(%i3) f_contin : diff (\rho, t) + diff (\rho*r*V[r], r)*(1/r) = 0;
drho d
r V ---- + r (-- (V )) rho + V rho
drho r dr dr r r
(%o3) ---- + ------------------------------------ = 0
dt r
(%i4) base : diff (V[b]*r*\rho, r) = 0;
drho d
(%o4) V r ---- + (-- (V )) r rho + V rho = 0
b dr dr b b
(%i5) V_sub : V[r] = V[b] + \epsilon*V[r];
(%o5) V = epsilon V + V
r r b
(%i6) subst (V_sub, f_contin);
drho drho d
(%o6) ---- + (r (epsilon V + V ) ---- + r (-- (epsilon V + V )) rho
dt r b dr dr r b
+ (epsilon V + V ) rho)/r = 0
r b
(%i7) %o6, nouns;
drho drho d d
(%o7) ---- + (r (epsilon V + V ) ---- + r (epsilon (-- (V )) + -- (V )) rho
dt r b dr dr r dr b
+ (epsilon V + V ) rho)/r = 0
r b
(%i8) expand (r*%o7 - base);
drho drho d
(%o8) r ---- + epsilon r V ---- + epsilon r (-- (V )) rho + epsilon V rho = 0
dt r dr dr r r
The function subst (a,b,c) substitutes a for b in c. It uses 3 arguments, your first subst works because its interpreted as subst (V[b] + \epsilon*V[r],V[r], f_contin);
Your second subst is probably interpreted as subst (0,diff (V[b]*r*\rho, r),%) therefor nothing is substituted. What do you want to substitute for what?

Divide a Point in Elliptic Curve Cryptography

I'm using Elliptic Curve to design a security system. P is a point on elliptic curve. The receiver must obtain P using formula k^-1(kP). The receiver does not know P but knows k. I need to compute k^-1(R) where R=kP. How can I do this using Point Multiplication or Point Addition.
I suggest first learning a bit more about ECC (for example, read some of Paar's book and listen to his course at http://www.crypto-textbook.com/) before tackling something this complex. For this particular question, ask yourself: "What does the inverse of k mean?"
Very interesting question you have! I was happy to implement from scratch Python solution for your task, see code at the bottom of my answer.
Each elliptic curve has an integer order q. If we have any point P on curve then it is well known that q * P = Zero, in other words multiplying any point by order q gives zero-point (infinity point).
Multiplying zero (infinity) point by any number gives zero again, i.e. j * Zero = Zero for any integer j. Adding any point P to zero-point gives P, i.e. Zero + P = P.
In our task we have some k such that R = k * P. We can very easily (very fast) compute Modular Inverse of k modulo order q, using for example Extended Euclidean Algorithm.
Inverse of k modulo q by definition is such that k * k^-1 = 1 (mod q), which by definition of modulus is equal k * k^-1 = j * q + 1 for some integer j.
Then k^-1 * R = k^-1 * k * P = (j * q + 1) * P = j * (q * P) + P = j * Zero + P = Zero + P = P. Thus multiplying R by k^-1 gives P, if k^-1 is inverse of k modulo q.
You can read about point addition and multiplication formulas on this Wiki.
Lets now check our formulas in Python programming language. I decided to implement from scratch simple class ECPoint, which implements all curve operations (addition and multiplication), see code below.
We take any ready-made curve, for example most popular 256-bit curve secp256k1, which is used in Bitcoin. Its parameters can be found here (this doc contains many other popular standard curves), also you can read about this specific curve on Bitcoin Wiki Page.
Following code is fully self-contained Python script, doesn't need any external dependencies and modules. You can run it straight away on any computer. ECPoint class implements all curve arithmetics. Function test() does following operations: we take standard secp256k1 params with some base point G, we compute any random point P = random * G, then we generate random k, compute R = k * P, compute modular inverse k^-1 (mod q) by using function modular_inverse() (which uses extended Euclidean algorithm egcd()), compute found_P = k^-1 * R and check that it is equal to P, i.e. check that k^-1 * R == P, print resulting k^-1 * R. All random values are 256-bit.
Try it online!
def egcd(a, b):
# https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
ro, r, so, s, to, t = a, b, 1, 0, 0, 1
while r != 0:
ro, (q, r) = r, divmod(ro, r)
so, s = s, so - q * s
to, t = t, to - q * t
return ro, so, to
def modular_inverse(a, mod):
# https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
g, s, t = egcd(a, mod)
assert g == 1, 'Value not invertible by modulus!'
return s % mod
class ECPoint:
#classmethod
def Int(cls, x):
return int(x)
#classmethod
def std_point(cls, name):
if name == 'secp256k1':
# https://en.bitcoin.it/wiki/Secp256k1
# https://www.secg.org/sec2-v2.pdf
p = 0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_FFFFFC2F
a = 0
b = 7
x = 0x79BE667E_F9DCBBAC_55A06295_CE870B07_029BFCDB_2DCE28D9_59F2815B_16F81798
y = 0x483ADA77_26A3C465_5DA4FBFC_0E1108A8_FD17B448_A6855419_9C47D08F_FB10D4B8
q = 0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_BAAEDCE6_AF48A03B_BFD25E8C_D0364141
else:
assert False
return ECPoint(x, y, a, b, p, q)
def __init__(self, x, y, A, B, N, q, *, prepare = True):
if prepare:
N = self.Int(N)
A, B, x, y, q = [self.Int(e) % N for e in [A, B, x, y, q]]
assert (4 * A ** 3 + 27 * B ** 2) % N != 0
assert (y ** 2 - x ** 3 - A * x - B) % N == 0, (
x, y, A, B, N, (y ** 2 - x ** 3 - A * x) % N)
assert N % 4 == 3
assert y == pow(x ** 3 + A * x + B, (N + 1) // 4, N)
self.A, self.B, self.N, self.x, self.y, self.q = A, B, N, x, y, q
def __add__(self, other):
A, N = self.A, self.N
Px, Py, Qx, Qy = self.x, self.y, other.x, other.y
if Px == Qx and Py == Qy:
s = ((Px * Px * 3 + A) * self.inv(Py * 2, N)) % N
else:
s = ((Py - Qy) * self.inv(Px - Qx, N)) % N
x = (s * s - Px - Qx) % N
y = (s * (Px - x) - Py) % N
return ECPoint(x, y, A, self.B, N, self.q, prepare = False)
def __rmul__(self, other):
other = self.Int(other - 1)
r = self
while True:
if other & 1:
r = r + self
if other == 1:
return r
other >>= 1
self = self + self
#classmethod
def inv(cls, a, n):
return modular_inverse(a, n)
def __repr__(self):
return str(dict(x = self.x, y = self.y, A = self.A,
B = self.B, N = self.N, q = self.q))
def __eq__(self, other):
for i, (a, b) in enumerate([
(self.x, other.x), (self.y, other.y), (self.A, other.A),
(self.B, other.B), (self.N, other.N), (self.q, other.q)]):
if a != b:
return False
return True
def test():
import random
bits = 256
P = random.randrange(1 << bits) * ECPoint.std_point('secp256k1')
k = random.randrange(1 << bits)
R = k * P
found_P = modular_inverse(k, R.q) * R
assert found_P == P
print(found_P)
if __name__ == '__main__':
test()
Output:
{
'x': 108051465657467150531748691374311160382608428790397210924352716318223953013557,
'y': 4462548165448905789984443302412298811224817997977472205419179335194291964455,
'A': 0,
'B': 7,
'N': 115792089237316195423570985008687907853269984665640564039457584007908834671663,
'q': 115792089237316195423570985008687907852837564279074904382605163141518161494337
}

Parse Error (x + 1) when Compiling in Haskell

I'm doing this assignment and I'm desperate to get this to work.
I know that this isn't the smartest way, and that it is not the most efficient way. I did this purely because I want to test how inefficient this code is.
transition_from_conductor :: Element_w_Coord Cell -> List_2D Cell -> Cell
transition_from_conductor element world = case num_of_heads_around_conductor (0, element) world of
1 -> Head
2 -> Head
_ -> Conductor
where
num_of_heads_around_conductor :: (Int, Element_w_Coord Cell) -> List_2D Cell -> Int
num_of_heads_around_conductor (i, (cell, (x, y))) ((w_cell, (w_x, w_y): rest)) = case rest of
[] -> i
_ -> case (w_cell, w_x, w_y) of
(Head, (x + 1), y) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x + 1), (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x + 1), (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x - 1), y) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x - 1), (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x - 1), (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, x, (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, x, (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
If I try to run this in terminal, it gives me parse error (x + 1) on the
(Head, (x + 1), y) .....
What did I do wrong? and how do I fix it?
A few things...
type List_2D e = [Element_w_Coord e]
type Element_w_Coord e = (e, Coord)
type Coord = (X_Coord, Y_Coord)
type X_Coord = Integer
type Y_Coord = Integer
Thanks guys :D
Two things wrong here:
you can't use an arithmetic expression in a pattern
when you try to pattern match against x and y you intend to constrain those parts of the pattern to be equal to the existing x and y variables, but you instead create new variables x and y
I would use guards.
_ -> case (w_cell, w_x, w_y) of
(Head, x', y')
| x' == x + 1 && y' == y -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x + 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x + 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
And then simplify:
_ -> case (w_cell, w_x, w_y) of
(Head, x', y')
| x' == x + 1 && (y' == y || y' == y + 1 || y' == y - 1)
|| x' == x - 1 && (y' == y || y' == y + 1 || y' == y - 1)
|| x' == x && (y' == y + 1 || y' == y - 1)
-> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
No doubt this can be simplified further.
What you have used is an "n + k" pattern, which has been removed from the language. You can no longer pattern match on integers using '+'. For the most part, pattern matches are restricted to constructors and literals.
To achieve the same result as your pattern match, I suggest:
(Head, x0, y) -> let x = x0 - 1 in ...
Notice that there is also more wrong with this code - the pattern matches are overlapped. For example: Even with n+k support, there is no case in which the pattern:
(Head, (x + 1), y)
Fails and the next pattern of:
(Head, (x + 1), (y + 1))
succeeds. In other words, you have many cases that can never be executed.

Resources