c-kzg-4844/src/debug_util.c

120 lines
2.9 KiB
C
Raw Normal View History

2021-02-01 20:15:45 +00:00
/*
* Copyright 2021 Benjamin Edgington
*
* 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
*
* http://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.
*/
2021-02-03 21:00:14 +00:00
#include "debug_util.h"
2021-02-01 20:15:45 +00:00
//
// General Utilities
//
2021-02-01 20:15:45 +00:00
// Big-endian
2021-02-01 20:15:45 +00:00
void print_bytes_as_hex(byte *bytes, int start, int len) {
for (int i = start; i < start + len; i++) {
printf("%02x", bytes[i]);
}
}
// Little-endian
void print_bytes_as_hex_le(byte *bytes, int start, int len) {
for (int i = start + len - 1; i >= start; i--) {
printf("%02x", bytes[i]);
}
}
//
// Fr utilities
//
// Print a `blst_fr`
void print_fr(const blst_fr *a) {
blst_scalar b;
blst_scalar_from_fr(&b, a);
print_bytes_as_hex_le(b.b, 0, 32);
}
//
// Fp Utilities
//
void print_limbs(const blst_fp *fp) {
printf("(%08lx, %08lx, %08lx, %08lx, %08lx, %08lx)",
fp->l[0], fp->l[1], fp->l[2], fp->l[3], fp->l[4], fp->l[5]);
}
//
// G1 and G2 utilities
//
2021-02-02 23:06:42 +00:00
void print_p1_bytes(byte p1[96]) {
2021-02-01 20:15:45 +00:00
printf("[0x");
2021-02-02 23:06:42 +00:00
print_bytes_as_hex(p1, 0, 48);
2021-02-01 20:15:45 +00:00
printf(",0x");
2021-02-02 23:06:42 +00:00
print_bytes_as_hex(p1, 48, 48);
2021-02-01 20:15:45 +00:00
printf("]\n");
2021-02-02 23:06:42 +00:00
}
/* "Pretty" print serialisation of a point in G1 */
2021-02-02 23:06:42 +00:00
void print_p1(const blst_p1 *p1) {
byte *p1_bytes = (byte *)malloc(96);
blst_p1_serialize(p1_bytes, p1);
print_p1_bytes(p1_bytes);
free(p1_bytes);
}
/* "Pretty" print serialisation of an affine point in G1 */
2021-02-02 23:06:42 +00:00
void print_p1_affine(const blst_p1_affine *p1) {
byte *p1_bytes = (byte *)malloc(96);
blst_p1_affine_serialize(p1_bytes, p1);
print_p1_bytes(p1_bytes);
free(p1_bytes);
2021-02-01 20:15:45 +00:00
}
/* "Pretty" print internals of a point in G1 */
void print_p1_limbs(const blst_p1 *p1) {
printf("x = ");
print_limbs(&p1->x);
printf(", y = ");
print_limbs(&p1->y);
printf(", z = ");
print_limbs(&p1->z);
printf("\n");
}
/* "Pretty" print internals of an affine point in G1 */
void print_p1_affine_limbs(const blst_p1_affine *p1) {
printf("x = ");
print_limbs(&p1->x);
printf(", y = ");
print_limbs(&p1->y);
printf("\n");
}
/* "Pretty" print serialisation of an affine point in G2 */
2021-02-01 20:15:45 +00:00
void print_p2_affine(const blst_p2_affine *p2) {
byte *p2_hex = (byte *)malloc(192);
blst_p2_affine_serialize(p2_hex, p2);
printf("[(0x");
print_bytes_as_hex(p2_hex, 0, 48);
printf(",0x");
print_bytes_as_hex(p2_hex, 48, 48);
printf("),(0x");
print_bytes_as_hex(p2_hex, 96, 48);
printf(",0x");
print_bytes_as_hex(p2_hex, 144, 48);
printf(")]\n");
free(p2_hex);
}