Explanation about RFC3284 - VCDIFF format - vcdiff

i'm new trying to read this type of documentation, and i'm confused in how the instruction of VCDIFF works, this is the original doc:
https://www.rfc-editor.org/rfc/rfc3284
This part:
ADD: This instruction has two arguments, a size x and a sequence
of x bytes to be copied.
COPY: This instruction has two arguments, a size x and an address
p in the string U. The arguments specify the substring of U
that must be copied. We shall assert that such a substring
must be entirely contained in either S or T.
RUN: This instruction has two arguments, a size x and a byte b,
that will be repeated x times.
Now the doc put an example:
a b c d e f g h i j k l m n o p
a b c d w x y z e f g h e f g h e f g h e f g h z z z z
COPY 4, 0
ADD 4, w x y z
COPY 4, 4
COPY 12, 24
RUN 4, z
I don't understand what every op did, i think the first copy is the first "a b c d", the add now includes "w x y z", now i don't understand well how the next two copies works.
If i think would be useful if someone can show what do that instructions, like "this instruction have this string as result and the next this", just to can compare step by step :D
Thx.

It looks like at the point you are executing this you will know the length of the output. In this "language" the input and output are consecutive in "memory". So you start with:
abcdefghijklmnop----------------------------
|<- S ->||<- T ->|
First COPY 4 bytes starting at offset 0 in the combined string:
ABCDefghijklmnopABCD------------------------
|<- S ->||<- T ->|
Then ADD 4 bytes, literally w x y z:
abcdefghijklmnopabcdWXYZ--------------------
|<- S ->||<- T ->|
Then COPY 4 bytes starting at offset 4:
abcdEFGHijklmnopabcdwxyzEFGH----------------
|<- S ->||<- T ->|
Then COPY 12 bytes starting at offset 24. This is a little tricky, because offset 24 is the "efgh" we just wrote and we haven't written the last 8 bytes yet, but if you do it one byte at a time the overlap doesn't matter:
|<- from ->|
|<- to ->|
abcdEFGHijklmnopabcdwxyzefghEFGHEFGHEFGH----
|<- S ->||<- T ->|
Finally there is a RUN of 4 consecutive bytes all "z":
abcdEFGHijklmnopabcdwxyzefghefghefghefghZZZZ
|<- S ->||<- T ->|

Related

intel 8080: Am I missing something in this code?

Here is a code for the fibonnaci sequence taken from rosettacode.com
FIBNCI: MOV C, A ; C will store the counter
DCR C ; decrement, because we know f(1) already
MVI A, 1
MVI B, 0
LOOP: MOV D, A
ADD B ; A := A + B
MOV B, D
DCR C
JNZ LOOP ; jump if not zero
RET ; return from subroutine
If the value taken from A is originally 0 and we decrement C, does C become -1? if so what happens to that value at the 2nd DCR; and what does the the JNZ instruction see or do?
This is my first contact with assembly language so its a bit confusing at the moment. I'm thinking that if C is already -1 and counting when we reach the JNZ instruction, wouldn't this code be stuck in a loop? Or is the JNZ looking elsewhere?
Intel 8080 registers A, B, C, ... are 8 bit.
So if A was 0, then C becomes -1, which is encoded in 8 bits as 0b11111111 (all eight bits set to 1). When you treat that as unsigned 8 bit value, it's equal to 255.
Now if you would increment that value by 1, it would become 256, which in binary is 0b100000000 -> as C is 8 bit wide, that value would be truncated to 0b00000000, which is 0. So -1 + 1 = 0, as expected (and 255 + 1 = 0 in unsigned Math, because you hit the 8 bit limit, so the value "overflows").
The second DCR will decrease that -1/255 value, the C will then contain -2 (which equals to 254 unsigned, as 255 - 1 = 254, and in binary looks like 0b11111110).
JNZ will loop till zero, so that means the loop will run 255 times (for A=0 argument, for A=1 it will run 256 times), until the C does reach again zero from the 255 (meanwhile the A and B registers containing F(n-2) and F(n-1) will overflow many times, thus rendering the result unusable ... the last correct result is for A=13 being 233 I think (too lazy to verify))
For the start check for input < 2:
FIBNCI: CPI 2 ;return if A < 2
RC ;F(0) = 0, F(1) = 1
The rest of the code seems to be OK. F(2) = 1, F(3) = 2, F(4) = 3, ...
You could modify the code to use double add (DAD) to get a 16 bit result. The largest input for 8 bit result is decimal 13: fib(13) = 233. The largest input for 16 bit result is 24: F(24) = 46368.

Elapsed time of unsigned wrapping timer

Consider I have a timer that returns a uint32_t value (representing a number of ticks), always counts upwards, and wraps to 0 after reaching UINT32_MAX.
Suppose I need to take an elapsed time from time a to time b, and I don't know how high the timer might be initially and whether it will wrap between a and b. Both a and b are type uint32_t and get assigned to the timer's return value.
Is it a correct statement that we can take (uint32_t)(b-a) to get the elapsed time so long as no more than UINT32_MAX ticks have elapsed — and will it be correct even if the timer wrapped once? What is the proof for this?
Let N = 232. Let A and B be the timestamps of the start and end before wrapping to the [0, N) range, and assume A ≤ B < A + N. Then a = A % N and b = B % N. We are interested in computing the duration D = B - A.
When a ≤ b, it is trivial that D = B - A = b - a.
What about when a > b? Then a ≤ b + N and it must be that D = B - A = b + N - a.
But b - a is of course congruent b + N - a modulo N. Since addition and subtraction between std::uint32_t is all modulo N, you can safely compute your answer as D = b - a. The subtraction operator between two std::uint32_t values is already a std::uint32_t, so there's no reason to specify a cast as in (std::uint32_t)(b - a).

2-D FFT using MPI_Alltoall in MPI

I have a 4X6 matrix split as two 2X6 matrices on 2 processors. Since the rows have been split in a contiguous way (C Language) on the processors, we can carry out a 1-D FFT on the rows. The problem is we need an MPI_Alltoall() to collect contiguous columns on a particular processor for 1-D column FFT i.e. Processor 0 has:
A B C D E F
G H I J K L
Processor 1 has:
M N O P Q R
S T U V W X
The MPI_Alltoall() needs to convert this to the following on Processor 0:
A G M S
B H N T
C I O U
And the following on Processor 2:
D J P V
E K Q W
F L R X
I tried to define a vector having count=2, blocklength=1, stride=6 as the sending type vector, set its extent to MPI_INT, displacement to 0 and sent 3 instances of this vector using MPI_Alltoall(). This according to me should send A G, B H, and C I to processor 0 and D J, E K, and F L to processor 1. Similarly M S, N T, and O U to processor 0 and P V, Q W, and R X to processor 1.
Is my interpretation correct ? If yes, then how do I receive these 3 instances of vectors on the receiving side ( What/How do I define the datatype?).

How to find d, given p, q, and e in RSA?

I know I need to use the extended euclidean algorithm, but I'm not sure exactly what calculations I need to do. I have huge numbers. Thanks
Well, d is chosen such that d * e == 1 modulo (p-1)(q-1), so you could use the Euclidean algorithm for that (finding the modular multiplicative inverse).
If you are not interested in understanding the algorithm, you can just call BigInteger#modInverse directly.
d = e.modInverse(p_1.multiply(q_1))
Given that, p=11, q=7, e =17, n=77, φ (n) = 60 and d=?
First substitute values from the formula:-
ed mod φ (n) =1
17 d mod 60 = 1
The next step: – take the totient of n, which is 60 to your left hand side and [e] to your right hand side.
60 = 17
3rd step: – ask how many times 17 goes to 60. That is 3.5….. Ignore the remainder and take 3.
60 = 3(17)
Step 4: – now you need to balance this equation 60 = 3(17) such that left hand side equals to right hand side. How?
60 = 3(17) + 9 <== if you multiply 3 by 17 you get 51 then plus 9, that is 60. Which means both sides are now equal.
Step 5: – Now take 17 to your left hand side and 9 to your right hand side.
17 = 9
Step 6:- ask how many times 9 goes to 17. That is 1.8…….
17 = 1(9)
Step 7:- Step 4: – now you need to balance this 17 = 1(9)
17 = 1(9) + 8 <== if you multiply 1 by 9 you get 9 then plus 8, that is 17. Which means both sides are now equal.
Step 8:- again take 9 to your left hand side and 8 to your right hand side.
9 = 1(8)
9 = 1(8) + 1 <== once you reached +1 to balance your equation, you may stop and start doing back substitution.
Step A:-Last equation in step 8 which is 9 = 1(8) + 1 can be written as follows:
1.= 9 – 1(8)
Step B:-We know what is (8) by simple saying 8 = 17 – 1(9) from step 7. Now we can re-write step A as:-
1=9 -1(17 – 1(9)) <== here since 9=1(9) we can re-write as:-
1=1(9)-1(17) +1(9) <== group similar terms. In this case you add 1(9) with 1(9) – that is 2(9).
1=2(9)-1(17)
Step C: – We know what is (9) by simple saying 9 = 60 – 3(17) from step 4. Now we can re-write step B as:-
1=2(60-3(17) -1(17)
1=2(60)-6(17) -1(17) <== group similar terms. In this case you add 6(17) with 1(17) – that is 7(17).
1=2(60)-7(17) <== at this stage we can stop, nothing more to substitute, therefore take the value next 17. That is 7. Subtract it with the totient.
60-7=d
Then therefore the value of d= 53.
I just want to augment the Sidudozo's answer and clarify some important points.
First of all, what should we pass to Extended Euclidean Algorthim to compute d ?
Remember that ed mod φ(n) = 1 and cgd(e, φ(n)) = 1.
Knowing that the Extended Euclidean Algorthim is based on the formula cgd(a,b) = as + bt, hence cgd(e, φ(n)) = es + φ(n)t = 1, where d should be equal to s + φ(n) in order to satisfy the
ed mod φ(n) = 1 condition.
So, given the e=17 and φ(n)=60 (borrowed from the Sidudozo's answer), we substitute the corresponding values in the formula mentioned above:
cgd(e, φ(n)) = es + φ(n)t = 1 ⇔ 17s + 60t = 1.
At the end of the Sidudozo's answer we obtain s = -7. Thus d = s + φ(n) ⇔ d = -7 + 60 ⇒ d = 53.
Let's verify the results. The condition was ed mod φ(n) = 1.
Look 17 * 53 mod 60 = 1. Correct!
The approved answer by Thilo is incorrect as it uses Euler's totient function instead of Carmichael's totient function to find d. While the original method of RSA key generation uses Euler's function, d is typically derived using Carmichael's function instead for reasons I won't get into. The math needed to find the private exponent d given p q and e without any fancy notation would be as follows:
d = e^-1*mod(((p-1)/GCD(p-1,q-1))(q-1))
Why is this? Because d is defined in the relationship
de = 1*mod(λ(n))
Where λ(n) is Carmichael's function which is
λ(n)=lcm(p-1,q-1)
Which can be expanded to
λ(n)=((p-1)/GCD(p-1,q-1))(q-1)
So inserting this into the original expression that defines d we get
de = 1*mod(((p-1)/GCD(p-1,q-1))(q-1))
And just rearrange that to the final formula
d = e^-1*mod(((p-1)/GCD(p-1,q-1))(q-1))
More related information can be found here.
Here's the code for it, in python:
def inverse(a, n):
t, newt = 0, 1
r, newr = n, a
while newr:
quotient = r // newr # floor division
t, newt = newt, t - quotient * newt
r, newr = newr, r - quotient * newr
if r > 1:
return None # there's no solution
if t < 0:
t = t + n
return t
inverse(17, 60) # returns 53
adapted from pseudocode found in wiki: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Pseudocode
Simply use this formula,
d = (1+K(phi))/e. (Very useful when e and phi are small numbers)
Lets say, e = 3 and phi = 40
we assume K = 0, 1, 2... until your d value is not a decimal
assume K = 0, then
d = (1+0(40))/3 = 0. (if it is a decimal increase the K value, don't bother finding the exact value of the decimal)
assume K = 2, then
d = (1+2(40)/3) = 81/3 = 27
d = 27.
Assuming K will become exponentially easy with practice.
Taken the values p=7, q=11 and e=17.
then the value of n=p*q=77 and f(n)=(p-1)(q-1)=60.
Therefore, our public key pair is,(e,n)=(7,77)
Now for calvulating the value of d we have the constraint,
e*d == 1 mod (f(n)), [here "==" represents the **congruent symbol**].
17*d == 1 mod 60
(17*53)*d == 53 mod 60, [7*53=901, which gives modulus value 1]
1*d == 53 mod 60
hence,this gives the value of d=53.
Therefore our private key pair will be, (d,n)=(53,77).
Hope this help. Thank you!

Little help with null space of a matrix

This requires a little knowledge about Matlab and I have none. I was just wondering if someone could point me in the right direction and give me some pointer :)
I have to write a matlab code for finding the Null spaces of matices
A and B, where B = A^T x A. And then nd the general solutions to AX = b1
and BX = b2, where b1= the column [1 2 3 4 5] and b2= the column [ 1 2 3 4 5 6 7 8].
My concern is that I dont really know how to go about this code.
This is what I have so far and I do not think i am in the right track. I have a specific matrix as below.
The rows are divided by semi-colon.
A = [ 1 2 3 4 5 6 7 8;
1 2^2 3^2 4^2 5^2 6^2 7^2 8^2;
1 2^3 3^3 4^3 5^3 6^3 7^3 8^3;
1 2^4 3^4 4^4 5^4 6^4 7^4 8^4;
6 8 1 1 7 9 0 7 ]
B = A’A (this is how transpose is written)
C = null(A)
D = null(B)
I feel like there should be a rref somewhere - I'm just not getting anywhere. Please point me in the right direction.
Ok so I updated it to the this now....My username changed from jona and I dont know why
A = [ 1 2 3 4 5 6 7 8;
1 2^2 3^2 4^2 5^2 6^2 7^2 8^2;
1 2^3 3^3 4^3 5^3 6^3 7^3 8^3;
1 2^4 3^4 4^4 5^4 6^4 7^4 8^4;
6 8 1 1 7 9 0 7 ]
B = A’*A (this is how transpose is written)
null(A)
null(B)
b1=[1; 2; 3; 4; 5 ];
b2=[1; 2; 3; 4; 5; 6; 7; 8 ];
end
rref(A,b1)
rref(B,b2)
end
However I still don't feel this is right :(
#Chris A. I know the null space is the solution to Ax=0. However I'm confused on how to use it to find general solution using the b1 and b2 I have. Is it possible for you to explain to me the connection? I don't undertand the book as much.
In MATLAB, arithmetic operations need to be explicit, i.e. a(b+c) should be written as a*(b+c)
Have you tried writing B as
B=A'*A;
Also, you seem to be using a different character for the transpose... You're using ’, the unicode character for single right quotation when you should be using ' or the unicode character for apostrophe.
Ok, so the bottom line is that the null space is the set of all vectors x such that A * x = 0. You got that right. And C is an orthonormal basis for the vectors in the null space. So that means if you have a particular solution (let's call it v) such that A * v = b1 then the space of solutions is the vector v plus any combination of vectors in the null space.
For the case of A, the size (second dimension) of your C will tell you the dimension of the null space. Each one of the vectors in C will be a vector in the null space.
To get v you can do v = A \ b1. You can write arbitrary combinations of vectors in C by C * c where little c is a column vector that is the size of the null space.
The general solution is thus v + C * c where c is any vector that is the dimension of the null space. To see that this solve the system, just plug it back in
A * (v + C * c) =
A * v + A * C * c =
b1 + 0 * c =
b1
Edit: It's the exact same idea for finding the solution to A'*A * x = b2, just anywhere you see A in the above discussion, replace it with A'*A and anywhere you see b1 replace it with b2. The solutions to A * x = b1 and A'*A * x = b2 are separate problems.

Resources