Revert example code update

This commit is contained in:
Jon Staab 2024-09-09 12:38:51 -07:00 committed by fiatjaf_
parent 7edd3d23a9
commit e7eb776288

45
13.md
View File

@ -48,37 +48,30 @@ Validating
Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id: Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id:
```c ```c
#include <stdio.h> int zero_bits(unsigned char b)
#include <stdlib.h> {
#include <string.h> int n = 0;
int countLeadingZeroes(const char *hex) { if (b == 0)
int count = 0; return 8;
for (int i = 0; i < strlen(hex); i++) { while (b >>= 1)
int nibble = (int)strtol((char[]){hex[i], '\0'}, NULL, 16); n++;
if (nibble == 0) {
count += 4;
} else {
count += __builtin_clz(nibble) - 28;
break;
}
}
return count; return 7-n;
} }
int main(int argc, char *argv[]) { /* find the number of leading zero bits in a hash */
if (argc != 2) { int count_leading_zero_bits(unsigned char *hash)
fprintf(stderr, "Usage: %s <hex_string>\n", argv[0]); {
return 1; int bits, total, i;
} for (i = 0, total = 0; i < 32; i++) {
bits = zero_bits(hash[i]);
const char *hex_string = argv[1]; total += bits;
int result = countLeadingZeroes(hex_string); if (bits != 8)
printf("Leading zeroes in hex string %s: %d\n", hex_string, result); break;
}
return 0; return total;
} }
``` ```