File: _crypto-test_sort_uint32.inc

package info (click to toggle)
tinyssh 20250501-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (98 lines) | stat: -rw-r--r-- 3,341 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
/* ----- sort/uint32, derived from supercop/crypto_sort/try.c */

/*
derived from djb work from lib25519/libntruprime
mj modifications:
- rename files to test-crypto.c and _crypto_<>.<>.inc
- fix compiler warnings
- include crypto.h
- use less rounds for valgrind test
- reformat using clang-format
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "crypto.h"

#define fail ((ok = 0), printf)
static const char *sort_uint32_checksums[] = {
    "83fc714d7acc0375aa6657bd36d3324a6cbc503f68019345651be8a88534c3e2",
    "30921a0be2d73a185181f0ec842fa0fa73cd6e958fc03411d435f14a3fc64c89",
};

static void (*crypto_sort)(void *, long long);
#define crypto_sort_BYTES crypto_sort_uint32_BYTES

static void *storage_sort_uint32_x;
static unsigned char *test_sort_uint32_x;
static void *storage_sort_uint32_x2;
static unsigned char *test_sort_uint32_x2;

static void test_sort_uint32_impl(long long impl) {
    unsigned char *x = test_sort_uint32_x;
    unsigned char *x2 = test_sort_uint32_x2;
    long long xlen;
    long long xwords;

    if (targetn && atol(targetn) != impl) return;
    crypto_sort = crypto_sort_uint32;
    for (long long checksumbig = 0; checksumbig < 2; ++checksumbig) {
        long long loops = checksumbig ? 4096 : 1024;
        long long maxtest = checksumbig ? 4096 : 128;

        if (checksumbig && valgrind) break;
        checksum_clear();

        for (long long loop = 0; loop < loops; ++loop) {
            xwords = myrandom() % (maxtest + 1);
            xlen = xwords * crypto_sort_BYTES;

            input_prepare(x2, x, xlen);
            endianness(x, xwords, crypto_sort_BYTES);
            secret(x, xlen);
            crypto_sort(x, xwords);
            public(x, xlen);
            endianness(x, xwords, crypto_sort_BYTES);
            checksum(x, xlen);
            output_compare(x2, x, xlen, "crypto_sort");

            double_canary(x2, x, xlen);
            endianness(x2, xwords, crypto_sort_BYTES);
            secret(x2, xlen);
            crypto_sort(x2, xwords);
            public(x2, xlen);
            endianness(x2, xwords, crypto_sort_BYTES);
            if (memcmp(x2, x, xlen) != 0)
                fail("failure: crypto_sort is nondeterministic\n");
        }
        checksum_expected(sort_uint32_checksums[checksumbig]);
    }
}

void test_sort_uint32(void) {
    long long maxalloc = 0;
    if (targeto && strcmp(targeto, "sort")) return;
    if (targetp && strcmp(targetp, "uint32")) return;
    storage_sort_uint32_x = callocplus(crypto_sort_uint32_BYTES * 4096);
    test_sort_uint32_x =
        aligned(storage_sort_uint32_x, crypto_sort_uint32_BYTES * 4096);
    if (crypto_sort_uint32_BYTES * 4096 > maxalloc)
        maxalloc = crypto_sort_uint32_BYTES * 4096;
    storage_sort_uint32_x2 = callocplus(maxalloc);
    test_sort_uint32_x2 =
        aligned(storage_sort_uint32_x2, crypto_sort_uint32_BYTES * 4096);

    for (long long offset = 0; offset < 1; ++offset) {
        if (targetoffset && atol(targetoffset) != offset) continue;
        if (offset && valgrind) break;
        printf("sort_uint32 offset %lld\n", offset);
        forked(test_sort_uint32_impl, -1);
        ++test_sort_uint32_x;
        ++test_sort_uint32_x2;
    }
    free(storage_sort_uint32_x2);
    free(storage_sort_uint32_x);
}
#undef crypto_sort_BYTES