Basic ArithmeticΒΆ

This basic example shows how to perform basic arithmetics in various finite fields that are supported by Fifi.

The complete sender example is shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Steinwurf ApS 2018.
// Distributed under the "STEINWURF EVALUATION LICENSE 1.0".
// See accompanying file LICENSE.rst or
// http://www.steinwurf.com/licensing

#include <cstdint>
#include <cstdlib>

#include <fifi/api/create_default_field.hpp>

/// @example basic_arithmetic.cpp
///
/// An example to show how to perform basic arithmetics in finite fields.

void run_binary()
{
    printf("//-------------------------------------------------------------\n");
    printf("// binary field: GF(2)\n");
    printf("//-------------------------------------------------------------\n");

    auto field = fifi::api::create_default_field(fifi::api::field::binary);
    uint32_t a = 0;
    uint32_t b = 1;

    printf(" min value: %d\n", field->min_value());
    printf(" max value: %d\n", field->max_value());
    printf(" %d + %d = %d\n", a, b, field->add(a, b));
    printf(" %d - %d = %d\n", a, b, field->subtract(a, b));
    printf(" %d * %d = %d\n", a, b, field->multiply(a, b));
    printf(" %d / %d = %d\n", a, b, field->divide(a, b));
    printf(" ~%d = %d\n", b, field->invert(b));

    printf("\n");
}

void run_binary4()
{
    printf("//-------------------------------------------------------------\n");
    printf("// binary4 field: GF(2^4)\n");
    printf("//-------------------------------------------------------------\n");

    auto field = fifi::api::create_default_field(fifi::api::field::binary4);
    uint32_t a = 2;
    uint32_t b = 11;

    printf(" min value: %d\n", field->min_value());
    printf(" max value: %d\n", field->max_value());
    printf(" %d + %d = %d\n", a, b, field->add(a, b));
    printf(" %d - %d = %d\n", a, b, field->subtract(a, b));
    printf(" %d * %d = %d\n", a, b, field->multiply(a, b));
    printf(" %d / %d = %d\n", a, b, field->divide(a, b));
    printf(" ~%d = %d\n", b, field->invert(b));

    printf("\n");
}

void run_binary8()
{
    printf("//-------------------------------------------------------------\n");
    printf("// binary8 field: GF(2^8)\n");
    printf("//-------------------------------------------------------------\n");

    auto field = fifi::api::create_default_field(fifi::api::field::binary8);
    uint32_t a = 22;
    uint32_t b = 101;

    printf(" min value: %d\n", field->min_value());
    printf(" max value: %d\n", field->max_value());
    printf(" %d + %d = %d\n", a, b, field->add(a, b));
    printf(" %d - %d = %d\n", a, b, field->subtract(a, b));
    printf(" %d * %d = %d\n", a, b, field->multiply(a, b));
    printf(" %d / %d = %d\n", a, b, field->divide(a, b));
    printf(" ~%d = %d\n", b, field->invert(b));

    printf("\n");
}

int main()
{
    printf("Basic arithmetics in various finite fields:\n\n");

    run_binary();
    run_binary4();
    run_binary8();

    return 0;
}