Paolo Veronelli wrote:
Is there any standard protocol for calculating a unique number from a record/atom ? The question is related to RSA signing records by authors.Any consideration about this matter and oz can be helpful.

Waiting for replies, I completed the functor so we have a solution to blame :P
I doubt the Hash procedure is reliable as based on the Pickle functor.
Is that serialization portable,repetibile on different platforms and stable in time or planned to change in the future.
The last hypothesis could invalidate all the past-to-change signings.

I'm not concerned about code speed , but *any* suggestion on the code is welcome.

Thanks again
Paolino


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RSA functor %%%%%%%%%%%%%%%%

functor
import OS Pickle
export new:NewRSA verify:Verify sign:Sign
define
   fun {ModPow B E M} % exponentiation under modulo
      A={NewCell 1} CB={NewCell B}
      proc {Loop E}
         if E>0 then
            if (E mod 2)==1 then A:=(@[EMAIL PROTECTED]) mod M end
            CB:=(@[EMAIL PROTECTED]) mod M
            {Loop (E div 2)}
         end
      end
   in {Loop E} @A end
   fun {RandIn P} % between (0..P) random generator
      Y={OS.randLimits _ $}
      fun {Rand X L}
         C D=(P-1) div L
         R=X+L*(if D>Y then C=true {OS.rand}
                else C=false {OS.rand} mod D
                end)
      in
         if C then {Rand R L*Y} else R+1 end
      end
   in {Rand 0 1} end
   fun {Fermat P K} %Fermat test
      for I in 0..K default:true return:R do
         if {ModPow {RandIn P} P-1 P} \= 1 then {R false} end
      end
   end
fun {APrime Bits K} B={Pow 2 Bits} in % generate a prime of Bits bits passing K Fermat tests
      for return:R do A=1+{RandIn B} in
         if {Fermat A K} then {R A} end
      end
   end
   proc {Euclide A B X Y} % Extended Euclidean algorithm
      if B==0 then X=1 Y=0 else
         local X1 Y1 {Euclide B (A mod B) X1 Y1}
         in X=Y1 Y=X1-Y1*(A div B) end
      end
   end  
   proc {NewRSA Bits K Pr M} X % generate an RSA key
      P={APrime Bits K} Q={APrime Bits K} T=(P-1)*(Q-1)
   in
      M=P*Q {Euclide 65537 T X _} Pr=T+X mod T
   end
   fun {Hash V M}
      N={NewCell 0} B={Pickle.pack V}
   in
      for I in 0..{ByteString.length B}-1 do
         N:[EMAIL PROTECTED] B I}
      end
      @N mod M
   end
   fun {Sign V E M} {ModPow {Hash V M} E M} end
   fun {Verify S V M} {ModPow S 65537 M}=={Hash V M} end
end

%% Test %%
% local
%    NBits=256 FermatParameter=100 Private Modulus TestLoop=100
% in
%    {NewRSA NBits FermatParameter Private Modulus}
%    {Browse 'generated key'(private:Private modulus:Modulus)}
%    {Browse success=
%     for I in 0..TestLoop default:success return:R do F={RandIn Modulus} in
% if {Verify {Sign F Private Modulus} F Modulus} then skip else {R failure} end
%     end
%    }
% end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RSA functor ends %%%%%%%%%%%%%%%%

        

        
                
___________________________________ Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB http://mail.yahoo.it


_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to