Skip to content

Commit

Permalink
new import
Browse files Browse the repository at this point in the history
  • Loading branch information
bhess committed Feb 28, 2024
1 parent dce2078 commit d8c7641
Show file tree
Hide file tree
Showing 42 changed files with 519 additions and 306 deletions.
2 changes: 1 addition & 1 deletion docs/algorithms/sig/mayo.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- **Authors' website**: https://pqmayo.org
- **Specification version**: 1.1.
- **Primary Source**<a name="primary-source"></a>:
- **Source**: https://github.com/PQCMayo/MAYO-C/commit/af6ddc71af9451e00c55433b224825eb6f6fea2b with copy_from_upstream patches
- **Source**: https://github.com/PQCMayo/MAYO-C/commit/5002c0c1772b7ea7eb75da14d8b12c42903f5fda with copy_from_upstream patches
- **Implementation license (SPDX-Identifier)**: Apache-2.0


Expand Down
2 changes: 1 addition & 1 deletion docs/algorithms/sig/mayo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ website: https://pqmayo.org
nist-round: 1
spec-version: 1.1
primary-upstream:
source: https://github.com/PQCMayo/MAYO-C/commit/af6ddc71af9451e00c55433b224825eb6f6fea2b
source: https://github.com/PQCMayo/MAYO-C/commit/5002c0c1772b7ea7eb75da14d8b12c42903f5fda
with copy_from_upstream patches
spdx-license-identifier: Apache-2.0
parameter-sets:
Expand Down
2 changes: 1 addition & 1 deletion scripts/copy_from_upstream/copy_from_upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ upstreams:
name: pqmayo
git_url: https://github.com/PQCMayo/MAYO-C.git
git_branch: bhe-liboqs-integration
git_commit: af6ddc71af9451e00c55433b224825eb6f6fea2b
git_commit: 5002c0c1772b7ea7eb75da14d8b12c42903f5fda
sig_meta_path: 'META/{pretty_name_full}_META.yml'
sig_scheme_path: '.'
patches: [pqmayo-aes.patch, pqmayo-mem.patch]
Expand Down
20 changes: 10 additions & 10 deletions src/sig/mayo/pqmayo_mayo_1_avx2/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ crypto_sign_keypair(unsigned char *pk, unsigned char *sk) {

#ifndef PQM4
int
crypto_sign(unsigned char *sm, unsigned long long *smlen,
const unsigned char *m, unsigned long long mlen,
crypto_sign(unsigned char *sm, size_t *smlen,
const unsigned char *m, size_t mlen,
const unsigned char *sk) {
return mayo_sign(MAYO_PARAMS, sm, smlen, m, mlen, sk);
}

int
crypto_sign_signature(unsigned char *sig,
unsigned long long *siglen, const unsigned char *m,
unsigned long long mlen, const unsigned char *sk) {
size_t *siglen, const unsigned char *m,
size_t mlen, const unsigned char *sk) {
return mayo_sign_signature(MAYO_PARAMS, sig, siglen, m, mlen, sk);
}

int
crypto_sign_open(unsigned char *m, unsigned long long *mlen,
const unsigned char *sm, unsigned long long smlen,
crypto_sign_open(unsigned char *m, size_t *mlen,
const unsigned char *sm, size_t smlen,
const unsigned char *pk) {
return mayo_open(MAYO_PARAMS, m, mlen, sm, smlen, pk);
}

int
crypto_sign_verify(const unsigned char *sig, unsigned long long siglen,
const unsigned char *m, unsigned long long mlen,
crypto_sign_verify(const unsigned char *sig, size_t siglen,
const unsigned char *m, size_t mlen,
const unsigned char *pk) {
if (siglen != CRYPTO_BYTES)
return -1;
Expand All @@ -50,7 +50,7 @@ crypto_sign(unsigned char *sm, size_t *smlen,
const unsigned char *m, size_t mlen,
const unsigned char *sk) {

unsigned long long smlen_ll;
size_t smlen_ll;
int rc = mayo_sign(MAYO_PARAMS, sm, &smlen_ll, m, mlen, sk);
*smlen = smlen_ll;
return rc;
Expand All @@ -60,7 +60,7 @@ int
crypto_sign_open(unsigned char *m, size_t *mlen,
const unsigned char *sm, size_t smlen,
const unsigned char *pk) {
unsigned long long mlen_ll;
size_t mlen_ll;
int rc = mayo_open(MAYO_PARAMS, m, &mlen_ll, sm, smlen, pk);
*mlen = mlen_ll;
return rc;
Expand Down
16 changes: 8 additions & 8 deletions src/sig/mayo/pqmayo_mayo_1_avx2/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ crypto_sign_keypair(unsigned char *pk, unsigned char *sk);
#ifndef PQM4
#define crypto_sign MAYO_NAMESPACE(crypto_sign)
int
crypto_sign(unsigned char *sm, unsigned long long *smlen,
const unsigned char *m, unsigned long long mlen,
crypto_sign(unsigned char *sm, size_t *smlen,
const unsigned char *m, size_t mlen,
const unsigned char *sk);

#define crypto_sign_signature MAYO_NAMESPACE(crypto_sign_signature)
int
crypto_sign_signature(unsigned char *sig,
unsigned long long *siglen, const unsigned char *m,
unsigned long long mlen, const unsigned char *sk);
size_t *siglen, const unsigned char *m,
size_t mlen, const unsigned char *sk);

#define crypto_sign_open MAYO_NAMESPACE(crypto_sign_open)
int
crypto_sign_open(unsigned char *m, unsigned long long *mlen,
const unsigned char *sm, unsigned long long smlen,
crypto_sign_open(unsigned char *m, size_t *mlen,
const unsigned char *sm, size_t smlen,
const unsigned char *pk);

#define crypto_sign_verify MAYO_NAMESPACE(crypto_sign_verify)
int
crypto_sign_verify(const unsigned char *sig, unsigned long long siglen,
const unsigned char *m, unsigned long long mlen,
crypto_sign_verify(const unsigned char *sig, size_t siglen,
const unsigned char *m, size_t mlen,
const unsigned char *pk);
#else
#include <stddef.h>
Expand Down
4 changes: 4 additions & 0 deletions src/sig/mayo/pqmayo_mayo_1_avx2/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <mayo.h>
#include <stdint.h>

#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define TARGET_BIG_ENDIAN
#endif

#if defined(MAYO_AVX) && (M_MAX == 64)
#include <shuffle_arithmetic_64.h>
#endif
Expand Down
72 changes: 47 additions & 25 deletions src/sig/mayo/pqmayo_mayo_1_avx2/mayo.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void encode(const unsigned char *m, unsigned char *menc, int mlen) {
}
}

static void compute_rhs(const mayo_params_t *p, const uint64_t *_vPv, unsigned char *t, unsigned char *y){
static void compute_rhs(const mayo_params_t *p, const uint64_t *_vPv, const unsigned char *t, unsigned char *y){
#ifndef ENABLE_PARAMS_DYNAMIC
(void) p;
#endif
Expand All @@ -62,10 +62,18 @@ static void compute_rhs(const mayo_params_t *p, const uint64_t *_vPv, unsigned c
// reduce mod f(X)
for (int jj = 0; jj < F_TAIL_LEN; jj++) {
if(jj%2 == 0){
#ifdef TARGET_BIG_ENDIAN
temp_bytes[(((jj/2 + 8) / 8) * 8) - 1 - (jj/2)%8] ^= mul_f(top, PARAM_f_tail(p)[jj]);
#else
temp_bytes[jj/2] ^= mul_f(top, PARAM_f_tail(p)[jj]);
#endif
}
else {
#ifdef TARGET_BIG_ENDIAN
temp_bytes[(((jj/2 + 8) / 8) * 8) - 1 - (jj/2)%8] ^= mul_f(top, PARAM_f_tail(p)[jj]) << 4;
#else
temp_bytes[jj/2] ^= mul_f(top, PARAM_f_tail(p)[jj]) << 4;
#endif
}
}

Expand All @@ -80,8 +88,14 @@ static void compute_rhs(const mayo_params_t *p, const uint64_t *_vPv, unsigned c
// add to y
for (int i = 0; i < PARAM_m(p); i+=2)
{
#ifdef TARGET_BIG_ENDIAN
y[i] = t[i] ^ (temp_bytes[(((i/2 + 8) / 8) * 8) - 1 - (i/2)%8] & 0xF);
y[i+1] = t[i+1] ^ (temp_bytes[(((i/2 + 8) / 8) * 8) - 1 - (i/2)%8] >> 4);
#else
y[i] = t[i] ^ (temp_bytes[i/2] & 0xF);
y[i+1] = t[i+1] ^ (temp_bytes[i/2] >> 4);
#endif

}
}

Expand Down Expand Up @@ -208,6 +222,11 @@ static void compute_A(const mayo_params_t *p, const uint64_t *_VtL, unsigned cha
}
}

#ifdef TARGET_BIG_ENDIAN
for (int i = 0; i < (((PARAM_o(p)*PARAM_k(p)+15)/16)*16)*MAYO_M_OVER_8; ++i)
A[i] = BSWAP64(A[i]);
#endif

for (int r = 0; r < PARAM_m(p); r+=16)
{
for (int c = 0; c < PARAM_A_cols(p)-1 ; c+=16)
Expand Down Expand Up @@ -235,8 +254,8 @@ int mayo_keypair(const mayo_params_t *p, unsigned char *pk, unsigned char *sk) {
}

int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
unsigned long long *siglen, const unsigned char *m,
unsigned long long mlen, const unsigned char *csk) {
size_t *siglen, const unsigned char *m,
size_t mlen, const unsigned char *csk) {
int ret = MAYO_OK;
unsigned char tenc[M_BYTES_MAX], t[M_MAX]; // no secret data
unsigned char y[M_MAX]; // secret data
Expand Down Expand Up @@ -264,6 +283,9 @@ int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
const int param_v_bytes = PARAM_v_bytes(p);
const int param_r_bytes = PARAM_r_bytes(p);
const int param_P1_bytes = PARAM_P1_bytes(p);
#ifdef TARGET_BIG_ENDIAN
const int param_P2_bytes = PARAM_P2_bytes(p);
#endif
const int param_sig_bytes = PARAM_sig_bytes(p);
const int param_A_cols = PARAM_A_cols(p);
const int param_digest_bytes = PARAM_digest_bytes(p);
Expand All @@ -286,11 +308,11 @@ int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
alignas (32) uint64_t Mtmp[K_MAX * O_MAX * M_MAX / 16] = {0};

#ifdef TARGET_BIG_ENDIAN
for (int i = 0; i < param_P1_bytes / 4; ++i) {
P1[i] = BSWAP32(P1[i]);
for (int i = 0; i < param_P1_bytes / 8; ++i) {
P1[i] = BSWAP64(P1[i]);
}
for (int i = 0; i < param_P2_bytes / 4; ++i) {
L[i] = BSWAP32(L[i]);
for (int i = 0; i < param_P2_bytes / 8; ++i) {
L[i] = BSWAP64(L[i]);
}
#endif

Expand Down Expand Up @@ -377,13 +399,13 @@ int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
}

int mayo_sign(const mayo_params_t *p, unsigned char *sm,
unsigned long long *smlen, const unsigned char *m,
unsigned long long mlen, const unsigned char *csk) {
size_t *smlen, const unsigned char *m,
size_t mlen, const unsigned char *csk) {
int ret = MAYO_OK;
const int param_sig_bytes = PARAM_sig_bytes(p);
unsigned long long siglen = param_sig_bytes;
size_t siglen = param_sig_bytes;
ret = mayo_sign_signature(p, sm, &siglen, m, mlen, csk);
if (ret != MAYO_OK || siglen != (unsigned long long) param_sig_bytes)
if (ret != MAYO_OK || siglen != (size_t) param_sig_bytes)
goto err;

memmove(sm + param_sig_bytes, m, mlen);
Expand All @@ -393,10 +415,10 @@ int mayo_sign(const mayo_params_t *p, unsigned char *sm,
}

int mayo_open(const mayo_params_t *p, unsigned char *m,
unsigned long long *mlen, const unsigned char *sm,
unsigned long long smlen, const unsigned char *pk) {
size_t *mlen, const unsigned char *sm,
size_t smlen, const unsigned char *pk) {
const int param_sig_bytes = PARAM_sig_bytes(p);
if (smlen < (unsigned long long)param_sig_bytes) {
if (smlen < (size_t)param_sig_bytes) {
return MAYO_ERR;
}
int result = mayo_verify(p, sm + param_sig_bytes, smlen - param_sig_bytes, sm,
Expand Down Expand Up @@ -535,8 +557,8 @@ int mayo_expand_sk(const mayo_params_t *p, const unsigned char *csk,
uint64_t *P2 = P + (param_P1_bytes / 8);

#ifdef TARGET_BIG_ENDIAN
for (int i = 0; i < (param_P1_bytes + param_P2_bytes) / 4; ++i) {
P[i] = BSWAP32(P[i]);
for (int i = 0; i < (param_P1_bytes + param_P2_bytes) / 8; ++i) {
P[i] = BSWAP64(P[i]);
}
#endif

Expand All @@ -549,8 +571,8 @@ int mayo_expand_sk(const mayo_params_t *p, const unsigned char *csk,
memcpy(sk->o, S + param_pk_seed_bytes, param_O_bytes);

#ifdef TARGET_BIG_ENDIAN
for (int i = 0; i < (param_P1_bytes + param_P2_bytes) / 4; ++i) {
P[i] = BSWAP32(P[i]);
for (int i = 0; i < (param_P1_bytes + param_P2_bytes) / 8; ++i) {
P[i] = BSWAP64(P[i]);
}
#endif

Expand All @@ -560,7 +582,7 @@ int mayo_expand_sk(const mayo_params_t *p, const unsigned char *csk,
}

int mayo_verify(const mayo_params_t *p, const unsigned char *m,
unsigned long long mlen, const unsigned char *sig,
size_t mlen, const unsigned char *sig,
const unsigned char *cpk) {
unsigned char tEnc[M_BYTES_MAX];
unsigned char t[M_MAX];
Expand Down Expand Up @@ -594,14 +616,14 @@ int mayo_verify(const mayo_params_t *p, const unsigned char *m,
uint64_t *P3 = P2 + (param_P2_bytes / 8);

#ifdef TARGET_BIG_ENDIAN
for (int i = 0; i < param_P1_bytes / 4; ++i) {
P1[i] = BSWAP32(P1[i]);
for (int i = 0; i < param_P1_bytes / 8; ++i) {
P1[i] = BSWAP64(P1[i]);
}
for (int i = 0; i < param_P2_bytes / 4; ++i) {
P2[i] = BSWAP32(P2[i]);
for (int i = 0; i < param_P2_bytes / 8; ++i) {
P2[i] = BSWAP64(P2[i]);
}
for (int i = 0; i < param_P3_bytes / 4; ++i) {
P3[i] = BSWAP32(P3[i]);
for (int i = 0; i < param_P3_bytes / 8; ++i) {
P3[i] = BSWAP64(P3[i]);
}
#endif

Expand Down
15 changes: 8 additions & 7 deletions src/sig/mayo/pqmayo_mayo_1_avx2/mayo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define MAYO_H

#include <stdint.h>
#include <stdlib.h>

#define F_TAIL_LEN 5
#define F_TAIL_64 \
Expand Down Expand Up @@ -321,8 +322,8 @@ int mayo_keypair(const mayo_params_t *p, unsigned char *pk, unsigned char *sk);

#define mayo_sign_signature MAYO_NAMESPACE(mayo_sign_signature)
int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
unsigned long long *siglen, const unsigned char *m,
unsigned long long mlen, const unsigned char *csk);
size_t *siglen, const unsigned char *m,
size_t mlen, const unsigned char *csk);

/**
* MAYO signature generation.
Expand All @@ -341,8 +342,8 @@ int mayo_sign_signature(const mayo_params_t *p, unsigned char *sig,
*/
#define mayo_sign MAYO_NAMESPACE(mayo_sign)
int mayo_sign(const mayo_params_t *p, unsigned char *sm,
unsigned long long *smlen, const unsigned char *m,
unsigned long long mlen, const unsigned char *sk);
size_t *smlen, const unsigned char *m,
size_t mlen, const unsigned char *sk);

/**
* Mayo open signature.
Expand All @@ -361,8 +362,8 @@ int mayo_sign(const mayo_params_t *p, unsigned char *sm,
*/
#define mayo_open MAYO_NAMESPACE(mayo_open)
int mayo_open(const mayo_params_t *p, unsigned char *m,
unsigned long long *mlen, const unsigned char *sm,
unsigned long long smlen, const unsigned char *pk);
size_t *mlen, const unsigned char *sm,
size_t smlen, const unsigned char *pk);

/**
* Mayo compact keypair generation.
Expand Down Expand Up @@ -427,7 +428,7 @@ int mayo_expand_sk(const mayo_params_t *p, const unsigned char *csk,
*/
#define mayo_verify MAYO_NAMESPACE(mayo_verify)
int mayo_verify(const mayo_params_t *p, const unsigned char *m,
unsigned long long mlen, const unsigned char *sig,
size_t mlen, const unsigned char *sig,
const unsigned char *pk);

#endif
2 changes: 2 additions & 0 deletions src/sig/mayo/pqmayo_mayo_1_avx2/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#if defined(__GNUC__) || defined(__clang__)
#define BSWAP32(i) __builtin_bswap32((i))
#define BSWAP64(i) __builtin_bswap64((i))
#else
#define BSWAP32(i) ((((i) >> 24) & 0xff) | (((i) >> 8) & 0xff00) | (((i) & 0xff00) << 8) | ((i) << 24))
#define BSWAP64(i) ((BSWAP32((i) >> 32) & 0xffffffff) | (BSWAP32(i) << 32))
#endif

// a > b -> b - a is negative
Expand Down
Loading

0 comments on commit d8c7641

Please sign in to comment.