[C/C++] Memory Management

http://www.linuxjournal.com/article/6390

http://puschitz.com/pblog/?p=28

http://www.nedprod.com/programs/portable/nedmalloc/

Advertisements

Erlang] Message pass mechanism

You have to be very carefully about using message passing. The overhead is much more than you think. Here is the thing:

Comparition version:

A ———–> B
A <———– B

Original version:

A ———–> B
A <———– B
A <———– B

The original version can be almost ten times slower than comparition version!

[Erlang] Diffie-Hellman Key Exchange

Recently I try to write Diffie-Hellman key exchange protocol. And I found that Erlang’s library crypto provide  dh_generate_key & dh_compute_key API.


Types
byte() = 0 ... 255
ioelem() = byte() | binary() | iolist()
iolist() = [ioelem()]
Mpint() = <<ByteLen:32/integer-big, Bytes:ByteLen/binary>>
dh_generate_key(DHParams) -> {PublicKey,PrivateKey}
dh_generate_key(PrivateKey, DHParams) -> {PublicKey,PrivateKey}
  • DHParameters = [P, G]
  • P, G = Mpint Where P is the shared prime number and G is the shared generator.
  • PublicKey, PrivateKey = Mpint()

Generates a Diffie-Hellman PublicKey and PrivateKey (if not given).

dh_compute_key(OthersPublicKey, MyPrivateKey, DHParams) -> SharedSecret
  • DHParameters = [P, G]
  • P, G = Mpint Where P is the shared prime number and G is the shared generator.
  • OthersPublicKey, MyPrivateKey = Mpint()
  • SharedSecret = binary()

Computes the shared secret from the private key and the other party’s public key.

So this is how to use the API.

crypto:start().

% you can use dh_generate_parameters(512, 2) to generate

DHParams = [<<LenP:32, BinP/binary>>, <<LenG:32, BinG/binary>>],

{<<LenPub:32, PubKey/binary>>, PrivKey} =crypto:dh_generate_key(DHParams), SessionKey = crypto:dh_compute_key(<<LenA:32, BinA/binary>>,

PrivKey,  DHParams),

For more information, you can check /usr/local/lib/erlang/lib/crypto-1.6.3/src/crypto.erl