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

47
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:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int zero_bits(unsigned char b)
{
int n = 0;
int countLeadingZeroes(const char *hex) {
int count = 0;
if (b == 0)
return 8;
for (int i = 0; i < strlen(hex); i++) {
int nibble = (int)strtol((char[]){hex[i], '\0'}, NULL, 16);
if (nibble == 0) {
count += 4;
} else {
count += __builtin_clz(nibble) - 28;
while (b >>= 1)
n++;
return 7-n;
}
/* find the number of leading zero bits in a hash */
int count_leading_zero_bits(unsigned char *hash)
{
int bits, total, i;
for (i = 0, total = 0; i < 32; i++) {
bits = zero_bits(hash[i]);
total += bits;
if (bits != 8)
break;
}
}
return count;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <hex_string>\n", argv[0]);
return 1;
}
const char *hex_string = argv[1];
int result = countLeadingZeroes(hex_string);
printf("Leading zeroes in hex string %s: %d\n", hex_string, result);
return 0;
return total;
}
```