aboutsummaryrefslogtreecommitdiff
path: root/src/revocation/revocation_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/revocation/revocation_api.c')
-rw-r--r--src/revocation/revocation_api.c57
1 files changed, 40 insertions, 17 deletions
diff --git a/src/revocation/revocation_api.c b/src/revocation/revocation_api.c
index 12cb63c57..3815e47b0 100644
--- a/src/revocation/revocation_api.c
+++ b/src/revocation/revocation_api.c
@@ -394,9 +394,8 @@ static unsigned int
394count_leading_zeroes (const struct GNUNET_HashCode *hash) 394count_leading_zeroes (const struct GNUNET_HashCode *hash)
395{ 395{
396 unsigned int hash_count; 396 unsigned int hash_count;
397
398 hash_count = 0; 397 hash_count = 0;
399 while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))) 398 while ((0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)))
400 hash_count++; 399 hash_count++;
401 return hash_count; 400 return hash_count;
402} 401}
@@ -467,15 +466,12 @@ GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_PowP *pow,
467 } 466 }
468 467
469 /** 468 /**
470 * First, check if any duplicates are in the PoW set 469 * First, check if PoW set is strictly monotically increasing
471 */ 470 */
472 for (unsigned int i = 0; i < POW_COUNT; i++) 471 for (unsigned int i = 0; i < POW_COUNT-1; i++)
473 { 472 {
474 for (unsigned int j = i + 1; j < POW_COUNT; j++) 473 if (GNUNET_ntohll (pow->pow[i]) >= GNUNET_ntohll (pow->pow[i+1]))
475 { 474 return GNUNET_NO;
476 if (pow->pow[i] == pow->pow[j])
477 return GNUNET_NO;
478 }
479 } 475 }
480 GNUNET_memcpy (&buf[sizeof(uint64_t)], 476 GNUNET_memcpy (&buf[sizeof(uint64_t)],
481 &pow->timestamp, 477 &pow->timestamp,
@@ -486,8 +482,8 @@ GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_PowP *pow,
486 for (unsigned int i = 0; i < POW_COUNT; i++) 482 for (unsigned int i = 0; i < POW_COUNT; i++)
487 { 483 {
488 pow_val = GNUNET_ntohll (pow->pow[i]); 484 pow_val = GNUNET_ntohll (pow->pow[i]);
489 GNUNET_memcpy (buf, &pow_val, sizeof(uint64_t)); 485 GNUNET_memcpy (buf, &pow->pow[i], sizeof(uint64_t));
490 GNUNET_CRYPTO_pow_hash ("gnunet-revocation-proof-of-work", 486 GNUNET_CRYPTO_pow_hash ("GnsRevocationPow",
491 buf, 487 buf,
492 sizeof(buf), 488 sizeof(buf),
493 &result); 489 &result);
@@ -495,6 +491,7 @@ GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_PowP *pow,
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Score %u with %" PRIu64 " (#%u)\n", 492 "Score %u with %" PRIu64 " (#%u)\n",
497 tmp_score, pow_val, i); 493 tmp_score, pow_val, i);
494
498 score += tmp_score; 495 score += tmp_score;
499 496
500 } 497 }
@@ -580,9 +577,14 @@ GNUNET_REVOCATION_pow_start (struct GNUNET_REVOCATION_PowP *pow,
580 unsigned int difficulty) 577 unsigned int difficulty)
581{ 578{
582 struct GNUNET_REVOCATION_PowCalculationHandle *pc; 579 struct GNUNET_REVOCATION_PowCalculationHandle *pc;
580 struct GNUNET_TIME_Relative ttl;
581
583 582
584 pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle); 583 pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle);
585 pc->pow = pow; 584 pc->pow = pow;
585 ttl = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_YEARS,
586 epochs);
587 pc->pow->ttl = GNUNET_TIME_relative_hton (ttl);
586 pc->current_pow = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 588 pc->current_pow = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
587 UINT64_MAX); 589 UINT64_MAX);
588 pc->difficulty = difficulty; 590 pc->difficulty = difficulty;
@@ -590,6 +592,18 @@ GNUNET_REVOCATION_pow_start (struct GNUNET_REVOCATION_PowP *pow,
590 return pc; 592 return pc;
591} 593}
592 594
595/**
596 * Comparison function for quicksort
597 *
598 * @param a left element
599 * @param b right element
600 * @return a-b
601 */
602static int
603cmp_pow_value (const void *a, const void *b)
604{
605 return ( GNUNET_ntohll(*(uint64_t*)a) - GNUNET_ntohll(*(uint64_t*)b));
606}
593 607
594/** 608/**
595 * Calculate a key revocation valid for broadcasting for a number 609 * Calculate a key revocation valid for broadcasting for a number
@@ -609,6 +623,8 @@ GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc)
609 + sizeof (uint64_t)] GNUNET_ALIGN; 623 + sizeof (uint64_t)] GNUNET_ALIGN;
610 struct GNUNET_HashCode result; 624 struct GNUNET_HashCode result;
611 unsigned int zeros; 625 unsigned int zeros;
626 int ret;
627 uint64_t pow_nbo;
612 628
613 pc->current_pow++; 629 pc->current_pow++;
614 630
@@ -618,15 +634,15 @@ GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc)
618 for (unsigned int i = 0; i < POW_COUNT; i++) 634 for (unsigned int i = 0; i < POW_COUNT; i++)
619 if (pc->current_pow == pc->best[i].pow) 635 if (pc->current_pow == pc->best[i].pow)
620 return GNUNET_NO; 636 return GNUNET_NO;
621 637 pow_nbo = GNUNET_htonll (pc->current_pow);
622 GNUNET_memcpy (buf, &pc->current_pow, sizeof(uint64_t)); 638 GNUNET_memcpy (buf, &pow_nbo, sizeof(uint64_t));
623 GNUNET_memcpy (&buf[sizeof(uint64_t)], 639 GNUNET_memcpy (&buf[sizeof(uint64_t)],
624 &pc->pow->timestamp, 640 &pc->pow->timestamp,
625 sizeof (uint64_t)); 641 sizeof (uint64_t));
626 GNUNET_memcpy (&buf[sizeof(uint64_t) * 2], 642 GNUNET_memcpy (&buf[sizeof(uint64_t) * 2],
627 &pc->pow->key, 643 &pc->pow->key,
628 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)); 644 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
629 GNUNET_CRYPTO_pow_hash ("gnunet-revocation-proof-of-work", 645 GNUNET_CRYPTO_pow_hash ("GnsRevocationPow",
630 buf, 646 buf,
631 sizeof(buf), 647 sizeof(buf),
632 &result); 648 &result);
@@ -637,15 +653,22 @@ GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc)
637 { 653 {
638 pc->best[i].bits = zeros; 654 pc->best[i].bits = zeros;
639 pc->best[i].pow = pc->current_pow; 655 pc->best[i].pow = pc->current_pow;
640 pc->pow->pow[i] = GNUNET_htonll (pc->current_pow); 656 pc->pow->pow[i] = pow_nbo;
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "New best score %u with %" PRIu64 " (#%u)\n", 658 "New best score %u with %" PRIu64 " (#%u)\n",
643 zeros, pc->current_pow, i); 659 zeros, pc->current_pow, i);
660
644 break; 661 break;
645 } 662 }
646 } 663 }
647 return calculate_score (pc) >= pc->difficulty + pc->epochs ? GNUNET_YES : 664 ret = calculate_score (pc) >= pc->difficulty + pc->epochs ? GNUNET_YES :
648 GNUNET_NO; 665 GNUNET_NO;
666 if (GNUNET_YES == ret)
667 {
668 /* Sort POWs) */
669 qsort (pc->pow->pow, POW_COUNT, sizeof (uint64_t), &cmp_pow_value);
670 }
671 return ret;
649} 672}
650 673
651 674