packages feed

cardano-c-clib-0.1.1.0: vendor/src/witness_set/vkey_witness_set.c

/**
 * \file vkey_witness_set.c
 *
 * \author angel.castillo
 * \date   Sep 22, 2024
 *
 * Copyright 2024 Biglup Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* INCLUDES ******************************************************************/

#include <cardano/object.h>
#include <cardano/witness_set/vkey_witness_set.h>

#include "../allocators.h"
#include "../cbor/cbor_validation.h"
#include "../collections/array.h"

#include <assert.h>
#include <string.h>

/* STRUCTURES ****************************************************************/

/**
 * \brief Represents a Cardano vkey_witness_set list.
 */
typedef struct cardano_vkey_witness_set_t
{
    cardano_object_t base;
    cardano_array_t* array;
    bool             uses_tags;
    bool             preserves_duplicates;
} cardano_vkey_witness_set_t;

/* STATIC FUNCTIONS **********************************************************/

/**
 * \brief Deallocates a vkey_witness_set list object.
 *
 * This function is responsible for properly deallocating a vkey_witness_set list object (`cardano_vkey_witness_set_t`)
 * and its associated resources.
 *
 * \param object A void pointer to the vkey_witness_set object to be deallocated. The function casts this
 *               pointer to the appropriate type (`cardano_vkey_witness_set_t*`).
 *
 * \note It is assumed that this function is called only when the reference count of the vkey_witness_set
 *       object reaches zero, as part of the reference counting mechanism implemented for managing the
 *       lifecycle of these objects.
 */
static void
cardano_vkey_witness_set_deallocate(void* object)
{
  assert(object != NULL);

  cardano_vkey_witness_set_t* list = (cardano_vkey_witness_set_t*)object;

  if (list->array != NULL)
  {
    cardano_array_unref(&list->array);
  }

  _cardano_free(list);
}

/**
 * \brief Determines whether two vkey witnesses are the same witness.
 *
 * Two vkey witnesses are the same witness when they have the same public key and the same signature. Witnesses
 * with the same public key and different signatures are different witnesses.
 *
 * \param lhs The first vkey witness to compare.
 * \param rhs The second vkey witness to compare.
 * \return true if both witnesses have the same public key and the same signature, false otherwise.
 */
static bool
is_same_witness(cardano_vkey_witness_t* lhs, cardano_vkey_witness_t* rhs)
{
  cardano_ed25519_public_key_t* vkey    = cardano_vkey_witness_get_vkey(rhs);
  cardano_ed25519_signature_t*  lhs_sig = cardano_vkey_witness_get_signature(lhs);
  cardano_ed25519_signature_t*  rhs_sig = cardano_vkey_witness_get_signature(rhs);

  cardano_ed25519_public_key_unref(&vkey);
  cardano_ed25519_signature_unref(&lhs_sig);
  cardano_ed25519_signature_unref(&rhs_sig);

  if ((vkey == NULL) || (lhs_sig == NULL) || (rhs_sig == NULL) || !cardano_vkey_witness_has_public_key(lhs, vkey))
  {
    return false;
  }

  const size_t lhs_size = cardano_ed25519_signature_get_bytes_size(lhs_sig);
  const size_t rhs_size = cardano_ed25519_signature_get_bytes_size(rhs_sig);

  return (lhs_size == rhs_size) && (memcmp(cardano_ed25519_signature_get_data(lhs_sig), cardano_ed25519_signature_get_data(rhs_sig), lhs_size) == 0);
}

/**
 * \brief Determines whether the witness at the given index is repeated later in the vkey witness set.
 *
 * \param vkey_witness_set The vkey witness set to search.
 * \param index The index of the witness to look for.
 * \return true if a witness after \p index is the same witness, false otherwise.
 */
static bool
is_repeated_later(const cardano_vkey_witness_set_t* vkey_witness_set, const size_t index)
{
  cardano_object_t* item  = cardano_array_get(vkey_witness_set->array, index);
  bool              found = false;

  for (size_t i = index + 1U; (i < cardano_array_get_size(vkey_witness_set->array)) && !found; ++i)
  {
    cardano_object_t* other = cardano_array_get(vkey_witness_set->array, i);

    found = is_same_witness((cardano_vkey_witness_t*)((void*)item), (cardano_vkey_witness_t*)((void*)other));

    cardano_object_unref(&other);
  }

  cardano_object_unref(&item);

  return found;
}

/**
 * \brief Determines whether the witness at the given index is part of the encoding of the vkey witness set.
 *
 * A vkey witness set decoded from CBOR encodes every witness it was decoded with. Otherwise, a witness repeated
 * later in the set is left out, so that the encoding never holds the same witness twice and the last occurrence of
 * each witness keeps its position.
 *
 * \param vkey_witness_set The vkey witness set being encoded.
 * \param index The index of the witness.
 * \return true if the witness is written when the set is encoded, false otherwise.
 */
static bool
is_encoded(const cardano_vkey_witness_set_t* vkey_witness_set, const size_t index)
{
  return vkey_witness_set->preserves_duplicates || !is_repeated_later(vkey_witness_set, index);
}

/* DEFINITIONS ****************************************************************/

cardano_error_t
cardano_vkey_witness_set_new(cardano_vkey_witness_set_t** vkey_witness_set)
{
  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_vkey_witness_set_t* list = _cardano_malloc(sizeof(cardano_vkey_witness_set_t));

  if (list == NULL)
  {
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  list->base.ref_count       = 1;
  list->base.last_error[0]   = '\0';
  list->base.deallocator     = cardano_vkey_witness_set_deallocate;
  list->uses_tags            = true;
  list->preserves_duplicates = false;

  list->array = cardano_array_new(128);

  if (list->array == NULL)
  {
    _cardano_free(list);
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  *vkey_witness_set = list;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_vkey_witness_set_from_cbor(cardano_cbor_reader_t* reader, cardano_vkey_witness_set_t** vkey_witness_set)
{
  if (reader == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_vkey_witness_set_t* list   = NULL;
  cardano_error_t             result = cardano_vkey_witness_set_new(&list);

  if (result != CARDANO_SUCCESS)
  {
    return result;
  }

  cardano_cbor_reader_state_t state;

  if (cardano_cbor_reader_peek_state(reader, &state) != CARDANO_SUCCESS)
  {
    cardano_vkey_witness_set_unref(&list);
    return CARDANO_ERROR_DECODING;
  }

  list->uses_tags            = (state == CARDANO_CBOR_READER_STATE_TAG);
  list->preserves_duplicates = true;

  if (state == CARDANO_CBOR_READER_STATE_TAG)
  {
    const cardano_error_t read_tag_result = cardano_cbor_validate_tag("vkey_witness_set", reader, CARDANO_CBOR_TAG_SET);

    if (read_tag_result != CARDANO_SUCCESS)
    {
      cardano_vkey_witness_set_unref(&list);
      return read_tag_result;
    }
  }

  int64_t length = 0;
  result         = cardano_cbor_reader_read_start_array(reader, &length);

  if (result != CARDANO_SUCCESS)
  {
    cardano_vkey_witness_set_unref(&list);
    return result;
  }

  state = CARDANO_CBOR_READER_STATE_UNDEFINED;

  while (state != CARDANO_CBOR_READER_STATE_END_ARRAY)
  {
    result = cardano_cbor_reader_peek_state(reader, &state);

    if (result != CARDANO_SUCCESS)
    {
      cardano_vkey_witness_set_unref(&list);
      return result;
    }

    if (state == CARDANO_CBOR_READER_STATE_END_ARRAY)
    {
      break;
    }

    cardano_vkey_witness_t* element = NULL;

    result = cardano_vkey_witness_from_cbor(reader, &element);

    if (result != CARDANO_SUCCESS)
    {
      cardano_vkey_witness_set_unref(&list);
      return result;
    }

    const size_t old_size = cardano_array_get_size(list->array);
    const size_t new_size = cardano_array_push(list->array, (cardano_object_t*)((void*)element));

    cardano_vkey_witness_unref(&element);

    if ((old_size + 1U) != new_size)
    {
      cardano_vkey_witness_set_unref(&list);
      return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
    }
  }

  result = cardano_cbor_validate_end_array("vkey_witness_set", reader);

  if (result != CARDANO_SUCCESS)
  {
    cardano_vkey_witness_set_unref(&list);
    return result;
  }

  *vkey_witness_set = list;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_vkey_witness_set_to_cbor(const cardano_vkey_witness_set_t* vkey_witness_set, cardano_cbor_writer_t* writer)
{
  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (writer == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  assert(vkey_witness_set->array != NULL);

  cardano_error_t result = CARDANO_SUCCESS;

  if (vkey_witness_set->uses_tags)
  {
    result = cardano_cbor_writer_write_tag(writer, CARDANO_CBOR_TAG_SET);

    if (result != CARDANO_SUCCESS)
    {
      return result;
    }
  }

  const size_t array_size    = cardano_array_get_size(vkey_witness_set->array);
  size_t       encoded_count = 0U;

  for (size_t i = 0U; i < array_size; ++i)
  {
    if (is_encoded(vkey_witness_set, i))
    {
      ++encoded_count;
    }
  }

  result = cardano_cbor_writer_write_start_array(writer, (int64_t)encoded_count);

  if (result != CARDANO_SUCCESS)
  {
    return result;
  }

  for (size_t i = 0U; i < array_size; ++i)
  {
    cardano_object_t* element = cardano_array_get(vkey_witness_set->array, i);

    if (element == NULL)
    {
      cardano_cbor_writer_set_last_error(writer, "Element in vkey_witness_set list is NULL");
      return CARDANO_ERROR_ENCODING;
    }

    if (is_encoded(vkey_witness_set, i))
    {
      result = cardano_vkey_witness_to_cbor((cardano_vkey_witness_t*)((void*)element), writer);
    }

    cardano_object_unref(&element);

    if (result != CARDANO_SUCCESS)
    {
      return result;
    }
  }

  return result;
}

cardano_error_t
cardano_vkey_witness_set_to_cip116_json(
  const cardano_vkey_witness_set_t* witnesses,
  cardano_json_writer_t*            writer)
{
  if ((witnesses == NULL) || (writer == NULL))
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_json_writer_write_start_array(writer);

  size_t          length = cardano_vkey_witness_set_get_length(witnesses);
  cardano_error_t error  = CARDANO_SUCCESS;

  for (size_t i = 0U; i < length; ++i)
  {
    cardano_vkey_witness_t* witness = NULL;
    error                           = cardano_vkey_witness_set_get(witnesses, i, &witness);

    if (error != CARDANO_SUCCESS)
    {
      return error;
    }

    error = cardano_vkey_witness_to_cip116_json(witness, writer);
    cardano_vkey_witness_unref(&witness);

    if (error != CARDANO_SUCCESS)
    {
      return error;
    }
  }

  cardano_json_writer_write_end_array(writer);

  return error;
}

size_t
cardano_vkey_witness_set_get_length(const cardano_vkey_witness_set_t* vkey_witness_set)
{
  if (vkey_witness_set == NULL)
  {
    return 0;
  }

  return cardano_array_get_size(vkey_witness_set->array);
}

cardano_error_t
cardano_vkey_witness_set_get(
  const cardano_vkey_witness_set_t* vkey_witness_set,
  size_t                            index,
  cardano_vkey_witness_t**          element)
{
  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (element == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_object_t* object = cardano_array_get(vkey_witness_set->array, index);

  if (object == NULL)
  {
    return CARDANO_ERROR_OUT_OF_BOUNDS_MEMORY_READ;
  }

  *element = (cardano_vkey_witness_t*)((void*)object);

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_vkey_witness_set_add(cardano_vkey_witness_set_t* vkey_witness_set, cardano_vkey_witness_t* element)
{
  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (element == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_ed25519_public_key_t* vkey = cardano_vkey_witness_get_vkey(element);
  cardano_ed25519_signature_t*  sig  = cardano_vkey_witness_get_signature(element);

  cardano_ed25519_signature_unref(&sig);
  cardano_ed25519_public_key_unref(&vkey);

  if ((vkey == NULL) || (sig == NULL))
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  bool found = false;

  for (size_t i = 0U; i < cardano_array_get_size(vkey_witness_set->array); ++i)
  {
    cardano_object_t* item = cardano_array_get(vkey_witness_set->array, i);

    cardano_vkey_witness_t* witness = (cardano_vkey_witness_t*)((void*)item);
    cardano_object_unref(&item);

    if (cardano_vkey_witness_has_public_key(witness, vkey))
    {
      cardano_error_t result = cardano_vkey_witness_set_signature(witness, sig);

      if (result != CARDANO_SUCCESS)
      {
        return result;
      }

      found = true;
    }
  }

  if (!found)
  {
    const size_t original_size = cardano_array_get_size(vkey_witness_set->array);
    const size_t new_size      = cardano_array_push(vkey_witness_set->array, (cardano_object_t*)((void*)element));

    if (new_size != (original_size + 1U))
    {
      return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
    }
  }

  vkey_witness_set->preserves_duplicates = false;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_vkey_witness_set_apply(
  cardano_vkey_witness_set_t* vkey_witness_set,
  cardano_vkey_witness_set_t* new_vkey_witnesses)
{
  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (new_vkey_witnesses == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  for (size_t i = 0; i < cardano_array_get_size(new_vkey_witnesses->array); ++i)
  {
    cardano_vkey_witness_t* element = NULL;

    cardano_error_t result = cardano_vkey_witness_set_get(new_vkey_witnesses, i, &element);
    cardano_vkey_witness_unref(&element);

    if (result != CARDANO_SUCCESS)
    {
      return result;
    }

    result = cardano_vkey_witness_set_add(vkey_witness_set, element);

    if (result != CARDANO_SUCCESS)
    {
      return result;
    }
  }

  return CARDANO_SUCCESS;
}

bool
cardano_vkey_witness_set_get_use_tag(const cardano_vkey_witness_set_t* vkey_witness_set)
{
  if (vkey_witness_set == NULL)
  {
    return false;
  }

  return vkey_witness_set->uses_tags;
}

cardano_error_t
cardano_vkey_witness_set_set_use_tag(cardano_vkey_witness_set_t* vkey_witness_set, const bool use_tag)
{
  if (vkey_witness_set == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  vkey_witness_set->uses_tags = use_tag;

  return CARDANO_SUCCESS;
}

void
cardano_vkey_witness_set_clear_cbor_cache(cardano_vkey_witness_set_t* vkey_witness_set)
{
  if (vkey_witness_set == NULL)
  {
    return;
  }

  vkey_witness_set->uses_tags            = true;
  vkey_witness_set->preserves_duplicates = false;
}

void
cardano_vkey_witness_set_unref(cardano_vkey_witness_set_t** vkey_witness_set)
{
  if ((vkey_witness_set == NULL) || (*vkey_witness_set == NULL))
  {
    return;
  }

  cardano_object_t* object = &(*vkey_witness_set)->base;
  cardano_object_unref(&object);

  if (object == NULL)
  {
    *vkey_witness_set = NULL;
    return;
  }
}

void
cardano_vkey_witness_set_ref(cardano_vkey_witness_set_t* vkey_witness_set)
{
  if (vkey_witness_set == NULL)
  {
    return;
  }

  cardano_object_ref(&vkey_witness_set->base);
}

size_t
cardano_vkey_witness_set_refcount(const cardano_vkey_witness_set_t* vkey_witness_set)
{
  if (vkey_witness_set == NULL)
  {
    return 0;
  }

  return cardano_object_refcount(&vkey_witness_set->base);
}

void
cardano_vkey_witness_set_set_last_error(cardano_vkey_witness_set_t* vkey_witness_set, const char* message)
{
  cardano_object_set_last_error(&vkey_witness_set->base, message);
}

const char*
cardano_vkey_witness_set_get_last_error(const cardano_vkey_witness_set_t* vkey_witness_set)
{
  return cardano_object_get_last_error(&vkey_witness_set->base);
}

/**
 * Deep copies one element for the typed collection.
 */
static cardano_error_t
clone_array_element(const cardano_object_t* source, cardano_object_t** clone)
{
  cardano_vkey_witness_t* copy   = NULL;
  const cardano_error_t   result = cardano_vkey_witness_deep_clone((const cardano_vkey_witness_t*)((const void*)source), &copy);

  *clone = (cardano_object_t*)((void*)copy);

  return result;
}

cardano_error_t
cardano_vkey_witness_set_deep_clone(const cardano_vkey_witness_set_t* source, cardano_vkey_witness_set_t** clone)
{
  if (clone == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  *clone = NULL;

  if (source == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_vkey_witness_set_t* copy = _cardano_malloc(sizeof(cardano_vkey_witness_set_t));

  if (copy == NULL)
  {
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  copy->base.ref_count       = 1U;
  copy->base.deallocator     = source->base.deallocator;
  copy->base.last_error[0]   = '\0';
  copy->array                = NULL;
  copy->uses_tags            = source->uses_tags;
  copy->preserves_duplicates = source->preserves_duplicates;

  if (source->array != NULL)
  {
    const cardano_error_t result = cardano_array_deep_clone(source->array, clone_array_element, &copy->array);

    if (result != CARDANO_SUCCESS)
    {
      copy->base.deallocator(copy);
      return result;
    }
  }

  *clone = copy;

  return CARDANO_SUCCESS;
}