Class: MLKEM::Crypto::SymmetricPrimitives

Inherits:
Object
  • Object
show all
Defined in:
lib/ml_kem/crypto/symmetric_primitives.rb

Overview

Provides symmetric cryptographic primitives as defined by ML-KEM (Kyber), including hash functions h, g, j and a pseudorandom function (PRF).

These are used for key derivation, random generation, and message hashing.

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.g(x) ⇒ Array<String>

Hash function g: SHA3-512, split into two 32-byte outputs.

Examples:

g1, g2 = SymmetricPrimitives.g("seed")

Parameters:

  • x (String)

    Input data to hash.

Returns:

  • (Array<String>)

    An array containing two 32-byte strings.

Since:

  • 0.1.0



33
34
35
36
# File 'lib/ml_kem/crypto/symmetric_primitives.rb', line 33

def g(x)
  hash = HashFunctions.sha3_512(x)
  [hash[0...32], hash[32...64]]
end

.h(x) ⇒ String

Hash function h: SHA3-256

Examples:

digest = SymmetricPrimitives.h("message")

Parameters:

  • x (String)

    Input data to hash.

Returns:

  • (String)

    32-byte digest.

Since:

  • 0.1.0



22
23
24
# File 'lib/ml_kem/crypto/symmetric_primitives.rb', line 22

def h(x)
  HashFunctions.sha3_256(x)
end

.j(s) ⇒ String

Hash function j: SHAKE256 with 32-byte output.

Examples:

result = SymmetricPrimitives.j("domain")

Parameters:

  • s (String)

    Input data to hash.

Returns:

  • (String)

    32-byte XOF output.

Since:

  • 0.1.0



45
46
47
# File 'lib/ml_kem/crypto/symmetric_primitives.rb', line 45

def j(s)
  HashFunctions.shake256(s, 32)
end

.prf(eta, s, b) ⇒ String

Pseudorandom Function (PRF)

Uses SHAKE256 to expand ‘s || b` into `eta * 64` bytes of pseudorandom data.

Examples:

prf_output = SymmetricPrimitives.prf(3, "key", 0x01)

Parameters:

  • eta (Integer)

    Security parameter (e.g., 2 or 3).

  • s (String)

    Secret seed.

  • b (Integer)

    A single byte to include in domain separation.

Returns:

  • (String)

    Pseudorandom byte string of length ‘eta * 64`.

Since:

  • 0.1.0



60
61
62
63
# File 'lib/ml_kem/crypto/symmetric_primitives.rb', line 60

def prf(eta, s, b)
  input = s + [b].pack('C*')
  HashFunctions.shake256(input, eta * 64)
end