Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used boost::shared_ptr throughout for memory management #1

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
948b95f
converting to use boost::shared_ptr (as Ptr), ran query-replace-regex…
cce Dec 18, 2010
46145d6
use new_ptr instead of new (query-replace-regexp 'new \([A-Za-z:_]+\)…
cce Dec 18, 2010
7972845
run in ZKP dir: sed -i.bak s/boost::shared_ptr/Ptr/g *.cpp *.h *.hpp
cce Dec 18, 2010
fdcf2ec
clarify (mini)NTL and license for ZZ.c
cce Dec 19, 2010
82f2d96
Merge branch 'master' into shared_ptr
cce Dec 19, 2010
3fa150e
[shared_ptr branch] got rid of deletes, unnecessary copy constructors…
cce Dec 19, 2010
8108abd
running tests (not all pass), rebuilt param files
cce Dec 19, 2010
fbeeaaf
trying to fix
cce Dec 19, 2010
1edc573
imitate the way copying new groups worked with old BankParameters
cce Dec 19, 2010
df9db21
use loadString instead of loadGZString for client tests
cce Feb 3, 2011
80a4991
params from master
cce Feb 3, 2011
261b44c
get rid of buymX.xml
cce Feb 3, 2011
c68957c
use std::map to fix serialization tests for now
cce Feb 3, 2011
a0d3922
Merge remote branch 'origin/shared_ptr' into broken-shared_ptr
cce Feb 3, 2011
7ab320f
Revert "add operator== testers to some classes (for debugging)"
cce Feb 3, 2011
0c3d2da
fix interleaved/repeated withdraw tests with shared_ptr
cce Feb 3, 2011
245890e
regenerate params with std::map and shared_ptr
cce Feb 3, 2011
96a9ee6
shared_ptr seems to work!
cce Feb 3, 2011
9be3034
fixed tests 13 and 14
cce Feb 4, 2011
4857032
getting rid of some residual debugging stuff
Jan 31, 2011
eeff6a4
remove seller destructor
cce Feb 7, 2011
1efbc9c
new unordered_map serialization
cce Feb 4, 2011
f7caf46
check for CACHE_POWERS environment variable
cce Feb 9, 2011
62274b0
Merge branch 'shared_ptr' of github.com:brownie/cashlib into shared_ptr
cce Feb 20, 2011
c9a212b
fix serialize ZZ=0 problem (did mpz_sizeinbase change?)
cce Feb 20, 2011
3163f13
print out difference in len/l
cce Feb 20, 2011
72b60ee
deserialize messages from GZ-compressed strings by default
cce Feb 20, 2011
02ceba5
Revert "print out difference in len/l"
cce Feb 20, 2011
6618484
fixed test 10
cce Feb 20, 2011
7ad4c0b
make brownie timers thread-safe (use boost::thread_specific_ptr)
cce Feb 25, 2011
777e759
allow option to Interpreter::check() to enable/disable multiexponenti…
cce Feb 26, 2011
56bc7cf
skip power-caching for user-to-bank proofs by default
cce Feb 26, 2011
e469ced
make InterpreterCache thread-safe (use TLS from boost::thread_specifi…
cce Feb 26, 2011
b7667a7
Merge remote-tracking branch 'origin/master' into shared_ptr-merge
cce Apr 7, 2013
4440e91
fix build after merge (remove copy constructors again)
cce Apr 7, 2013
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
2 changes: 2 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ brownieTest
Serialize.h.gch
zkptest
libcash.a
buym1.xml
buym2.xml
98 changes: 50 additions & 48 deletions src/Arbiter.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Arbiter.h"
#include "VECiphertext.h"

using boost::shared_ptr;

vector<string> Arbiter::initiatorResolve(const ZZ &r){
return buyerResolve(r);
}
Expand All @@ -22,27 +20,29 @@ vector<unsigned> Arbiter::sellerResolveI(const ResolutionPair &keyMessagePair){
// first, store the keys
keys = keyMessagePair.first;
// now, unwrap and check the buyMessage, then store everything
BuyMessage* buyMessage = keyMessagePair.second;
Coin coinPrime = buyMessage->getCoinPrime();
VECiphertext escrow = *buyMessage->getEscrow();
Ptr<BuyMessage> buyMessage = keyMessagePair.second;
Ptr<Coin> coinPrime = buyMessage->getCoinPrime();
Ptr<VECiphertext> escrow = buyMessage->getEscrow();
// want to store the contract as well (for stage II)
contract = *buyMessage->getContract();
contract = buyMessage->getContract();
// check the timeout to make sure it hasn't passed
if(contract.checkTimeout(timeoutTolerance)) {
endorsement = verifiableDecrypter->decrypt(escrow.getCiphertext(),
saveString(contract), hashAlg);
if(contract->checkTimeout(timeoutTolerance)) {
endorsement = verifiableDecrypter->decrypt(escrow->getCiphertext(),
saveString(*contract), hashAlg);
// make sure the endorsement on the coin is valid
if(coinPrime.verifyEndorsement(endorsement)){
if(coinPrime->verifyEndorsement(endorsement)){
// construct verifiers based on the data in the contract and
// return a set of challenges
hash_t ptHash = contract.getPTHashB();
hash_t ctHash = contract.getCTHashB();
ptVerifier = shared_ptr<MerkleVerifier>(new MerkleVerifier(ptHash,
contract.getNumPTHashBlocksB(),
MerkleContract(ptHash.key,ptHash.alg)));
ctVerifier = shared_ptr<MerkleVerifier>(new MerkleVerifier(ctHash,
contract.getNumCTHashBlocksB(),
MerkleContract(ctHash.key,ctHash.alg)));
hash_t ptHash = contract->getPTHashB();
hash_t ctHash = contract->getCTHashB();
ptVerifier = new_ptr<MerkleVerifier>(
ptHash,
contract->getNumPTHashBlocksB(),
MerkleContract(ptHash.key,ptHash.alg));
ctVerifier = new_ptr<MerkleVerifier>(
ctHash,
contract->getNumCTHashBlocksB(),
MerkleContract(ctHash.key,ctHash.alg));
return ptVerifier->getChallenges();
} else {
throw CashException(CashException::CE_FE_ERROR,
Expand All @@ -54,12 +54,12 @@ vector<unsigned> Arbiter::sellerResolveI(const ResolutionPair &keyMessagePair){
}
}

vector<ZZ> Arbiter::sellerResolveII(const MerkleProof* proof){
vector<ZZ> Arbiter::sellerResolveII(Ptr<const MerkleProof> proof){
// check the proof against the keys provided in Stage I
if(verifyKeys(proof)){
if(updateDB){
vector<ZZ> rVec;
rVec.push_back(contract.getID());
rVec.push_back(contract->getID());
// store the keys (for the buyer later)
updateDB(Hash::hash(rVec,hashAlg), keys);
}
Expand All @@ -71,19 +71,19 @@ vector<ZZ> Arbiter::sellerResolveII(const MerkleProof* proof){
}
}

bool Arbiter::verifyKeys(const MerkleProof* proof) {
bool Arbiter::verifyKeys(Ptr<const MerkleProof> proof) {
// the arbiter needs to check the encrypted blocks decrypt correctly
bool validDecryption = true;
vector<EncBuffer*> cTextBlocks = proof->getCTextBlocks();
vector<Buffer*> decryptedBlocks(cTextBlocks.size());
vector<Ptr<EncBuffer> > cTextBlocks = proof->getCTextBlocks();
vector<Ptr<Buffer> > decryptedBlocks(cTextBlocks.size());
vector<hash_t> hashedBlocks(cTextBlocks.size());
unsigned i = 0;
// it checks the decryption in the following three steps:
do {
// 1. decrypt the blocks
unsigned index = (keys.size() == 1) ? 0 : i;
decryptedBlocks[i] = cTextBlocks[i]->decrypt(keys[index],
contract.getEncAlgB());
contract->getEncAlgB());
// 2. hash them
hashedBlocks[i] = proof->getPTContract()->hash(decryptedBlocks[i]);
// 3. check if they match the public plaintext hashes
Expand All @@ -92,28 +92,28 @@ bool Arbiter::verifyKeys(const MerkleProof* proof) {
} while(validDecryption && i < cTextBlocks.size());

// now finish verifying using the MerkleVerifiers
if(contract.getPTHashB().type == Hash::TYPE_MERKLE){
if(contract->getPTHashB().type == Hash::TYPE_MERKLE){
return (validDecryption &&
ctVerifier->verifyProofs(proof->getCTextProof()) &&
ptVerifier->verifyProofs(proof->getPTextProof()));
} else {
hash_t ptHash = proof->getPTContract()->hash(proof->getPlaintext());
return (validDecryption &&
ctVerifier->verifyProofs(proof->getCTextProof()) &&
(ptHash == message->getContract().getPTHashB()));
(ptHash == message->getContract()->getPTHashB()));
}
}

bool Arbiter::verifyDecryption(const MerkleProof* proof){
bool Arbiter::verifyDecryption(Ptr<const MerkleProof> proof){
bool validDecryption = true;
vector<EncBuffer*> cTextBlocks = proof->getCTextBlocks();
vector<Buffer*> decryptedBlocks(cTextBlocks.size());
vector<Ptr<EncBuffer> > cTextBlocks = proof->getCTextBlocks();
vector<Ptr<Buffer> > decryptedBlocks(cTextBlocks.size());
vector<hash_t> hashedBlocks(cTextBlocks.size());
unsigned i = 0;
do{
unsigned index = (keys.size() == 1) ? 0 : i;
decryptedBlocks[i] = cTextBlocks[i]->decrypt(keys[index],
contract.getEncAlgA());
contract->getEncAlgA());
hashedBlocks[i] = proof->getCTContract()->hash(decryptedBlocks[i]);
validDecryption = hashedBlocks[i] == proof->getPTextProof()[i][0].node;
i++;
Expand All @@ -123,25 +123,25 @@ bool Arbiter::verifyDecryption(const MerkleProof* proof){
return (validDecryption && ctVerifier->verifyProofs(ctProof));
}

vector<unsigned> Arbiter::responderResolveI(const FEResolutionMessage* req) {
vector<unsigned> Arbiter::responderResolveI(Ptr<const FEResolutionMessage> req) {
return responderResolveI(req->getKeys(), req->getMessage(),
req->getSetupMessage());
}

// this method sets up the resolve for a failed barter
vector<unsigned> Arbiter::responderResolveI(const vector<string> &ks,
const FEMessage* msg,
const FESetupMessage* setup) {
Ptr<const FEMessage> msg,
Ptr<const FESetupMessage> setup) {
// stores keys and message
keys = ks;
message = msg;
Coin coinPrime = setup->getCoinPrime();
// this is the regular encryption
vector<ZZ> sigEscrow = message->getEscrow();
// this is the verifiable encryption
VECiphertext vEscrow = *setup->getEscrow();
Ptr<VECiphertext> vEscrow = setup->getEscrow();
contract = message->getContract();
const Signature::Key* sigPK = setup->getPK();
Ptr<const Signature::Key> sigPK = setup->getPK();

// verify that the signature given in BarterMessage is correct
bool sigCorrect = Signature::verify(*sigPK, message->getSignature(),
Expand All @@ -153,12 +153,12 @@ vector<unsigned> Arbiter::responderResolveI(const vector<string> &ks,
}

// also need to make sure the contract hasn't expired
if (!contract.checkTimeout(timeoutTolerance)){
if (!contract->checkTimeout(timeoutTolerance)){
throw CashException(CashException::CE_FE_ERROR,
"[Arbiter::responderResolveI] contract has expired");
}

vector<ZZ> end = verifiableDecrypter->decrypt(vEscrow.getCiphertext(),
vector<ZZ> end = verifiableDecrypter->decrypt(vEscrow->getCiphertext(),
sigPK->publicKeyString(),
hashAlg);
// now verify the endorsement (and store it if it's valid)
Expand All @@ -167,14 +167,16 @@ vector<unsigned> Arbiter::responderResolveI(const vector<string> &ks,
endorsement = end;

// set up merkle verifiers and return challenges
hash_t ptHash = contract.getPTHashB();
hash_t ctHash = contract.getCTHashB();
ptVerifier = boost::shared_ptr<MerkleVerifier>(new MerkleVerifier(ptHash,
contract.getNumPTHashBlocksB(),
MerkleContract(ptHash.key,ptHash.alg)));
ctVerifier = boost::shared_ptr<MerkleVerifier>(new MerkleVerifier(ctHash,
contract.getNumCTHashBlocksB(),
MerkleContract(ctHash.key,ctHash.alg)));
hash_t ptHash = contract->getPTHashB();
hash_t ctHash = contract->getCTHashB();
ptVerifier = new_ptr<MerkleVerifier>(
ptHash,
contract->getNumPTHashBlocksB(),
MerkleContract(ptHash.key,ptHash.alg));
ctVerifier = new_ptr<MerkleVerifier>(
ctHash,
contract->getNumCTHashBlocksB(),
MerkleContract(ctHash.key,ctHash.alg));
// XXX: right now this is only returning 0 every time!!
return ptVerifier->getChallenges();
} else {
Expand All @@ -183,11 +185,11 @@ vector<unsigned> Arbiter::responderResolveI(const vector<string> &ks,
}
}

vector<string> Arbiter::responderResolveII(const MerkleProof* proof){
vector<string> Arbiter::responderResolveII(Ptr<const MerkleProof> proof){
if(verifyKeys(proof)){
// decrypt the signature escrow
vector<ZZ> m = message->getEscrow();
string label = saveString(contract);
string label = saveString(*contract);
vector<ZZ> initiatorVals = regularDecrypter->decrypt(m, label, hashAlg);
vector<string> initiatorKeys(initiatorVals.size());
for(unsigned i = 0; i < initiatorVals.size(); i++){
Expand All @@ -202,7 +204,7 @@ vector<string> Arbiter::responderResolveII(const MerkleProof* proof){
}
}

vector<ZZ> Arbiter::responderResolveIII(const MerkleProof* proof){
vector<ZZ> Arbiter::responderResolveIII(Ptr<const MerkleProof> proof){
if (!verifyDecryption(proof)){
return endorsement;
} else {
Expand Down
30 changes: 15 additions & 15 deletions src/Arbiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
/*! \brief This class is for resolving any disputes that may arise in the
* course of a fair exchange protocol */

typedef pair<vector<string>, BuyMessage*> ResolutionPair;
typedef pair<vector<string>, Ptr<BuyMessage> > ResolutionPair;

class Arbiter {
public:
Arbiter(const VEDecrypter* vD, const VEDecrypter* rD,
Arbiter(Ptr<const VEDecrypter> vD, Ptr<const VEDecrypter> rD,
const hashalg_t &h, int t)
: verifiableDecrypter(vD), regularDecrypter(rD), hashAlg(h),
timeoutTolerance(t) {}
Expand All @@ -32,23 +32,23 @@ class Arbiter {
/*! if the proof verifies, output the buyer's endorsement and
* store the seller's keys in the database (for the buyer to
* retrieve at some later date) */
vector<ZZ> sellerResolveII(const MerkleProof* proof);
vector<ZZ> sellerResolveII(Ptr<const MerkleProof> proof);

// the following are resolutions for the responder
/*! Stage I: the responder sends a request, and the arbiter
* checks the validity of the two messages and stores the keys */
vector<unsigned> responderResolveI(const FEResolutionMessage* request);
vector<unsigned> responderResolveI(Ptr<const FEResolutionMessage> request);
vector<unsigned> responderResolveI(const vector<string> &keys,
const FEMessage* message,
const FESetupMessage* setupMessage);
Ptr<const FEMessage> message,
Ptr<const FESetupMessage> setupMessage);

/*! Stage II: if the proof verifies, return the initiator's keys */
vector<string> responderResolveII(const MerkleProof* proof);
vector<string> responderResolveII(Ptr<const MerkleProof> proof);

/*! Stage III: if the initiator's keys were incorrect, the responder
* sends a proof of this. if this proof is valid the arbiter will
* return the endorsement */
vector<ZZ> responderResolveIII(const MerkleProof* prooof);
vector<ZZ> responderResolveIII(Ptr<const MerkleProof> prooof);

//used to test stuff
void setKeys(const vector<string> &ks){ keys = ks; }
Expand All @@ -60,21 +60,21 @@ class Arbiter {
void updateDatabase(const ZZ &sessionID, const string &key);

/*! takes in a proof and verifies it using the stored keys */
bool verifyKeys(const MerkleProof* proof);
bool verifyKeys(Ptr<const MerkleProof> proof);

/*! much like verifyKeys except it ignores plaintext proofs; this
* is a helper for responderResolveIII */
bool verifyDecryption(const MerkleProof* proof);
bool verifyDecryption(Ptr<const MerkleProof> proof);

const VEDecrypter* verifiableDecrypter;//, regularDecrypter;
const VEDecrypter* regularDecrypter;
Ptr<const VEDecrypter> verifiableDecrypter;//, regularDecrypter;
Ptr<const VEDecrypter> regularDecrypter;
hashalg_t hashAlg;
int timeoutTolerance;
boost::shared_ptr<MerkleVerifier> ptVerifier, ctVerifier;
Ptr<MerkleVerifier> ptVerifier, ctVerifier;
vector<ZZ> endorsement;
vector<string> keys;
FEContract contract;
const FEMessage* message;
Ptr<FEContract> contract;
Ptr<const FEMessage> message;
};

#endif
8 changes: 3 additions & 5 deletions src/Bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@
class Bank {

public:
Bank(const GroupPrime* grpPrime, const GroupRSA* secret)
Bank(Ptr<const GroupPrime> grpPrime, Ptr<const GroupRSA> secret)
: groupPrime(grpPrime), secretKey(secret) {}

Bank(const Bank &o)
: groupPrime(o.groupPrime), secretKey(o.secretKey) {}

~Bank() { delete groupPrime; delete secretKey; }

/*! returns a random number from prime-order group */
ZZ randomNumber();

/*! on input a partial commitment A', outputs a full commitment A */
ZZ fullCommitment(const ZZ &part, const ZZ &bankPart);

private:
const GroupPrime* groupPrime;
const GroupRSA* secretKey;
Ptr<const GroupPrime> groupPrime;
Ptr<const GroupRSA> secretKey;
};

#endif /*_BANK_H_*/
39 changes: 17 additions & 22 deletions src/BankParameters.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "BankParameters.h"

BankParameters::BankParameters(vector<GroupRSA> &secrets, GroupPrime &ecashGrp,
vector<int> &denoms)
: coinDenominations(denoms)
BankParameters::BankParameters(const vector<Ptr<GroupRSA> > &secrets,
Ptr<GroupPrime> ecashGrp,
const vector<int> &denoms)
: type(BankParameters::TYPE_SECRET),
coinDenominations(denoms)
{
ecashGroup = new GroupPrime(ecashGrp);
type = BankParameters::TYPE_SECRET;
ecashGroup = new_ptr<GroupPrime>(*ecashGrp);
for (unsigned i = 0; i < secrets.size(); i++) {
secretKeys.push_back(new GroupRSA(secrets[i]));
secretKeys.push_back(new_ptr<GroupRSA>(*secrets[i]));
}

// set up maps that associate a given key with a denomination
Expand All @@ -18,22 +19,14 @@ BankParameters::BankParameters(vector<GroupRSA> &secrets, GroupPrime &ecashGrp,
}

BankParameters::BankParameters(const BankParameters &o)
: ecashGroup(new GroupPrime(*o.ecashGroup)), type(o.type),
secretKeys(o.secretKeys), groupToDenom(o.groupToDenom),
denomToGroup(o.denomToGroup), coinDenominations(o.coinDenominations)
: ecashGroup(new_ptr<GroupPrime>(*o.ecashGroup)), type(o.type),
secretKeys(o.secretKeys), groupToDenom(o.groupToDenom),
denomToGroup(o.denomToGroup), coinDenominations(o.coinDenominations)
{
}

BankParameters::~BankParameters() {
/*delete ecashGroup;
for (unsigned i = 0 ; i < secretKeys.size() ; i++) {
delete secretKeys[i];
}
secretKeys.clear();*/
}

const GroupRSA* BankParameters::getBankKey(int denomination) const {
map<int,GroupRSA*>::const_iterator i = denomToGroup.find(denomination);
Ptr<const GroupRSA> BankParameters::getBankKey(int denomination) const {
map<int,Ptr<GroupRSA> >::const_iterator i = denomToGroup.find(denomination);
if (i == denomToGroup.end()) {
throw CashException(CashException::CE_UNKNOWN_ERROR,
"[BankParameters:getBankKey] Tried to find BankKey for "
Expand All @@ -43,8 +36,8 @@ const GroupRSA* BankParameters::getBankKey(int denomination) const {
return i->second;
}

int BankParameters::getCoinDenomination(GroupRSA* group) const {
map<GroupRSA*,int>::const_iterator i = groupToDenom.find(group);
int BankParameters::getCoinDenomination(Ptr<GroupRSA> group) const {
map<Ptr<GroupRSA> ,int>::const_iterator i = groupToDenom.find(group);
if (i == groupToDenom.end()) {
throw CashException(CashException::CE_UNKNOWN_ERROR,
"[BankParameters:getCoinDenomination] Tried to find denomination "
Expand All @@ -55,7 +48,9 @@ int BankParameters::getCoinDenomination(GroupRSA* group) const {

void BankParameters::makePublic() {
for(unsigned i = 0 ; i < secretKeys.size(); i++) {
secretKeys[i]->clearSecrets();
// copy key, so as not to overwrite original secret key
secretKeys[i] = new_ptr<GroupRSA>(*secretKeys[i]);
secretKeys[i]->clearSecrets();
}
type = BankParameters::TYPE_PUBLIC;
}
Expand Down
Loading