Skip to content

Commit

Permalink
Change GF(2^128) API
Browse files Browse the repository at this point in the history
from `r = op(a, b)` to `op(r, a, b)`
  • Loading branch information
guanzhi committed Apr 11, 2024
1 parent 6a55fd1 commit f9e9b20
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 194 deletions.
37 changes: 15 additions & 22 deletions include/gmssl/gf128.h
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 Down Expand Up @@ -27,27 +27,20 @@
extern "C" {
#endif

//typedef unsigned __int128 gf128_t;

// the least significant bit of lo is a_0, the most significant bit of hi is a_127
// so x^7 + x^2 + x + 1 is 0x87
typedef struct {
uint64_t lo;
uint64_t hi;
} gf128_t;


// Note: send by value is comptabile with uint128_t and sse2
gf128_t gf128_from_hex(const char *s);
int gf128_equ_hex(gf128_t a, const char *s);
gf128_t gf128_zero(void);
gf128_t gf128_add(gf128_t a, gf128_t b);
gf128_t gf128_mul(gf128_t a, gf128_t b);
gf128_t gf128_mul2(gf128_t a);
gf128_t gf128_from_bytes(const uint8_t p[16]);
void gf128_to_bytes(gf128_t a, uint8_t p[16]);
int gf128_print(FILE *fp, int fmt ,int ind, const char *label, gf128_t a);
void gf128_print_bits(gf128_t a);

typedef uint64_t gf128_t[2];

void gf128_set_zero(gf128_t r);
void gf128_set_one(gf128_t r);
void gf128_add(gf128_t r, const gf128_t a, const gf128_t b);
void gf128_mul(gf128_t r, const gf128_t a, const gf128_t b);
void gf128_mul_by_2(gf128_t r, const gf128_t a);
void gf128_from_bytes(gf128_t r, const uint8_t p[16]);
void gf128_to_bytes(const gf128_t a, uint8_t p[16]);
int gf128_from_hex(gf128_t r, const char *s);
int gf128_equ_hex(const gf128_t a, const char *s);
int gf128_print(FILE *fp, int fmt, int ind, const char *label, const gf128_t a);


#ifdef __cplusplus
}
Expand Down
168 changes: 87 additions & 81 deletions src/gf128.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 @@ -22,6 +22,7 @@
#include <gmssl/endian.h>
#include <gmssl/error.h>


static uint64_t reverse_bits(uint64_t a)
{
uint64_t r = 0;
Expand All @@ -36,30 +37,19 @@ static uint64_t reverse_bits(uint64_t a)
return r;
}

gf128_t gf128_zero(void)
void gf128_set_zero(gf128_t r)
{
uint8_t zero[16] = {0};
return gf128_from_bytes(zero);
r[0] = 0;
r[1] = 0;
}

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

int gf128_equ_hex(gf128_t a, const char *s)
{
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;
Expand All @@ -77,8 +67,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 @@ -92,102 +83,117 @@ int gf128_print(FILE *fp, int fmt, int ind, const char *label, gf128_t a)
return 1;
}

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 a, gf128_t b)
void gf128_add(gf128_t r, const gf128_t a, const gf128_t b)
{
gf128_t r;
r.hi = a.hi ^ b.hi;
r.lo = a.lo ^ b.lo;
return r;
r[0] = a[0] ^ b[0];
r[1] = a[1] ^ b[1];
}

#ifndef ENABLE_GMUL_AARCH64
gf128_t gf128_mul(gf128_t a, gf128_t b)
void gf128_mul(gf128_t r, const gf128_t a, const gf128_t b)
{
gf128_t r = {0, 0};
uint64_t mask = (uint64_t)1 << 63;
const uint64_t mask = (uint64_t)1 << 63;
uint64_t b0 = b[0];
uint64_t b1 = b[1];
uint64_t r0 = 0; // incase r is a or b
uint64_t r1 = 0;
int i;


for (i = 0; i < 64; i++) {
if (r.hi & mask) {
r.hi = r.hi << 1 | r.lo >> 63;
r.lo = (r.lo << 1);
r.lo ^= 0x87;
if (r1 & mask) {
r1 = r1 << 1 | r0 >> 63;
r0 = r0 << 1;
r0 ^= 0x87;
} else {
r.hi = r.hi << 1 | r.lo >> 63;
r.lo = r.lo << 1;
r1 = r1 << 1 | r0 >> 63;
r0 = r0 << 1;
}

if (b.hi & mask) {
r.hi ^= a.hi;
r.lo ^= a.lo;
if (b1 & mask) {
r1 ^= a[1];
r0 ^= a[0];
}

b.hi <<= 1;
b1 <<= 1;
}
for (i = 0; i < 64; i++) {
if (r.hi & mask) {
r.hi = r.hi << 1 | r.lo >> 63;
r.lo = (r.lo << 1) ^ 0x87;
if (r1 & mask) {
r1 = r1 << 1 | r0 >> 63;
r0 = r0 << 1;
r0 ^= 0x87;
} else {
r.hi = r.hi << 1 | r.lo >> 63;
r.lo = r.lo << 1;
r1 = r1 << 1 | r0 >> 63;
r0 = r0 << 1;
}

if (b.lo & mask) {
r.hi ^= a.hi;
r.lo ^= a.lo;
if (b0 & mask) {
r1 ^= a[1];
r0 ^= a[0];
}

b.lo <<= 1;
b0 <<= 1;
}

return r;
r[0] = r0;
r[1] = r1;
}
#else

extern void gmul(uint64_t r[2], const uint64_t a[2], const uint64_t b[2]);
#endif

gf128_t gf128_mul(gf128_t a, gf128_t b)
void gf128_mul_by_2(gf128_t r, const gf128_t a)
{
gf128_t r;
gmul(&r, &a, &b);
return r;
}
const uint64_t mask = (uint64_t)1 << 63;

#endif
if (a[1] & mask) {
r[1] = a[1] << 1 | a[0] >> 63;
r[0] = a[0] << 1;
r[0] ^= 0x87;
} else {
r[1] = a[1] << 1 | a[0] >> 63;
r[0] = a[0] << 1;
}
}

gf128_t gf128_mul2(gf128_t a)
int gf128_from_hex(gf128_t r, const char *s)
{
gf128_t r;
uint8_t bytes[16];
size_t len;

if (a.hi & ((uint64_t)1 << 63)) {
r.hi = a.hi << 1 | a.lo >> 63;
r.lo = a.lo << 1;
r.lo ^= 0x87;
} else {
r.hi = a.hi << 1 | a.lo >> 63;
r.lo = a.lo << 1;
if (strlen(s) != sizeof(bytes) * 2) {
error_print();
return -1;
}
if (hex_to_bytes(s, strlen(s), bytes, &len) != 1) {
error_print();
return -1;
}
gf128_from_bytes(r, bytes);
return 1;
}

return r;
int gf128_equ_hex(const gf128_t a, const char *s)
{
gf128_t b;

if (gf128_from_hex(b, s) != 1) {
error_print();
return -1;
}
if (a[0] != b[0] || a[1] != b[1]) {
return 0;
}
return 1;
}

4 changes: 2 additions & 2 deletions src/gf128_aarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
= c + (e0 + w0) * x^64 + (d0 + w1) * f0
*/
.text
.globl _gmul
.globl _gf128_mul
.align 4
_gmul:
_gf128_mul:
// load (a0, a1)
ld1 {v1.2d},[x1]
// load (b0, b1)
Expand Down
Loading

0 comments on commit f9e9b20

Please sign in to comment.