Advertisement
Zeda

miller_rabin_2

Nov 28th, 2015
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. miller_rabin_2:
  2. ;;Performs a Miller-Rabin probable prime test, with base 2
  3. ;;Input: HL
  4. ;; NC if failed, C if passed
  5.     ld b,h  ;
  6.     ld c,l  ;
  7.     dec hl  ;
  8.     ld a,l  ;
  9.     rrca    ;
  10.     ccf     ;
  11.     ret nc  ;
  12.     or h    ;
  13.     ret z   ;
  14.     ld de,2 ;
  15.     ld a,17 ;
  16.     dec a       ;\
  17.     add hl,hl   ; |Get size while prepping for modular exponentiation by repeated squaring
  18.     jr nc,$-2   ;/
  19. mr2_loop:
  20.     push af
  21.     ld a,h      ;\
  22.     or l        ; |If HL is zero, time to go to part 2 of the test
  23.     jr z,mr2sqr ;/
  24.     call sqr_mod;  DE*DE mod (mod)
  25.     add hl,hl   ; |If incoming bit is 1,
  26.     jr nc,$+18  ;/ mul DE by 2, else skip
  27.     ex de,hl    ;\
  28.     add hl,hl   ; |Mul DE by 2,
  29.     jr nc,$+8   ; |\
  30.     or a        ; | |
  31.     sbc hl,bc   ; | |Now MOD
  32.     jp $+8      ; | |
  33.     sbc hl,bc   ; | |
  34.     jr nc,$+3   ; | |
  35.     add hl,bc   ; |/
  36.     ex de,hl    ;/
  37.     pop af
  38.     dec a
  39.     jp mr2_loop
  40. mr2sqr:
  41.     ;;if DE = 1, or BC-1, then return c flag set
  42.     pop hl  ;counter in H
  43.     or d
  44.     jr nz,$+7
  45.     or e
  46.     dec a
  47.     scf
  48.     ret z
  49.     or a
  50.     ex de,hl
  51.     inc hl
  52.     sbc hl,bc
  53.     ccf
  54.     ret z
  55.     add hl,bc
  56.     dec hl
  57.     ex de,hl
  58. mr2sqr_loop:
  59.     call sqr_mod
  60.     ;;if DE = 1, return NC
  61.     ;;If DE = BC-1, return C
  62.     ld a,d
  63.     or a
  64.     jr nz,$+5
  65.     dec e
  66.     ret z
  67.     inc e
  68.     inc de
  69.     ex de,hl
  70.     sbc hl,bc
  71.     ccf
  72.     ret z
  73.     add hl,bc
  74.     dec hl
  75.     ex de,hl
  76.     dec h
  77.     jr nz,mr2sgr_loop
  78.     ret
  79.  
  80. sqr_mod:
  81. ;;DE*DE mod BC, DE<BC
  82. ;;1343.625cc avg
  83.     push hl
  84.     ld hl,0
  85.     ld a,d
  86.     rla \ jr nc,$+4 \ ld h,d \ ld l,e
  87.     call sqr_mod_sub0+3 ;577.125 avg
  88.     ld a,e
  89.     call sqr_mod_sub0   ;662cc avg
  90.     ex de,hl
  91.     pop hl
  92.     ret
  93. sqr_mod_sub0:
  94. ;;662cc avg
  95.     call sqr_mod_sub
  96.     call sqr_mod_sub
  97.     call sqr_mod_sub
  98.     call sqr_mod_sub
  99.     call sqr_mod_sub
  100.     call sqr_mod_sub
  101.     call sqr_mod_sub
  102. sqr_mod_sub:
  103. ;;max: 110cc
  104. ;;min: 38
  105. ;;avg: 67.875cc
  106. ;;38+{0,14}     ;.5     *45
  107. ;;79+{0,14}     ;.25    *86
  108. ;;81+{0,14}+{0,15};.25  *95.5
  109.     add hl,hl \ jr nc,$+5 \ ccf \ sbc hl,bc
  110.     rla \ ret nc
  111.     add hl,de \ jr nc,$+8 \ ccf \ sbc hl,bc \ ret
  112.     sbc hl,bc \ ret nc \ add hl,bc
  113.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement