Skip to content
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
6 changes: 3 additions & 3 deletions simplicity-sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ This crate provides Rust definitions for the FFI structures and methods.

## Vendoring

The default build process is to build using the vendored libsecp256k1 sources in
The default build process is to build using the vendored simplicity sources in
the depend folder. There is no support for symbols yet.

To update the vendored sources, use the `vendor-simplicity.sh` script:

```
$ ./vendor-libsecp.sh depend <rev>
$ ./vendor-simplicity.sh depend <rev>
```

- Where `<rev>` is the git revision of libsecp256k1 to checkout.
- Where `<rev>` is the git revision of ElementsProject/simplicity to checkout.
2 changes: 2 additions & 0 deletions simplicity-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fn main() {
.flag_if_supported("-fno-inline-functions")
.files(files)
.files(test_files)
.file(Path::new("depend/wrapper.c"))
.file(Path::new("depend/env.c"))
.include(include)
.compile("simplicity.a");
}
76 changes: 76 additions & 0 deletions simplicity-sys/depend/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdlib.h>
#include <stdalign.h>
#include "simplicity/elements/env.h"
#include "simplicity/primitive/elements/primitive.h"

const size_t c_sizeof_rawBuffer = sizeof(rawBuffer);
const size_t c_sizeof_rawOutput = sizeof(rawOutput);
const size_t c_sizeof_rawInput = sizeof(rawInput);
const size_t c_sizeof_rawTransaction = sizeof(rawTransaction);
const size_t c_sizeof_rawTapEnv = sizeof(rawTapEnv);
const size_t c_sizeof_txEnv = sizeof(txEnv);

const size_t c_alignof_rawBuffer = alignof(rawBuffer);
const size_t c_alignof_rawOutput = alignof(rawOutput);
const size_t c_alignof_rawInput = alignof(rawInput);
const size_t c_alignof_rawTransaction = alignof(rawTransaction);
const size_t c_alignof_rawTapEnv = alignof(rawTapEnv);
const size_t c_alignof_txEnv = alignof(txEnv);

void c_set_rawBuffer(rawBuffer *result, const unsigned char *buf, unsigned int len)
{
*result = (rawBuffer){.buf = buf, .len = len};
}

void c_set_rawOutput(rawOutput *result, const unsigned char *asset, const unsigned char *value, const unsigned char *nonce, const rawBuffer *scriptPubKey,
const rawBuffer *surjectionProof, const rawBuffer *rangeProof)
{
*result = (rawOutput){.asset = asset, .value = value, .nonce = nonce, .scriptPubKey = *scriptPubKey, .surjectionProof = *surjectionProof, .rangeProof = *rangeProof};
}

void c_set_rawInput(rawInput *result, const rawBuffer *annex, const unsigned char *pegin, const rawBuffer *scriptSig,
const unsigned char *prevTxid, unsigned int prevIx,
const unsigned char *asset, const unsigned char *value, const rawBuffer *scriptPubKey,
unsigned int sequence,
const unsigned char *blindingNonce, const unsigned char *assetEntropy, const unsigned char *amount, const unsigned char *inflationKeys,
const rawBuffer *amountRangePrf, const rawBuffer *inflationKeysRangePrf)
{
*result = (rawInput){.annex = annex, .scriptSig = *scriptSig, .prevTxid = prevTxid, .pegin = pegin, .issuance = {.blindingNonce = blindingNonce, .assetEntropy = assetEntropy, .amount = amount, .inflationKeys = inflationKeys, .amountRangePrf = *amountRangePrf, .inflationKeysRangePrf = *inflationKeysRangePrf}, .txo = {.asset = asset, .value = value, .scriptPubKey = *scriptPubKey}, .prevIx = prevIx, .sequence = sequence};
}

void c_set_rawTransaction(rawTransaction *result, unsigned int version,
const rawInput *input, unsigned int numInputs,
const rawOutput *output, unsigned int numOutputs,
unsigned int lockTime)
{
*result = (rawTransaction){
.version = version,
.input = input,
.numInputs = numInputs,
.output = output,
.numOutputs = numOutputs,
.lockTime = lockTime,
};
}

void c_set_rawTapEnv(rawTapEnv *result, const unsigned char *controlBlock, unsigned char branchLen, const unsigned char *scriptCMR)
{
*result = (rawTapEnv){.controlBlock = controlBlock, .branchLen = branchLen, .scriptCMR = scriptCMR};
}

void c_set_txEnv(txEnv *result, const transaction *tx, const tapEnv *taproot, const unsigned char *genesisHash, unsigned int ix)
{
sha256_midstate genesis;
sha256_toMidstate(genesis.s, genesisHash);
*result = build_txEnv(tx, taproot, &genesis, ix);
}

void c_free_tapEnv(tapEnv *env)
{
free(env);
}

void c_free_transaction(transaction *tx)
{
free(tx);
}
45 changes: 45 additions & 0 deletions simplicity-sys/depend/wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef WRAPPERS_H
#define WRAPPERS_H
#include "simplicity/frame.h"

#include <stdalign.h>

const size_t c_sizeof_UWORD = sizeof(UWORD);
const size_t c_sizeof_frameItem = sizeof(frameItem);

const size_t c_alignof_UWORD = alignof(UWORD);
const size_t c_alignof_frameItem = alignof(frameItem);

void c_initReadFrame(frameItem *frame, size_t n, UWORD *from)
{
*frame = initReadFrame(n, from);
}

void c_initWriteFrame(frameItem *frame, size_t n, UWORD *from)
{
*frame = initWriteFrame(n, from);
}

/* Expose readBit. Internal readBit is static inline. */
bool c_readBit(frameItem *frame)
{
return readBit(frame);
}

/* Expose writeBit. Internal writeBit is static inline. */
void c_writeBit(frameItem *frame, bool bit)
{
writeBit(frame, bit);
}

void c_forwardBits(frameItem *frame, size_t n)
{
forwardBits(frame, n);
}

void c_skipBits(frameItem *frame, size_t n)
{
skipBits(frame, n);
}

#endif
Loading