Post your code optimizations

Simon F said:
That won't produce a useful result when, say, computing coordinates for 3D graphics!

Yep, but we only have to worry about +/-0, +/-inf not having a multiplicative inverse. :) (well, presumably there are numbers for which no exact inverse exists and you get something close to 1, which begs the question, does a float format exist which provides exact inverses for every member of the field?)

I'm quite familiar with the joys of Reed-Solomon codes, especially with regard to DVB television.

Did you use GF(2^N) or another field isomorphic to it? I find the math in this area very beautiful, and RS+GF seemed almost magical the first time I worked it out on paper.

The same field can be used to securely split any secret into N pieces, M of which are needed to reconstruct them. Since 2 points determine a line, 3 pointers a parabola, etc if one wants to require M points to reconstruct a secret, one generates a random polynomial of degree M-1 with the secret being the constant factor (coefficient of x^0). Next, plug in N random values into this polynomial to get N points. Hand out one of these to each secret holder. Now, if any M of these people can get together, they can reconstruct the original curve. Reconstruction depends on inverses existing for all possible values.
 
DemoCoder said:
Yep, but we only have to worry about +/-0, +/-inf not having a multiplicative inverse. :) (well, presumably there are numbers for which no exact inverse exists and you get something close to 1, which begs the question, does a float format exist which provides exact inverses for every member of the field?)
You've lost me now. We both know that floats aren't even a group under addition, but I thought you were talking about using N-bit ints, where the only problem is the division.

You can't use GF(2^N) to do "vector maths" because addition and multiplication won't produce a sensible result if you try to use them, say, to manipulate vertices.

Did you use GF(2^N) or another field isomorphic to it? I find the math in this area very beautiful, and RS+GF seemed almost magical the first time I worked it out on paper.
That would be telling :) I must admit, when I got back into it I really wished I'd remembered more from my Combinatorics lectures at Uni.

The same field can be used to securely split any secret into N pieces, M of which are needed to reconstruct them. Since 2 points determine a line, 3 pointers a parabola, etc if one wants to require M points to reconstruct a secret, one generates a random polynomial of degree M-1 with the secret being the constant factor (coefficient of x^0). Next, plug in N random values into this polynomial to get N points. Hand out one of these to each secret holder. Now, if any M of these people can get together, they can reconstruct the original curve. Reconstruction depends on inverses existing for all possible values.
I'm just trying to picture this... I guess none of your random points is 0 :).......

....so... Each person is given a unique pair of {Ni, Poly(Ni)}, and any degree M-1 curve is uniquely determined by M points along it. I'm guessing one can just use this with (arbitrary precision) integers but perhaps there is something I'm missing that makes that insecure. We then just use linear algebra (Gaussian elimination) to solve for the coefficients of the polynomial?

I can see that you could just use R-S, by taking your S-word secret and "encrypting" that with a freely shared known key to produce a new S-word secret, S', such that you need all of S' to retrieve S (this avoids cases where partial knowledge of the secret is useful).

You then use R-S to compute a C "word" checksum. You then divvy up the encrypted S'+Checksum to your 'customers'. As soon as someone had collected just S** words of the S+C result, they could decrypt it. (**I'm assuming each "word" would be tagged with its location and so you can correct 2* the usual number of errors)
 
Last edited by a moderator:
Simon F said:
I'm guessing one can just use this with (arbitrary precision) integers but perhaps there is something I'm missing that makes that insecure.
I've been told that you can use Z_p (integers from [0;p-1] where p is a prime, so it's a GF(p)). If p wasn't prime, then Z_p was only a ring, and some "nasty tricks" would be available to break the secret knowing less than M parts.
EDIT: I figured that the field is necessary for constructing the polynomial, and it has to be finite for storage reasons.

We then just use linear algebra (Gaussian elimination) to solve for the coefficients of the polynomial?
You can use Lagrange's method.
 
Last edited by a moderator:
Simon F said:
(snip stuff about distributed secrets)
There's short but good description of this in Bruce Schneier's "Applied Cryptography" book; I even held a presentation on the subject once, but OpenOffice refused to open my old StarWriter files with the notes / handouts from 2001 with a "Generic Input/Output Error" after asking my to select the right format from an unsorted list of about 80... :S
 
Simon F said:
You've lost me now. We both know that floats aren't even a group under addition, but I thought you were talking about using N-bit ints, where the only problem is the division.

At the end I was just speculating in wondering if anyone ever came up with an efficient "perfect" float (not those multiprecision rational classes) that is a field with additive and multiplicative inverses everywhere.

You can't use GF(2^N) to do "vector maths" because addition and multiplication won't produce a sensible result if you try to use them, say, to manipulate vertices.

Yes, you need a field (or maybe Euclidean domain) with a metric and a well-defined relation (perhaps equivalence relation) to do that. It's certainly possible to define meaningful inner products and metrics within these fields. You can do it algebra with GF using Galois arrays, but it still won't be meaningful for say, vertex manipulation unless you have a metric and relation. I never studied Finite Field vector spaces, but I'm bet they come up with GF vector spaces with distance metric, and capable of linear algebra in that area.


....so... Each person is given a unique pair of {Ni, Poly(Ni)}, and any degree M-1 curve is uniquely determined by M points along it. I'm guessing one can just use this with (arbitrary precision) integers but perhaps there is something I'm missing that makes that insecure. We then just use linear algebra (Gaussian elimination) to solve for the coefficients of the polynomial?

Yes, you can use Z_p arbitrary precision ints (under a prime modulus), but it is very slow compared to GF(2^N). You can use either Gaussian elimination or Lagrange interpolation to solve.
 
DemoCoder said:
Yes, you can use Z_p arbitrary precision ints, but it is very slow compared to GF(2^N).
Is it the "modulo p" operation that makes a Z_p slow compared to a GF(2^n)?

In the special case of p=2^m-1 (Mersenne-prime), the "modulo p" operation can be optimized on binary computers, using the property that any number is congruent to the sum of its digits in its base-B notation, modulo (B-1). So the "modulo (2^m-1)" operation can be computed by repeatedly replacing the number with the sum of its m-bit sequences, until it becomes less than 2^m (and if the result is 2^m-1, then the residue is 0).
E.g. 15 mod 3 can be computed as follows:
15 = 1111b
11b + 11b = 0110b
01b + 10b = 11b -> 0b = 0

EDIT: Is this still slower than a GF(2^n)?
 
Last edited by a moderator:
In GF(2^n), addition is (a XOR b), and multiplication is exp(log(a) + log(b)) where exp and log are computed via log/antilog tables. (because GF(2^n) has a generator g such that every element of GF is equal to some power of g, so your antilog table is just g^0 .. g^n, etc) If N is sufficiently large, than multiplication is done by performing a regular multiplication, and then dividing by an irreducible polynomial.
 
DemoCoder said:
If N is sufficiently large, than multiplication is done by performing a regular multiplication, and then dividing by an irreducible polynomial.
Errr? That doesn't sound right to me at all. Surely there is a mapping from addition modulo (2^N)-1 to multiplication over GF(2^N) but I can't see that you can use mul in one to get mul in the other!
 
Simon F said:
Errr? That doesn't sound right to me at all. Surely there is a mapping from addition modulo (2^N)-1 to multiplication over GF(2^N) but I can't see that you can use mul in one to get mul in the other!

Err, I wasn't clear, I meant regular polynomial multiplication (you can't "divide by an irreducible polynomial" either if you think division = machine instruction)

Elements in GF(2^N) are binary polynomials. Given two elements a=a_n x^n + a_n-1 x^n-1 + .. and b=b_n x^n + b_n-1 x^n-1 + ..., a*b = product of those two polynomials modulo an irreducible polynomial. This can be done via the standard russian peasant algorithm, shifting and xoring at each step, then reducing via modulus. There are some tricks done via tables of identities (generated from the irreducible modulus, e.g. if m = x^2 + x + 1, then x^2 = x+1, and x = x^2 + 1, and x^x+x = 1, etc). In reality, for many applications 2^8 or 2^16 are chosen for which tables yield very fast code. For very large fields, such as those used in Elliptic Curve Cryptography, they cherry pick custom field modulus with certain properties to speed up inversion and multiplication on standard CPUs (lookup Optimal Extension Fields)

But in the case of GF(2^8)..GF(2^16) we have

a + b = a ^ b

and

a*b = antilog[log[a] + log]

which needless to say, runs very fast on most CPUs.
 
DemoCoder said:
But in the case of GF(2^8)..GF(2^16) we have
a + b = a ^ b
and
a*b = antilog[log[a] + log]
which needless to say, runs very fast on most CPUs.

Of course... but it's a bad way to do it in hardware!


Oh, btw, I was thinking about your "metric on a finite field" and I don't think you can get anything sensible. I started to type something up but it'd take too long.
 
Simon F said:
Of course... but it's a bad way to do it in hardware!


Oh, btw, I was thinking about your "metric on a finite field" and I don't think you can get anything sensible. I started to type something up but it'd take too long.

By sensible, do you mean Euclidean? The only requirements IIRC are that if D is a metric, then D(x,y) >= 0, D(x,y) = 0 iff x==y, D(x,y) = D(y,x) (symmetric), and D(x,z) <= D(x,y) + D(y,z) (triangle inequality)

Triangle inequality might be the tricky one, but off the top of my head, XOR satisfies the first 3 conditions. A XOR B = B XOR A and A XOR B == 0 iff A == B, and A XOR B >= 0 (we don't have any negative numbers).

I'm traveling right now, but I think about it some more and see if I can find something. Then the question of course is, if that something exists, does one that satisfies Euclidean properties exist?

There are sometimes surprising structures which satisify properties needed to be say a plane, but don't look anything like our common sense notions. For example, we once had a discussion in one of the forums about Projective Planes of order N which look nothing like traditional planes, but are in fact, discrete combinatoric structures. Nevertheless, they are useful for some things, although they do not permit general purpose geometric algorithms based on notions of continuum.
 
I think you guys lost most of us. Care to explain the above in a way we mere mortals can grasp it?

;)
 
DiGuru said:
I think you guys lost most of us. Care to explain the above in a way we mere mortals can grasp it?

;)
A metric, in differential geometry, is used to measure length on a surface of arbitrary curvature. This is why it must be positive (length always is). This is why it must be symmetric (the length from x to y is the same as that from y to x). This is why it must be zero if the arguments are the same (the length from x to x is zero).

The triangle inequality basically just says that no matter the curvature of your surface, if you lay a triangle down on it, the length of one side can never exceed the sum of the lengths of the other two.

So the idea is, if you can define a metric that satisfies the above quantities, then there is some surface which has curvature that is described by this metric.
 
Thanks!

But, what does that have to do with encryption (if I got that right)?
 
DiGuru said:
Thanks!

But, what does that have to do with encryption (if I got that right)?
Probably nothing - there were two simultaneous topics. The summary of the relevant one is this: Since we know that floats may lose precision and "(a+b)-b" might not be "a", can we use 32-bit ints? The comment was "what happened if you get overflow" and I pointed out that, as long as you dont use division, you can always "undo" operations, i.e. (a+b)-b==a.

But division is necessary (example was finding average of 2 values) so the question was raised "can we use a finite field", i.e, where division is possible. The debate then was "can a sensible (i.e. useful) metric be defined". My feeling is the answer is "no" because one would hope that if "a < b" then you would have "a < b < b + b < b + b + b..... " and because the field is finite, you'll hit the "pigeon hole principle" and things will wrap around.
 
In terms of a metric, a XOR b is sufficient to define a metric space. I looked it up and the Hamming Distance defines satisfies the triangle inequality.

I think what you're looking for is not a metric space, but a totally ordered set with an equivalence relation and perhaps some additional properties.

Edit: I just though of an equivalence relation

Let an element x in GF(2^n) be represented as g^k where g is a generator for the field.

Then x < y = lg(x) < lg(y) where lg is the discrete logarithm base g.

This places all elements in order of which power of the generator they appear as. This will not satisfy b < b + b with addition defined as XOR.

I'm not sure having a finite set that wraps around is a "problem" if your set has a big enough range to do useful work.

There's no need to focus on GF(2^8) tho. The integers mod 65537 form a finite field, and satisify all of the properties required, and there is an efficient hardware implementation (this fact is used in IDEA cipher) In fact, 2^32 + 1 is also prime and satisifies the same desirable properties. (namely, you can do vector space algebra with nice equivalence relations and metrics, and additive and multiplicative inverses exist for every element)
 
DemoCoder said:
In terms of a metric, a XOR b is sufficient to define a metric space. I looked it up and the Hamming Distance defines satisfies the triangle inequality.
Certainly but I don't think I'd want to see, say, a first person game that measures the distance travelled with hamming ...:???:
 
Many more metrics are useful for mathematical manipulations than are useful for describing the real world. And even then, many more metrics are useful for describing the real world than are useful for describing everyday interactions.
 
DiGuru said:
I think you guys lost most of us. Care to explain the above in a way we mere mortals can grasp it?

;)

Abstract algebra is a field of math that attempts to "abstract" many of the facets of number systems and algebra to study the pure structure of these systems. I'm sure if you've done operator overloading in a programming language, you're familiar with the concept of creating new types and new operators to deal with them.

Abstract algebra starts off with the simplist structure called a Group, this branch of the theory called "Group Theory" (ok, there are simplest structures, like semigroups for the nitpickers)

A group consists of a set of elements (which may be infinite), an identity element (usually denoted 'e'), and a binary operation that takes two elements of the set and produces another element of the set. Associativity must hold, such as (a*b)*c=a*(b*c) and for each element a, there must exist an element b, such that a*b=e, that is, b is the inverse of a. You may more clearly see this as "a/b = 1" that is, a = 1/b if this were regular algebra.

Examples of groups are things like, the integers modulo prime p with multiplication as the operation. Or, say, the possible rotations of a square (0 degrees, 90 degrees, 180 degrees, 270 degrees, horizontal flip, verticle flip, and two diagonal flips).

For example, given element D_90, it's inverse is D_270, that is D_90 * D_270 = D_0 (identity element, 0 degree rotation) You can see that if you rotate a square by 90 degrees, and then by 270 degrees, it's the same as no rotation.

Now, given a group, we are generally interested in questions like: how many elements does it have? (called the order of the group) Does the commutative property hold (a*b=b*a)? How many subgroups does it have? What are the orders of the elements? (the order of an element a is the what is the smallest value n, such that a^n = e) What groups are isomorphic to this group? What are its homomorphisms, etc.

Sounds boring, but the study of groups led mathematicians to realize that many of what they thought were completely different systems were in fact, exactly the same with different syntax, or intimately related in surprising ways.

Next up in Ring Theory. A Ring is like a group, but has two binary operations (+,*) call them addition and multiplication if you wish. Addition is the same as in groups, but a Ring may or may not have a multiplicative identity (call it '1' or unity) whereas it MUST have an additive identity (call it '0'). Moreover, even if it has a multiplicative identity, each element need not have a multiplicative inverse.

For example, consider the integers modulo 4. 3 has an inverse (3*3 mod 4 = 1) but 2 does not (no x exists such that x * 2 = 1 mod 4). The integers modulo 4 form a ring with unity (has a '1') Rings have lots of interesting subdivisions, there are things called Ideals, Unique Factorization Domains, Principlal Ideal Domains, Integral Domains, Euclidean Domains, etc.

Leading up to the next step: Field Theory. A Field is a Ring with unity ('1') where every element MUST have an inverse. The integers modulo a prime is an example. The Rational Numbers are an integer. Etc. In fields, lots of fun surrounds something called Extension Fields.

Now, none of this would be interesting unless there were some pretty startling results. For example, there is something called a Simple Group and the Classification of the Simple Groups theorem (10,000 pages long proof! took 30 years of work by hundreds!) But, in the end, they classified every possible simple group (there are 4 main classes, and 26 sporadic groups, the largest of which is the Monster Group, lookup Monstrous Moonshine for some fun). What did they discover? A sort of "Periodic Table of the Elements" for all of algebra. Simply put, any group you can possibly think of, can be constructed via Simple Groups, like molecules are constructed by atoms. (A similar theorem exists for commutative groups. ANY group whose operation is commutative can be constructed as a product of cyclic groups of prime power order. For example, any group you design with 6 elements can be represented as the cartesian product of integers modulo 2 and integers modulo 3)

Now what does this have to do with cryptography? Depends on which kind of cryptography. Number Theory is a subset of algebra algebra, and many of the fundamental cryptographic primitives like public key cryptography, bit-commitment, oblivious transfer, key exchange, etc rely on properties of finite fields.

Take the RSA public key algorithm. We know from Fermat that x^(p-1) mod p = 1. For example, 2^6 mod 7 = 64 mod 7 = 1. Or 5 ^ 2 mod 3 = 25 mod 3 = 1. Euler extended this to x^phi(m) = 1 mod m. The phi function is defined as the number of elements e less than m such that gcd(e,m) = 1. For example, phi(6). The elements less than 6 are 1, 2, 3, 4, 5, and only 1 and 5 share no common divisors with 6, so phi(6) = 2.

Therefore, x^2 mod 6 should be equal to 1 (if gcd(x,6) == 1). Like 7^2 = 49 mod 6 and 11 ^ 2 = 121 mod 6 and 13^2 = 169 mod 6)

Now, here's the problem: Calculating phi(m) is *really really hard* unless you know the factors of m. If you know the factors of m=pq, then phi(m) = phi(p) * phi(q) and phi(x) where x is prime is just x-1. So returning to phi(6), we have phi(6)=phi(2)*phi(3) = (2-1)*(3-1) = 1*2 = 2 which agrees with our brute force calculation.

Now we have the necessary components for public key cryptography. Let M be our message.

M^phi(m) = 1 mod m

then of course

M^(phi(m) + 1) = M mod m

that is, M raised to this power, returns M

We also know that

phi(m) + 1 = e*d mod phi(m)

That is, given a carefully chosen element 'e' in the ring defined by integers mod phi(m), there must be a element 'd' that is the multiplicative inverse of e.

Let us call 'e' the encryption key, and 'd' the decryption key.

Now I encrypt a message to you. I take M and raise it to the 'e' power modulo m. C = M^e mod m. I send you C.

It is impossible for you to take the 'e'th root of M mod m.

Now, to decrypt, you simply take C^d mod m. Now let's proove this decrypts it.

C^d = (M^e)^d = M^(e*d) mod m.

If e*d = 1 mod phi(m) than by the definition of congruence, e*d = 1 + r*phi(m) for some integer r.

Therefore we have

M^(1 + r*phi(m))

or

M^1 * M^(r*phi(m))

or

M^1 * M^(phi(m)^r) mod m

but M^(phi(m)) = 1 mod m from Euler's theorem so this reduces to

M^1 * 1^r mod m

= M^1 = M
 
Back
Top