github.com/yggdrasil-network/yggdrasil-go@v0.5.6/contrib/yggdrasil-brute-simple/yggdrasil-brute-multi-ed25519.c (about)

     1  /*
     2  seed: 32 random bytes
     3  sk: sha512(seed)
     4  sk[0] &= 248
     5  sk[31] &= 127
     6  sk[31] |= 64
     7  
     8  pk: scalarmult_ed25519_base(sk)
     9  
    10  
    11  increment seed
    12  generate sk
    13  generate pk
    14  hash = sha512(mypub)
    15  
    16  if besthash:
    17  	bestseed = seed
    18  	bestseckey = sk
    19  	bestpubkey = pk
    20  	besthash = hash
    21  */
    22  
    23  #include "yggdrasil-brute.h"
    24  
    25  
    26  int main(int argc, char **argv) {
    27  	int i;
    28  	int j;
    29  	time_t starttime;
    30  	time_t requestedtime;
    31  
    32  	unsigned char bestsklist[NUMKEYS][64]; /* sk contains pk */
    33  	unsigned char besthashlist[NUMKEYS][64];
    34  
    35  	unsigned char seed[32];
    36  	unsigned char sk[64];
    37  	unsigned char pk[32];
    38  	unsigned char hash[64];
    39  
    40  	unsigned int runs = 0;
    41  	int where;
    42  
    43  	if (argc != 2) {
    44  		fprintf(stderr, "usage: ./yggdrasil-brute-multi-curve25519 <seconds>\n");
    45  		return 1;
    46  	}
    47  
    48  	if (sodium_init() < 0) {
    49  		/* panic! the library couldn't be initialized, it is not safe to use */
    50  		printf("sodium init failed!\n");
    51  		return 1;
    52  	}
    53  
    54  	starttime = time(NULL);
    55  	requestedtime = atoi(argv[1]);
    56  
    57  	if (requestedtime < 0) requestedtime = 0;
    58  	fprintf(stderr, "Searching for yggdrasil ed25519 keys (this will take slightly longer than %ld seconds)\n", requestedtime);
    59  
    60  	sodium_memzero(bestsklist, NUMKEYS * 64);
    61  	sodium_memzero(besthashlist, NUMKEYS * 64);
    62  	randombytes_buf(seed, 32);
    63  
    64  	do {
    65  		/* generate pubkey, hash, compare, increment secret.
    66  		 * this loop should take 4 seconds on modern hardware */
    67  		for (i = 0; i < (1 << 17); ++i) {
    68  			++runs;
    69  			crypto_hash_sha512(sk, seed, 32);
    70  
    71  			if (crypto_scalarmult_ed25519_base(pk, sk) != 0) {
    72  				printf("scalarmult to create pub failed!\n");
    73  				return 1;
    74  			}
    75  			memcpy(sk + 32, pk, 32);
    76  
    77  			crypto_hash_sha512(hash, pk, 32);
    78  
    79  			/* insert into local list of good key */
    80  			where = find_where(hash, besthashlist);
    81  			if (where >= 0) {
    82  				insert_64(bestsklist, sk, where);
    83  				insert_64(besthashlist, hash, where);
    84  				randombytes_buf(seed, 32);
    85  			}
    86  			for (j = 1; j < 31; ++j) if (++seed[j]) break;
    87  		}
    88  	} while (time(NULL) - starttime < requestedtime || runs < NUMKEYS);
    89  
    90  	fprintf(stderr, "!! Secret key is seed concatenated with public !!\n");
    91  	fprintf(stderr, "---hash--- ------------------------------seed------------------------------ -----------------------------public-----------------------------\n");
    92  	for (i = 0; i < NUMKEYS; ++i) {
    93  		for (j = 0; j < 5; ++j) printf("%02x", besthashlist[i][j]);
    94  		printf(" ");
    95  		for (j = 0; j < 32; ++j) printf("%02x", bestsklist[i][j]);
    96  		printf(" ");
    97  		for (j = 32; j < 64; ++j) printf("%02x", bestsklist[i][j]);
    98  		printf("\n");
    99  	}
   100  
   101  	sodium_memzero(bestsklist, NUMKEYS * 64);
   102  	sodium_memzero(sk, 64);
   103  	sodium_memzero(seed, 32);
   104  
   105  	return 0;
   106  }