Finite fields, also known as
commutative division rings, consist of finite sets of numbers, while number systems we use in daily life such as rational numbers contain an infinite number of elements. Finite fields have various applications, including cryptography algorithms such as Rijndael used in AES (Advanced Encryption Standard).
An arithmetic operation performed in a finite field will result in an element within that field. For example, if you have a field of 5 numbers (
cardinality p=5)..
4+1 will wrap around to the beginning to equal 0. Hence 10 is equivalent to 5 is equivalent to 0.
Here are example arithmetic tables for a finite field with a cardinality of 5..
+  0  1  2  3  4 

0  0  1  2  3  4 

1  1  2  3  4  0 

2  2  3  4  0  1 

3  3  4  0  1  2 

4  4  0  1  2  3 

    0  1  2  3  4 

0  0  1  2  3  4 

1  4  0  1  2  3 

2  3  4  0  1  2 

3  2  3  4  0  1 

4  1  2  3  4  0 

  *  0  1  2  3  4 

0  0  0  0  0  0 

1  0  1  2  3  4 

2  0  2  4  1  3 

3  0  3  1  4  2 

4  0  4  3  2  1 

  /  0  1  2  3  4 

1  0  1  2  3  4 

2  0  3  1  4  2 

3  0  2  4  1  3 

4  0  4  3  2  1 


Expressions such as 1 / 2 = 3 make more sense if you consider the number 6 wraps around to 1 in this field. In the general case, in a finite field of cardinality p, any number n is equivalent to mod(n,p);
Finite field operations in SQLOne way to build finite field arithmetic support into SQL would be to write functions for each arithmetic operator which can handle arbitrary cardinalityby having an integer argument for each operand a and b, and one for the cardinality p. E.g.:
CREATE FUNCTION plus(a int, b int, p int) RETURNING int;RETURN mod(a + b, p);END FUNCTION;
CREATE FUNCTION times(a int, b int, p int) RETURNING int;RETURN mod(a * b, p);END FUNCTION;
CREATE FUNCTION minus(a int, b int, p int) RETURNING int;DEFINE c int;LET c = a  b;WHILE c < 0 LET c = c + p;END WHILE;RETURN mod(c, p);END FUNCTION;
CREATE FUNCTION divide(a int, b int, p int) RETURNING int;WHILE mod(a,b) != 0 LET a = a + p;END WHILERETURN mod(a/b,p);END FUNCTION;
This approach allows the flexibility of being able to choose the cardinality when the function is executed, for example if I was generating a polyalphabetic cipher and wanted to evaluate 7 + 20 in a 26 element field, I could call:
execute function plus(7, 20, 26)...(expression) 1
This lacks support for the builtin '+','','*','/' operators though. A way to fix this would be to sacrifice flexibility and create a DISTINCT type for a member of a finite field of specific cardinality.
Example: the following SQL will create a new data type called
finite5 representing a member of a finite field of cardinality 5, add operator support and test itself by generating arithmetic tables.
CREATE DISTINCT TYPE finite5 AS int;
CREATE FUNCTION plus(a finite5, b finite5) RETURNING finite5;RETURN mod(a::int + b::int, 5)::finite5;END FUNCTION;
CREATE FUNCTION times(a finite5, b finite5) RETURNING finite5;RETURN mod(a::int * b::int, 5)::finite5;END FUNCTION;
CREATE FUNCTION minus(a finite5, b finite5) RETURNING finite5;DEFINE c int;LET c = a::int  b::int;WHILE c < 0 LET c = c + 5;END WHILE;RETURN mod(c, 5)::finite5;END FUNCTION;
CREATE FUNCTION divide(a finite5, b finite5) RETURNING finite5;DEFINE a1 int;LET a1 = a::int;while mod(a1,b::int) != 0 LET a1 = a1 + 5;END WHILERETURN mod(a1/b::int,5)::finite5;END FUNCTION;
 try some test data..CREATE TABLE t1(a finite5);INSERT INTO t1 VALUES(0::finite5);INSERT INTO t1 VALUES(1::finite5);INSERT INTO t1 VALUES(2::finite5);INSERT INTO t1 VALUES(3::finite5);INSERT INTO t1 VALUES(4::finite5);
CREATE TABLE t2(b finite5);INSERT INTO t2 VALUES(0::finite5);INSERT INTO t2 VALUES(1::finite5);INSERT INTO t2 VALUES(2::finite5);INSERT INTO t2 VALUES(3::finite5);INSERT INTO t2 VALUES(4::finite5);
 generate arithmetic tables..select a, " + ", b, " = ", a + b from t1,t2;select a, "  ", b, " = ", a  b from t1,t2;select a, " * ", b, " = ", a * b from t1,t2;select a, " / ", b, " = ", a / b from t1,t2 where b::int > 0;
I also considered defining arithmetic functions for a ROW type of the form:
CREATE ROW TYPE finite(a int, cardinality int);
This might be more cumbersome to use but would have the flexibility of "dynamic cardinality" and arithmetic operator support. It would also lead tothe interesting property of being able to execute "mixedcardinality" operationsbetween elements of differing cardinality, though I guess the results would just depend on how the support function algorithms chose to use the cardinalityof the operands.[
Read More]