Skip to content

Commit

Permalink
allocate space for 2D arrays in Go, rather than C
Browse files Browse the repository at this point in the history
  • Loading branch information
cce committed Nov 17, 2023
1 parent 2dde9bc commit a50dc52
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
18 changes: 5 additions & 13 deletions crypto/batchverifier.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#include "sodium.h"
int ed25519_batch_wrapper(const unsigned char *messages1D,
int ed25519_batch_wrapper(const unsigned char **messages2D,
const unsigned char **publicKeys2D,
const unsigned char **signatures2D,
const unsigned char *messages1D,
const unsigned long long *mlen,
const unsigned char *publicKeys1D,
const unsigned char *signatures1D,
size_t num,
int *valid) {
int ret;
const unsigned char **messages2D, **publicKeys2D, **signatures2D;
messages2D = malloc(num * sizeof(unsigned char *));
publicKeys2D = malloc(num * sizeof(unsigned char *));
signatures2D = malloc(num * sizeof(unsigned char *));

// fill 2-D arrays for messages, pks, sigs from provided 1-D arrays
unsigned long long mpos = 0;
for (size_t i = 0; i < num; i++) {
Expand All @@ -19,10 +16,5 @@ int ed25519_batch_wrapper(const unsigned char *messages1D,
publicKeys2D[i] = &publicKeys1D[i*crypto_sign_ed25519_PUBLICKEYBYTES];
signatures2D[i] = &signatures1D[i*crypto_sign_ed25519_BYTES];
}
ret = crypto_sign_ed25519_open_batch(messages2D, mlen, publicKeys2D, signatures2D, num, valid);

free(messages2D);
free(publicKeys2D);
free(signatures2D);
return ret;
return crypto_sign_ed25519_open_batch(messages2D, mlen, publicKeys2D, signatures2D, num, valid);
}
17 changes: 12 additions & 5 deletions crypto/batchverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ package crypto
// sizeofPtr = sizeof(void*),
// sizeofULongLong = sizeof(unsigned long long),
// };
// int ed25519_batch_wrapper(const unsigned char *messages1D,
// int ed25519_batch_wrapper(const unsigned char **messages2D,
// const unsigned char **publicKeys2D,
// const unsigned char **signatures2D,
// const unsigned char *messages1D,
// const unsigned long long *mlen,
// const unsigned char *publicKeys1D,
// const unsigned char *signatures1D,
Expand Down Expand Up @@ -124,13 +127,13 @@ func (b *BatchVerifier) Verify() error {
// if some signatures are invalid, true will be set in failed at the corresponding indexes, and
// ErrBatchVerificationFailed for err
func (b *BatchVerifier) VerifyWithFeedback() (failed []bool, err error) {
if b.GetNumberOfEnqueuedSignatures() == 0 {
if len(b.messages) == 0 {
return nil, nil
}

const estimatedMessageSize = 64
msgLengths := make([]uint64, 0, len(b.messages))
var messages = make([]byte, 0, b.GetNumberOfEnqueuedSignatures()*estimatedMessageSize)
var messages = make([]byte, 0, len(b.messages)*estimatedMessageSize)

lenWas := 0
for i := range b.messages {
Expand All @@ -152,13 +155,17 @@ func batchVerificationImpl(messages []byte, msgLengths []uint64, publicKeys []Si

numberOfSignatures := len(msgLengths)
valid := make([]C.int, numberOfSignatures)
messages2D := make([]*C.uchar, numberOfSignatures)
publicKeys2D := make([]*C.uchar, numberOfSignatures)
signatures2D := make([]*C.uchar, numberOfSignatures)

// call the batch verifier
allValid := C.ed25519_batch_wrapper(
&messages2D[0], &publicKeys2D[0], &signatures2D[0],
(*C.uchar)(&messages[0]),
(*C.ulonglong)(&msgLengths[0]),
(*C.uchar)(&(publicKeys[0][0])),
(*C.uchar)(&(signatures[0][0])),
(*C.uchar)(&publicKeys[0][0]),
(*C.uchar)(&signatures[0][0]),
C.size_t(numberOfSignatures),
(*C.int)(&valid[0]))

Expand Down

0 comments on commit a50dc52

Please sign in to comment.