Skip to content

Commit

Permalink
feat: add sha2-sha256, keccak256, ripemd160, blake2b bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Jan 8, 2025
1 parent fdc0233 commit d54bd81
Show file tree
Hide file tree
Showing 10 changed files with 1,367 additions and 0 deletions.
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

0 comments on commit d54bd81

Please sign in to comment.