Home | History | Annotate | Line # | Download | only in bench
      1 /*	$NetBSD: iterated_hash.c,v 1.2 2025/01/26 16:25:47 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #include <stdbool.h>
     17 #include <stdint.h>
     18 #include <stdio.h>
     19 #include <string.h>
     20 
     21 #include <isc/iterated_hash.h>
     22 #include <isc/random.h>
     23 #include <isc/time.h>
     24 
     25 #include <dns/name.h>
     26 
     27 static void
     28 time_it(const int count, const int iterations, const unsigned char *salt,
     29 	const int saltlen, const unsigned char *in, const int inlen) {
     30 	uint8_t out[NSEC3_MAX_HASH_LENGTH] = { 0 };
     31 	isc_time_t start, finish;
     32 
     33 	printf("%d iterations, %d salt length, %d input length: ", iterations,
     34 	       saltlen, inlen);
     35 	fflush(stdout);
     36 
     37 	start = isc_time_now_hires();
     38 
     39 	int i = 0;
     40 	while (i++ < count) {
     41 		isc_iterated_hash(out, 1, iterations, salt, saltlen, in, inlen);
     42 	}
     43 
     44 	finish = isc_time_now_hires();
     45 
     46 	uint64_t microseconds = isc_time_microdiff(&finish, &start);
     47 	printf("%0.2f us per iterated_hash()\n", (double)microseconds / count);
     48 	fflush(stdout);
     49 }
     50 
     51 int
     52 main(void) {
     53 	uint8_t salt[DNS_NAME_MAXWIRE];
     54 	uint8_t in[DNS_NAME_MAXWIRE];
     55 	size_t saltlen = sizeof(salt);
     56 	size_t inlen = sizeof(in);
     57 
     58 	isc_random_buf(salt, saltlen);
     59 	isc_random_buf(in, inlen);
     60 
     61 	time_it(10000, 150, salt, saltlen, in, inlen);
     62 	time_it(10000, 15, salt, saltlen, in, inlen);
     63 	time_it(10000, 0, salt, saltlen, in, inlen);
     64 
     65 	saltlen = 32;
     66 	inlen = 32;
     67 
     68 	time_it(10000, 150, salt, 32, in, inlen);
     69 	time_it(10000, 15, salt, 32, in, inlen);
     70 	time_it(10000, 0, salt, saltlen, in, inlen);
     71 
     72 	saltlen = 0;
     73 	inlen = 1;
     74 
     75 	time_it(10000, 150, salt, 32, in, inlen);
     76 	time_it(10000, 15, salt, 32, in, inlen);
     77 	time_it(10000, 0, salt, saltlen, in, inlen);
     78 }
     79