Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sha2-sha256, keccak256, ripemd160, blake2b bindings #30

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ build/ckb-js-vm: build/ckb-c-stdlib/impl.o \
build/secp256k1/precomputed_ecmult.o \
build/src/ckb_module.o \
build/src/secp256k1_module.o \
build/src/hash_module.o \
build/src/qjs.o \
build/src/std_module.o \
deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a
Expand Down
75 changes: 75 additions & 0 deletions src/conversion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2014-2018 The Bitcoin Core developers
* Distributed under the MIT software license, see the accompanying
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*
* only used on little endian
*/

#ifndef CONVERSION_H
#define CONVERSION_H

#include <stdint.h>
#include <string.h>

uint32_t static inline le32toh_(uint32_t x) { return x; }

uint32_t static inline htole32_(uint32_t x) { return x; }

uint32_t static inline be32toh_(uint32_t x) { return bswap_32(x); }

uint32_t static inline htobe32_(uint32_t x) { return bswap_32(x); }

uint64_t static inline le64toh_(uint64_t x) { return x; }

uint64_t static inline htole64_(uint64_t x) { return x; }

uint32_t static inline be64toh_(uint64_t x) { return bswap_64(x); }

uint64_t static inline htobe64_(uint64_t x) { return bswap_64(x); }

uint32_t static inline ReadLE32(const unsigned char* ptr) {
uint32_t x;
memcpy((char*)&x, ptr, 4);
return le32toh_(x);
}

void static inline WriteLE32(unsigned char* ptr, uint32_t x) {
uint32_t v = htole32_(x);
memcpy(ptr, (char*)&v, 4);
}

uint32_t static inline ReadBE32(const unsigned char* ptr) {
uint32_t x;
memcpy((char*)&x, ptr, 4);
return be32toh_(x);
}

void static inline WriteBE32(unsigned char* ptr, uint32_t x) {
uint32_t v = htobe32_(x);
memcpy(ptr, (char*)&v, 4);
}

void static inline WriteLE64(unsigned char* ptr, uint64_t x) {
uint64_t v = htole64_(x);
memcpy(ptr, (char*)&v, 8);
}

uint64_t static inline ReadLE64(const unsigned char* ptr) {
uint64_t x;
memcpy((char*)&x, ptr, 8);
return le64toh_(x);
}

void static inline WriteBE64(unsigned char* ptr, uint64_t x) {
uint64_t v = htobe64_(x);
memcpy(ptr, (char*)&v, 8);
}

uint64_t static inline ReadBE64(const unsigned char* ptr) {
uint64_t x;
memcpy((char*)&x, ptr, 8);
return be64toh_(x);
}

#endif
Loading
Loading