From 93b1a18fd61890eb4976a7f52d17e0990ccca661 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 8 Oct 2016 21:57:51 +0200 Subject: Fix some serious bugs with casting --- bigint.cpp | 24 ++++++++++++++++++++---- bigint.h | 4 ++-- rsa.cpp | 4 ++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/bigint.cpp b/bigint.cpp index 639d263..8cd06ab 100644 --- a/bigint.cpp +++ b/bigint.cpp @@ -16,8 +16,15 @@ Bigint Bigint::two(2); Bigint::Bigint() :sign(1){} +Bigint::Bigint(Bigint &&o) + :digits(move(o.digits)),sign(o.sign){ + o.normalise(); + checkconsistent(); +} + Bigint::Bigint(const string &repr){ stringstream(repr)>>*this; + checkconsistent(); } Bigint::Bigint(slongdigit_t v) @@ -145,6 +152,15 @@ void Bigint::checkconsistent(){ assert(digits.size()!=0||sign==1); } +Bigint& Bigint::operator=(Bigint &&o){ + sign=o.sign; + digits=move(o.digits); + o.normalise(); + normalise(); + checkconsistent(); + return *this; +} + Bigint& Bigint::operator=(slongdigit_t v){ digits.clear(); if(v==0){ @@ -498,21 +514,21 @@ string Bigint::serialiseMantissa() const { int sz=digits.size(); for(int i=0;i>(8*j))&255; + s[i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&0xff; } } return s; } void Bigint::deserialiseMantissa(const string &s){ - assert(s.size()%4==0); + assert(s.size()%sizeof(digit_t)==0); sign=1; - int sz=s.size()/4; + int sz=s.size()/sizeof(digit_t); digits.resize(sz); for(int i=0;i4); - int modlen=(unsigned char)(pubdeser[0])+(unsigned char)(pubdeser[1]<<8)+ - (unsigned char)(pubdeser[2]<<16)+(unsigned char)(pubdeser[3]<<24); + int modlen=(uint8_t)pubdeser[0]+((uint8_t)pubdeser[1]<<8)+ + ((uint8_t)pubdeser[2]<<16)+((uint8_t)pubdeser[3]<<24); assert((int)pubdeser.size()-4>modlen); PrivateKey key; key.pub.mod.deserialiseMantissa(string(pubdeser.begin()+4,pubdeser.begin()+(4+modlen))); -- cgit v1.2.3-70-g09d2