Skip to content

Commit

Permalink
Update GF(2^128) AVX implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 11, 2024
1 parent 23e6d9f commit 1ab7104
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 83 deletions.
6 changes: 1 addition & 5 deletions include/gmssl/gf128.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/


/* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
* A + B mod f(x) = a xor b
* A * 2 mod f(x)
*/
// GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1

#ifndef GMSSL_GF128_H
#define GMSSL_GF128_H
Expand Down
8 changes: 1 addition & 7 deletions src/gf128.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
*
* http://www.apache.org/licenses/LICENSE-2.0
*/


/* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
* A + B mod f(x) = a xor b
* A * 2 mod f(x)
*/



#include <stdio.h>
#include <string.h>
Expand Down
110 changes: 39 additions & 71 deletions src/gf128_avx.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
Expand All @@ -8,12 +8,6 @@
*/


/* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
* A + B mod f(x) = a xor b
* A * 2 mod f(x)
*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand All @@ -22,37 +16,43 @@
#include <gmssl/gf128.h>
#include <gmssl/endian.h>
#include <gmssl/error.h>

#include <immintrin.h>


gf128_t gf128_zero(void)
static uint64_t reverse_bits(uint64_t a)
{
uint8_t zero[16] = {0};
return gf128_from_bytes(zero);
uint64_t r = 0;
int i;

for (i = 0; i < 63; i++) {
r |= a & 1;
r <<= 1;
a >>= 1;
}
r |= a & 1;
return r;
}

gf128_t gf128_from_hex(const char *s)
void gf128_set_zero(gf128_t r)
{
uint8_t bin[16];
size_t len;
hex_to_bytes(s, strlen(s), bin, &len);
return gf128_from_bytes(bin);
r[0] = 0;
r[1] = 0;
}

int gf128_equ_hex(gf128_t a, const char *s)
void gf128_set_one(gf128_t r)
{
uint8_t bin1[16];
uint8_t bin2[16];
size_t len;
hex_to_bytes(s, strlen(s), bin1, &len);
gf128_to_bytes(a, bin2);
return memcmp(bin1, bin2, sizeof(bin1)) == 0;
r[0] = 1;
r[1] = 0;
}

/*
void gf128_print_bits(gf128_t a)
{
int i;
a.hi = reverse_bits(a.hi);
a.lo = reverse_bits(a.lo);
for (i = 0; i < 64; i++) {
printf("%d", (int)(a.hi % 2));
a.hi >>= 1;
Expand All @@ -63,8 +63,9 @@ void gf128_print_bits(gf128_t a)
}
printf("\n");
}
*/

int gf128_print(FILE *fp, int fmt, int ind, const char *label, gf128_t a)
int gf128_print(FILE *fp, int fmt, int ind, const char *label, const gf128_t a)
{
uint8_t be[16];
int i;
Expand All @@ -78,65 +79,32 @@ int gf128_print(FILE *fp, int fmt, int ind, const char *label, gf128_t a)
return 1;
}


static uint64_t reverse_bits(uint64_t a)
{
uint64_t r = 0;
int i;

for (i = 0; i < 63; i++) {
r |= a & 1;
r <<= 1;
a >>= 1;
}
r |= a & 1;
return r;
}

gf128_t gf128_from_bytes(const uint8_t p[16])
void gf128_from_bytes(gf128_t r, const uint8_t p[16])
{
gf128_t r;

r.lo = GETU64(p);
r.hi = GETU64(p + 8);

r.lo = reverse_bits(r.lo);
r.hi = reverse_bits(r.hi);
return r;
r[0] = reverse_bits(GETU64(p));
r[1] = reverse_bits(GETU64(p + 8));
}

void gf128_to_bytes(gf128_t a, uint8_t p[16])
void gf128_to_bytes(const gf128_t a, uint8_t p[16])
{
a.lo = reverse_bits(a.lo);
a.hi = reverse_bits(a.hi);
PUTU64(p, a.lo);
PUTU64(p + 8, a.hi);
PUTU64(p, reverse_bits(a[0]));
PUTU64(p + 8, reverse_bits(a[1]));
}

gf128_t gf128_add(gf128_t ga, gf128_t gb)
void gf128_add(gf128_t r, const gf128_t a, const gf128_t b)
{
uint8_t r[16], a[16], b[16];

gf128_to_bytes(ga, a);
gf128_to_bytes(gb, b);

__m128i a1 = _mm_loadu_si128((const __m128i*)a);
__m128i b1 = _mm_loadu_si128((const __m128i*)b);
__m128i T0 = _mm_xor_si128(a1, b1);

_mm_storeu_si128((__m128i*)r, T0);

return gf128_from_bytes(r);
r[0] = a[0] ^ b[0];
r[1] = a[1] ^ b[1];
}


gf128_t gf128_mul(gf128_t ga, gf128_t gb)
void gf128_mul(gf128_t gr, const gf128_t ga, const gf128_t gb)
{
const __m128i MASK = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
__m128i a1, b1;
__m128i T0, T1, T2, T3, T4, T5;
uint8_t r[16], a[16], b[16];

// FIXME: directly load a, b
gf128_to_bytes(ga, a);
gf128_to_bytes(gb, b);

Expand Down Expand Up @@ -191,10 +159,10 @@ gf128_t gf128_mul(gf128_t ga, gf128_t gb)

T3 = _mm_shuffle_epi8(T3, MASK);
_mm_storeu_si128((__m128i*)r, T3);
return gf128_from_bytes(r);
gf128_from_bytes(gr, r);
}

gf128_t gf128_mul2(gf128_t ga)
void gf128_mul_by_2(gf128_t gr, const gf128_t ga)
{
const __m128i MASK = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
__m128i MASK1 = _mm_set_epi8(0xe1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
Expand All @@ -221,5 +189,5 @@ gf128_t gf128_mul2(gf128_t ga)

T5 = _mm_shuffle_epi8(T5, MASK);
_mm_storeu_si128((__m128i*)r, T5);
return gf128_from_bytes(r);
gf128_from_bytes(gr, r);
}

0 comments on commit 1ab7104

Please sign in to comment.