GNUnet 0.22.2
gnsrecord_pow.c File Reference
#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_util_lib.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_signatures.h"
#include <inttypes.h>
#include "gnsrecord_crypto.h"
Include dependency graph for gnsrecord_pow.c:

Go to the source code of this file.

Data Structures

struct  BestPow
 API for proof of work. More...
 
struct  GNUNET_GNSRECORD_PowCalculationHandle
 The handle to a PoW calculation. More...
 

Functions

static unsigned int calculate_score (const struct GNUNET_GNSRECORD_PowCalculationHandle *ph)
 Calculate the average zeros in the pows. More...
 
struct GNUNET_GNSRECORD_SignaturePurposePSGNR_create_signature_message (const struct GNUNET_GNSRECORD_PowP *pow)
 Create the revocation metadata to sign for a revocation message. More...
 
static enum GNUNET_GenericReturnValue check_signature_identity (const struct GNUNET_GNSRECORD_PowP *pow, const struct GNUNET_CRYPTO_PublicKey *key)
 
static enum GNUNET_GenericReturnValue check_signature (const struct GNUNET_GNSRECORD_PowP *pow)
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_check_pow (const struct GNUNET_GNSRECORD_PowP *pow, unsigned int difficulty, struct GNUNET_TIME_Relative epoch_duration)
 Check if the given proof-of-work is valid. More...
 
static enum GNUNET_GenericReturnValue sign_pow_identity (const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
 
static enum GNUNET_GenericReturnValue sign_pow (const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
 
void GNUNET_GNSRECORD_pow_init (const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
 Initializes a fresh PoW computation. More...
 
struct GNUNET_GNSRECORD_PowCalculationHandleGNUNET_GNSRECORD_pow_start (struct GNUNET_GNSRECORD_PowP *pow, int epochs, unsigned int difficulty)
 Starts a proof-of-work calculation given the pow object as well as target epochs and difficulty. More...
 
static int cmp_pow_value (const void *a, const void *b)
 Comparison function for quicksort. More...
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_pow_round (struct GNUNET_GNSRECORD_PowCalculationHandle *pc)
 Calculate a key revocation valid for broadcasting for a number of epochs. More...
 
size_t GNUNET_GNSRECORD_proof_get_size (const struct GNUNET_GNSRECORD_PowP *pow)
 
void GNUNET_GNSRECORD_pow_stop (struct GNUNET_GNSRECORD_PowCalculationHandle *pc)
 Stop a PoW calculation. More...
 

Variables

static struct GNUNET_CRYPTO_PowSalt salt = { "GnsRevocationPow" }
 

Function Documentation

◆ calculate_score()

static unsigned int calculate_score ( const struct GNUNET_GNSRECORD_PowCalculationHandle ph)
static

Calculate the average zeros in the pows.

Parameters
phthe PowHandle
Returns
the average number of zeros.

Definition at line 93 of file gnsrecord_pow.c.

94{
95 double sum = 0.0;
96 for (unsigned int j = 0; j<POW_COUNT; j++)
97 sum += ph->best[j].bits;
98 return sum / POW_COUNT;
99}
#define POW_COUNT
The proof-of-work narrowing factor.
unsigned int bits
Corresponding zero bits in hash.
Definition: gnsrecord_pow.c:46
struct BestPow best[POW_COUNT]
Current set of found PoWs.
Definition: gnsrecord_pow.c:59

References GNUNET_GNSRECORD_PowCalculationHandle::best, BestPow::bits, POW_COUNT, and consensus-simulation::sum.

Referenced by GNUNET_GNSRECORD_pow_round().

Here is the caller graph for this function:

◆ GNR_create_signature_message()

struct GNUNET_GNSRECORD_SignaturePurposePS * GNR_create_signature_message ( const struct GNUNET_GNSRECORD_PowP pow)

Create the revocation metadata to sign for a revocation message.

Parameters
powthe PoW to sign
Returns
the signature purpose

Definition at line 103 of file gnsrecord_pow.c.

104{
106 const struct GNUNET_CRYPTO_PublicKey *pk;
107 size_t ksize;
108
109 pk = (const struct GNUNET_CRYPTO_PublicKey *) &pow[1];
111 spurp = GNUNET_malloc (sizeof (*spurp) + ksize);
112 spurp->timestamp = pow->timestamp;
114 spurp->purpose.size = htonl (sizeof(*spurp) + ksize);
116 (char*) &spurp[1],
117 ksize);
118 return spurp;
119}
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
ssize_t GNUNET_CRYPTO_public_key_get_length(const struct GNUNET_CRYPTO_PublicKey *key)
Get the compacted length of a GNUNET_CRYPTO_PublicKey.
Definition: crypto_pkey.c:85
ssize_t GNUNET_CRYPTO_write_public_key_to_buffer(const struct GNUNET_CRYPTO_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_PublicKey to a compact buffer.
Definition: crypto_pkey.c:128
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_SIGNATURE_PURPOSE_GNS_REVOCATION
GNS zone key revocation (GNS)
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
An identity key as per LSD0001.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the revocation.
The signature object we use for the PoW.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the revocation.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
The signature purpose.

References GNUNET_CRYPTO_public_key_get_length(), GNUNET_CRYPTO_write_public_key_to_buffer(), GNUNET_malloc, GNUNET_SIGNATURE_PURPOSE_GNS_REVOCATION, pk, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_GNSRECORD_SignaturePurposePS::purpose, GNUNET_CRYPTO_EccSignaturePurpose::size, GNUNET_GNSRECORD_PowP::timestamp, and GNUNET_GNSRECORD_SignaturePurposePS::timestamp.

Referenced by check_signature_identity(), run_with_key(), and sign_pow_identity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_signature_identity()

static enum GNUNET_GenericReturnValue check_signature_identity ( const struct GNUNET_GNSRECORD_PowP pow,
const struct GNUNET_CRYPTO_PublicKey key 
)
static

Definition at line 123 of file gnsrecord_pow.c.

125{
127 unsigned char *sig;
128 size_t ksize;
129 int ret;
130
132 spurp = GNR_create_signature_message (pow);
133 sig = ((unsigned char*) &pow[1] + ksize);
134 ret =
137 &spurp->purpose,
138 sig,
139 key);
140 GNUNET_free (spurp);
141 return ret == GNUNET_OK ? GNUNET_OK : GNUNET_SYSERR;
142}
struct GNUNET_GNSRECORD_SignaturePurposePS * GNR_create_signature_message(const struct GNUNET_GNSRECORD_PowP *pow)
Create the revocation metadata to sign for a revocation message.
static int ret
Final status code.
Definition: gnunet-arm.c:93
struct GNUNET_HashCode key
The key used in the DHT.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_signature_verify_raw_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const unsigned char *sig, const struct GNUNET_CRYPTO_PublicKey *pub)
Verify a given signature.
Definition: crypto_pkey.c:348
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_free(ptr)
Wrapper around free.

References GNR_create_signature_message(), GNUNET_CRYPTO_public_key_get_length(), GNUNET_CRYPTO_signature_verify_raw_(), GNUNET_free, GNUNET_OK, GNUNET_SIGNATURE_PURPOSE_GNS_REVOCATION, GNUNET_SYSERR, key, GNUNET_GNSRECORD_SignaturePurposePS::purpose, and ret.

Referenced by check_signature().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_signature()

static enum GNUNET_GenericReturnValue check_signature ( const struct GNUNET_GNSRECORD_PowP pow)
static

Definition at line 146 of file gnsrecord_pow.c.

147{
148 const struct GNUNET_CRYPTO_PublicKey *pk;
149
150 pk = (const struct GNUNET_CRYPTO_PublicKey *) &pow[1];
151 return check_signature_identity (pow, pk);
152}
static enum GNUNET_GenericReturnValue check_signature_identity(const struct GNUNET_GNSRECORD_PowP *pow, const struct GNUNET_CRYPTO_PublicKey *key)

References check_signature_identity(), and pk.

Referenced by GNUNET_GNSRECORD_check_pow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign_pow_identity()

static enum GNUNET_GenericReturnValue sign_pow_identity ( const struct GNUNET_CRYPTO_PrivateKey key,
struct GNUNET_GNSRECORD_PowP pow 
)
static

Predate the validity period to prevent rejections due to unsynchronized clocks

Definition at line 266 of file gnsrecord_pow.c.

268{
271 const struct GNUNET_CRYPTO_PublicKey *pk;
272 size_t ksize;
273 char *sig;
275
282 pk = (const struct GNUNET_CRYPTO_PublicKey *) &pow[1];
286 sig = ((char*) &pow[1]) + ksize;
288 &rp->purpose,
289 (void*) sig);
290 GNUNET_free (rp);
291 if (result == GNUNET_SYSERR)
292 return GNUNET_NO;
293 else
294 return result;
295}
static char * rp
Relying party.
static int result
Global testing status.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_raw_(const struct GNUNET_CRYPTO_PrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, unsigned char *sig)
Sign a given block.
Definition: crypto_pkey.c:266
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_NO
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
#define GNUNET_TIME_UNIT_WEEKS
One week.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Subtract a given relative duration from the given start time.
Definition: time.c:471
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
Time for absolute times used by GNUnet, in microseconds.

References GNR_create_signature_message(), GNUNET_CRYPTO_public_key_get_length(), GNUNET_CRYPTO_sign_raw_(), GNUNET_free, GNUNET_NO, GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_hton(), GNUNET_TIME_absolute_subtract(), GNUNET_TIME_UNIT_WEEKS, key, pk, result, rp, and GNUNET_GNSRECORD_PowP::timestamp.

Referenced by sign_pow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign_pow()

static enum GNUNET_GenericReturnValue sign_pow ( const struct GNUNET_CRYPTO_PrivateKey key,
struct GNUNET_GNSRECORD_PowP pow 
)
static

Definition at line 299 of file gnsrecord_pow.c.

301{
303
304 pk = (struct GNUNET_CRYPTO_PublicKey *) &pow[1];
306 return sign_pow_identity (key, pow);
307}
static enum GNUNET_GenericReturnValue sign_pow_identity(const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_key_get_public(const struct GNUNET_CRYPTO_PrivateKey *privkey, struct GNUNET_CRYPTO_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: crypto_pkey.c:430

References GNUNET_CRYPTO_key_get_public(), key, pk, and sign_pow_identity().

Referenced by GNUNET_GNSRECORD_pow_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmp_pow_value()

static int cmp_pow_value ( const void *  a,
const void *  b 
)
static

Comparison function for quicksort.

Parameters
aleft element
bright element
Returns
a-b

Definition at line 354 of file gnsrecord_pow.c.

355{
356 return (GNUNET_ntohll (*(uint64_t*) a) - GNUNET_ntohll (*(uint64_t*) b));
357}
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:54

References GNUNET_ntohll().

Referenced by GNUNET_GNSRECORD_pow_round().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ salt

struct GNUNET_CRYPTO_PowSalt salt = { "GnsRevocationPow" }
static

Definition at line 84 of file gnsrecord_pow.c.

Referenced by GNUNET_GNSRECORD_check_pow(), and GNUNET_GNSRECORD_pow_round().