Skip to content

Module hb_crypto.erl

Implements the cryptographic functions and wraps the primitives used in HyperBEAM.

Description

Abstracted such that this (extremely!) dangerous code can be carefully managed.

HyperBEAM currently implements two hashpath algorithms:

  • sha-256-chain: A simple chained SHA-256 hash.

  • accumulate-256: A SHA-256 hash that chains the given IDs and accumulates their values into a single commitment.

The accumulate algorithm is experimental and at this point only exists to allow us to test multiple HashPath algorithms in HyperBEAM.

Function Index

accumulate/1Accumulate two IDs, or a list of IDs, into a single commitment.
accumulate/2
count_zeroes/1*Count the number of leading zeroes in a bitstring.
sha256/1Wrap Erlang's crypto:hash/2 to provide a standard interface.
sha256_chain/2Add a new ID to the end of a SHA-256 hash chain.
sha256_chain_test/0*Check that sha-256-chain correctly produces a hash matching the machine's OpenSSL lib's output.

Function Details

accumulate/1

accumulate(IDs) -> any()

Accumulate two IDs, or a list of IDs, into a single commitment. This function requires that the IDs given are already cryptographically-secure, 256-bit values. No further cryptographic operations are performed upon the values, they are simply added together.

This is useful in situations where the ordering of the IDs is not important, or explicitly detrimental to the utility of the final commitment. No ordering information is preserved in the final commitment.

accumulate/2

accumulate(ID1, ID2) -> any()

count_zeroes/1 *

count_zeroes(X1) -> any()

Count the number of leading zeroes in a bitstring.

sha256/1

sha256(Data) -> any()

Wrap Erlang's crypto:hash/2 to provide a standard interface. Under-the-hood, this uses OpenSSL.

sha256_chain/2

sha256_chain(ID1, ID2) -> any()

Add a new ID to the end of a SHA-256 hash chain.

sha256_chain_test/0 *

sha256_chain_test() -> any()

Check that sha-256-chain correctly produces a hash matching the machine's OpenSSL lib's output. Further (in case of a bug in our or Erlang's usage of OpenSSL), check that the output has at least has a high level of entropy.