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)..
 {0,1,2,3,4}
Here are example arithmetic tables for a finite field with a cardinality of 5..




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 SQL
One 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;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:
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;
execute function plus(7, 20, 26)...(expression) 1This 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;I also considered defining arithmetic functions for a ROW type of the form:
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;
 CREATE ROW TYPE finite(a int, cardinality int);
Comments (0)