tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

test-internals.c (6388B)


      1 /* Tor: Removed, file is included in ed25519.c instead. */
      2 /* #include <stdio.h> */
      3 /* #include "ed25519-donna.h" */
      4 
      5 static int
      6 test_adds(void) {
      7 #if defined(HAVE_UINT128) && !defined(ED25519_SSE2)
      8 /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
      9 static const bignum25519 max_bignum = {
     10 	0x7ffffffffffff,0x8000000001230,0x7ffffffffffff,0x7ffffffffffff,0x7ffffffffffff
     11 };
     12 
     13 #if 0
     14 /* what max_bignum should fully reduce to */
     15 static const unsigned char max_bignum_raw[32] = {
     16 	0x12,0x00,0x00,0x00,0x00,0x00,0x88,0x91,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     17 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
     18 };
     19 #endif
     20 
     21 /* (max_bignum + max_bignum)^2 */
     22 static const unsigned char max_bignum2_squared_raw[32] = {
     23 	0x10,0x05,0x00,0x00,0x00,0x00,0x80,0xdc,0x51,0x00,0x00,0x00,0x00,0x61,0xed,0x4a,
     24 	0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     25 };
     26 
     27 /* ((max_bignum + max_bignum) + max_bignum)^2 */
     28 static const unsigned char max_bignum3_squared_raw[32] = {
     29 	0x64,0x0b,0x00,0x00,0x00,0x00,0x20,0x30,0xb8,0x00,0x00,0x00,0x40,0x1a,0x96,0xe8,
     30 	0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     31 };
     32 #else
     33 /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
     34 static const bignum25519 ALIGN(16) max_bignum = {
     35 	0x3ffffff,0x2000300,0x3ffffff,0x1ffffff,0x3ffffff,
     36 	0x1ffffff,0x3ffffff,0x1ffffff,0x3ffffff,0x1ffffff
     37 };
     38 
     39 /* what max_bignum should fully reduce to */
     40 static const unsigned char max_bignum2_squared_raw[32] = {
     41 	0x10,0x05,0x00,0x40,0xc2,0x06,0x40,0x80,0x41,0x02,0x00,0x00,0x00,0x00,0x00,0x00,
     42 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     43 };
     44 
     45 /* (max_bignum * max_bignum) */
     46 static const unsigned char max_bignum3_squared_raw[32] = {
     47 	0x64,0x0b,0x00,0x10,0x35,0x0f,0x90,0x60,0x13,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
     48 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     49 };
     50 #endif
     51 unsigned char result[32];
     52 /* static const bignum25519 ALIGN(16) zero = {0}; */
     53 bignum25519 ALIGN(16) a, b /* , c */;
     54 /* size_t i; */
     55 
     56 /* a = (max_bignum + max_bignum) */
     57 curve25519_add(a, max_bignum, max_bignum);
     58 
     59 /* b = ((max_bignum + max_bignum) * (max_bignum + max_bignum)) */
     60 curve25519_mul(b, a, a);
     61 curve25519_contract(result, b);
     62 if (memcmp(result, max_bignum2_squared_raw, 32) != 0)
     63 	return -1;
     64 curve25519_square(b, a);
     65 curve25519_contract(result, b);
     66 if (memcmp(result, max_bignum2_squared_raw, 32) != 0)
     67 	return -1;
     68 
     69 /* b = (max_bignum + max_bignum + max_bignum) */
     70 curve25519_add_after_basic(b, a, max_bignum);
     71 
     72 /* a = ((max_bignum + max_bignum + max_bignum) * (max_bignum + max_bignum + max_bignum)) */
     73 curve25519_mul(a, b, b);
     74 curve25519_contract(result, a);
     75 if (memcmp(result, max_bignum3_squared_raw, 32) != 0)
     76 	return -1;
     77 curve25519_square(a, b);
     78 curve25519_contract(result, a);
     79 if (memcmp(result, max_bignum3_squared_raw, 32) != 0)
     80 	return -1;
     81 
     82 return 0;
     83 }
     84 
     85 static int
     86 test_subs(void) {
     87 #if defined(HAVE_UINT128) && !defined(ED25519_SSE2)
     88 /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
     89 static const bignum25519 max_bignum = {
     90 	0x7ffffffffffff,0x8000000001230,0x7ffffffffffff,0x7ffffffffffff,0x7ffffffffffff
     91 };
     92 
     93 /* what max_bignum should fully reduce to */
     94 static const unsigned char max_bignum_raw[32] = {
     95 	0x12,0x00,0x00,0x00,0x00,0x00,0x88,0x91,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     96 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
     97 };
     98 
     99 /* (max_bignum * max_bignum) */
    100 static const unsigned char max_bignum_squared_raw[32] = {
    101 	0x44,0x01,0x00,0x00,0x00,0x00,0x20,0x77,0x14,0x00,0x00,0x00,0x40,0x58,0xbb,0x52,
    102 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    103 };
    104 #else
    105 /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
    106 static const bignum25519 ALIGN(16) max_bignum = {
    107 	0x3ffffff,0x2000300,0x3ffffff,0x1ffffff,0x3ffffff,
    108 	0x1ffffff,0x3ffffff,0x1ffffff,0x3ffffff,0x1ffffff
    109 };
    110 
    111 /* what max_bignum should fully reduce to */
    112 static const unsigned char max_bignum_raw[32] = {
    113 	0x12,0x00,0x00,0x04,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    114 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    115 };
    116 
    117 /* (max_bignum * max_bignum) */
    118 static const unsigned char max_bignum_squared_raw[32] = {
    119 	0x44,0x01,0x00,0x90,0xb0,0x01,0x10,0x60,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    120 	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    121 };
    122 #endif
    123 unsigned char result[32];
    124 static const bignum25519 ALIGN(16) zero = {0};
    125 bignum25519 ALIGN(16) a, b /* , c */;
    126 /* size_t i; */
    127 
    128 /* a = max_bignum - 0, which expands to 2p + max_bignum - 0 */
    129 curve25519_sub(a, max_bignum, zero);
    130 curve25519_contract(result, a);
    131 if (memcmp(result, max_bignum_raw, 32) != 0)
    132 	return -1;
    133 
    134 /* b = (max_bignum * max_bignum) */
    135 curve25519_mul(b, a, a);
    136 curve25519_contract(result, b);
    137 if (memcmp(result, max_bignum_squared_raw, 32) != 0)
    138 	return -1;
    139 curve25519_square(b, a);
    140 curve25519_contract(result, b);
    141 if (memcmp(result, max_bignum_squared_raw, 32) != 0)
    142 	return -1;
    143 
    144 /* b = ((a - 0) - 0) */
    145 curve25519_sub_after_basic(b, a, zero);
    146 curve25519_contract(result, b);
    147 if (memcmp(result, max_bignum_raw, 32) != 0)
    148 	return -1;
    149 
    150 /* a = (max_bignum * max_bignum) */
    151 curve25519_mul(a, b, b);
    152 curve25519_contract(result, a);
    153 if (memcmp(result, max_bignum_squared_raw, 32) != 0)
    154 	return -1;
    155 curve25519_square(a, b);
    156 curve25519_contract(result, a);
    157 if (memcmp(result, max_bignum_squared_raw, 32) != 0)
    158 	return -1;
    159 
    160 
    161 return 0;
    162 }
    163 
    164 /* Tor: Removed, tests are invoked as a function instead. */
    165 #if 0
    166 int
    167 main() {
    168 int ret = 0;
    169 int single;
    170 single = test_adds();
    171 if (single) printf("test_adds: FAILED\n");
    172 ret |= single;
    173 single = test_subs();
    174 if (single) printf("test_subs: FAILED\n");
    175 ret |= single;
    176 if (!ret) printf("success\n");
    177 return ret;
    178 }
    179 #endif
    180 
    181 /* Tor: Added for initialization self-testing. */
    182 int
    183 ed25519_donna_selftest(void)
    184 {
    185 int ret = 0;
    186 ret |= test_adds();
    187 ret |= test_subs();
    188 return (ret == 0) ? 0 : -1;
    189 }