aboutsummaryrefslogtreecommitdiff
path: root/test/test_fp.ml
blob: 2398e81c1673a7278ac0a754c3cdac64f66c80d9 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module Fp = Plonkers.Bls12_381.Fields.Fp

let fp = Primitives.Testables.fp
let f = Primitives.Literals.fp

let test_strconv () =
  let a =
    f
      "3992956950364942669405818869712257089296386788638306564835014403687782923490610333520388803678873783219740875733957"
  in
  Alcotest.(check string)
    "equals"
    "3992956950364942669405818869712257089296386788638306564835014403687782923490610333520388803678873783219740875733957"
    (Fp.to_string a);
  Alcotest.(check fp)
    "equals" a
    (Fp.of_string
       "3992956950364942669405818869712257089296386788638306564835014403687782923490610333520388803678873783219740875733957")

let test_add () =
  Alcotest.(check fp)
    "equals"
    (f
       "2374810751739477100463590330921093690716317946317392463784626903791011820633183400586641651596983124871770987834796")
    (Fp.add
       (f
          "2539779101900456646568144715229762367285486475239783931158627624678891805390869875565288868071668287807229226543736")
       (f
          "3837441205060687847313235441427235479987714291016616417958057415236151665733151389464040412654330501102436033850847"));
  Alcotest.(check fp)
    "equals" Fp.zero
    (Fp.add
       (f
          "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786")
       Fp.one);
  Alcotest.(check fp)
    "equals"
    (f
       "384476669312388421311524501123823125955339059132692747612224236132178269378031149492449497597195990768128059508632")
    (Fp.add
       (f
          "384476669312388421311524501123823125955339059132692747612224236132178269378031149492449497597195990768128059508632")
       Fp.zero)

let test_sub () =
  Alcotest.(check fp)
    "equals"
    (f
       "165319330386450259644373203077974807220645910781758561016457044290634131611762591723983872825173884569834926691719")
    (Fp.sub
       (f
          "1405893192175380362982591361710914795706649812325486837308052211302962206739907466200494461868518523181162574586698")
       (f
          "1240573861788930103338218158632939988486003901543728276291595167012328075128144874476510589043344638611327647894979"));
  Alcotest.(check fp)
    "equals"
    (f
       "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559786")
    (Fp.sub Fp.zero Fp.one);
  Alcotest.(check fp)
    "equals"
    (f
       "273700764787627457799060044282637660524549878241623469690390886482859643881080320614421407637255214635882056143889")
    (Fp.sub
       (f
          "273700764787627457799060044282637660524549878241623469690390886482859643881080320614421407637255214635882056143889")
       Fp.zero)

let test_mul () =
  Alcotest.(check fp)
    "equals"
    (f
       "1560876702381502869417555148367252552481105620035123238299609747598054883306126748205686202973654059380727975591234")
    (Fp.mul
       (f
          "279547634005300029102205018299671482205774783353195390298355861578412563978526787881524215497557795647889312315751")
       (f
          "3427620255462168869577649592045372288347103136173632621660416459924986394929772961627679067072044927454006261449499"));
  Alcotest.(check fp)
    "equals"
    (f
       "593967545910422807191227856826044389349650065275129056544730655947755371733753954313104753215106376170448509392675")
    (Fp.mul
       (f
          "593967545910422807191227856826044389349650065275129056544730655947755371733753954313104753215106376170448509392675")
       Fp.one);
  Alcotest.(check fp)
    "equals" Fp.zero
    (Fp.mul
       (f
          "703545455926396945740715333992050752005021427598800842803073904307617362496908026217784215025628566198772571250209")
       Fp.zero)

let test_div () =
  Alcotest.(check fp)
    "equals"
    (f
       "2059618063413968434526823257816555562470552560374598768804163111617667105525168050037627517320850541807313010917014")
    (Fp.div
       (f
          "1381636354886970246730091838529094596789078057530348045495053859864349304785635308306479084085939937199744370598307")
       (f
          "688727003716696760215586516252987190967903805200491271639028323343688532340510486329341963276230891156889916003308"));
  Alcotest.(check fp)
    "equals"
    (f
       "2348053930693773880547098734394756539121977002913164528291476932235726021574920900040357432776074408113136192282638")
    (Fp.div
       (f
          "2348053930693773880547098734394756539121977002913164528291476932235726021574920900040357432776074408113136192282638")
       Fp.one);
  Alcotest.(check fp)
    "equals" Fp.zero
    (Fp.div
       (f
          "1347873489628120094114318613072587370478512551696948280446283440627587705929216354976256274092780667293679712470563")
       Fp.zero)

let test_neg () =
  Alcotest.(check fp)
    "equals"
    (f
       "1775670041761213755815401858726796002231574244090972443195008370472258001347895474907169245049301843890576532344557")
    (Fp.neg
       (f
          "2226739513460453637602387967009108154325308575848035442137049765651773649142942389535518384079713820147317740215230"));
  Alcotest.(check fp)
    "equals"
    (f
       "-267753732583775504900422651862463267675519388944913356213411204915826527539794931857138413100398101938254677755732")
    (Fp.neg
       (f
          "267753732583775504900422651862463267675519388944913356213411204915826527539794931857138413100398101938254677755732"));
  let a =
    f
      "2497097842558359777178016466699043409599265754417843473351354622561745125294339148741115501035447061946244255033869"
  in
  Alcotest.(check fp) "equals" Fp.zero (Fp.add a (Fp.neg a))

let test_inv () =
  Alcotest.(check fp)
    "equals"
    (f
       "752719029789714125672585145391317961774250034420382245434007703830544244069267564372796838570703363298508273612998")
    (Fp.inv
       (f
          "74640385774984699849001899276665348548471758265379665870133980059008860817791552369863298623063427760979069744495"));
  let a =
    f
      "736160090838971531915667613244838885092950617728976245438065331888702337925537742686895723638527382814325809582891"
  in
  Alcotest.(check fp) "equals" Fp.one (Fp.mul a (Fp.inv a));
  Alcotest.(check fp) "equals" Fp.zero (Fp.inv Fp.zero)

let test_pow () =
  Alcotest.(check fp) "equals" Fp.zero Fp.(pow zero Z.zero);
  let a =
    f
      "883009121996480731997844824055715487937601322843563036349689188317953865689440811145131419685576769413625710611759"
  in
  let exp =
    Z.of_string
      "223757086517925205380917559391900479644866286017013462881064438760593187397026055204642886097455808395507938587263"
  in
  Alcotest.(check fp)
    "equals"
    (f
       "3268225642397183913064723644900368513801378259146181554748612053788987883399665250663628812236993697779970815748294")
    (Fp.pow a exp);
  let a =
    f
      "2283425044965116906757153222622463605433968256276699869444255593166230225314960979860307763136427211928023277465543"
  in
  let exp =
    Z.of_string
      "-1313828300586738506740341541676431011123481634174240343155076118345421135367256885266553742244726431317227000519371"
  in
  Alcotest.(check fp)
    "equals"
    (f
       "3656881829387973665970026963644201607212675144469633255094847585836426137076083194800285021271892592999115642210326")
    (Fp.pow a exp)

let tests =
  Alcotest.
    [
      test_case "String Conversion" `Quick test_strconv;
      test_case "Addition" `Quick test_add;
      test_case "Subtraction" `Quick test_sub;
      test_case "Multiplication" `Quick test_mul;
      test_case "Division" `Quick test_div;
      test_case "Additive Inverse" `Quick test_neg;
      test_case "Multiplicative Inverse" `Quick test_inv;
      test_case "Exponentiation" `Quick test_pow;
    ]