Skip to content

Commit

Permalink
[lib] refs #992 - Fix memory alignment in SKY_cipher_GenerateKeyPairs
Browse files Browse the repository at this point in the history
  • Loading branch information
olemis committed Apr 3, 2018
1 parent 4614842 commit c339937
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ include/libskycoin.h

# Eclipse temp files
.cproject
metadata/**
.metadata/**
.project


16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,20 @@ build-libc: configure-build ## Build libskycoin C client library
go build -buildmode=c-archive -o $(BUILDLIB_DIR)/libskycoin.a $(LIB_FILES)
mv $(BUILDLIB_DIR)/libskycoin.h $(INCLUDE_DIR)/

## Build libskycoin C client library and executable C test suites
## with debug symbols. Use this target to debug the source code
## with the help of an IDE
build-libc-dbg: configure-build
rm -Rf $(BUILDLIB_DIR)/*
go build -buildmode=c-shared -o $(BUILDLIB_DIR)/libskycoin.so $(LIB_FILES)
go build -buildmode=c-archive -o $(BUILDLIB_DIR)/libskycoin.a $(LIB_FILES)
mv $(BUILDLIB_DIR)/libskycoin.h $(INCLUDE_DIR)/
$(CC) -g -o $(BIN_DIR)/test_libskycoin_shared $(LIB_DIR)/cgo/tests/*.c -lskycoin $(LDLIBS) $(LDFLAGS)
$(CC) -g -o $(BIN_DIR)/test_libskycoin_static $(LIB_DIR)/cgo/tests/*.c $(BUILDLIB_DIR)/libskycoin.a $(LDLIBS) $(LDFLAGS)

test-libc: build-libc ## Run tests for libskycoin C client library
cp $(LIB_DIR)/cgo/tests/*.c $(BUILDLIB_DIR)/
$(CC) -o $(BIN_DIR)/test_libskycoin_shared $(BUILDLIB_DIR)/*.c -lskycoin $(LDLIBS) $(LDFLAGS)
$(CC) -o $(BIN_DIR)/test_libskycoin_static $(BUILDLIB_DIR)/*.c $(BUILDLIB_DIR)/libskycoin.a $(LDLIBS) $(LDFLAGS)
$(CC) -o $(BIN_DIR)/test_libskycoin_shared $(LIB_DIR)/cgo/tests/*.c -lskycoin $(LDLIBS) $(LDFLAGS)
$(CC) -o $(BIN_DIR)/test_libskycoin_static $(LIB_DIR)/cgo/tests/*.c $(BUILDLIB_DIR)/libskycoin.a $(LDLIBS) $(LDFLAGS)
$(LDPATHVAR)="$(LDPATH):$(BUILD_DIR)/usr/lib:$(BUILDLIB_DIR)" $(BIN_DIR)/test_libskycoin_shared
$(LDPATHVAR)="$(LDPATH):$(BUILD_DIR)/usr/lib" $(BIN_DIR)/test_libskycoin_static

Expand Down
20 changes: 10 additions & 10 deletions lib/cgo/cipher.crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
cipher "github.com/skycoin/skycoin/src/cipher"

"fmt"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -114,6 +115,9 @@ func SKY_cipher_PubKey_Verify(_pk *C.PubKey) uint32 {
__pk := (*[1 << 30]byte)(
unsafe.Pointer(_pk))[:SizeofPubKey:SizeofPubKey]
pk := (*cipher.PubKey)(unsafe.Pointer(&__pk))

fmt.Println("PubKey in Go ", pk)

err := pk.Verify()
errcode := libErrorCode(err)
return errcode
Expand Down Expand Up @@ -296,17 +300,13 @@ func SKY_cipher_VerifySignature(_pubkey *C.PubKey, _sig *C.Sig, _hash *C.SHA256)

//export SKY_cipher_GenerateKeyPair
func SKY_cipher_GenerateKeyPair(_arg0 *C.PubKey, _arg1 *C.SecKey) {
fmt.Println(unsafe.Pointer(_arg0), unsafe.Pointer(_arg1))
__arg0 := (*[1 << 30]byte)(
unsafe.Pointer(_arg0))[:SizeofPubKey:SizeofPubKey]
arg0 := (*cipher.PubKey)(unsafe.Pointer(&__arg0))
__arg1 := (*[1 << 30]byte)(
unsafe.Pointer(_arg1))[:SizeofSecKey:SizeofSecKey]
arg1 := (*cipher.SecKey)(unsafe.Pointer(&__arg1))
fmt.Printf("Go Pointers %p %p\n", unsafe.Pointer(_arg0), unsafe.Pointer(_arg1))

p, s := cipher.GenerateKeyPair()
fmt.Println(p)
copy(arg0[:], p[:])
copy(arg1[:], s[:])
copyToBuffer(reflect.ValueOf(p[:]), unsafe.Pointer(_arg0), uint(SizeofPubKey))
copyToBuffer(reflect.ValueOf(s[:]), unsafe.Pointer(_arg1), uint(SizeofSecKey))

fmt.Println("PubKey in Go ", p)
}

//export SKY_cipher_GenerateDeterministicKeyPair
Expand Down
2 changes: 1 addition & 1 deletion lib/cgo/libsky_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func copyString(src string, dest *C.GoString_) bool {
func getBufferData(src reflect.Value) (bufferAddr unsafe.Pointer, elemSize C.size_t) {
firstElem := src.Index(0)
elemSize = C.size_t(firstElem.Type().Size())
bufferAddr = unsafe.Pointer(firstElem.UnsafeAddr())
bufferAddr = unsafe.Pointer(src.Pointer())
return
}

Expand Down
88 changes: 52 additions & 36 deletions lib/cgo/tests/check_cipher.crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,40 @@ void randBytes(GoSlice *bytes, size_t n) {
bytes->len = (GoInt) n;
}

#define SIZE_ALL -1

// TODO: Move to libsky_string.c
void strnhex(unsigned char* buf, char *str, int n){
unsigned char * pin = buf;
const char * hex = "0123456789ABCDEF";
char * pout = str;
for(; *pin && n; --n){
*pout++ = hex[(*pin>>4)&0xF];
*pout++ = hex[(*pin++)&0xF];
}
*pout = 0;
}

// TODO: Move to libsky_string.c
void strhex(unsigned char* buf, char *str){
strnhex(buf, str, SIZE_ALL);
}

// TODO: Move to libsky_io.c
void fprintbuff(FILE *f, void *buff, size_t n) {
unsigned char *ptr = (unsigned char *) buff;
fprintf(f, "[ ");
for (; n; --n, ptr++) {
fprintf(f, "%02d ", *ptr);
}
fprintf(f, "]");
}

void setup(void) {
srand ((unsigned int) time (NULL));
}

/*
Test(asserts, TestNewPubKey) {
unsigned char buff[101];
GoSlice slice;
Expand Down Expand Up @@ -61,25 +91,6 @@ Test(asserts, TestNewPubKey) {
cr_assert(eq(u8[33], pk, buff));
}
#define SIZE_ALL -1

// TODO: Move to libsky_string.c
void strnhex(unsigned char* buf, char *str, int n){
unsigned char * pin = buf;
const char * hex = "0123456789ABCDEF";
char * pout = str;
for(; *pin && n; --n){
*pout++ = hex[(*pin>>4)&0xF];
*pout++ = hex[(*pin++)&0xF];
}
*pout = 0;
}

// TODO: Move to libsky_string.c
void strhex(unsigned char* buf, char *str){
strnhex(buf, str, SIZE_ALL);
}

Test(asserts, TestPubKeyFromHex) {
PubKey p, p1;
GoString s;
Expand Down Expand Up @@ -186,19 +197,25 @@ Test(asserts, TestPubKeyVerifyNil) {
errcode = SKY_cipher_PubKey_Verify(&p);
cr_assert(errcode == SKY_ERROR);
}

*/
Test(asserts, TestPubKeyVerifyDefault1) {
PubKey p;
SecKey s;
char strBuff[101];

fprintf(stderr, "C pointers %p %p\n", &p, &s);

fprintf(stderr, "p1 %p %p\n", &p, &s);
SKY_cipher_GenerateKeyPair(&p, &s);
fprintf(stderr, "p2\n");

strnhex(p, strBuff, 33);
fprintf(stderr, "PubKey in C ");
fprintbuff(stderr, p, 33);
fprintf(stderr, "\n");

unsigned int errcode = SKY_cipher_PubKey_Verify(&p);
fprintf(stderr, "p3\n");
cr_assert(errcode == SKY_OK);
}

/*
Test(asserts, TestPubKeyVerifyDefault2) {
PubKey p;
SecKey s;
Expand All @@ -207,9 +224,7 @@ Test(asserts, TestPubKeyVerifyDefault2) {
for (i = 0; i < 1024; ++i) {
fprintf(stderr, "p1 %p %p\n", &p, &s);
SKY_cipher_GenerateKeyPair(&p, &s);
fprintf(stderr, "p2\n");
unsigned int errcode = SKY_cipher_PubKey_Verify(&p);
fprintf(stderr, "p3\n");
cr_assert(errcode == SKY_OK);
}
}
Expand All @@ -221,16 +236,16 @@ Test(asserts, TestPubKeyToAddressHash) {
SKY_cipher_GenerateKeyPair(&p, &s);
SKY_cipher_PubKey_ToAddressHash(&p, &h);
/* TODO: Translate code snippet
*
* x := sha256.Sum256(p[:])
* x = sha256.Sum256(x[:])
* rh := ripemd160.New()
* rh.Write(x[:])
* y := rh.Sum(nil)
* assert.True(t, bytes.Equal(h[:], y))
*
*/
// TODO: Translate code snippet
//
// x := sha256.Sum256(p[:])
// x = sha256.Sum256(x[:])
// rh := ripemd160.New()
// rh.Write(x[:])
// y := rh.Sum(nil)
// assert.True(t, bytes.Equal(h[:], y))
//
//
}
Test(asserts, TestPubKeyToAddress) {
Expand Down Expand Up @@ -783,3 +798,4 @@ Test(asserts, TestSecKeyHashTest) {
errcode = SKY_cipher_TestSecKeyHash(&sk, &h);
cr_assert(errcode == SKY_ERROR);
}
*/

0 comments on commit c339937

Please sign in to comment.