-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathx86_mult256x256_Modulo_OrdreCourbeSepc256k.asm
More file actions
172 lines (151 loc) · 6.08 KB
/
x86_mult256x256_Modulo_OrdreCourbeSepc256k.asm
File metadata and controls
172 lines (151 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
; multiplication de 2 entier 256 bits modulo xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
; version x86 32 bits
.MODEL FLAT ; requis par masm
; déclaration de l'existence d'une fonction externe
multiplication_256x256_512 PROTO C
.DATA
; 0x14551231950b75fc4402da1732fc9bebf
_2P256_MoinsP_Ordre qword 402da1732fc9bebfh, 4551231950b75fc4h, 000000000000001h, 000000000000000h
.CODE
; somme de 2 entier 256 bits => 256 bits + Carry Flag
; edi,src = SRC
; ebx = DST
_add_256_256 PROC
;+0
mov eax,[edi]
add eax,[esi]
mov [ebx],eax
;+4
mov eax,[edi+04h]
adc eax,[esi+04h]
mov [ebx+04h],eax
;+8
mov eax,[edi+08h]
adc eax,[esi+08h]
mov [ebx+08h],eax
;+12
mov eax,[edi+0Ch]
adc eax,[esi+0Ch]
mov [ebx+0Ch],eax
;+16
mov eax,[edi+10h]
adc eax,[esi+10h]
mov [ebx+10h],eax
;+20
mov eax,[edi+14h]
adc eax,[esi+14h]
mov [ebx+14h],eax
;+24
mov eax,[edi+18h]
adc eax,[esi+18h]
mov [ebx+18h],eax
;+28
mov eax,[edi+1Ch]
adc eax,[esi+1Ch]
mov [ebx+1Ch],eax
; fin
ret
_add_256_256 ENDP
;------------------------------------------
_mult256x256_Modulo_OrdreCourbeSepc256k PROC
;void mult256x256_Modulo_OrdreCourbeSepc256k(byte* pNombreA, byte* pNombreB, OUT byte* pResultat256)
;prologue
push ebp
push edi
push esi
push ebx
mov ebp,esp
sub esp,120h ; reserverve mem var locale : 4 entier 512 bits
; ebp-40h ; AB512
; ebp-80h ; resteModulo512
; ebp-C0h ; resteModulo2_512
; ebp-100h ; testDepassement = Resultat + _2P256_MoinsP_Ordre
; recu param
mov ecx,[ebp +20] ; pA
mov esi,[ebp +24] ; pB
; mov edi,[ebp +28] ; pResultat256
; multiplication A*B => 512 bits
lea eax,[ebp-40h] ; AB512 (dest)
push eax
push esi ; B
push ecx ; A
call multiplication_256x256_512; AB512 = A * B
; multiplication des 256 bits de poids fort par 2^256 - P => 0x14551231950b75fc4402da1732fc9bebf
lea eax,[ebp-80h] ; resteModulo512 (dest)
push eax
lea eax,[ebp-20h] ; AxB_512.High
push eax
lea eax,[_2P256_MoinsP_Ordre] ; 2"256-P
push eax
call multiplication_256x256_512; resteModulo512 = AxB_512.High * 0x14551231950b75fc4402da1732fc9bebf
; calcul de :
; A*B.High*0x14551231950b75fc4402da1732fc9bebf + A*B.Low
; résultat : 256 bits + 32 de "carry"
; UI64 C0 = AxB_512.low.v0;
lea edi,[ebp-40h] ; AxB_512.low
lea esi,[ebp-80h] ; resteModulo512.Low
mov ebx,[ebp +28] ; Resultat256 (dest)
call _add_256_256 ; Resultat256 = AxB_512.low + resteModulo512.Low
; ajout du carry au poids fort de resteModulo512.High, avant mutliplication
mov eax,0 ; eax= 0, sans modifier c
adc [esi+20h],eax ; resteModulo512.High
adc [esi+24h],eax
adc [esi+28h],eax
adc [esi+2Ch],eax
adc [esi+30h],eax
adc [esi+34h],eax
adc [esi+38h],eax
adc [esi+3Ch],eax
; multiplication du poids fort de <resteModulo512>
lea eax,[ebp-00C0h] ; resteModulo2_512 (dest)
push eax
lea eax,[_2P256_MoinsP_Ordre] ; 2"256-P
push eax
lea esi,[esi+20h] ; resteModulo512.High
push esi
call multiplication_256x256_512; resteModulo2_512 = resteModulo512.High * 0x14551231950b75fc4402da1732fc9bebf
; 2eme addiction au résultat
; *pResultat256 = *pResultat256 + temp3.Low
mov edi,[ebp +28] ; edi = pResultat256
lea esi,[ebp-00C0h]; resteModulo2_512.low
mov ebx,[ebp +28] ; ebx = pResultat256 (dest)
call _add_256_256 ; pResultat256 = pResultat256 + resteModulo2_512.low
; ajout du carry au poids fort de resteModulo512.High, avant mutliplication
mov eax,0 ; eax= 0, sans modifier c
adc eax,[esi+20h] ; resteModulo2_512.High
test eax,eax
jz pasDeDepassement ; si eax = 0, goto pasDeDepassement
cmp eax,2 ; 2 cas max possiblle
jne _x1 ; si eax !2 , goto _x1:
; il faut encore ajouter 0x14551231950b75fc4402da1732fc9bebf *2
mov ebx,[ebp +28] ; Resultat256 (dest)
lea esi,[_2P256_MoinsP_Ordre] ; ajoute 0x14551231950b75fc4402da1732fc9bebf
mov edi,[ebp +28] ; Resultat256
call _add_256_256 ; Resultat256 = AxB_512.low + resteModu
_x1:
; il faut encore ajouter 0x14551231950b75fc4402da1732fc9bebf
mov ebx,[ebp +28] ; Resultat256 (dest)
lea esi,[_2P256_MoinsP_Ordre] ; ajoute 0x14551231950b75fc4402da1732fc9bebf
mov edi,[ebp +28] ; Resultat256
call _add_256_256 ; Resultat256 = AxB_512.low + resteModulo512.Low
pasDeDepassement:
; cas ou le résultat est plus grand que P = xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
; on fait l'addition pour voir
lea ebx,[ebp-0100h] ; testDepassement (dest)
lea esi,[_2P256_MoinsP_Ordre] ; 0x14551231950b75fc4402da1732fc9bebf
mov edi,[ebp +28] ; Resultat256
call _add_256_256 ; testDepassement = Resultat256 +0x14551231950b75fc4402da1732fc9bebf
jnc fin ; si pas de dépassement, terminé
; on dépasse le modulo
mov ebx,edi ; testDepassement (dest)
call _add_256_256 ; Resultat256 = Resultat256 + 0x14551231950b75fc4402da1732fc9bebf
fin:
; epiloqgue
mov esp,ebp
pop ebx
pop esi
pop edi
pop ebp
ret 12 ; 3 * 4 octest mis sur la pile par l'appelant
_mult256x256_Modulo_OrdreCourbeSepc256k ENDP
END