#include "common/bytes.h"
#include <stdio.h>
void bytes_from_uint64(uint8_t out[8], uint64_t n) {
for (int i = 7; i >= 0; i--) {
out[i] = n & 0xFF;
n >>= 8;
}
}
void bytes_from_g1(Bytes48 *out, const g1_t *in) {
blst_p1_compress(out->bytes, in);
}
void bytes_from_bls_field(Bytes32 *out, const fr_t *in) {
blst_scalar s;
blst_scalar_from_fr(&s, in);
blst_bendian_from_scalar(out->bytes, &s);
}
C_KZG_RET bytes_to_bls_field(fr_t *out, const Bytes32 *b) {
blst_scalar tmp;
blst_scalar_from_bendian(&tmp, b->bytes);
if (!blst_scalar_fr_check(&tmp)) return C_KZG_BADARGS;
blst_fr_from_scalar(out, &tmp);
return C_KZG_OK;
}
static C_KZG_RET validate_kzg_g1(g1_t *out, const Bytes48 *b) {
blst_p1_affine p1_affine;
if (blst_p1_uncompress(&p1_affine, b->bytes) != BLST_SUCCESS) return C_KZG_BADARGS;
blst_p1_from_affine(out, &p1_affine);
if (blst_p1_is_inf(out)) return C_KZG_OK;
if (!blst_p1_in_g1(out)) return C_KZG_BADARGS;
return C_KZG_OK;
}
C_KZG_RET bytes_to_kzg_commitment(g1_t *out, const Bytes48 *b) {
return validate_kzg_g1(out, b);
}
C_KZG_RET bytes_to_kzg_proof(g1_t *out, const Bytes48 *b) {
return validate_kzg_g1(out, b);
}
void hash_to_bls_field(fr_t *out, const Bytes32 *b) {
blst_scalar tmp;
blst_scalar_from_bendian(&tmp, b->bytes);
blst_fr_from_scalar(out, &tmp);
}
void print_bytes32(const Bytes32 *bytes) {
for (size_t i = 0; i < 32; i++) {
printf("%02x", bytes->bytes[i]);
}
printf("\n");
}
void print_bytes48(const Bytes48 *bytes) {
for (size_t i = 0; i < 48; i++) {
printf("%02x", bytes->bytes[i]);
}
printf("\n");
}