summaryrefslogtreecommitdiffstats
path: root/src/erasure-code/jerasure/gf-complete/tools/gf_mult.c
blob: 815bd8b26fc4d1f1bdbee0147001086194145f4d (plain)
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
/*
 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
 *
 * gf_mult.c
 *
 * Multiplies two numbers in gf_2^w
 */

#include <stdio.h>
#include <getopt.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include "gf_complete.h"
#include "gf_method.h"
#include "gf_general.h"

void usage(int why)
{
  fprintf(stderr, "usage: gf_mult a b w [method] - does multiplication of a and b in GF(2^w)\n");
  if (why == 'W') {
    fprintf(stderr, "Bad w.\n");
    fprintf(stderr, "Legal w are: 1 - 32, 64 and 128.\n");
    fprintf(stderr, "Append 'h' to w to treat a, b and the product as hexadecimal.\n");
    fprintf(stderr, "w=128 is hex only (i.e. '128' will be an error - do '128h')\n");
  }
  if (why == 'A') fprintf(stderr, "Bad a\n");
  if (why == 'B') fprintf(stderr, "Bad b\n");
  if (why == 'M') {
    fprintf(stderr, "Bad Method Specification: ");
    gf_error();
  }
  exit(1);
}

int main(int argc, char **argv)
{
  int hex, w;
  gf_t gf;
  gf_general_t a, b, c;
  char output[50];

  if (argc < 4) usage(' ');

  if (sscanf(argv[3], "%d", &w) == 0) usage('W');
  if (w <= 0 || (w > 32 && w != 64 && w != 128)) usage('W');

  hex = (strchr(argv[3], 'h') != NULL);
  if (!hex && w == 128) usage('W');

  if (argc == 4) {
    if (gf_init_easy(&gf, w) == 0) usage('M');
  } else {
    if (create_gf_from_argv(&gf, w, argc, argv, 4) == 0) usage('M');
  }
 
  if (!gf_general_s_to_val(&a, w, argv[1], hex)) usage('A');
  if (!gf_general_s_to_val(&b, w, argv[2], hex)) usage('B');

  gf_general_multiply(&gf, &a, &b, &c);
  gf_general_val_to_s(&c, w, output, hex);
  
  printf("%s\n", output);
  exit(0);
}