Browse Source

change Eval data structure to single code blob

pull/4/head
Scott Sadler 6 years ago
parent
commit
39c9911e9c
  1. 11
      src/cc/betprotocol.cpp
  2. 28
      src/cc/betprotocol.h
  3. 19
      src/cc/disputepayout.cpp
  4. 64
      src/cc/eval.cpp
  5. 41
      src/cc/eval.h
  6. 19
      src/cc/importpayout.cpp
  7. 1
      src/cryptoconditions/.gitignore
  8. 8
      src/cryptoconditions/Makefile.am
  9. 34
      src/cryptoconditions/include/cryptoconditions.h
  10. 2
      src/cryptoconditions/src/anon.c
  11. 94
      src/cryptoconditions/src/asn/AuxFingerprintContents.c
  12. 38
      src/cryptoconditions/src/asn/AuxFingerprintContents.h
  13. 78
      src/cryptoconditions/src/asn/AuxFulfillment.c
  14. 39
      src/cryptoconditions/src/asn/AuxFulfillment.h
  15. 78
      src/cryptoconditions/src/asn/AuxSha512Fulfillment.c
  16. 39
      src/cryptoconditions/src/asn/AuxSha512Fulfillment.h
  17. 9
      src/cryptoconditions/src/asn/CryptoConditions.asn
  18. 94
      src/cryptoconditions/src/asn/EvalFingerprintContents.c
  19. 38
      src/cryptoconditions/src/asn/EvalFingerprintContents.h
  20. 46
      src/cryptoconditions/src/asn/EvalFulfillment.c
  21. 3
      src/cryptoconditions/src/asn/EvalFulfillment.h
  22. 6
      src/cryptoconditions/src/asn/Makefile.am.sample
  23. 10
      src/cryptoconditions/src/asn/asn_system.h
  24. 12
      src/cryptoconditions/src/cryptoconditions.c
  25. 5
      src/cryptoconditions/src/ed25519.c
  26. 63
      src/cryptoconditions/src/eval.c
  27. 17
      src/cryptoconditions/src/internal.h
  28. 2
      src/cryptoconditions/src/prefix.c
  29. 4
      src/cryptoconditions/src/preimage.c
  30. 4
      src/cryptoconditions/src/secp256k1.c
  31. 2
      src/cryptoconditions/src/threshold.c
  32. 1
      src/cryptoconditions/tests/custom-vectors/1000_test-minimal-eval.json
  33. 9
      src/komodo_cc.cpp
  34. 2
      src/komodo_cc.h
  35. 33
      src/test-komodo/test_cryptoconditions.cpp
  36. 48
      src/test-komodo/test_eval_bet.cpp
  37. 2
      src/test-komodo/test_eval_notarisation.cpp

11
src/cc/betprotocol.cpp

@ -18,7 +18,9 @@ std::vector<CC*> BetProtocol::PlayerConditions()
CC* BetProtocol::MakeDisputeCond()
{
CC *disputePoker = CCNewEval(disputeFunc, CheckSerialize(disputeHeader));
CC *disputePoker = CCNewEval(E_MARSHAL(
ss << disputeCode << VARINT(waitBlocks) << vmParams;
));
CC *anySig = CCNewThreshold(1, PlayerConditions());
@ -79,8 +81,9 @@ CC* BetProtocol::MakePayoutCond(uint256 signedSessionTxHash)
CC *import;
{
std::vector<unsigned char> vHash(signedSessionTxHash.begin(), signedSessionTxHash.end());
CC *importEval = CCNewEval("ImportPayout", vHash);
CC *importEval = CCNewEval(E_MARSHAL(
ss << EVAL_IMPORTPAYOUT << signedSessionTxHash;
));
CC *oneof = CCNewThreshold(1, PlayerConditions());
@ -120,7 +123,7 @@ CMutableTransaction BetProtocol::MakeImportPayoutTx(std::vector<CTxOut> payouts,
mtx.vin.push_back(CTxIn(signedStakeTxHash, 0, CScript()));
mtx.vout = payouts;
CScript proofData;
proofData << OP_RETURN << CheckSerialize(std::make_pair(momProof, signedDisputeTx));
proofData << OP_RETURN << E_MARSHAL(ss << momProof << signedDisputeTx);
mtx.vout.insert(mtx.vout.begin(), CTxOut(0, proofData));
return mtx;
}

28
src/cc/betprotocol.h

@ -1,6 +1,7 @@
#ifndef BETPROTOCOL_H
#define BETPROTOCOL_H
#include "cc/eval.h"
#include "pubkey.h"
#include "primitives/block.h"
#include "primitives/transaction.h"
@ -29,36 +30,19 @@ public:
};
class DisputeHeader
{
public:
int waitBlocks;
std::vector<unsigned char> vmParams;
DisputeHeader() {}
DisputeHeader(int w, std::vector<unsigned char> vmp) : waitBlocks(w), vmParams(vmp) {}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(VARINT(waitBlocks));
READWRITE(vmParams);
}
};
class BetProtocol
{
protected:
char* disputeFunc = (char*) "DisputeBet";
std::vector<CC*> playerConditions();
public:
EvalCode disputeCode;
std::vector<CPubKey> players;
DisputeHeader disputeHeader;
std::vector<unsigned char> vmParams;
uint32_t waitBlocks;
// Utility
BetProtocol(std::vector<CPubKey> ps, DisputeHeader dh) : players(ps), disputeHeader(dh) {}
BetProtocol(EvalCode dc, std::vector<CPubKey> ps, uint32_t wb, std::vector<uint8_t> vmp)
: disputeCode(dc), waitBlocks(wb), vmParams(vmp), players(ps) {}
std::vector<CC*> PlayerConditions();
// on PANGEA

19
src/cc/disputepayout.cpp

@ -13,7 +13,7 @@
* Crypto-Condition EVAL method that resolves a dispute of a session
*
* IN: vm - AppVM virtual machine to verify states
* IN: cond - CC EVAL node
* IN: params - condition params
* IN: disputeTx - transaction attempting to resolve dispute
* IN: nIn - index of input of dispute tx
*
@ -22,7 +22,7 @@
* in 0: Spends Session TX first output, reveals DisputeHeader
* out 0: OP_RETURN hash of payouts
*/
bool Eval::DisputePayout(AppVM &vm, const CC *cond, const CTransaction &disputeTx, unsigned int nIn)
bool Eval::DisputePayout(AppVM &vm, std::vector<uint8_t> params, const CTransaction &disputeTx, unsigned int nIn)
{
if (disputeTx.vout.size() == 0) return Invalid("no-vouts");
@ -31,12 +31,11 @@ bool Eval::DisputePayout(AppVM &vm, const CC *cond, const CTransaction &disputeT
if (!GetOpReturnHash(disputeTx.vout[0].scriptPubKey, payoutHash))
return Invalid("invalid-payout-hash");
// load dispute header
DisputeHeader disputeHeader;
std::vector<unsigned char> headerData(
cond->paramsBin, cond->paramsBin+cond->paramsBinLength);
if (!CheckDeserialize(headerData, disputeHeader))
return Invalid("invalid-dispute-header");
// load params
uint16_t waitBlocks;
std::vector<uint8_t> vmParams;
if (!E_UNMARSHAL(params, ss >> VARINT(waitBlocks); ss >> vmParams))
return Invalid("malformed-params");
// ensure that enough time has passed
{
@ -47,7 +46,7 @@ bool Eval::DisputePayout(AppVM &vm, const CC *cond, const CTransaction &disputeT
if (!GetTxConfirmed(disputeTx.vin[0].prevout.hash, sessionTx, sessionBlock))
return Error("couldnt-get-parent");
if (GetCurrentHeight() < sessionBlock.nHeight + disputeHeader.waitBlocks)
if (GetCurrentHeight() < sessionBlock.nHeight + waitBlocks)
return Invalid("dispute-too-soon"); // Not yet
}
@ -64,7 +63,7 @@ bool Eval::DisputePayout(AppVM &vm, const CC *cond, const CTransaction &disputeT
std::vector<unsigned char> vmState;
if (!spends[i].vout.size() > 0) continue;
if (!GetOpReturnData(spends[i].vout[0].scriptPubKey, vmState)) continue;
auto out = vm.evaluate(disputeHeader.vmParams, vmState);
auto out = vm.evaluate(vmParams, vmState);
uint256 resultHash = SerializeHash(out.second);
if (out.first > maxLength) {
maxLength = out.first;

64
src/cc/eval.cpp

@ -24,8 +24,11 @@ bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn)
if (eval->state.IsValid()) return true;
std::string lvl = eval->state.IsInvalid() ? "Invalid" : "Error!";
fprintf(stderr, "CC Eval %s %s: %s spending tx %s\n", lvl.data(), cond->method,
eval->state.GetRejectReason().data(), tx.vin[nIn].prevout.hash.GetHex().data());
fprintf(stderr, "CC Eval %s %s: %s spending tx %s\n",
EvalToStr(cond->code[0]).data(),
lvl.data(),
eval->state.GetRejectReason().data(),
tx.vin[nIn].prevout.hash.GetHex().data());
if (eval->state.IsError()) fprintf(stderr, "Culprit: %s\n", EncodeHexTx(tx).data());
return false;
}
@ -36,24 +39,17 @@ bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn)
*/
bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
{
if (strcmp(cond->method, "TestEval") == 0) {
bool valid = cond->paramsBinLength == 8 && memcmp(cond->paramsBin, "TestEval", 8) == 0;
return valid ? Valid() : Invalid("testing");
}
if (cond->codeLength == 0)
return Invalid("empty-eval");
if (strcmp(cond->method, "ImportPayout") == 0) {
return ImportPayout(cond, txTo, nIn);
}
uint8_t ecode = cond->code[0];
std::vector<uint8_t> vparams(cond->code+1, cond->code+cond->codeLength);
/* Example of how you might call DisputePayout
if (strcmp(ASSETCHAINS_SYMBOL, "PANGEA") == 0) {
if (strcmp(cond->method, "DisputePoker") == 0) {
return DisputePayout(PokerVM(), cond, txTo, nIn);
}
if (ecode == EVAL_IMPORTPAYOUT) {
return ImportPayout(vparams, txTo, nIn);
}
*/
return Invalid("no-such-method");
return Invalid("invalid-code");
}
@ -147,8 +143,25 @@ bool Eval::CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t t
}
extern char ASSETCHAINS_SYMBOL[16];
/*
* Get MoM from a notarisation tx hash
*/
bool Eval::GetNotarisationData(const uint256 notaryHash, NotarisationData &data) const
{
CTransaction notarisationTx;
CBlockIndex block;
if (!GetTxConfirmed(notaryHash, notarisationTx, block)) return false;
if (!CheckNotaryInputs(notarisationTx, block.nHeight, block.nTime)) return false;
if (notarisationTx.vout.size() < 2) return false;
if (!data.Parse(notarisationTx.vout[1].scriptPubKey)) return false;
return true;
}
/*
* Notarisation data, ie, OP_RETURN payload in notarisation transactions
*/
extern char ASSETCHAINS_SYMBOL[16];
bool NotarisationData::Parse(const CScript scriptPK)
{
@ -179,17 +192,14 @@ bool NotarisationData::Parse(const CScript scriptPK)
}
/*
* Get MoM from a notarisation tx hash
* Misc
*/
bool Eval::GetNotarisationData(const uint256 notaryHash, NotarisationData &data) const
std::string EvalToStr(EvalCode c)
{
CTransaction notarisationTx;
CBlockIndex block;
if (!GetTxConfirmed(notaryHash, notarisationTx, block)) return false;
if (!CheckNotaryInputs(notarisationTx, block.nHeight, block.nTime)) return false;
if (notarisationTx.vout.size() < 2) return false;
if (!data.Parse(notarisationTx.vout[1].scriptPubKey)) return false;
return true;
FOREACH_EVAL(EVAL_GENERATE_STRING);
char s[10];
sprintf(s, "0x%x", c);
return std::string(s);
}

41
src/cc/eval.h

@ -10,6 +10,22 @@
#include "primitives/transaction.h"
/*
* Eval codes
*
* Add to below macro to generate new code.
*
* If at some point a new interpretation model is introduced,
* there should be a code identifying it. For example,
* a possible code is EVAL_BITCOIN_SCRIPT, where the entire binary
* after the code is interpreted as a bitcoin script.
*/
#define FOREACH_EVAL(EVAL) \
EVAL(EVAL_IMPORTPAYOUT, 0xe1)
typedef uint8_t EvalCode;
class AppVM;
class NotarisationData;
@ -31,12 +47,12 @@ public:
/*
* Dispute a payout using a VM
*/
bool DisputePayout(AppVM &vm, const CC *cond, const CTransaction &disputeTx, unsigned int nIn);
bool DisputePayout(AppVM &vm, std::vector<uint8_t> params, const CTransaction &disputeTx, unsigned int nIn);
/*
* Test an ImportPayout CC Eval condition
*/
bool ImportPayout(const CC *cond, const CTransaction &payoutTx, unsigned int nIn);
bool ImportPayout(std::vector<uint8_t> params, const CTransaction &importTx, unsigned int nIn);
/*
* IO functions
@ -88,23 +104,36 @@ public:
};
/*
* Eval code utilities.
*/
#define EVAL_GENERATE_DEF(L,I) const uint8_t L = I;
#define EVAL_GENERATE_STRING(L,I) if (c == I) return #L;
FOREACH_EVAL(EVAL_GENERATE_DEF);
std::string EvalToStr(EvalCode c);
/*
* Serialisation boilerplate
*/
#define E_MARSHAL(body) SerializeF([&] (CDataStream &ss) {body;})
template <class T>
std::vector<unsigned char> CheckSerialize(const T in)
std::vector<uint8_t> SerializeF(const T f)
{
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << in;
f(ss);
return std::vector<unsigned char>(ss.begin(), ss.end());
}
#define E_UNMARSHAL(params, body) DeserializeF(params, [&] (CDataStream &ss) {body;})
template <class T>
bool CheckDeserialize(const std::vector<unsigned char> vIn, T &out)
bool DeserializeF(const std::vector<unsigned char> vIn, T f)
{
CDataStream ss(vIn, SER_NETWORK, PROTOCOL_VERSION);
try {
ss >> out;
f(ss);
if (ss.eof()) return true;
} catch(...) {}
return false;

19
src/cc/importpayout.cpp

@ -12,7 +12,7 @@
* Crypto-Condition EVAL method that verifies a payout against a transaction
* notarised on another chain.
*
* IN: cond - CC EVAL node
* IN: params - condition params
* IN: importTx - Payout transaction on value chain (KMD)
* IN: nIn - index of input of stake
*
@ -29,7 +29,7 @@
* out 0: OP_RETURN hash of payouts
* out 1-: anything
*/
bool Eval::ImportPayout(const CC *cond, const CTransaction &importTx, unsigned int nIn)
bool Eval::ImportPayout(const std::vector<uint8_t> params, const CTransaction &importTx, unsigned int nIn)
{
if (importTx.vout.size() == 0) return Invalid("no-vouts");
@ -37,10 +37,9 @@ bool Eval::ImportPayout(const CC *cond, const CTransaction &importTx, unsigned i
MoMProof proof;
CTransaction disputeTx;
{
std::pair<MoMProof&, CTransaction&> pair(proof, disputeTx);
std::vector<unsigned char> vopret;
GetOpReturnData(importTx.vout[0].scriptPubKey, vopret);
if (!CheckDeserialize(vopret, pair))
if (!E_UNMARSHAL(vopret, ss >> proof; ss >> disputeTx))
return Invalid("invalid-payload");
}
@ -56,16 +55,18 @@ bool Eval::ImportPayout(const CC *cond, const CTransaction &importTx, unsigned i
// Check disputeTx spends sessionTx.0
// condition ImportPayout params is session ID from other chain
{
if (cond->paramsBinLength != 32) return Invalid("malformed-params");
COutPoint prevout = disputeTx.vin[0].prevout;
if (memcmp(prevout.hash.begin(), cond->paramsBin, 32) != 0 ||
prevout.n != 0) return Invalid("wrong-session");
uint256 sessionHash;
if (!E_UNMARSHAL(params, ss >> sessionHash))
return Invalid("malformed-params");
if (disputeTx.vin[0].prevout != COutPoint(sessionHash, 0))
return Invalid("wrong-session");
}
// Check disputeTx solves momproof from vout[0]
{
NotarisationData data;
if (!GetNotarisationData(proof.notarisationHash, data)) return Invalid("coudnt-load-mom");
if (!GetNotarisationData(proof.notarisationHash, data))
return Invalid("coudnt-load-mom");
if (data.MoM != proof.Exec(disputeTx.GetHash()))
return Invalid("mom-check-fail");

1
src/cryptoconditions/.gitignore

@ -22,3 +22,4 @@
converter-sample.c
config.*
.pytest_cache
src/asn/asn_system.h

8
src/cryptoconditions/Makefile.am

@ -48,7 +48,6 @@ libcryptoconditions_core_la_SOURCES = \
src/asn/ThresholdFingerprintContents.c \
src/asn/RsaFingerprintContents.c \
src/asn/Ed25519FingerprintContents.c \
src/asn/EvalFingerprintContents.c \
src/asn/EvalFulfillment.c \
src/asn/Secp256k1FingerprintContents.c \
src/asn/Secp256k1Fulfillment.c \
@ -83,3 +82,10 @@ test:
test-debug-interactive:
gdb -ex run --args python3 -m pytest -s -x -v
asn:
cd src/asn; \
mv asn_system.h asn_system.bak; \
rm *.c *.h; \
asn1c CryptoConditions.asn; \
mv asn_system.bak asn_system.h

34
src/cryptoconditions/include/cryptoconditions.h

@ -40,18 +40,18 @@ typedef struct CC {
struct CCType *type;
union {
// public key types
struct { unsigned char *publicKey, *signature; };
struct { uint8_t *publicKey, *signature; };
// preimage
struct { unsigned char *preimage; uint16_t preimageLength; };
struct { uint8_t *preimage; size_t preimageLength; };
// threshold
struct { long threshold; uint8_t size; struct CC **subconditions; };
// prefix
struct { unsigned char *prefix; uint16_t prefixLength; struct CC *subcondition;
uint16_t maxMessageLength; };
struct { uint8_t *prefix; size_t prefixLength; struct CC *subcondition;
size_t maxMessageLength; };
// eval
struct { char method[64]; unsigned char *paramsBin; uint16_t paramsBinLength; };
struct { uint8_t *code; size_t codeLength; };
// anon
struct { unsigned char fingerprint[32]; uint32_t subtypes; unsigned long cost;
struct { uint8_t fingerprint[32]; uint32_t subtypes; unsigned long cost;
struct CCType *conditionType; };
};
} CC;
@ -63,7 +63,7 @@ typedef struct CC {
*/
typedef struct CCVisitor {
int (*visit)(CC *cond, struct CCVisitor visitor);
const unsigned char *msg;
const uint8_t *msg;
size_t msgLength;
void *context;
} CCVisitor;
@ -73,20 +73,20 @@ typedef struct CCVisitor {
* Public methods
*/
int cc_isFulfilled(const CC *cond);
int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength,
int doHashMessage, const unsigned char *condBin, size_t condBinLength,
int cc_verify(const struct CC *cond, const uint8_t *msg, size_t msgLength,
int doHashMessage, const uint8_t *condBin, size_t condBinLength,
VerifyEval verifyEval, void *evalContext);
int cc_visit(CC *cond, struct CCVisitor visitor);
int cc_signTreeEd25519(CC *cond, const unsigned char *privateKey,
const unsigned char *msg, uint16_t msgLength);
int cc_signTreeSecp256k1Msg32(CC *cond, const unsigned char *privateKey, const unsigned char *msg32);
size_t cc_conditionBinary(const CC *cond, unsigned char *buf);
size_t cc_fulfillmentBinary(const CC *cond, unsigned char *buf, size_t bufLength);
static int cc_secp256k1VerifyTreeMsg32(const CC *cond, const unsigned char *msg32);
int cc_signTreeEd25519(CC *cond, const uint8_t *privateKey, const uint8_t *msg,
const size_t msgLength);
int cc_signTreeSecp256k1Msg32(CC *cond, const uint8_t *privateKey, const uint8_t *msg32);
int cc_secp256k1VerifyTreeMsg32(const CC *cond, const uint8_t *msg32);
size_t cc_conditionBinary(const CC *cond, uint8_t *buf);
size_t cc_fulfillmentBinary(const CC *cond, uint8_t *buf, size_t bufLength);
struct CC* cc_conditionFromJSON(cJSON *params, char *err);
struct CC* cc_conditionFromJSONString(const char *json, char *err);
struct CC* cc_readConditionBinary(const unsigned char *cond_bin, size_t cond_bin_len);
struct CC* cc_readFulfillmentBinary(const unsigned char *ffill_bin, size_t ffill_bin_len);
struct CC* cc_readConditionBinary(const uint8_t *cond_bin, size_t cond_bin_len);
struct CC* cc_readFulfillmentBinary(const uint8_t *ffill_bin, size_t ffill_bin_len);
struct CC* cc_new(int typeId);
struct cJSON* cc_conditionToJSON(const CC *cond);
char* cc_conditionToJSONString(const CC *cond);

2
src/cryptoconditions/src/anon.c

@ -10,7 +10,7 @@
struct CCType CC_AnonType;
static CC *mkAnon(const Condition_t *asnCond) {
CC *mkAnon(const Condition_t *asnCond) {
CCType *realType = getTypeByAsnEnum(asnCond->present);
if (!realType) {

94
src/cryptoconditions/src/asn/AuxFingerprintContents.c

@ -1,94 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#include "AuxFingerprintContents.h"
static int
memb_method_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
size_t size;
if(!sptr) {
ASN__CTFAIL(app_key, td, sptr,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
size = st->size;
if((size == 64)) {
/* Constraint check succeeded */
return 0;
} else {
ASN__CTFAIL(app_key, td, sptr,
"%s: constraint failed (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
}
static asn_TYPE_member_t asn_MBR_AuxFingerprintContents_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct AuxFingerprintContents, method),
(ASN_TAG_CLASS_CONTEXT | (0 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
memb_method_constraint_1,
0, /* PER is not compiled, use -gen-PER */
0,
"method"
},
{ ATF_NOFLAGS, 0, offsetof(struct AuxFingerprintContents, conditionAux),
(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"conditionAux"
},
};
static const ber_tlv_tag_t asn_DEF_AuxFingerprintContents_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_AuxFingerprintContents_tag2el_1[] = {
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* method */
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 } /* conditionAux */
};
static asn_SEQUENCE_specifics_t asn_SPC_AuxFingerprintContents_specs_1 = {
sizeof(struct AuxFingerprintContents),
offsetof(struct AuxFingerprintContents, _asn_ctx),
asn_MAP_AuxFingerprintContents_tag2el_1,
2, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* Start extensions */
-1 /* Stop extensions */
};
asn_TYPE_descriptor_t asn_DEF_AuxFingerprintContents = {
"AuxFingerprintContents",
"AuxFingerprintContents",
SEQUENCE_free,
SEQUENCE_print,
SEQUENCE_constraint,
SEQUENCE_decode_ber,
SEQUENCE_encode_der,
SEQUENCE_decode_xer,
SEQUENCE_encode_xer,
0, 0, /* No PER support, use "-gen-PER" to enable */
0, /* Use generic outmost tag fetcher */
asn_DEF_AuxFingerprintContents_tags_1,
sizeof(asn_DEF_AuxFingerprintContents_tags_1)
/sizeof(asn_DEF_AuxFingerprintContents_tags_1[0]), /* 1 */
asn_DEF_AuxFingerprintContents_tags_1, /* Same as above */
sizeof(asn_DEF_AuxFingerprintContents_tags_1)
/sizeof(asn_DEF_AuxFingerprintContents_tags_1[0]), /* 1 */
0, /* No PER visible constraints */
asn_MBR_AuxFingerprintContents_1,
2, /* Elements count */
&asn_SPC_AuxFingerprintContents_specs_1 /* Additional specs */
};

38
src/cryptoconditions/src/asn/AuxFingerprintContents.h

@ -1,38 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#ifndef _AuxFingerprintContents_H_
#define _AuxFingerprintContents_H_
#include <asn_application.h>
/* Including external dependencies */
#include <OCTET_STRING.h>
#include <constr_SEQUENCE.h>
#ifdef __cplusplus
extern "C" {
#endif
/* AuxFingerprintContents */
typedef struct AuxFingerprintContents {
OCTET_STRING_t method;
OCTET_STRING_t conditionAux;
/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
} AuxFingerprintContents_t;
/* Implementation */
extern asn_TYPE_descriptor_t asn_DEF_AuxFingerprintContents;
#ifdef __cplusplus
}
#endif
#endif /* _AuxFingerprintContents_H_ */
#include <asn_internal.h>

78
src/cryptoconditions/src/asn/AuxFulfillment.c

@ -1,78 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#include "AuxFulfillment.h"
static asn_TYPE_member_t asn_MBR_AuxFulfillment_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct AuxFulfillment, method),
(ASN_TAG_CLASS_CONTEXT | (0 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"method"
},
{ ATF_NOFLAGS, 0, offsetof(struct AuxFulfillment, conditionAux),
(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"conditionAux"
},
{ ATF_NOFLAGS, 0, offsetof(struct AuxFulfillment, fulfillmentAux),
(ASN_TAG_CLASS_CONTEXT | (2 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"fulfillmentAux"
},
};
static const ber_tlv_tag_t asn_DEF_AuxFulfillment_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_AuxFulfillment_tag2el_1[] = {
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* method */
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 }, /* conditionAux */
{ (ASN_TAG_CLASS_CONTEXT | (2 << 2)), 2, 0, 0 } /* fulfillmentAux */
};
static asn_SEQUENCE_specifics_t asn_SPC_AuxFulfillment_specs_1 = {
sizeof(struct AuxFulfillment),
offsetof(struct AuxFulfillment, _asn_ctx),
asn_MAP_AuxFulfillment_tag2el_1,
3, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* Start extensions */
-1 /* Stop extensions */
};
asn_TYPE_descriptor_t asn_DEF_AuxFulfillment = {
"AuxFulfillment",
"AuxFulfillment",
SEQUENCE_free,
SEQUENCE_print,
SEQUENCE_constraint,
SEQUENCE_decode_ber,
SEQUENCE_encode_der,
SEQUENCE_decode_xer,
SEQUENCE_encode_xer,
0, 0, /* No PER support, use "-gen-PER" to enable */
0, /* Use generic outmost tag fetcher */
asn_DEF_AuxFulfillment_tags_1,
sizeof(asn_DEF_AuxFulfillment_tags_1)
/sizeof(asn_DEF_AuxFulfillment_tags_1[0]), /* 1 */
asn_DEF_AuxFulfillment_tags_1, /* Same as above */
sizeof(asn_DEF_AuxFulfillment_tags_1)
/sizeof(asn_DEF_AuxFulfillment_tags_1[0]), /* 1 */
0, /* No PER visible constraints */
asn_MBR_AuxFulfillment_1,
3, /* Elements count */
&asn_SPC_AuxFulfillment_specs_1 /* Additional specs */
};

39
src/cryptoconditions/src/asn/AuxFulfillment.h

@ -1,39 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#ifndef _AuxFulfillment_H_
#define _AuxFulfillment_H_
#include <asn_application.h>
/* Including external dependencies */
#include <OCTET_STRING.h>
#include <constr_SEQUENCE.h>
#ifdef __cplusplus
extern "C" {
#endif
/* AuxFulfillment */
typedef struct AuxFulfillment {
OCTET_STRING_t method;
OCTET_STRING_t conditionAux;
OCTET_STRING_t fulfillmentAux;
/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
} AuxFulfillment_t;
/* Implementation */
extern asn_TYPE_descriptor_t asn_DEF_AuxFulfillment;
#ifdef __cplusplus
}
#endif
#endif /* _AuxFulfillment_H_ */
#include <asn_internal.h>

78
src/cryptoconditions/src/asn/AuxSha512Fulfillment.c

@ -1,78 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#include "AuxSha512Fulfillment.h"
static asn_TYPE_member_t asn_MBR_AuxSha512Fulfillment_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct AuxSha512Fulfillment, method),
(ASN_TAG_CLASS_CONTEXT | (0 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"method"
},
{ ATF_NOFLAGS, 0, offsetof(struct AuxSha512Fulfillment, conditionAux),
(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"conditionAux"
},
{ ATF_NOFLAGS, 0, offsetof(struct AuxSha512Fulfillment, fulfillmentAux),
(ASN_TAG_CLASS_CONTEXT | (2 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"fulfillmentAux"
},
};
static const ber_tlv_tag_t asn_DEF_AuxSha512Fulfillment_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_AuxSha512Fulfillment_tag2el_1[] = {
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* method */
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 }, /* conditionAux */
{ (ASN_TAG_CLASS_CONTEXT | (2 << 2)), 2, 0, 0 } /* fulfillmentAux */
};
static asn_SEQUENCE_specifics_t asn_SPC_AuxSha512Fulfillment_specs_1 = {
sizeof(struct AuxSha512Fulfillment),
offsetof(struct AuxSha512Fulfillment, _asn_ctx),
asn_MAP_AuxSha512Fulfillment_tag2el_1,
3, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* Start extensions */
-1 /* Stop extensions */
};
asn_TYPE_descriptor_t asn_DEF_AuxSha512Fulfillment = {
"AuxSha512Fulfillment",
"AuxSha512Fulfillment",
SEQUENCE_free,
SEQUENCE_print,
SEQUENCE_constraint,
SEQUENCE_decode_ber,
SEQUENCE_encode_der,
SEQUENCE_decode_xer,
SEQUENCE_encode_xer,
0, 0, /* No PER support, use "-gen-PER" to enable */
0, /* Use generic outmost tag fetcher */
asn_DEF_AuxSha512Fulfillment_tags_1,
sizeof(asn_DEF_AuxSha512Fulfillment_tags_1)
/sizeof(asn_DEF_AuxSha512Fulfillment_tags_1[0]), /* 1 */
asn_DEF_AuxSha512Fulfillment_tags_1, /* Same as above */
sizeof(asn_DEF_AuxSha512Fulfillment_tags_1)
/sizeof(asn_DEF_AuxSha512Fulfillment_tags_1[0]), /* 1 */
0, /* No PER visible constraints */
asn_MBR_AuxSha512Fulfillment_1,
3, /* Elements count */
&asn_SPC_AuxSha512Fulfillment_specs_1 /* Additional specs */
};

39
src/cryptoconditions/src/asn/AuxSha512Fulfillment.h

@ -1,39 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#ifndef _AuxSha512Fulfillment_H_
#define _AuxSha512Fulfillment_H_
#include <asn_application.h>
/* Including external dependencies */
#include <OCTET_STRING.h>
#include <constr_SEQUENCE.h>
#ifdef __cplusplus
extern "C" {
#endif
/* AuxSha512Fulfillment */
typedef struct AuxSha512Fulfillment {
OCTET_STRING_t method;
OCTET_STRING_t conditionAux;
OCTET_STRING_t fulfillmentAux;
/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
} AuxSha512Fulfillment_t;
/* Implementation */
extern asn_TYPE_descriptor_t asn_DEF_AuxSha512Fulfillment;
#ifdef __cplusplus
}
#endif
#endif /* _AuxSha512Fulfillment_H_ */
#include <asn_internal.h>

9
src/cryptoconditions/src/asn/CryptoConditions.asn

@ -78,14 +78,14 @@ Crypto-Conditions DEFINITIONS AUTOMATIC TAGS ::= BEGIN
}
EvalFulfillment ::= SEQUENCE {
method OCTET STRING (SIZE(64)),
paramsBin OCTET STRING
code OCTET STRING
}
-- Fingerprint Content
-- The PREIMAGE-SHA-256 condition fingerprint content is not DER encoded
-- The fingerprint content is the preimage
-- Same for Eval
PrefixFingerprintContents ::= SEQUENCE {
prefix OCTET STRING,
@ -110,9 +110,4 @@ Crypto-Conditions DEFINITIONS AUTOMATIC TAGS ::= BEGIN
publicKey OCTET STRING (SIZE(33))
}
EvalFingerprintContents ::= SEQUENCE {
method OCTET STRING (SIZE(64)),
paramsBin OCTET STRING
}
END

94
src/cryptoconditions/src/asn/EvalFingerprintContents.c

@ -1,94 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#include "EvalFingerprintContents.h"
static int
memb_method_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
size_t size;
if(!sptr) {
ASN__CTFAIL(app_key, td, sptr,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
size = st->size;
if((size == 64)) {
/* Constraint check succeeded */
return 0;
} else {
ASN__CTFAIL(app_key, td, sptr,
"%s: constraint failed (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
}
static asn_TYPE_member_t asn_MBR_EvalFingerprintContents_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct EvalFingerprintContents, method),
(ASN_TAG_CLASS_CONTEXT | (0 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
memb_method_constraint_1,
0, /* PER is not compiled, use -gen-PER */
0,
"method"
},
{ ATF_NOFLAGS, 0, offsetof(struct EvalFingerprintContents, paramsBin),
(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"paramsBin"
},
};
static const ber_tlv_tag_t asn_DEF_EvalFingerprintContents_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_EvalFingerprintContents_tag2el_1[] = {
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* method */
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 } /* paramsBin */
};
static asn_SEQUENCE_specifics_t asn_SPC_EvalFingerprintContents_specs_1 = {
sizeof(struct EvalFingerprintContents),
offsetof(struct EvalFingerprintContents, _asn_ctx),
asn_MAP_EvalFingerprintContents_tag2el_1,
2, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* Start extensions */
-1 /* Stop extensions */
};
asn_TYPE_descriptor_t asn_DEF_EvalFingerprintContents = {
"EvalFingerprintContents",
"EvalFingerprintContents",
SEQUENCE_free,
SEQUENCE_print,
SEQUENCE_constraint,
SEQUENCE_decode_ber,
SEQUENCE_encode_der,
SEQUENCE_decode_xer,
SEQUENCE_encode_xer,
0, 0, /* No PER support, use "-gen-PER" to enable */
0, /* Use generic outmost tag fetcher */
asn_DEF_EvalFingerprintContents_tags_1,
sizeof(asn_DEF_EvalFingerprintContents_tags_1)
/sizeof(asn_DEF_EvalFingerprintContents_tags_1[0]), /* 1 */
asn_DEF_EvalFingerprintContents_tags_1, /* Same as above */
sizeof(asn_DEF_EvalFingerprintContents_tags_1)
/sizeof(asn_DEF_EvalFingerprintContents_tags_1[0]), /* 1 */
0, /* No PER visible constraints */
asn_MBR_EvalFingerprintContents_1,
2, /* Elements count */
&asn_SPC_EvalFingerprintContents_specs_1 /* Additional specs */
};

38
src/cryptoconditions/src/asn/EvalFingerprintContents.h

@ -1,38 +0,0 @@
/*
* Generated by asn1c-0.9.28 (http://lionet.info/asn1c)
* From ASN.1 module "Crypto-Conditions"
* found in "CryptoConditions.asn"
*/
#ifndef _EvalFingerprintContents_H_
#define _EvalFingerprintContents_H_
#include <asn_application.h>
/* Including external dependencies */
#include <OCTET_STRING.h>
#include <constr_SEQUENCE.h>
#ifdef __cplusplus
extern "C" {
#endif
/* EvalFingerprintContents */
typedef struct EvalFingerprintContents {
OCTET_STRING_t method;
OCTET_STRING_t paramsBin;
/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
} EvalFingerprintContents_t;
/* Implementation */
extern asn_TYPE_descriptor_t asn_DEF_EvalFingerprintContents;
#ifdef __cplusplus
}
#endif
#endif /* _EvalFingerprintContents_H_ */
#include <asn_internal.h>

46
src/cryptoconditions/src/asn/EvalFulfillment.c

@ -6,64 +6,28 @@
#include "EvalFulfillment.h"
static int
memb_method_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
size_t size;
if(!sptr) {
ASN__CTFAIL(app_key, td, sptr,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
size = st->size;
if((size == 64)) {
/* Constraint check succeeded */
return 0;
} else {
ASN__CTFAIL(app_key, td, sptr,
"%s: constraint failed (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
}
static asn_TYPE_member_t asn_MBR_EvalFulfillment_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct EvalFulfillment, method),
{ ATF_NOFLAGS, 0, offsetof(struct EvalFulfillment, code),
(ASN_TAG_CLASS_CONTEXT | (0 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
memb_method_constraint_1,
0, /* PER is not compiled, use -gen-PER */
0,
"method"
},
{ ATF_NOFLAGS, 0, offsetof(struct EvalFulfillment, paramsBin),
(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
-1, /* IMPLICIT tag at current level */
&asn_DEF_OCTET_STRING,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"paramsBin"
"code"
},
};
static const ber_tlv_tag_t asn_DEF_EvalFulfillment_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_EvalFulfillment_tag2el_1[] = {
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* method */
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 } /* paramsBin */
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 } /* code */
};
static asn_SEQUENCE_specifics_t asn_SPC_EvalFulfillment_specs_1 = {
sizeof(struct EvalFulfillment),
offsetof(struct EvalFulfillment, _asn_ctx),
asn_MAP_EvalFulfillment_tag2el_1,
2, /* Count of tags in the map */
1, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* Start extensions */
-1 /* Stop extensions */
@ -88,7 +52,7 @@ asn_TYPE_descriptor_t asn_DEF_EvalFulfillment = {
/sizeof(asn_DEF_EvalFulfillment_tags_1[0]), /* 1 */
0, /* No PER visible constraints */
asn_MBR_EvalFulfillment_1,
2, /* Elements count */
1, /* Elements count */
&asn_SPC_EvalFulfillment_specs_1 /* Additional specs */
};

3
src/cryptoconditions/src/asn/EvalFulfillment.h

@ -20,8 +20,7 @@ extern "C" {
/* EvalFulfillment */
typedef struct EvalFulfillment {
OCTET_STRING_t method;
OCTET_STRING_t paramsBin;
OCTET_STRING_t code;
/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;

6
src/cryptoconditions/src/asn/Makefile.am.sample

@ -15,8 +15,7 @@ ASN_MODULE_SOURCES= \
ThresholdFingerprintContents.c \
RsaFingerprintContents.c \
Ed25519FingerprintContents.c \
Secp256k1FingerprintContents.c \
EvalFingerprintContents.c
Secp256k1FingerprintContents.c
ASN_MODULE_HEADERS= \
Condition.h \
@ -35,8 +34,7 @@ ASN_MODULE_HEADERS= \
ThresholdFingerprintContents.h \
RsaFingerprintContents.h \
Ed25519FingerprintContents.h \
Secp256k1FingerprintContents.h \
EvalFingerprintContents.h
Secp256k1FingerprintContents.h
ASN_MODULE_HEADERS+=INTEGER.h
ASN_MODULE_HEADERS+=NativeEnumerated.h

10
src/cryptoconditions/src/asn/asn_system.h

@ -125,14 +125,4 @@ typedef unsigned int uint32_t;
#define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0))
#endif /* offsetof */
//#ifndef MIN /* Suitable for comparing primitive types (integers) */
//#if defined(__GNUC__)
//#define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \
// ((_a)<(_b)?(_a):(_b)); })
//#else /* !__GNUC__ */
//#define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */
//#endif /* __GNUC__ */
//#endif /* MIN */
#endif /* ASN_SYSTEM_H */

12
src/cryptoconditions/src/cryptoconditions.c

@ -69,7 +69,7 @@ char *cc_conditionUri(const CC *cond) {
}
static ConditionTypes_t asnSubtypes(uint32_t mask) {
ConditionTypes_t asnSubtypes(uint32_t mask) {
ConditionTypes_t types;
uint8_t buf[4] = {0,0,0,0};
int maxId = 0;
@ -89,7 +89,7 @@ static ConditionTypes_t asnSubtypes(uint32_t mask) {
}
static uint32_t fromAsnSubtypes(const ConditionTypes_t types) {
uint32_t fromAsnSubtypes(const ConditionTypes_t types) {
uint32_t mask = 0;
for (int i=0; i<types.size*8; i++) {
if (types.buf[i >> 3] & (1 << (7 - i % 8))) {
@ -125,7 +125,7 @@ size_t cc_fulfillmentBinary(const CC *cond, unsigned char *buf, size_t length) {
}
static void asnCondition(const CC *cond, Condition_t *asn) {
void asnCondition(const CC *cond, Condition_t *asn) {
asn->present = cc_isAnon(cond) ? cond->conditionType->asnType : cond->type->asnType;
// This may look a little weird - we dont have a reference here to the correct
@ -140,14 +140,14 @@ static void asnCondition(const CC *cond, Condition_t *asn) {
}
static Condition_t *asnConditionNew(const CC *cond) {
Condition_t *asnConditionNew(const CC *cond) {
Condition_t *asn = calloc(1, sizeof(Condition_t));
asnCondition(cond, asn);
return asn;
}
static Fulfillment_t *asnFulfillmentNew(const CC *cond) {
Fulfillment_t *asnFulfillmentNew(const CC *cond) {
return cond->type->toFulfillment(cond);
}
@ -167,7 +167,7 @@ CCType *getTypeByAsnEnum(Condition_PR present) {
}
static CC *fulfillmentToCC(Fulfillment_t *ffill) {
CC *fulfillmentToCC(Fulfillment_t *ffill) {
CCType *type = getTypeByAsnEnum(ffill->present);
if (!type) {
fprintf(stderr, "Unknown fulfillment type: %i\n", ffill->present);

5
src/cryptoconditions/src/ed25519.c

@ -58,7 +58,8 @@ static int ed25519Sign(CC *cond, CCVisitor visitor) {
/*
* Sign ed25519 conditions in a tree
*/
int cc_signTreeEd25519(CC *cond, const unsigned char *privateKey, const unsigned char *msg, uint16_t msgLength) {
int cc_signTreeEd25519(CC *cond, const unsigned char *privateKey, const unsigned char *msg,
const size_t msgLength) {
unsigned char pk[32], skpk[64];
ed25519_create_keypair(pk, skpk, privateKey);
@ -74,7 +75,7 @@ static unsigned long ed25519Cost(const CC *cond) {
}
static CC *ed25519FromJSON(const cJSON *params, unsigned char *err) {
static CC *ed25519FromJSON(const cJSON *params, char *err) {
size_t binsz;
cJSON *pk_item = cJSON_GetObjectItem(params, "publicKey");

63
src/cryptoconditions/src/eval.c

@ -1,7 +1,6 @@
#include "asn/Condition.h"
#include "asn/Fulfillment.h"
#include "asn/EvalFulfillment.h"
#include "asn/EvalFingerprintContents.h"
#include "asn/OCTET_STRING.h"
#include "cryptoconditions.h"
#include "internal.h"
@ -12,10 +11,9 @@ struct CCType CC_EvalType;
static unsigned char *evalFingerprint(const CC *cond) {
EvalFingerprintContents_t *fp = calloc(1, sizeof(EvalFingerprintContents_t));
OCTET_STRING_fromBuf(&fp->method, cond->method, strlen(cond->method));
OCTET_STRING_fromBuf(&fp->paramsBin, cond->paramsBin, cond->paramsBinLength);
return hashFingerprintContents(&asn_DEF_EvalFingerprintContents, fp);
unsigned char *hash = calloc(1, 32);
sha256(cond->code, cond->codeLength, hash);
return hash;
}
@ -24,40 +22,26 @@ static unsigned long evalCost(const CC *cond) {
}
static CC *evalFromJSON(const cJSON *params, unsigned char *err) {
size_t paramsBinLength;
unsigned char *paramsBin = 0;
static CC *evalFromJSON(const cJSON *params, char *err) {
size_t codeLength;
unsigned char *code = 0;
cJSON *method_item = cJSON_GetObjectItem(params, "method");
if (!checkString(method_item, "method", err)) {
return NULL;
}
if (strlen(method_item->valuestring) > 64) {
strcpy(err, "method must be less than or equal to 64 bytes");
return NULL;
}
if (!jsonGetBase64(params, "params", err, &paramsBin, &paramsBinLength)) {
if (!jsonGetBase64(params, "code", err, &code, &codeLength)) {
return NULL;
}
CC *cond = cc_new(CC_Eval);
strcpy(cond->method, method_item->valuestring);
cond->paramsBin = paramsBin;
cond->paramsBinLength = paramsBinLength;
cond->code = code;
cond->codeLength = codeLength;
return cond;
}
static void evalToJSON(const CC *cond, cJSON *params) {
// add method
cJSON_AddItemToObject(params, "method", cJSON_CreateString(cond->method));
static void evalToJSON(const CC *cond, cJSON *code) {
// add params
unsigned char *b64 = base64_encode(cond->paramsBin, cond->paramsBinLength);
cJSON_AddItemToObject(params, "params", cJSON_CreateString(b64));
// add code
unsigned char *b64 = base64_encode(cond->code, cond->codeLength);
cJSON_AddItemToObject(code, "code", cJSON_CreateString(b64));
free(b64);
}
@ -67,13 +51,10 @@ static CC *evalFromFulfillment(const Fulfillment_t *ffill) {
EvalFulfillment_t *eval = &ffill->choice.evalSha256;
memcpy(cond->method, eval->method.buf, eval->method.size);
cond->method[eval->method.size] = 0;
OCTET_STRING_t octets = eval->paramsBin;
cond->paramsBinLength = octets.size;
cond->paramsBin = malloc(octets.size);
memcpy(cond->paramsBin, octets.buf, octets.size);
OCTET_STRING_t octets = eval->code;
cond->codeLength = octets.size;
cond->code = malloc(octets.size);
memcpy(cond->code, octets.buf, octets.size);
return cond;
}
@ -83,8 +64,7 @@ static Fulfillment_t *evalToFulfillment(const CC *cond) {
Fulfillment_t *ffill = calloc(1, sizeof(Fulfillment_t));
ffill->present = Fulfillment_PR_evalSha256;
EvalFulfillment_t *eval = &ffill->choice.evalSha256;
OCTET_STRING_fromBuf(&eval->method, cond->method, strlen(cond->method));
OCTET_STRING_fromBuf(&eval->paramsBin, cond->paramsBin, cond->paramsBinLength);
OCTET_STRING_fromBuf(&eval->code, cond->code, cond->codeLength);
return ffill;
}
@ -95,7 +75,7 @@ int evalIsFulfilled(const CC *cond) {
static void evalFree(CC *cond) {
free(cond->paramsBin);
free(cond->code);
}
@ -108,9 +88,8 @@ static uint32_t evalSubtypes(const CC *cond) {
* The JSON api doesn't contain custom verifiers, so a stub method is provided suitable for testing
*/
int jsonVerifyEval(CC *cond, void *context) {
if (strcmp(cond->method, "testEval") == 0) {
return memcmp(cond->paramsBin, "testEval", cond->paramsBinLength) == 0;
}
if (cond->codeLength == 9 && memcmp(cond->code, "TestEval", 8))
return cond->code[8];
fprintf(stderr, "Cannot verify eval; user function unknown\n");
return 0;
}

17
src/cryptoconditions/src/internal.h

@ -39,21 +39,20 @@ typedef struct CCType {
/*
* Globals
*/
struct CCType *CCTypeRegistry[];
struct CCType *CCTypeRegistry[32];
int CCTypeRegistryLength;
/*
* Internal API
*/
static uint32_t fromAsnSubtypes(ConditionTypes_t types);
static CC *mkAnon(const Condition_t *asnCond);
static void asnCondition(const CC *cond, Condition_t *asn);
static Condition_t *asnConditionNew(const CC *cond);
static Fulfillment_t *asnFulfillmentNew(const CC *cond);
static cJSON *jsonEncodeCondition(cJSON *params, char *err);
static struct CC *fulfillmentToCC(Fulfillment_t *ffill);
static struct CCType *getTypeByAsnEnum(Condition_PR present);
uint32_t fromAsnSubtypes(ConditionTypes_t types);
CC *mkAnon(const Condition_t *asnCond);
void asnCondition(const CC *cond, Condition_t *asn);
Condition_t *asnConditionNew(const CC *cond);
Fulfillment_t *asnFulfillmentNew(const CC *cond);
struct CC *fulfillmentToCC(Fulfillment_t *ffill);
struct CCType *getTypeByAsnEnum(Condition_PR present);
/*

2
src/cryptoconditions/src/prefix.c

@ -74,7 +74,7 @@ static uint32_t prefixSubtypes(const CC *cond) {
}
static CC *prefixFromJSON(const cJSON *params, unsigned char *err) {
static CC *prefixFromJSON(const cJSON *params, char *err) {
cJSON *mml_item = cJSON_GetObjectItem(params, "maxMessageLength");
if (!cJSON_IsNumber(mml_item)) {
strcpy(err, "maxMessageLength must be a number");

4
src/cryptoconditions/src/preimage.c

@ -10,13 +10,13 @@
struct CCType CC_PreimageType;
static CC *preimageFromJSON(const cJSON *params, unsigned char *err) {
static CC *preimageFromJSON(const cJSON *params, char *err) {
cJSON *preimage_item = cJSON_GetObjectItem(params, "preimage");
if (!cJSON_IsString(preimage_item)) {
strcpy(err, "preimage must be a string");
return NULL;
}
unsigned char *preimage_b64 = preimage_item->valuestring;
char *preimage_b64 = preimage_item->valuestring;
CC *cond = cc_new(CC_Preimage);
cond->preimage = base64_decode(preimage_b64, &cond->preimageLength);

4
src/cryptoconditions/src/secp256k1.c

@ -97,7 +97,7 @@ int secp256k1Verify(CC *cond, CCVisitor visitor) {
}
static int cc_secp256k1VerifyTreeMsg32(const CC *cond, const unsigned char *msg32) {
int cc_secp256k1VerifyTreeMsg32(const CC *cond, const unsigned char *msg32) {
int subtypes = cc_typeMask(cond);
if (subtypes & (1 << CC_PrefixType.typeId) &&
subtypes & (1 << CC_Secp256k1Type.typeId)) {
@ -209,7 +209,7 @@ static CC *cc_secp256k1Condition(const unsigned char *publicKey, const unsigned
}
static CC *secp256k1FromJSON(const cJSON *params, unsigned char *err) {
static CC *secp256k1FromJSON(const cJSON *params, char *err) {
CC *cond = 0;
unsigned char *pk = 0, *sig = 0;
size_t pkSize, sigSize;

2
src/cryptoconditions/src/threshold.c

@ -165,7 +165,7 @@ static Fulfillment_t *thresholdToFulfillment(const CC *cond) {
}
static CC *thresholdFromJSON(const cJSON *params, unsigned char *err) {
static CC *thresholdFromJSON(const cJSON *params, char *err) {
cJSON *threshold_item = cJSON_GetObjectItem(params, "threshold");
if (!cJSON_IsNumber(threshold_item)) {
strcpy(err, "threshold must be a number");

1
src/cryptoconditions/tests/custom-vectors/1000_test-minimal-eval.json

@ -1,7 +1,6 @@
{
"json": {
"type": "eval-sha-256",
"method": "testEval",
"params": "dGVzdEV2YWw"
},
"cost": 131072,

9
src/komodo_cc.cpp

@ -59,13 +59,12 @@ CC* CCNewSecp256k1(CPubKey k)
}
CC* CCNewEval(std::string method, std::vector<unsigned char> paramsBin)
CC* CCNewEval(std::vector<unsigned char> code)
{
CC *cond = cc_new(CC_Eval);
strcpy(cond->method, method.data());
cond->paramsBin = (unsigned char*) malloc(paramsBin.size());
memcpy(cond->paramsBin, paramsBin.data(), paramsBin.size());
cond->paramsBinLength = paramsBin.size();
cond->code = (unsigned char*) malloc(code.size());
memcpy(cond->code, code.data(), code.size());
cond->codeLength = code.size();
return cond;
}

2
src/komodo_cc.h

@ -37,7 +37,7 @@ bool IsSignedCryptoCondition(const CC *cond);
* Construct crypto conditions
*/
CC* CCNewPreimage(std::vector<unsigned char> preimage);
CC* CCNewEval(std::string method, std::vector<unsigned char> paramsBin);
CC* CCNewEval(std::vector<unsigned char> code);
CC* CCNewSecp256k1(CPubKey k);
CC* CCNewThreshold(int t, std::vector<CC*> v);

33
src/test-komodo/test_cryptoconditions.cpp

@ -4,6 +4,7 @@
#include "base58.h"
#include "key.h"
#include "komodo_cc.h"
#include "cc/eval.h"
#include "primitives/transaction.h"
#include "script/interpreter.h"
#include "script/serverchecker.h"
@ -84,8 +85,8 @@ TEST_F(CCTest, testMayAcceptCryptoCondition)
{ "type": "threshold-sha-256",
"threshold": 1,
"subfulfillments": [
{ "type": "eval-sha-256", "method": "test", "params": "" },
{ "type": "eval-sha-256", "method": "test", "params": "" }
{ "type": "eval-sha-256", "code": "" },
{ "type": "eval-sha-256", "code": "" }
]
})!!");
ASSERT_FALSE(CCPubKey(cond).MayAcceptCryptoCondition());
@ -140,9 +141,22 @@ TEST_F(CCTest, testVerifyCryptoCondition)
ASSERT_FALSE(Verify(cond));
}
extern Eval* EVAL_TEST;
TEST_F(CCTest, testVerifyEvalCondition)
{
class EvalMock : public Eval
{
public:
bool Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
{ return cond->code[0] ? Valid() : Invalid(""); }
};
EvalMock eval;
EVAL_TEST = &eval;
CC *cond;
ScriptError error;
CMutableTransaction mtxTo;
@ -156,20 +170,11 @@ TEST_F(CCTest, testVerifyEvalCondition)
};
// ok
CCFromJson(cond, R"!!({
"type": "threshold-sha-256",
"threshold": 2,
"subfulfillments": [
{ "type": "secp256k1-sha-256", "publicKey": "AgWorQwdvFFfFJrzd5gaq1i4Nq8AjU16shvXb6+AVQtH" },
{ "type": "eval-sha-256", "method": "TestEval", "params": "" }
]})!!");
CC *ecCond = cond->subconditions[1];
ecCond->paramsBin = (unsigned char*) "TestEval";
ecCond->paramsBinLength = 8;
CCSign(mtxTo, cond); // will reorder subconditions
cond = CCNewThreshold(2, { CCNewSecp256k1(notaryKey.GetPubKey()), CCNewEval({1}) });
CCSign(mtxTo, cond);
ASSERT_TRUE(Verify(cond));
ecCond->paramsBin = (unsigned char*) "FailEval";
cond->subconditions[1]->code[0] = 0;
ASSERT_FALSE(Verify(cond));
}

48
src/test-komodo/test_eval_bet.cpp

@ -67,6 +67,7 @@ public:
}
};
const EvalCode EVAL_DISPUTEBET = 0xf2;
class EvalMock : public Eval
{
@ -79,14 +80,17 @@ public:
bool Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
{
if (strcmp(cond->method, "DisputeBet") == 0) {
EvalCode ecode = cond->code[0];
std::vector<uint8_t> vparams(cond->code+1, cond->code+cond->codeLength);
if (ecode == EVAL_DISPUTEBET) {
MockVM vm;
return DisputePayout(vm, cond, txTo, nIn);
return DisputePayout(vm, vparams, txTo, nIn);
}
if (strcmp(cond->method, "ImportPayout") == 0) {
return ImportPayout(cond, txTo, nIn);
if (ecode == EVAL_IMPORTPAYOUT) {
return ImportPayout(vparams, txTo, nIn);
}
return Invalid("invalid-method");
return Invalid("invalid-code");
}
bool GetSpendsConfirmed(uint256 hash, std::vector<CTransaction> &spendsOut) const
@ -148,7 +152,7 @@ public:
BetProtocol bet;
CAmount totalPayout;
ExampleBet() : bet(BetProtocol(players, DisputeHeader(2, VCH("BetHeader", 9)))), totalPayout(100) {}
ExampleBet() : bet(BetProtocol(EVAL_DISPUTEBET, players, 2, VCH("BetHeader", 9))), totalPayout(100) {}
~ExampleBet() {};
CTransaction SessionTx()
@ -180,7 +184,7 @@ public:
std::vector<CTxOut> Payouts(int playerIdx)
{
return MockVM().evaluate(bet.disputeHeader.vmParams, PlayerState(playerIdx)).second;
return MockVM().evaluate(bet.vmParams, PlayerState(playerIdx)).second;
}
CMutableTransaction DisputeTx(int playerIdx)
@ -280,7 +284,12 @@ TEST_F(TestBet, testMakeDisputeCond)
{
CC *disputeCond = ebet.DisputeCond();
EXPECT_EQ("(2 of 15,(1 of 5,5,5))", CCShowStructure(disputeCond));
EXPECT_EQ(0, memcmp("\x2\tBetHeader", (char*) disputeCond->subconditions[0]->paramsBin, 11));
CC *evalCond = disputeCond->subconditions[0];
uint8_t target[100];
sprintf((char*)target, "%c\x02\tBetHeader", EVAL_DISPUTEBET);
EXPECT_EQ(0, memcmp(target, evalCond->code, 12));
for (int i=0; i<players.size(); i++)
EXPECT_EQ(CCPubKey(CCNewSecp256k1(players[i])),
CCPubKey(disputeCond->subconditions[1]->subconditions[i]));
@ -315,10 +324,9 @@ TEST_F(TestBet, testDispute)
// Success
EXPECT_TRUE(TestCC(disputeTx, 0, disputeCond));
// Set result hash to some rubbish and check false
uint256 rubbishHash;
std::vector<unsigned char> rubbish(rubbishHash.begin(), rubbishHash.end());
disputeTx.vout[0].scriptPubKey = CScript() << OP_RETURN << rubbish;
// Set result hash to 0 and check false
uint256 nonsense;
disputeTx.vout[0].scriptPubKey = CScript() << OP_RETURN << E_MARSHAL(ss << nonsense);
EXPECT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
EXPECT_EQ("wrong-payout", eval.state.GetRejectReason());
@ -371,21 +379,21 @@ TEST_F(TestBet, testDisputeInvalidParams)
CC *evalCond = disputeCond->subconditions[0];
// too long
evalCond->paramsBin = (unsigned char*) realloc(evalCond->paramsBin, ++evalCond->paramsBinLength);
evalCond->code = (unsigned char*) realloc(evalCond->code, ++evalCond->codeLength);
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
EXPECT_EQ("invalid-dispute-header", eval.state.GetRejectReason());
EXPECT_EQ("malformed-params", eval.state.GetRejectReason());
// too short
eval.state = CValidationState();
evalCond->paramsBinLength = 1;
evalCond->codeLength = 1;
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
EXPECT_EQ("invalid-dispute-header", eval.state.GetRejectReason());
EXPECT_EQ("malformed-params", eval.state.GetRejectReason());
// is fine
eval.state = CValidationState();
evalCond->paramsBinLength = 11;
evalCond->codeLength = 12;
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
EXPECT_TRUE(TestCC(disputeTx, 0, disputeCond));
}
@ -435,7 +443,7 @@ TEST_F(TestBet, testMakePayoutCond)
{
CC *payoutCond = ebet.PayoutCond();
EXPECT_EQ("(1 of (3 of 5,5,5),(2 of (1 of 5,5,5),15))", CCShowStructure(payoutCond));
EXPECT_EQ(0, memcmp(payoutCond->subconditions[1]->subconditions[1]->paramsBin,
EXPECT_EQ(0, memcmp(payoutCond->subconditions[1]->subconditions[1]->code+1,
ebet.SessionTx().GetHash().begin(), 32));
}
@ -543,13 +551,13 @@ TEST_F(TestBet, testImportPayoutMangleSessionId)
CMutableTransaction importTx = ebet.ImportPayoutTx();
CC *payoutCond = ebet.PayoutCond();
payoutCond->subconditions[1]->subconditions[1]->paramsBinLength = 31;
payoutCond->subconditions[1]->subconditions[1]->codeLength = 31;
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
ASSERT_FALSE(TestCC(importTx, 0, payoutCond));
EXPECT_EQ("malformed-params", eval.state.GetRejectReason());
payoutCond = ebet.PayoutCond();
memset(payoutCond->subconditions[1]->subconditions[1]->paramsBin, 1, 32);
memset(payoutCond->subconditions[1]->subconditions[1]->code+1, 1, 32);
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
ASSERT_FALSE(TestCC(importTx, 0, payoutCond));
EXPECT_EQ("wrong-session", eval.state.GetRejectReason());

2
src/test-komodo/test_eval_notarisation.cpp

@ -110,7 +110,7 @@ TEST(TestEvalNotarisation, testGetNotarisation)
EXPECT_EQ(data.MoM.GetHex(), "88289b6566a48567f65c8e60ca65b7f3877bbdb97cfc3958da31bcf073a70b05");
MoMProof proof;
CheckDeserialize(vMomProof, proof);
E_UNMARSHAL(vMomProof, ss >> proof);
EXPECT_EQ(data.MoM, proof.Exec(proofTxHash));
}

Loading…
Cancel
Save