Skip to content

src/crypto/sr25519_types.hpp

Namespaces

Name
sgns
sgns::crypto
sgns::crypto::constants
sgns::crypto::constants::sr25519
sgns::crypto::constants::sr25519::vrf

Classes

Name
struct sgns::crypto::VRFOutput
struct sgns::crypto::VRFVerifyOutput
struct sgns::crypto::SR25519Keypair

Types

Name
enum @062351302067023307206141235337031057054102362257
enum @045157362313214045075122263133334236152064206063
using std::array< uint8_t, constants::sr25519::vrf::OUTPUT_SIZE > VRFPreOutput
using boost::multiprecision::uint128_t VRFThreshold
using std::array< uint8_t, constants::sr25519::vrf::PROOF_SIZE > VRFProof
using base::Blob< constants::sr25519::SECRET_SIZE > SR25519SecretKey
using base::Blob< constants::sr25519::PUBLIC_SIZE > SR25519PublicKey
using base::Blob< constants::sr25519::SEED_SIZE > SR25519Seed
using base::Blob< constants::sr25519::SIGNATURE_SIZE > SR25519Signature

Functions

Name
template <class Stream ,typename =std::enable_if_t>
Stream &
operator<<(Stream & s, const VRFOutput & o)
outputs object of type VRFOutput to stream
template <class Stream ,typename =std::enable_if_t>
Stream &
operator>>(Stream & s, VRFOutput & o)
decodes object of type VRFOutput from stream

Types Documentation

enum @062351302067023307206141235337031057054102362257

Enumerator Value Description
KEYPAIR_SIZE SR25519_KEYPAIR_SIZE
SECRET_SIZE SR25519_SECRET_SIZE
PUBLIC_SIZE SR25519_PUBLIC_SIZE
SIGNATURE_SIZE SR25519_SIGNATURE_SIZE
SEED_SIZE SR25519_SEED_SIZE

Important constants to deal with sr25519

enum @045157362313214045075122263133334236152064206063

Enumerator Value Description
PROOF_SIZE SR25519_VRF_PROOF_SIZE
OUTPUT_SIZE SR25519_VRF_OUTPUT_SIZE

Important constants to deal with vrf

using VRFPreOutput

using sgns::crypto::VRFPreOutput = 
std::array<uint8_t, constants::sr25519::vrf::OUTPUT_SIZE>;

using VRFThreshold

using sgns::crypto::VRFThreshold = boost::multiprecision::uint128_t;

using VRFProof

using sgns::crypto::VRFProof = std::array<uint8_t, constants::sr25519::vrf::PROOF_SIZE>;

using SR25519SecretKey

using sgns::crypto::SR25519SecretKey = base::Blob<constants::sr25519::SECRET_SIZE>;

using SR25519PublicKey

using sgns::crypto::SR25519PublicKey = base::Blob<constants::sr25519::PUBLIC_SIZE>;

using SR25519Seed

using sgns::crypto::SR25519Seed = base::Blob<constants::sr25519::SEED_SIZE>;

using SR25519Signature

using sgns::crypto::SR25519Signature = base::Blob<constants::sr25519::SIGNATURE_SIZE>;

Functions Documentation

function operator<<

template <class Stream ,
typename  =std::enable_if_t<Stream::is_encoder_stream>>
Stream & operator<<(
    Stream & s,
    const VRFOutput & o
)

outputs object of type VRFOutput to stream

Parameters:

  • s stream reference
  • o value to output

Template Parameters:

  • Stream output stream type

Return: reference to stream

function operator>>

template <class Stream ,
typename  =std::enable_if_t<Stream::is_decoder_stream>>
Stream & operator>>(
    Stream & s,
    VRFOutput & o
)

decodes object of type VRFOutput from stream

Parameters:

  • s stream reference
  • o value to output

Template Parameters:

  • Stream input stream type

Return: reference to stream

Source code

#ifndef SUPERGENIUS_SRC_CRYPTO_VRF_TYPES
#define SUPERGENIUS_SRC_CRYPTO_VRF_TYPES

extern "C" {
#include <sr25519.h>
}
#include <boost/multiprecision/cpp_int.hpp>
#include <gsl/span>

#include "base/blob.hpp"
#include "singleton/IComponent.hpp"

namespace sgns::crypto {
  namespace constants::sr25519 {
    enum {
      KEYPAIR_SIZE = SR25519_KEYPAIR_SIZE,
      SECRET_SIZE = SR25519_SECRET_SIZE,
      PUBLIC_SIZE = SR25519_PUBLIC_SIZE,
      SIGNATURE_SIZE = SR25519_SIGNATURE_SIZE,
      SEED_SIZE = SR25519_SEED_SIZE
    };

    namespace vrf {
      enum {
        PROOF_SIZE = SR25519_VRF_PROOF_SIZE,
        OUTPUT_SIZE = SR25519_VRF_OUTPUT_SIZE
      };
    }  // namespace vrf

  }  // namespace constants::sr25519

  using VRFPreOutput =
      std::array<uint8_t, constants::sr25519::vrf::OUTPUT_SIZE>;
  using VRFThreshold = boost::multiprecision::uint128_t;
  using VRFProof = std::array<uint8_t, constants::sr25519::vrf::PROOF_SIZE>;

  struct VRFOutput {
    // an internal representation of the generated random value
    VRFPreOutput output{};
    // the proof to the output, serves as the verification of its randomness
    VRFProof proof{};

    bool operator==(const VRFOutput &other) const;
    bool operator!=(const VRFOutput &other) const;
    //added to fix gtest link error
    friend std::ostream &operator<<(std::ostream &out, const VRFOutput &test_struct)
    {
      out << test_struct.output.size();
      // for(auto it = test_struct.output.begin(); it != test_struct.output.end() ; it++)
      // {
      //   out << *it ;
      // }

      out << test_struct.proof.size(); 
      // for(auto it = test_struct.proof.begin(); it != test_struct.proof.end() ; it++)
      // {
      //   out << *it ;
      // }  
      return out;
    }
    //end
  };

  struct VRFVerifyOutput {
    // indicates if the proof is valid
    bool is_valid;
    // indicates if the value is less than the provided threshold
    bool is_less;
    //added to fix gtest link error
    friend std::ostream &operator<<(std::ostream &out, const VRFVerifyOutput &test_struct)
    {
      return out << test_struct.is_valid << test_struct.is_less; 
    }
    //end
  };

  using SR25519SecretKey = base::Blob<constants::sr25519::SECRET_SIZE>;

  using SR25519PublicKey = base::Blob<constants::sr25519::PUBLIC_SIZE>;

  using SR25519Seed = base::Blob<constants::sr25519::SEED_SIZE>;

  struct SR25519Keypair : public IComponent {
    SR25519SecretKey secret_key;
    SR25519PublicKey public_key;

    SR25519Keypair() = default;
    SR25519Keypair(SR25519SecretKey sk, SR25519PublicKey pk):  secret_key(sk),public_key(pk) {};

    bool operator==(const SR25519Keypair &other) const;
    bool operator!=(const SR25519Keypair &other) const;

    friend std::ostream &operator<<(std::ostream &out, const SR25519Keypair &test_struct)
    {
      return out << test_struct.secret_key << test_struct.public_key; 
    }
    std::string GetName() override
    {
      return "SR25519Keypair";
    }

  };

  using SR25519Signature = base::Blob<constants::sr25519::SIGNATURE_SIZE>;

  template <class Stream,
            typename = std::enable_if_t<Stream::is_encoder_stream>>
  Stream &operator<<(Stream &s, const VRFOutput &o) {
    return s << o.output << o.proof;
  }

  template <class Stream,
            typename = std::enable_if_t<Stream::is_decoder_stream>>
  Stream &operator>>(Stream &s, VRFOutput &o) {
    return s >> o.output >> o.proof;
  }

}

#endif

Updated on 2026-03-04 at 13:10:44 -0800