Skip to content

Commit 5dab248

Browse files
committed
ffi: delete the c_set_rawElementsInput method
1 parent 32c66e0 commit 5dab248

3 files changed

Lines changed: 62 additions & 111 deletions

File tree

simplicity-sys/depend/env.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ void rustsimplicity_0_5_c_set_rawElementsBuffer(rawElementsBuffer *result, const
3030
*result = (rawElementsBuffer){.buf = buf, .len = len};
3131
}
3232

33-
void rustsimplicity_0_5_c_set_rawElementsInput(rawElementsInput *result, const rawElementsBuffer *annex, const unsigned char *pegin, const rawElementsBuffer *scriptSig,
34-
const unsigned char *prevTxid, unsigned int prevIx,
35-
const unsigned char *asset, const unsigned char *value, const rawElementsBuffer *scriptPubKey,
36-
unsigned int sequence,
37-
const unsigned char *blindingNonce, const unsigned char *assetEntropy, const unsigned char *amount, const unsigned char *inflationKeys,
38-
const rawElementsBuffer *amountRangePrf, const rawElementsBuffer *inflationKeysRangePrf)
39-
{
40-
*result = (rawElementsInput){.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};
41-
}
42-
4333
void rustsimplicity_0_5_c_set_rawElementsTransaction(rawElementsTransaction *result, unsigned int version,
4434
const unsigned char *txid,
4535
const rawElementsInput *input, unsigned int numInputs,

simplicity-sys/src/c_jets/c_env/elements.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,45 @@ pub struct CRawOutput<'raw> {
6969

7070
#[repr(C)]
7171
pub struct CRawInputIssuance<'raw> {
72-
blinding_nonce: Option<&'raw [c_uchar; 32]>,
73-
asset_entropy: Option<&'raw [c_uchar; 32]>,
74-
amount: *const c_uchar,
75-
inflation_keys: *const c_uchar,
76-
amount_range_proof: CRawBuffer,
77-
inflation_keys_range_proof: CRawBuffer,
72+
pub blinding_nonce: Option<&'raw [c_uchar; 32]>,
73+
pub asset_entropy: Option<&'raw [c_uchar; 32]>,
74+
pub amount: *const c_uchar,
75+
pub inflation_keys: *const c_uchar,
76+
pub amount_range_proof: CRawBuffer,
77+
pub inflation_keys_range_proof: CRawBuffer,
78+
}
79+
80+
impl<'raw> CRawInputIssuance<'raw> {
81+
/// Constructs a raw input issuance structure corresponding to "no issuance".
82+
pub fn no_issuance() -> Self {
83+
Self {
84+
blinding_nonce: None,
85+
asset_entropy: None,
86+
amount: core::ptr::null(),
87+
inflation_keys: core::ptr::null(),
88+
amount_range_proof: CRawBuffer::new(&[]),
89+
inflation_keys_range_proof: CRawBuffer::new(&[]),
90+
}
91+
}
7892
}
7993

8094
#[repr(C)]
8195
pub struct CRawInputTxo<'raw> {
82-
asset: Option<&'raw [c_uchar; 33]>,
83-
value: *const c_uchar,
84-
script_pubkey: CRawBuffer,
96+
pub asset: Option<&'raw [c_uchar; 33]>,
97+
pub value: *const c_uchar,
98+
pub script_pubkey: CRawBuffer,
8599
}
86100

87101
#[repr(C)]
88102
pub struct CRawInput<'raw> {
89-
annex: *const CRawBuffer,
90-
prev_txid: &'raw [c_uchar; 32],
91-
pegin: Option<&'raw [c_uchar; 32]>,
92-
issuance: CRawInputIssuance<'raw>,
93-
txo: CRawInputTxo<'raw>,
94-
script_sig: CRawBuffer,
95-
prev_txout_index: u32,
96-
sequence: u32,
103+
pub annex: *const CRawBuffer,
104+
pub prev_txid: &'raw [c_uchar; 32],
105+
pub pegin: Option<&'raw [c_uchar; 32]>,
106+
pub issuance: CRawInputIssuance<'raw>,
107+
pub txo: CRawInputTxo<'raw>,
108+
pub script_sig: CRawBuffer,
109+
pub prev_txout_index: u32,
110+
pub sequence: u32,
97111
}
98112

99113
#[derive(Debug)]
@@ -161,25 +175,6 @@ extern "C" {
161175

162176
#[link_name = "rustsimplicity_0_5_c_set_rawElementsBuffer"]
163177
pub fn c_set_rawBuffer(res: *mut CRawBuffer, buf: *const c_uchar, len: c_uint);
164-
#[link_name = "rustsimplicity_0_5_c_set_rawElementsInput"]
165-
pub fn c_set_rawInput(
166-
result: *mut CRawInput,
167-
annex: *const CRawBuffer,
168-
pegin: Option<&[c_uchar; 32]>,
169-
scriptSig: *const CRawBuffer,
170-
prevTxid: *const c_uchar,
171-
prevIx: c_uint,
172-
asset: Option<&[u8; 33]>,
173-
value: *const c_uchar,
174-
scriptPubKey: *const CRawBuffer,
175-
sequence: c_uint,
176-
blindingNonce: *const c_uchar,
177-
assetEntropy: *const c_uchar,
178-
amount: *const c_uchar,
179-
inflationKeys: *const c_uchar,
180-
amountRangePrf: *const CRawBuffer,
181-
inflationKeysRangePrf: *const CRawBuffer,
182-
);
183178

184179
#[link_name = "rustsimplicity_0_5_c_set_rawElementsTransaction"]
185180
pub fn c_set_rawTransaction(

src/jet/elements/c_env.rs

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! High level APIs for creating C FFI compatible environment.
44
//!
55
6-
use elements::secp256k1_zkp::ffi::CPtr;
76
use hashes::Hash;
87
use std::os::raw::{c_uchar, c_uint};
98

@@ -36,49 +35,38 @@ fn new_raw_output<'raw>(
3635
fn new_raw_input<'raw>(
3736
inp: &'raw elements::TxIn,
3837
in_utxo: &'raw ElementsUtxo,
39-
inp_data: &c_elements::RawInputData,
38+
inp_data: &'raw c_elements::RawInputData,
4039
) -> c_elements::CRawInput<'raw> {
41-
unsafe {
42-
let mut raw_input = std::mem::MaybeUninit::<c_elements::CRawInput>::uninit();
43-
44-
let (issue_nonce_ptr, issue_entropy_ptr, issue_amt_ptr, issue_infl_key_ptr) =
45-
if inp.has_issuance() {
46-
(
47-
inp.asset_issuance.asset_blinding_nonce.as_c_ptr(),
48-
inp.asset_issuance.asset_entropy.as_ptr(),
49-
value_ptr(inp.asset_issuance.amount, &inp_data.issuance_amount),
50-
value_ptr(
51-
inp.asset_issuance.inflation_keys,
52-
&inp_data.issuance_inflation_keys,
53-
),
54-
)
55-
} else {
56-
(
57-
std::ptr::null(),
58-
std::ptr::null(),
59-
std::ptr::null(),
60-
std::ptr::null(),
61-
)
62-
};
63-
c_elements::c_set_rawInput(
64-
raw_input.as_mut_ptr(),
65-
opt_ptr(annex_ptr(&inp_data.annex).as_ref()),
66-
inp_data.genesis_hash.as_ref(),
67-
&script_ptr(&inp.script_sig),
68-
AsRef::<[u8]>::as_ref(&inp.previous_output.txid).as_ptr(),
69-
inp.previous_output.vout as c_uint,
70-
inp_data.asset.as_ref(),
71-
value_ptr(in_utxo.value, &inp_data.value),
72-
&script_ptr(&in_utxo.script_pubkey),
73-
inp.sequence.0 as c_uint,
74-
issue_nonce_ptr, // FIXME: CHECK ASSET ISSUANCE IS NOT NULL. EASIER WITH NEW ELEMENTS VERSION.
75-
issue_entropy_ptr,
76-
issue_amt_ptr,
77-
issue_infl_key_ptr,
78-
&range_proof_ptr(&inp_data.amount_range_proof),
79-
&range_proof_ptr(&inp_data.inflation_keys_range_proof),
80-
);
81-
raw_input.assume_init()
40+
c_elements::CRawInput {
41+
// FIXME actually pass the annex in; see https://github.com/BlockstreamResearch/simplicity/issues/311 for some difficulty here.
42+
annex: core::ptr::null(),
43+
prev_txid: inp.previous_output.txid.as_ref(),
44+
pegin: inp_data.genesis_hash.as_ref(),
45+
issuance: if inp.has_issuance() {
46+
c_elements::CRawInputIssuance {
47+
blinding_nonce: Some(inp.asset_issuance.asset_blinding_nonce.as_ref()),
48+
asset_entropy: Some(&inp.asset_issuance.asset_entropy),
49+
amount: value_ptr(inp.asset_issuance.amount, &inp_data.issuance_amount),
50+
inflation_keys: value_ptr(
51+
inp.asset_issuance.inflation_keys,
52+
&inp_data.issuance_inflation_keys,
53+
),
54+
amount_range_proof: c_elements::CRawBuffer::new(&inp_data.amount_range_proof),
55+
inflation_keys_range_proof: c_elements::CRawBuffer::new(
56+
&inp_data.inflation_keys_range_proof,
57+
),
58+
}
59+
} else {
60+
c_elements::CRawInputIssuance::no_issuance()
61+
},
62+
txo: c_elements::CRawInputTxo {
63+
asset: inp_data.asset.as_ref(),
64+
value: value_ptr(in_utxo.value, &inp_data.value),
65+
script_pubkey: c_elements::CRawBuffer::new(in_utxo.script_pubkey.as_bytes()),
66+
},
67+
script_sig: c_elements::CRawBuffer::new(inp.script_sig.as_bytes()),
68+
prev_txout_index: inp.previous_output.vout,
69+
sequence: inp.sequence.to_consensus_u32(),
8270
}
8371
}
8472

@@ -218,28 +206,6 @@ fn value_ptr(value: confidential::Value, data: &[u8]) -> *const c_uchar {
218206
}
219207
}
220208

221-
fn opt_ptr<T>(t: Option<&T>) -> *const T {
222-
if let Some(t) = t {
223-
t
224-
} else {
225-
std::ptr::null()
226-
}
227-
}
228-
229-
fn script_ptr(script: &elements::Script) -> c_elements::CRawBuffer {
230-
c_elements::CRawBuffer::new(script.as_bytes())
231-
}
232-
233-
fn annex_ptr(annex: &Option<Vec<c_uchar>>) -> Option<c_elements::CRawBuffer> {
234-
annex
235-
.as_ref()
236-
.map(|annex| c_elements::CRawBuffer::new(annex))
237-
}
238-
239-
fn range_proof_ptr(rangeproof: &[c_uchar]) -> c_elements::CRawBuffer {
240-
c_elements::CRawBuffer::new(rangeproof)
241-
}
242-
243209
fn serialize_rangeproof(rangeproof: &Option<Box<RangeProof>>) -> Vec<c_uchar> {
244210
rangeproof
245211
.as_ref()

0 commit comments

Comments
 (0)