Multiplying bytes with XOR [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So, before I get into my question. I tried search this but I am probably not wording it correctly to get any valid results. So the purpose is to use in the a AES 128-bit encryption program.
I need to multiply an unsigned char (which would be the hexadecimal value) by 2 or 3 and this would be an XOR operation. So basically, is there a way to do it without typing it out like this.
(SBOX[0] ^ SBOX[0]) ^ SBOX[0]
If I have to do it this way, each line is going to be fairly long but can be done I believe. It would be nice if there is an operator to just say 3 ^ SBOX[0].

If you're doing AES, then you're doing your arithmetic in a Galois Field (specifically GF(28)). Thus rules that you're used to for standard integers no longer hold.
In particular, whilst addition is XOR (in GF(2n)), multiplication isn't repeated addition. Your example shows why - multiplication by two would be x ^ x == 0 always.
The actual steps (in code) depend on the reducing polynomial of your Galois field (and in any case, deriving them is way beyond my ability nowadays). However, they're summarised in multiple places on the web. And in many case, these explanations specifically target the S-box MixColumns operation, e.g. Wikipedia.

Related

Suppose you have an array of N elements. You need to find for how many i, Ai + A(i+1) is a square number. Is this question trivial? If so how? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I just wanted to know if the above question is trivial or not. More importantly, how can you recognize if an algorithm is trivial?
It depends what do you mean by trivial. If you talk about complexity, it is O(n*M(N)) where M(N) is the complexity of the underlying multiplication algorithm with N maximum of the array's values and n is the length of the array.
If you talk about implementation, it is one loop with one check that the sum of the neighbors is a perfect square. If the elements fit into int, double etc. you have sqrt function in the standard library. If your elements are arbitrary length integers or float point numbers, you either need to use an appropriate library or implement the handling of these numbers on your own, which might be not trivial.
This understanding should help you to answer your last question

C++ Why convert something to NaN? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My understanding is that NaN (Not a Number) is essentaly a constant that is returned from a mathematical function to indicate something went wrong or the calculation is invalid. So it makes sense that their are functions to check if a number is NaN or better yet, use the CERT Coding Standard to do error checking for mathematical errors ( https://www.securecoding.cert.org/confluence/display/c/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions ).
My question is this; why does std::nan() exist? Why would you ever want to take a valid number/string/value and convert it to NaN? (Refrence: http://en.cppreference.com/w/cpp/numeric/math/nan )
NaN is often used to indicate a null or missing value, especially in data analyisis and data science. So it is common for an application to initialize values to nan, in order to track whether a value has been provided or not without the overhead of using optional<T>-like structures.
Secondarily, it common to create custom math functions that you want to return nan for certain inputs. So it's more than just for completeness.
Suppose you want to implement std::acos. How would you return nan in case of invalid input (|arg| < 1)? It should be possible to implement such functions in C++. Beside that fact, that you may need to write a function which is not provided by STL, one of distinctive charts of C++ is that it's standard library can be written on C++.
IEEE 754 systematically introduced the use of NaN to represented numbers whose definitions could otherwise not be represented on computers.
You'll often see this for 0/0, ±inf / ±inf, 0 * ±inf, etc.

C++ sort() function algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Some days ago I wanted to use C++ sort() function to sort an array of strings, but I had a problem!
What algorithm does it use to sort the array? Is it a deterministic one or may it use different algorithms based on the type of the array?
Also, is there a clear time complexity analysis about it?
Does this function use the same algorithm for sorting numbers array and strings array?
It might or it might not. That is not specified by the standard.
And if we use it to sort an array of strings which the total size of them is less than 100,000 characters, would it work in less than 1 second(in the worst case)?
It might or it might not. It depends on the machine you're running the program on. Even if it will work in less than 1 second in worst case on a particular machine, it would be difficult to prove. But you can get a decent estimation by measuring. A measurement only applies to the machine it was performed, of course.

Multiplicaton of big integers (factorial) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I accidentally saw on Internet that functional programming language SML allows to do arbitrary precision integer arithmetic. I had written big integer arithmetic on C++ before and I decided(for curiosity) to compare my implementation with SML's by computing factorial of big numbers. I found out that SML program works about 15 times faster than mine. My implementation uses elementary school multiplication algorithm. But as I know the fast algorithms (such as FFT or Karatsuba's algorithm) worked better than elementary school multiplication when multipliers aren't much different. In this case they are, because (n-1)! is much greater than n. My question is what are the other possible reasons that the SML program works so faster.
Three possible reasons:
It uses multiple CPU cores (easy to test)
It uses SIMD instructions
It uses GPU (rare, but not unheard of)

How to design an algorithm that multiplies two floats without '*'? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How do I design an algorithm that takes two floats and multiplies them using only addition, bit shifting and bitwise operations?
I have already found one like this for integers, but that doesn't work with floats.
I have also found another that is much like what I need but log is also prohibited in my case.
The floats are stored according to the IEEE754 standard. I have also tried to keep their exponent part, and bitwise multiply their fractional part with no luck.
According to http://en.wikipedia.org/wiki/IEEE_floating_point, an IEEE754 number x = (-1)^s * c * b^q is represented by s,c,b,q , all are integers. for Two floating point numbers with the same base b is the same.
So the multiplication of two floating point numbers x and y is:
(-1)^(s1+s2)*c1*c2*b^(q1+q2) so the new floating point is represented by: s1+s2, c1*c2, b q1+q2 so you only have left to deal with multiplication of c1 and c2, both are integers so you are done.