P
Paul Rubin
Philippe Martin said:I actually need numbers much larger than 32 bits.
What is the max size hex number you need? What is the application if
you don't mind my asking?
Philippe Martin said:I actually need numbers much larger than 32 bits.
Paul said:What is the max size hex number you need? What is the application if
you don't mind my asking?
Philippe Martin said:Well I am under NDA so I cannot tell you what the application is - I need
numbers (dec) with up to 24 digits.
As I said, I went the other way - more data on the line (from dev 1 to dev
2) - but I feel there is no speed issue at this stage.
Paul said:You actually need to represent numbers up to 10**24??
What are your memory constraints?
PS: in smart cards, you count RAM in hundreds of bytes and EEPROM inPaul said:You actually need to represent numbers up to 10**24??
What are your memory constraints?
Philippe Martin said:On device #1 no constraint for my purpose. On the smartcard, the tradeoff is
between using EEPROM (plenty + slow + small life expectancy) for temp
variables versus RAM (very little) ... but I do not think it is an issue
eather in my case. Speed is what worries me most (crypto takes time).
Paul said:You should not have to do this decimal-hex conversion repeatedly in a
crypto operation. Do you have a public-key accelerator (or MAC unit)
on that cpu? Do you really care about speed? I had thought up a cute
O(n**3) scheme that might have been ok for 8 digits but probably not
for 24.
Philippe said:Yes, I came here for the "algorithm" question, not the code result.
'cause you're smartTo turn BCD x to binary integer y,
set y to zero
for each nibble n of x:
y = (((y shifted left 2) + y) shifted left 1) + n
Do you need instruction on extracting nibbles, and shifting and
adding integers?
A problem this small and simple does not call for a prototype.
To turn BCD x to binary integer y,
set y to zero
for each nibble n of x:
y = (((y shifted left 2) + y) shifted left 1) + n
Well I am under NDA so I cannot tell you what the application is - I need
numbers (dec) with up to 24 digits.
As I said, I went the other way - more data on the line (from dev 1 to dev
2) - but I feel there is no speed issue at this stage.
Seems to work.
Regards,
Philippe
**** PYTHON ******
l2 = [1,2,3,4]
l1 = [0,2,5,9]
def sup (l1, l2): #assume same length
for i in range(len(l1) ):
if l1 > l2:
return 1
if l1 < l2:
return -1
return 0
def add (l1, l2): #assume same length
r = []
idx = range (len(l1))
idx.reverse()
carry = 0
for i in idx:
if l1 + l2 > 10:
carry = 1
r.insert(0,(l1 + l2) % 10)
else:
r.insert(0,l1 + l2 + carry)
carry = 0
return r
def sub (l1,l2): #assume same length - sub l1 from l2
r = []
idx = range (len(l1))
idx.reverse()
carry = 0
for i in idx:
print l1 + carry, l2
if ((l2) - (l1+carry) < 0) :
print 'CARRY'
r.insert(0,(((10 + l2) - (l1+carry))))
carry = 1
else:
r.insert(0,(l2) - (l1+ carry))
carry = 0
return r
print sub (l1,l2)
***** AND AM JUST TESTING IT IN JAVACARD ******
//********************************************************************************
public byte CmpD(byte[] p_op1, byte[] p_op2, byte p_len) {
byte l_count = (byte)0;
for (; l_count < p_len; l_count += 1) {
short C = (short)(p_op1[l_count]);
short D = (short)(p_op2[l_count]);
if (C > D) return 1;
if (C < D) return -1;
}
return 0;
}
//********************************************************************************
public static void SubD(byte[] p_op1, byte[] p_op2, byte[] p_dest, byte
p_len) {
byte l_count = (byte)0;
byte l_carry = (byte)0;
for (l_count = (byte)(p_len - (byte)1); l_count >= (byte)0; l_count
-= (byte)1) {
if ((p_op2[l_count] - (byte)(p_op1[l_count]+l_carry) ) < 0) {
p_dest[l_count] = (byte)( ((byte)10 + p_op2[l_count]) -
(byte)(p_op1[l_count] + l_carry)) ;
l_carry = (byte)1;
}
else {
p_dest[l_count] = (byte)( p_op2[l_count] - (byte)(p_op
[l_count] + l_carry)) ;
l_carry = -(byte)0;
}
}
}
//********************************************************************************
public static void AddD(byte[] p_op1, byte[] p_op2, byte[] p_dest, byte
p_len) {
byte l_count = (byte)0;
byte l_carry = (byte)0;
for (l_count = (byte)(p_len - (byte)1); l_count >= (byte)0; l_count
-= (byte)1) {
if (p_op2[l_count] + (byte)(p_op1[l_count]) > 10) {
p_dest[l_count] = (byte)( ( p_op2[l_count] + p_op
[l_count] )% 10) ;
l_carry = (byte)1;
}
else {
p_dest[l_count] = (byte)( p_op2[l_count] + p_op1[l_count] +
l_carry) ;
l_carry = -(byte)0;
John said:So why don't you get a freely available "bignum" package, throw away
the bits you don' t want, and just compile it and use it, instead of
writing your own bug-ridden (see below) routines? Oh yeah, the bignum
package might use "long" and you think that you don't have access to
32-bit longs in the C compiler for the 8-bit device that you mistook
for an arm but then said is an Smc8831 [Google can't find it] with a
CPU that you think is a SC88 [but the manual whose URL you gave is for
an S1C88] ...
*** WHAT HAPPENS IF arg1 > arg2?
John said:**** SHOULD BE >=
currently add([6, 6], [4, 4] -> [10, 10]
*** try - 10 instead of % 10
If the first operand is > 19, you have a bug!
This might save a few CPU cycles on your smartcard
**** SHOULD CHECK FOR CARRY AT END
currently add([9], [8]) -> [7]
should return [1, 7] or handle overflow somehow
Philippe said:John said:So why don't you get a freely available "bignum" package, throw away
the bits you don' t want, and just compile it and use it, instead of
writing your own bug-ridden (see below) routines? Oh yeah, the bignum
package might use "long" and you think that you don't have access to
32-bit longs in the C compiler for the 8-bit device that you mistook
for an arm but then said is an Smc8831 [Google can't find it] with a
CPU that you think is a SC88 [but the manual whose URL you gave is for
an S1C88] ...
Thanks for the fixes - still looking at it.
You are correct, all "bignum" packages I found needed 32 bits.
Yes I still see from my documentation that there is no "long" handled by my
compiler.
I did make a mistake on the CPU (and I really do not care what it is) - you
wanted some ref (I still do not see why)
and I googled S1C88 and sent you a
link as that is the name of the compiler's directory.
Have you actually tried it? Do you mean it barfs on the word "long"
[meaning that it's not an ANSI-compliant C compiler], or that "long" is
only 16 bits?
because (1) [like I said before] gcc appears to be able to generate
code for a vast number of different CPUs (2) because I find it very
difficult to believe that a C compiler for the CPU on a device in
current use won't support 32-bit longs -- and so far you have presented
no credible evidence to the contrary
and is that or is it not the correct link for the documentation for the
compiler that you are using??????
Philippe said:Sorry forgot a few answers/comments:
John said:**** SHOULD BE >=
currently add([6, 6], [4, 4] -> [10, 10]
True, thanks
*** try - 10 instead of % 10
If the first operand is > 19, you have a bug!
This might save a few CPU cycles on your smartcard
can it ? >
each array value will be [0..9]
**** SHOULD CHECK FOR CARRY AT END
currently add([9], [8]) -> [7]
should return [1, 7] or handle overflow somehow
True, this actually should become an error from the card to the calling
device. That would be a pretty large number though.
I just do not want to leave it at one(1) if it is.
Simon said:Philippe, please! The suspense is killing me. What's the cpu!?
For the love of God, what's the CPU?
I-can't-take-it-anymore-it's-such-a-simple-question-ingly yours,
John said:Yes, please .....
I've found a C compiler manual on the web for the Epson S1C33 CPU as
well as the one for the S1C88 that Philippe pointed me at. They have
two things in common:
(1) explicitly mention support for 32-bit longs
(2) in the bottom right corner of most pages, it has the part number
(which includes S1Cxx) and the version number.
Philippe has what he believes to be the manual for the C compiler for
the CPU in the device, but couldn't find it on the web.
Perhaps if Philippe could divulge the part number that's in the bottom
right corner of the manual that he has, and/or any part number that
might be mentioned in the first few pages of that manual, enlightenment
may ensue ....
Cheers,
John
That was cute ... over and out !
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.