add curve25519 and toy

This commit is contained in:
~hatteb-mitlyd 2014-04-10 15:48:41 -07:00
parent 33bb87af0f
commit 8a7a789994
2 changed files with 92 additions and 0 deletions

34
try/bin/curve.hoon Normal file
View File

@ -0,0 +1,34 @@
::
:: /=main=/bin/curve/hoon
::
=> %= .
+
=> +
^/===/lib/cryo
==
|= [est=time eny=@uw]
|= ~
^- bowl
:_ ~ :_ ~
:- %$
!>
=+ pk=48.084.050.389.777.770.101.701.157.326.923.977.117.
307.187.144.965.043.058.462.938.058.489.685.090.437
=+ sk=(cla:curv 19.076.158.533.740.022.697.853.188.432.810.
029.468.508.100.820.210.985.396.154.491.514.718.125.885.303)
=+ ska=(gen:curv eny)
=+ sky=(gen:curv (shax eny))
:*
%public-test
:+
[%reference-public pk]
[%calculate-public (curve:curv sk 9)]
[%equals =((curve:curv sk 9) pk)]
%diffie-hellman
[%alice-secret ska]
[%bob-secret sky]
[%alice-calcs (curve:curv sky (curve:curv ska 9))]
[%bobp-calcs (curve:curv ska (curve:curv sky 9))]
%equals
=((curve:curv sky (curve:curv ska 9)) (curve:curv ska (curve:curv sky 9)))
==

View File

@ -337,4 +337,62 @@
=((scam bb ss) (ward u.rr (scam u.aa h)))
::
--
++ curv
=+ pol=486.662
|%
++ gen
|= ent=@
(cla (shaw 0 256 ent))
::
++ cla
|= raw=@
=+ low=(dis 248 (cut 3 [0 1] raw))
=+ hih=(con 64 (dis 127 (cut 3 [31 1] raw)))
=+ mid=(cut 3 [1 30] raw)
(can 3 [[1 low] [30 mid] [1 hih] ~])
::
++ sqr |= in=@ (mul in in)
::
++ pri (sub (bex 255) 19)
::
++ inv
|= in=@
(~(exp fo pri) (sub pri 2) in)
::
++ cad
|= [n=[x=@ z=@] m=[x=@ z=@] d=[x=@ z=@]]
=+ ^= xx
;: mul 4 z.d
(sqr (abs:si (dif:si (sun:si (mul x.m x.n)) (sun:si (mul z.m z.n)))))
==
=+ ^= zz
;: mul 4 x.d
(sqr (abs:si (dif:si (sun:si (mul x.m z.n)) (sun:si (mul z.m x.n)))))
==
[(mod xx pri) (mod zz pri)]
::
++ cub
|= [x=@ z=@]
=+ ^= xx
(mul (sqr (abs:si (dif:si (sun:si x) (sun:si z)))) (sqr (add x z)))
=+ ^= zz
:(mul 4 x z :(add (sqr x) :(mul pol x z) (sqr z)))
[(mod xx pri) (mod zz pri)]
::
++ curve
|= [n=@ base=@]
=+ one=[base 1]
=+ two=(cub one)
=+ i=253
=+ q=one
=+ r=(cub one)
|-
?: =(i 0)
=+ x=(cub q)
(mod (mul -.x (inv +.x)) pri)
=+ m=(rsh 0 i n)
?: =(0 (mod m 2))
$(i (dec i), r (cad q r one), q (cub q))
$(i (dec i), q (cad q r one), r (cub r))
--
--