aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorW. Kosior <koszko@koszko.org>2024-11-25 14:32:13 +0100
committerW. Kosior <koszko@koszko.org>2024-11-25 14:32:13 +0100
commitd15a4247cfef1ab4f1a2dd21aed2764cdb02e7d1 (patch)
treee4dcd5569b2456c5b48029a0d276feb77acf13bd
parent2fb61d59523feb5951706535036782f4eb1cc53d (diff)
downloadpq-blind-sigs-impl-d15a4247cfef1ab4f1a2dd21aed2764cdb02e7d1.tar.gz
pq-blind-sigs-impl-d15a4247cfef1ab4f1a2dd21aed2764cdb02e7d1.zip
Add polynomial addition and substraction in ring.
-rw-r--r--README.md8
-rw-r--r--poly_mul.c41
2 files changed, 38 insertions, 11 deletions
diff --git a/README.md b/README.md
index e25daa9..e109b02 100644
--- a/README.md
+++ b/README.md
@@ -18,18 +18,22 @@ $ make run_poly_mul
guix shell qemu -- qemu-x86_64 -cpu max poly_mul
Prime used for modulo operations: 127
55 + 31 mod [-63,63] = -41
-Give first polynomial to multiply:
+Give first polynomial for the experiment:
12 3 4 5
Read polynomial: 5*x^3+4*x^2+3*x+12
-Give second polynomial to multiply:
+Give second polynomial for the experiment:
11 1 2 3
Read polynomial: 3*x^3+2*x^2+x+11
Normal product of polynomials:
15*x^6+22*x^5+22*x^4+101*x^3+71*x^2+45*x+132
+Normal sum of polynomials:
+8*x^3+6*x^2+4*x+23
Give the degree m of X^m+1 polynomial to be used as divisor in the ring:
7
Product of polynomials in the ring:
15*x^6+22*x^5+22*x^4-26*x^3-56*x^2+45*x+5
+Sum of polynomials in the ring:
+8*x^3+6*x^2+4*x+23
```
Interestingly, only modulo operations in the latter range seem to be directly
diff --git a/poly_mul.c b/poly_mul.c
index db45bfd..dd86596 100644
--- a/poly_mul.c
+++ b/poly_mul.c
@@ -180,6 +180,18 @@ void poly_mul_in_ring(fmpz_poly_t res, fmpz_poly_t poly1, fmpz_poly_t poly2,
poly_to_ring(res, ctx);
}
+void poly_add_in_ring(fmpz_poly_t res, fmpz_poly_t poly1, fmpz_poly_t poly2,
+ poly_ring_ctx_t ctx) {
+ fmpz_poly_add(res, poly1, poly2);
+ poly_to_ring(res, ctx);
+}
+
+void poly_sub_in_ring(fmpz_poly_t res, fmpz_poly_t poly1, fmpz_poly_t poly2,
+ poly_ring_ctx_t ctx) {
+ fmpz_poly_sub(res, poly1, poly2);
+ poly_to_ring(res, ctx);
+}
+
int main(const int argc, const char* const* const argv) {
fmpz_t prime; /* integer for modulo operations */
mod_centered_0_ctx_t mod_ctx;
@@ -226,28 +238,34 @@ int main(const int argc, const char* const* const argv) {
} /* End of experiment 1 */
{ /* Experiment 2 */
- fmpz_poly_t poly1, poly2, poly_prod;
+ fmpz_poly_t poly1, poly2, poly_computed;
slong divisor_degree;
poly_ring_ctx_t poly_ring_ctx;
- printf("Give first polynomial to multiply:\n");
+ printf("Give first polynomial for the experiment:\n");
init_read_poly(poly1, stdin);
printf("Read polynomial: ");
fmpz_poly_print_pretty(poly1, "x");
putchar('\n');
- printf("Give second polynomial to multiply:\n");
+ printf("Give second polynomial for the experiment:\n");
init_read_poly(poly2, stdin);
printf("Read polynomial: ");
fmpz_poly_print_pretty(poly2, "x");
putchar('\n');
+ fmpz_poly_init(poly_computed);
+
printf("Normal product of polynomials:\n");
- fmpz_poly_init(poly_prod);
- fmpz_poly_mul(poly_prod, poly1, poly2);
- fmpz_poly_print_pretty(poly_prod, "x");
+ fmpz_poly_mul(poly_computed, poly1, poly2);
+ fmpz_poly_print_pretty(poly_computed, "x");
+ putchar('\n');
+
+ printf("Normal sum of polynomials:\n");
+ fmpz_poly_add(poly_computed, poly1, poly2);
+ fmpz_poly_print_pretty(poly_computed, "x");
putchar('\n');
printf("Give the degree m of X^m+1 polynomial to be used as ");
@@ -260,13 +278,18 @@ int main(const int argc, const char* const* const argv) {
poly_ring_ctx_init(poly_ring_ctx, mod_ctx, divisor_degree);
printf("Product of polynomials in the ring:\n");
- poly_mul_in_ring(poly_prod, poly1, poly2, poly_ring_ctx);
- fmpz_poly_print_pretty(poly_prod, "x");
+ poly_mul_in_ring(poly_computed, poly1, poly2, poly_ring_ctx);
+ fmpz_poly_print_pretty(poly_computed, "x");
+ putchar('\n');
+
+ printf("Sum of polynomials in the ring:\n");
+ poly_add_in_ring(poly_computed, poly1, poly2, poly_ring_ctx);
+ fmpz_poly_print_pretty(poly_computed, "x");
putchar('\n');
fmpz_poly_clear(poly1);
fmpz_poly_clear(poly2);
- fmpz_poly_clear(poly_prod);
+ fmpz_poly_clear(poly_computed);
poly_ring_ctx_clear(poly_ring_ctx);
} /* End of experiment 2 */