Skip to content

Commit 01b38ef

Browse files
committed
Add c bindings for create_discriminant, prove, and verify_n_wesolowski
1 parent fe87454 commit 01b38ef

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/c_bindings/c_wrapper.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "c_wrapper.h"
2+
#include <vector>
3+
#include <gmpxx.h>
4+
#include "../verifier.h"
5+
#include "../prover_slow.h"
6+
7+
extern "C" {
8+
// C wrapper function
9+
const char* create_discriminant_wrapper(const uint8_t* seed, size_t seed_size, int length) {
10+
std::vector<uint8_t> seedVector(seed, seed + seed_size);
11+
integer result = CreateDiscriminant(seedVector, length);
12+
13+
// Serialize the 'result' to a string
14+
std::string resultStr = result.to_string();
15+
16+
// Allocate a new C-string to hold the serialized result
17+
char* cResultStr = new char[resultStr.length() + 1];
18+
std::strcpy(cResultStr, resultStr.c_str());
19+
20+
return cResultStr; // Return the C-string
21+
}
22+
23+
ByteArray prove_wrapper(const uint8_t* challenge_hash, size_t challenge_size, const uint8_t* x_s, size_t x_s_size, int discriminant_size_bits, uint64_t num_iterations) {
24+
std::vector<uint8_t> challenge_hash_bytes(challenge_hash, challenge_hash + challenge_size);
25+
integer D = CreateDiscriminant(challenge_hash_bytes, discriminant_size_bits);
26+
form x = DeserializeForm(D, (const uint8_t*)x_s, x_s_size);
27+
std::vector<uint8_t> result = ProveSlow(D, x, num_iterations);
28+
29+
// Allocate memory for the result and copy data
30+
uint8_t* resultData = new uint8_t[result.size()];
31+
std::copy(result.begin(), result.end(), resultData);
32+
33+
// Create and return a ByteArray struct
34+
ByteArray resultArray = { resultData, result.size() };
35+
return resultArray;
36+
}
37+
38+
int verify_n_wesolowski_wrapper(const char* discriminant_str, size_t discriminant_size, const char* x_s, size_t x_s_size, const char* proof_blob, size_t proof_blob_size, uint64_t num_iterations, uint64_t disc_size_bits, uint64_t recursion) {
39+
std::vector<uint8_t> x_s_v(x_s, x_s + x_s_size);
40+
std::vector<uint8_t> proof_blob_v(proof_blob, proof_blob + proof_blob_size);
41+
42+
bool result = CheckProofOfTimeNWesolowski(integer(discriminant_str), x_s_v.data(), proof_blob_v.data(), proof_blob_v.size(), num_iterations, disc_size_bits, recursion);
43+
44+
return result ? 1 : 0;
45+
}
46+
}

src/c_bindings/c_wrapper.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include <stdint.h>
3+
4+
#ifdef __cplusplus
5+
#include <cstddef> // for size_t
6+
#include <cstdint> // for uint8_t
7+
extern "C" {
8+
#endif
9+
10+
const char* create_discriminant_wrapper(const uint8_t* seed, size_t seed_size, int length);
11+
12+
// Define a struct to hold the byte array and its length
13+
typedef struct {
14+
uint8_t* data;
15+
size_t length;
16+
} ByteArray;
17+
ByteArray prove_wrapper(const uint8_t* challenge_hash, size_t challenge_size, const uint8_t* x_s, size_t x_s_size, int discriminant_size_bits, uint64_t num_iterations);
18+
19+
int verify_n_wesolowski_wrapper(const char* discriminant_str, size_t discriminant_size, const char* x_s, size_t x_s_size, const char* proof_blob, size_t proof_blob_size, uint64_t num_iterations, uint64_t disc_size_bits, uint64_t recursion);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif

src/c_bindings/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# C Bindings
2+
3+
C bindings here wrap a few of the C++ functions with purely c compatible types, so that other languages which require
4+
c bindings (golang for example) can create bindings to the C++ functions.

0 commit comments

Comments
 (0)