grpc-flamingo 1.11.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (452) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1150 -176
  3. data/etc/roots.pem +40 -196
  4. data/include/grpc/grpc.h +49 -8
  5. data/include/grpc/grpc_security.h +123 -2
  6. data/include/grpc/grpc_security_constants.h +6 -0
  7. data/include/grpc/impl/codegen/fork.h +4 -4
  8. data/include/grpc/impl/codegen/grpc_types.h +26 -5
  9. data/include/grpc/impl/codegen/log.h +112 -0
  10. data/include/grpc/impl/codegen/port_platform.h +55 -4
  11. data/include/grpc/module.modulemap +2 -0
  12. data/include/grpc/support/log.h +2 -80
  13. data/include/grpc/support/string_util.h +2 -0
  14. data/include/grpc/support/sync.h +0 -16
  15. data/src/boringssl/err_data.c +602 -588
  16. data/src/core/ext/{census → filters/census}/grpc_context.cc +0 -0
  17. data/src/core/ext/filters/client_channel/channel_connectivity.cc +1 -1
  18. data/src/core/ext/filters/client_channel/client_channel.cc +1234 -1070
  19. data/src/core/ext/filters/client_channel/client_channel.h +5 -0
  20. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +113 -0
  21. data/src/core/ext/filters/client_channel/client_channel_channelz.h +71 -0
  22. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +9 -0
  23. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +1 -1
  24. data/src/core/ext/filters/client_channel/http_proxy.cc +22 -5
  25. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -2
  26. data/src/core/ext/filters/client_channel/lb_policy.h +30 -10
  27. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +11 -9
  28. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +120 -127
  29. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h +36 -0
  30. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.cc +36 -102
  31. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +37 -32
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +25 -22
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +4 -2
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.c +19 -0
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h +54 -0
  36. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.c +19 -0
  37. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h +54 -0
  38. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +4 -17
  39. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +37 -63
  40. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +306 -239
  41. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +444 -392
  42. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +538 -98
  43. data/src/core/ext/filters/client_channel/lb_policy_factory.cc +8 -0
  44. data/src/core/ext/filters/client_channel/lb_policy_factory.h +4 -0
  45. data/src/core/ext/filters/client_channel/method_params.h +4 -0
  46. data/src/core/ext/filters/client_channel/resolver.h +10 -0
  47. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +36 -19
  48. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +320 -0
  49. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +62 -9
  50. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +49 -294
  51. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +537 -0
  52. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +112 -87
  53. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +17 -2
  54. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +6 -5
  55. data/src/core/ext/filters/{load_reporting/server_load_reporting_filter.h → client_channel/resolver/dns/c_ares/grpc_ares_wrapper_posix.cc} +7 -8
  56. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +29 -0
  57. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +32 -15
  58. data/src/core/ext/filters/client_channel/retry_throttle.h +4 -0
  59. data/src/core/ext/filters/client_channel/subchannel.cc +58 -15
  60. data/src/core/ext/filters/client_channel/subchannel.h +11 -0
  61. data/src/core/ext/filters/deadline/deadline_filter.cc +18 -15
  62. data/src/core/ext/filters/deadline/deadline_filter.h +5 -5
  63. data/src/core/ext/filters/http/client/http_client_filter.cc +10 -9
  64. data/src/core/ext/filters/http/client_authority_filter.cc +6 -5
  65. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +6 -6
  66. data/src/core/ext/filters/http/server/http_server_filter.cc +123 -131
  67. data/src/core/ext/filters/http/server/http_server_filter.h +1 -1
  68. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +1 -1
  69. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  70. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +3 -2
  71. data/src/core/ext/transport/chttp2/transport/bin_decoder.cc +9 -8
  72. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +97 -48
  73. data/src/core/ext/transport/chttp2/transport/flow_control.cc +10 -7
  74. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +3 -3
  75. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +12 -8
  76. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +4 -3
  77. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +2 -2
  78. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +2 -2
  79. data/src/core/ext/transport/chttp2/transport/parsing.cc +14 -12
  80. data/src/core/ext/transport/chttp2/transport/stream_lists.cc +3 -3
  81. data/src/core/ext/transport/chttp2/transport/writing.cc +32 -27
  82. data/src/core/ext/transport/inproc/inproc_transport.cc +87 -49
  83. data/src/core/lib/channel/channel_args.cc +28 -0
  84. data/src/core/lib/channel/channel_args.h +4 -0
  85. data/src/core/lib/channel/channel_stack.cc +22 -29
  86. data/src/core/lib/channel/channel_stack.h +2 -2
  87. data/src/core/lib/channel/channel_stack_builder.cc +0 -3
  88. data/src/core/lib/channel/channel_stack_builder.h +0 -2
  89. data/src/core/lib/channel/channel_trace.cc +28 -63
  90. data/src/core/lib/channel/channel_trace.h +13 -17
  91. data/src/core/lib/channel/channelz.cc +153 -0
  92. data/src/core/lib/channel/channelz.h +133 -0
  93. data/src/core/lib/channel/channelz_registry.cc +145 -0
  94. data/src/core/lib/channel/channelz_registry.h +120 -0
  95. data/src/core/lib/channel/connected_channel.cc +8 -1
  96. data/src/core/lib/channel/handshaker.cc +71 -0
  97. data/src/core/lib/channel/handshaker.h +4 -0
  98. data/src/core/lib/debug/stats.h +7 -0
  99. data/src/core/lib/debug/stats_data.cc +5 -0
  100. data/src/core/lib/debug/stats_data.h +120 -0
  101. data/src/core/lib/debug/trace.cc +2 -1
  102. data/src/core/lib/debug/trace.h +12 -1
  103. data/src/core/lib/gpr/alloc.h +28 -0
  104. data/src/core/lib/gpr/arena.cc +38 -45
  105. data/src/core/lib/gpr/log.cc +8 -2
  106. data/src/core/lib/gpr/log_android.cc +4 -0
  107. data/src/core/lib/gpr/log_linux.cc +4 -0
  108. data/src/core/lib/gpr/log_posix.cc +4 -0
  109. data/src/core/lib/gpr/log_windows.cc +5 -0
  110. data/src/core/lib/gpr/string.cc +28 -0
  111. data/src/core/lib/gpr/string.h +10 -0
  112. data/src/core/lib/gprpp/abstract.h +5 -2
  113. data/src/core/lib/gprpp/fork.cc +268 -0
  114. data/src/core/lib/gprpp/fork.h +88 -0
  115. data/src/core/lib/gprpp/inlined_vector.h +87 -37
  116. data/src/core/lib/gprpp/memory.h +12 -0
  117. data/src/core/lib/gprpp/mutex_lock.h +42 -0
  118. data/src/core/lib/gprpp/orphanable.h +10 -12
  119. data/src/core/lib/gprpp/ref_counted.h +10 -12
  120. data/src/core/lib/gprpp/ref_counted_ptr.h +65 -8
  121. data/src/core/lib/gprpp/thd.h +0 -3
  122. data/src/core/lib/gprpp/thd_posix.cc +5 -54
  123. data/src/core/lib/gprpp/thd_windows.cc +0 -7
  124. data/src/core/lib/http/httpcli_security_connector.cc +1 -3
  125. data/src/core/lib/iomgr/call_combiner.cc +13 -13
  126. data/src/core/lib/iomgr/call_combiner.h +84 -1
  127. data/src/core/lib/iomgr/closure.h +6 -5
  128. data/src/core/lib/iomgr/combiner.cc +30 -13
  129. data/src/core/lib/iomgr/combiner.h +1 -1
  130. data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
  131. data/src/core/lib/iomgr/error.cc +12 -0
  132. data/src/core/lib/iomgr/error.h +5 -0
  133. data/src/core/lib/iomgr/ev_epoll1_linux.cc +138 -51
  134. data/src/core/lib/iomgr/ev_epollex_linux.cc +276 -93
  135. data/src/core/lib/iomgr/ev_epollsig_linux.cc +58 -50
  136. data/src/core/lib/iomgr/ev_poll_posix.cc +163 -42
  137. data/src/core/lib/iomgr/ev_posix.cc +88 -24
  138. data/src/core/lib/iomgr/ev_posix.h +48 -12
  139. data/src/core/lib/iomgr/exec_ctx.cc +15 -9
  140. data/src/core/lib/iomgr/exec_ctx.h +48 -20
  141. data/src/core/lib/iomgr/executor.cc +274 -142
  142. data/src/core/lib/iomgr/executor.h +82 -16
  143. data/src/core/lib/iomgr/fork_posix.cc +42 -19
  144. data/src/core/lib/iomgr/iocp_windows.cc +9 -4
  145. data/src/core/lib/iomgr/iomgr.cc +2 -0
  146. data/src/core/lib/iomgr/iomgr.h +5 -0
  147. data/src/core/lib/iomgr/iomgr_posix.cc +2 -2
  148. data/src/core/lib/iomgr/is_epollexclusive_available.cc +1 -0
  149. data/src/core/lib/iomgr/lockfree_event.cc +5 -1
  150. data/src/core/lib/iomgr/polling_entity.cc +11 -2
  151. data/src/core/lib/iomgr/pollset_custom.cc +2 -2
  152. data/src/core/lib/iomgr/port.h +51 -1
  153. data/src/core/lib/iomgr/resolve_address.h +1 -1
  154. data/src/core/lib/iomgr/resolve_address_posix.cc +4 -3
  155. data/src/core/lib/iomgr/resolve_address_windows.cc +3 -2
  156. data/src/core/lib/iomgr/resource_quota.cc +89 -12
  157. data/src/core/lib/iomgr/resource_quota.h +16 -0
  158. data/src/core/lib/iomgr/sockaddr_posix.h +1 -1
  159. data/src/core/lib/iomgr/socket_factory_posix.cc +1 -1
  160. data/src/core/lib/iomgr/socket_mutator.cc +1 -1
  161. data/src/core/lib/iomgr/socket_mutator.h +1 -1
  162. data/src/core/lib/iomgr/socket_utils.h +9 -0
  163. data/src/core/lib/iomgr/socket_utils_common_posix.cc +29 -1
  164. data/src/core/lib/iomgr/socket_utils_linux.cc +0 -1
  165. data/src/core/lib/iomgr/socket_utils_posix.cc +2 -3
  166. data/src/core/lib/iomgr/socket_utils_posix.h +3 -0
  167. data/src/core/lib/iomgr/socket_utils_uv.cc +4 -0
  168. data/src/core/lib/iomgr/socket_utils_windows.cc +4 -0
  169. data/src/core/lib/iomgr/socket_windows.cc +33 -0
  170. data/src/core/lib/iomgr/socket_windows.h +6 -0
  171. data/src/core/lib/iomgr/tcp_client_custom.cc +5 -5
  172. data/src/core/lib/iomgr/tcp_client_posix.cc +10 -11
  173. data/src/core/lib/iomgr/tcp_custom.cc +11 -11
  174. data/src/core/lib/iomgr/tcp_posix.cc +49 -36
  175. data/src/core/lib/iomgr/tcp_server_custom.cc +5 -5
  176. data/src/core/lib/iomgr/tcp_server_posix.cc +16 -36
  177. data/src/core/lib/iomgr/tcp_server_utils_posix.h +1 -1
  178. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +17 -5
  179. data/src/core/lib/iomgr/tcp_server_windows.cc +1 -0
  180. data/src/core/lib/iomgr/tcp_uv.cc +3 -0
  181. data/src/core/lib/iomgr/tcp_windows.cc +18 -2
  182. data/src/core/lib/iomgr/tcp_windows.h +2 -0
  183. data/src/core/lib/iomgr/timer.h +4 -3
  184. data/src/core/lib/iomgr/timer_generic.cc +133 -51
  185. data/src/core/lib/iomgr/timer_manager.cc +12 -14
  186. data/src/core/lib/iomgr/timer_uv.cc +3 -0
  187. data/src/core/lib/iomgr/udp_server.cc +106 -52
  188. data/src/core/lib/iomgr/udp_server.h +8 -4
  189. data/src/core/lib/json/json.cc +12 -1
  190. data/src/core/lib/json/json.h +5 -0
  191. data/src/core/lib/profiling/basic_timers.cc +1 -0
  192. data/src/core/lib/security/context/security_context.cc +8 -8
  193. data/src/core/lib/security/context/security_context.h +6 -2
  194. data/src/core/lib/security/credentials/alts/alts_credentials.h +0 -20
  195. data/src/core/lib/security/credentials/alts/check_gcp_environment_linux.cc +3 -2
  196. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_client_options.cc +7 -7
  197. data/src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h +1 -38
  198. data/src/core/lib/security/credentials/credentials.h +1 -0
  199. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +89 -115
  200. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +16 -0
  201. data/src/core/lib/security/credentials/jwt/json_token.h +2 -0
  202. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +2 -0
  203. data/src/core/lib/security/credentials/local/local_credentials.cc +77 -0
  204. data/src/core/lib/security/credentials/local/local_credentials.h +40 -0
  205. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +11 -7
  206. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -1
  207. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +17 -3
  208. data/src/core/lib/security/security_connector/alts_security_connector.cc +2 -1
  209. data/src/core/lib/security/security_connector/load_system_roots.h +29 -0
  210. data/src/core/lib/{gpr/fork.h → security/security_connector/load_system_roots_fallback.cc} +10 -13
  211. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +165 -0
  212. data/src/core/lib/security/security_connector/load_system_roots_linux.h +44 -0
  213. data/src/core/lib/security/security_connector/local_security_connector.cc +245 -0
  214. data/src/core/lib/security/security_connector/local_security_connector.h +58 -0
  215. data/src/core/lib/security/security_connector/security_connector.cc +79 -32
  216. data/src/core/lib/security/security_connector/security_connector.h +5 -3
  217. data/src/core/lib/security/transport/client_auth_filter.cc +5 -5
  218. data/src/core/lib/security/transport/secure_endpoint.cc +2 -2
  219. data/src/core/lib/security/transport/security_handshaker.cc +7 -2
  220. data/src/core/lib/security/transport/server_auth_filter.cc +4 -7
  221. data/src/core/lib/security/util/json_util.cc +4 -0
  222. data/src/core/lib/slice/slice.cc +6 -2
  223. data/src/core/lib/slice/slice_buffer.cc +27 -7
  224. data/src/core/lib/slice/slice_hash_table.h +4 -0
  225. data/src/core/lib/slice/slice_weak_hash_table.h +4 -0
  226. data/src/core/lib/surface/call.cc +119 -58
  227. data/src/core/lib/surface/call.h +7 -0
  228. data/src/core/lib/surface/channel.cc +50 -18
  229. data/src/core/lib/surface/channel.h +4 -0
  230. data/src/core/lib/surface/completion_queue.cc +153 -18
  231. data/src/core/lib/surface/completion_queue.h +20 -2
  232. data/src/core/lib/surface/completion_queue_factory.cc +13 -4
  233. data/src/core/lib/surface/init.cc +7 -8
  234. data/src/core/lib/surface/init.h +0 -1
  235. data/src/core/lib/surface/server.cc +16 -0
  236. data/src/core/lib/surface/version.cc +1 -1
  237. data/src/core/lib/transport/bdp_estimator.cc +3 -3
  238. data/src/core/lib/transport/bdp_estimator.h +2 -2
  239. data/src/core/lib/transport/byte_stream.cc +1 -1
  240. data/src/core/lib/transport/connectivity_state.cc +6 -7
  241. data/src/core/lib/transport/service_config.cc +2 -2
  242. data/src/core/lib/transport/service_config.h +3 -3
  243. data/src/core/lib/transport/transport.cc +22 -10
  244. data/src/core/lib/transport/transport.h +18 -18
  245. data/src/core/lib/transport/transport_op_string.cc +1 -8
  246. data/src/core/plugin_registry/grpc_plugin_registry.cc +0 -4
  247. data/src/core/tsi/alts/crypt/aes_gcm.cc +2 -0
  248. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +19 -7
  249. data/src/core/tsi/alts/handshaker/alts_handshaker_client.h +10 -0
  250. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h +2 -2
  251. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +38 -3
  252. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h +3 -0
  253. data/src/core/tsi/alts/handshaker/altscontext.pb.c +0 -1
  254. data/src/core/tsi/alts/handshaker/altscontext.pb.h +1 -2
  255. data/src/core/tsi/alts/handshaker/handshaker.pb.c +0 -1
  256. data/src/core/tsi/alts/handshaker/handshaker.pb.h +1 -2
  257. data/src/core/tsi/alts/handshaker/transport_security_common.pb.c +0 -1
  258. data/src/core/tsi/alts/handshaker/transport_security_common.pb.h +1 -1
  259. data/src/core/tsi/alts/handshaker/transport_security_common_api.h +2 -2
  260. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.cc +47 -1
  261. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_integrity_only_record_protocol.h +3 -1
  262. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +12 -11
  263. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.h +7 -2
  264. data/src/core/tsi/fake_transport_security.cc +1 -0
  265. data/src/core/tsi/grpc_shadow_boringssl.h +3006 -0
  266. data/src/core/tsi/local_transport_security.cc +209 -0
  267. data/src/core/tsi/local_transport_security.h +51 -0
  268. data/src/core/tsi/ssl/session_cache/ssl_session.h +2 -0
  269. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +5 -5
  270. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +6 -0
  271. data/src/core/tsi/ssl_transport_security.cc +245 -116
  272. data/src/core/tsi/ssl_types.h +2 -0
  273. data/src/core/tsi/transport_security.cc +14 -0
  274. data/src/core/tsi/transport_security.h +2 -0
  275. data/src/core/tsi/transport_security_interface.h +11 -1
  276. data/src/ruby/bin/math_client.rb +17 -9
  277. data/src/ruby/ext/grpc/extconf.rb +1 -26
  278. data/src/ruby/ext/grpc/rb_channel_credentials.c +3 -3
  279. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +42 -16
  280. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +65 -26
  281. data/src/ruby/lib/grpc/generic/active_call.rb +19 -23
  282. data/src/ruby/lib/grpc/generic/rpc_server.rb +2 -1
  283. data/src/ruby/lib/grpc/version.rb +1 -1
  284. data/src/ruby/pb/generate_proto_ruby.sh +7 -1
  285. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +4 -1
  286. data/src/ruby/spec/call_credentials_spec.rb +1 -1
  287. data/src/ruby/spec/call_spec.rb +1 -1
  288. data/src/ruby/spec/channel_credentials_spec.rb +1 -1
  289. data/src/ruby/spec/channel_spec.rb +1 -1
  290. data/src/ruby/spec/client_auth_spec.rb +1 -12
  291. data/src/ruby/spec/client_server_spec.rb +1 -1
  292. data/src/ruby/spec/compression_options_spec.rb +1 -1
  293. data/src/ruby/spec/error_sanity_spec.rb +1 -1
  294. data/src/ruby/spec/generic/client_stub_spec.rb +16 -4
  295. data/src/ruby/spec/generic/rpc_desc_spec.rb +1 -1
  296. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +1 -1
  297. data/src/ruby/spec/generic/service_spec.rb +1 -1
  298. data/src/ruby/spec/google_rpc_status_utils_spec.rb +1 -12
  299. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +1 -0
  300. data/src/ruby/spec/pb/health/checker_spec.rb +1 -1
  301. data/src/ruby/spec/server_credentials_spec.rb +1 -1
  302. data/src/ruby/spec/server_spec.rb +1 -1
  303. data/src/ruby/spec/spec_helper.rb +1 -0
  304. data/src/ruby/spec/support/services.rb +1 -1
  305. data/src/ruby/spec/time_consts_spec.rb +1 -1
  306. data/third_party/address_sorting/address_sorting.c +17 -11
  307. data/third_party/address_sorting/address_sorting_windows.c +43 -3
  308. data/third_party/address_sorting/include/address_sorting/address_sorting.h +3 -0
  309. data/third_party/boringssl/crypto/asn1/a_int.c +33 -28
  310. data/third_party/boringssl/crypto/asn1/a_mbstr.c +24 -22
  311. data/third_party/boringssl/crypto/asn1/a_utf8.c +13 -11
  312. data/third_party/boringssl/crypto/asn1/asn1_locl.h +3 -0
  313. data/third_party/boringssl/crypto/asn1/tasn_dec.c +40 -19
  314. data/third_party/boringssl/crypto/bio/fd.c +1 -0
  315. data/third_party/boringssl/crypto/bio/file.c +2 -0
  316. data/third_party/boringssl/crypto/bn_extra/convert.c +6 -5
  317. data/third_party/boringssl/crypto/bytestring/ber.c +1 -4
  318. data/third_party/boringssl/crypto/bytestring/cbb.c +116 -16
  319. data/third_party/boringssl/crypto/bytestring/cbs.c +151 -20
  320. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +203 -0
  321. data/third_party/boringssl/crypto/cipher_extra/e_rc2.c +2 -0
  322. data/third_party/boringssl/crypto/cipher_extra/e_tls.c +1 -2
  323. data/third_party/boringssl/crypto/cpu-aarch64-fuchsia.c +55 -0
  324. data/third_party/boringssl/crypto/cpu-aarch64-linux.c +2 -1
  325. data/third_party/boringssl/crypto/dsa/dsa.c +16 -54
  326. data/third_party/boringssl/crypto/ec_extra/ec_asn1.c +9 -10
  327. data/third_party/boringssl/crypto/ecdh/ecdh.c +4 -3
  328. data/third_party/boringssl/crypto/fipsmodule/bcm.c +11 -542
  329. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +57 -112
  330. data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +4 -3
  331. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +128 -70
  332. data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +32 -71
  333. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +64 -118
  334. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +284 -122
  335. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +31 -65
  336. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +274 -218
  337. data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +2 -1
  338. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +187 -27
  339. data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +1 -1
  340. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +124 -81
  341. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +8 -30
  342. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +321 -347
  343. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +326 -66
  344. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +77 -25
  345. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +199 -222
  346. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.h +27 -47
  347. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +155 -96
  348. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +1 -1
  349. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +10 -10
  350. data/third_party/boringssl/crypto/fipsmodule/des/internal.h +2 -0
  351. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +78 -47
  352. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +99 -163
  353. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +3 -10
  354. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +44 -23
  355. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +59 -90
  356. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +38 -65
  357. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64-table.h +5378 -5418
  358. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +17 -26
  359. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.h +15 -11
  360. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +45 -51
  361. data/third_party/boringssl/crypto/fipsmodule/ec/{util-64.c → util.c} +0 -5
  362. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +144 -264
  363. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +78 -62
  364. data/third_party/boringssl/crypto/fipsmodule/modes/ccm.c +256 -0
  365. data/third_party/boringssl/crypto/fipsmodule/modes/internal.h +36 -32
  366. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +9 -7
  367. data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +16 -40
  368. data/third_party/boringssl/crypto/fipsmodule/rsa/internal.h +1 -6
  369. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +57 -39
  370. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +309 -142
  371. data/third_party/boringssl/crypto/fipsmodule/self_check/self_check.c +581 -0
  372. data/third_party/boringssl/crypto/fipsmodule/tls/internal.h +39 -0
  373. data/third_party/boringssl/crypto/fipsmodule/tls/kdf.c +165 -0
  374. data/third_party/boringssl/crypto/internal.h +65 -2
  375. data/third_party/boringssl/crypto/mem.c +0 -2
  376. data/third_party/boringssl/crypto/obj/obj.c +6 -73
  377. data/third_party/boringssl/crypto/thread_pthread.c +35 -5
  378. data/third_party/boringssl/crypto/x509/a_strex.c +11 -11
  379. data/third_party/boringssl/crypto/x509/vpm_int.h +1 -0
  380. data/third_party/boringssl/crypto/x509/x509_vfy.c +4 -0
  381. data/third_party/boringssl/crypto/x509/x509_vpm.c +44 -22
  382. data/third_party/boringssl/crypto/x509/x_name.c +13 -0
  383. data/third_party/boringssl/include/openssl/aead.h +10 -0
  384. data/third_party/boringssl/include/openssl/asn1.h +2 -3
  385. data/third_party/boringssl/include/openssl/base.h +5 -14
  386. data/third_party/boringssl/include/openssl/bio.h +1 -1
  387. data/third_party/boringssl/include/openssl/bn.h +62 -18
  388. data/third_party/boringssl/include/openssl/bytestring.h +53 -28
  389. data/third_party/boringssl/include/openssl/crypto.h +4 -0
  390. data/third_party/boringssl/include/openssl/ec.h +10 -4
  391. data/third_party/boringssl/include/openssl/ec_key.h +7 -6
  392. data/third_party/boringssl/include/openssl/err.h +9 -9
  393. data/third_party/boringssl/include/openssl/evp.h +1 -1
  394. data/third_party/boringssl/include/openssl/rsa.h +35 -10
  395. data/third_party/boringssl/include/openssl/ssl.h +167 -19
  396. data/third_party/boringssl/include/openssl/ssl3.h +0 -1
  397. data/third_party/boringssl/include/openssl/stack.h +1 -1
  398. data/third_party/boringssl/include/openssl/tls1.h +10 -2
  399. data/third_party/boringssl/include/openssl/x509.h +4 -0
  400. data/third_party/boringssl/include/openssl/x509v3.h +1 -0
  401. data/third_party/boringssl/ssl/d1_both.cc +16 -2
  402. data/third_party/boringssl/ssl/dtls_method.cc +1 -1
  403. data/third_party/boringssl/ssl/handoff.cc +285 -0
  404. data/third_party/boringssl/ssl/handshake.cc +26 -12
  405. data/third_party/boringssl/ssl/handshake_client.cc +101 -95
  406. data/third_party/boringssl/ssl/handshake_server.cc +14 -2
  407. data/third_party/boringssl/ssl/internal.h +132 -79
  408. data/third_party/boringssl/ssl/s3_both.cc +2 -2
  409. data/third_party/boringssl/ssl/s3_lib.cc +3 -1
  410. data/third_party/boringssl/ssl/s3_pkt.cc +0 -18
  411. data/third_party/boringssl/ssl/ssl_aead_ctx.cc +1 -4
  412. data/third_party/boringssl/ssl/ssl_asn1.cc +47 -43
  413. data/third_party/boringssl/ssl/ssl_cipher.cc +12 -8
  414. data/third_party/boringssl/ssl/ssl_key_share.cc +3 -1
  415. data/third_party/boringssl/ssl/ssl_lib.cc +83 -14
  416. data/third_party/boringssl/ssl/ssl_privkey.cc +6 -0
  417. data/third_party/boringssl/ssl/ssl_stat.cc +6 -6
  418. data/third_party/boringssl/ssl/ssl_versions.cc +12 -85
  419. data/third_party/boringssl/ssl/ssl_x509.cc +59 -61
  420. data/third_party/boringssl/ssl/t1_enc.cc +73 -124
  421. data/third_party/boringssl/ssl/t1_lib.cc +367 -41
  422. data/third_party/boringssl/ssl/tls13_both.cc +8 -0
  423. data/third_party/boringssl/ssl/tls13_client.cc +98 -184
  424. data/third_party/boringssl/ssl/tls13_enc.cc +88 -158
  425. data/third_party/boringssl/ssl/tls13_server.cc +91 -137
  426. data/third_party/boringssl/ssl/tls_method.cc +0 -17
  427. data/third_party/boringssl/ssl/tls_record.cc +1 -10
  428. data/third_party/boringssl/third_party/fiat/curve25519.c +921 -2753
  429. data/third_party/boringssl/third_party/fiat/curve25519_tables.h +7880 -0
  430. data/third_party/boringssl/third_party/fiat/internal.h +32 -20
  431. data/third_party/boringssl/third_party/fiat/p256.c +1824 -0
  432. metadata +86 -71
  433. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.cc +0 -253
  434. data/src/core/ext/filters/load_reporting/server_load_reporting_filter.cc +0 -222
  435. data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.cc +0 -71
  436. data/src/core/ext/filters/load_reporting/server_load_reporting_plugin.h +0 -61
  437. data/src/core/lib/channel/channel_trace_registry.cc +0 -80
  438. data/src/core/lib/channel/channel_trace_registry.h +0 -43
  439. data/src/core/lib/gpr/fork.cc +0 -78
  440. data/src/core/tsi/transport_security_adapter.cc +0 -235
  441. data/src/core/tsi/transport_security_adapter.h +0 -41
  442. data/src/ruby/bin/apis/google/protobuf/empty.rb +0 -29
  443. data/src/ruby/bin/apis/pubsub_demo.rb +0 -241
  444. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub.rb +0 -159
  445. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub_services.rb +0 -88
  446. data/src/ruby/pb/test/client.rb +0 -764
  447. data/src/ruby/pb/test/server.rb +0 -252
  448. data/src/ruby/spec/pb/package_with_underscore/checker_spec.rb +0 -54
  449. data/src/ruby/spec/pb/package_with_underscore/data.proto +0 -23
  450. data/src/ruby/spec/pb/package_with_underscore/service.proto +0 -23
  451. data/third_party/boringssl/crypto/curve25519/x25519-x86_64.c +0 -247
  452. data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +0 -1674
@@ -1,1674 +0,0 @@
1
- /* Copyright (c) 2015, Google Inc.
2
- *
3
- * Permission to use, copy, modify, and/or distribute this software for any
4
- * purpose with or without fee is hereby granted, provided that the above
5
- * copyright notice and this permission notice appear in all copies.
6
- *
7
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
-
15
- // A 64-bit implementation of the NIST P-256 elliptic curve point
16
- // multiplication
17
- //
18
- // OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
19
- // Otherwise based on Emilia's P224 work, which was inspired by my curve25519
20
- // work which got its smarts from Daniel J. Bernstein's work on the same.
21
-
22
- #include <openssl/base.h>
23
-
24
- #if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
25
-
26
- #include <openssl/bn.h>
27
- #include <openssl/ec.h>
28
- #include <openssl/err.h>
29
- #include <openssl/mem.h>
30
-
31
- #include <string.h>
32
-
33
- #include "../delocate.h"
34
- #include "../../internal.h"
35
- #include "internal.h"
36
-
37
-
38
- // The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
39
- // can serialise an element of this field into 32 bytes. We call this an
40
- // felem_bytearray.
41
- typedef uint8_t felem_bytearray[32];
42
-
43
- // The representation of field elements.
44
- // ------------------------------------
45
- //
46
- // We represent field elements with either four 128-bit values, eight 128-bit
47
- // values, or four 64-bit values. The field element represented is:
48
- // v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192 (mod p)
49
- // or:
50
- // v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512 (mod p)
51
- //
52
- // 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
53
- // apart, but are 128-bits wide, the most significant bits of each limb overlap
54
- // with the least significant bits of the next.
55
- //
56
- // A field element with four limbs is an 'felem'. One with eight limbs is a
57
- // 'longfelem'
58
- //
59
- // A field element with four, 64-bit values is called a 'smallfelem'. Small
60
- // values are used as intermediate values before multiplication.
61
-
62
- #define NLIMBS 4
63
-
64
- typedef uint128_t limb;
65
- typedef limb felem[NLIMBS];
66
- typedef limb longfelem[NLIMBS * 2];
67
- typedef uint64_t smallfelem[NLIMBS];
68
-
69
- // This is the value of the prime as four 64-bit words, little-endian.
70
- static const uint64_t kPrime[4] = {0xfffffffffffffffful, 0xffffffff, 0,
71
- 0xffffffff00000001ul};
72
- static const uint64_t bottom63bits = 0x7ffffffffffffffful;
73
-
74
- static uint64_t load_u64(const uint8_t in[8]) {
75
- uint64_t ret;
76
- OPENSSL_memcpy(&ret, in, sizeof(ret));
77
- return ret;
78
- }
79
-
80
- static void store_u64(uint8_t out[8], uint64_t in) {
81
- OPENSSL_memcpy(out, &in, sizeof(in));
82
- }
83
-
84
- // bin32_to_felem takes a little-endian byte array and converts it into felem
85
- // form. This assumes that the CPU is little-endian.
86
- static void bin32_to_felem(felem out, const uint8_t in[32]) {
87
- out[0] = load_u64(&in[0]);
88
- out[1] = load_u64(&in[8]);
89
- out[2] = load_u64(&in[16]);
90
- out[3] = load_u64(&in[24]);
91
- }
92
-
93
- // smallfelem_to_bin32 takes a smallfelem and serialises into a little endian,
94
- // 32 byte array. This assumes that the CPU is little-endian.
95
- static void smallfelem_to_bin32(uint8_t out[32], const smallfelem in) {
96
- store_u64(&out[0], in[0]);
97
- store_u64(&out[8], in[1]);
98
- store_u64(&out[16], in[2]);
99
- store_u64(&out[24], in[3]);
100
- }
101
-
102
- // To preserve endianness when using BN_bn2bin and BN_bin2bn.
103
- static void flip_endian(uint8_t *out, const uint8_t *in, size_t len) {
104
- for (size_t i = 0; i < len; ++i) {
105
- out[i] = in[len - 1 - i];
106
- }
107
- }
108
-
109
- // BN_to_felem converts an OpenSSL BIGNUM into an felem.
110
- static int BN_to_felem(felem out, const BIGNUM *bn) {
111
- if (BN_is_negative(bn)) {
112
- OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
113
- return 0;
114
- }
115
-
116
- felem_bytearray b_out;
117
- // BN_bn2bin eats leading zeroes
118
- OPENSSL_memset(b_out, 0, sizeof(b_out));
119
- size_t num_bytes = BN_num_bytes(bn);
120
- if (num_bytes > sizeof(b_out)) {
121
- OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
122
- return 0;
123
- }
124
-
125
- felem_bytearray b_in;
126
- num_bytes = BN_bn2bin(bn, b_in);
127
- flip_endian(b_out, b_in, num_bytes);
128
- bin32_to_felem(out, b_out);
129
- return 1;
130
- }
131
-
132
- // felem_to_BN converts an felem into an OpenSSL BIGNUM.
133
- static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in) {
134
- felem_bytearray b_in, b_out;
135
- smallfelem_to_bin32(b_in, in);
136
- flip_endian(b_out, b_in, sizeof(b_out));
137
- return BN_bin2bn(b_out, sizeof(b_out), out);
138
- }
139
-
140
- // Field operations.
141
-
142
- static void felem_assign(felem out, const felem in) {
143
- out[0] = in[0];
144
- out[1] = in[1];
145
- out[2] = in[2];
146
- out[3] = in[3];
147
- }
148
-
149
- // felem_sum sets out = out + in.
150
- static void felem_sum(felem out, const felem in) {
151
- out[0] += in[0];
152
- out[1] += in[1];
153
- out[2] += in[2];
154
- out[3] += in[3];
155
- }
156
-
157
- // felem_small_sum sets out = out + in.
158
- static void felem_small_sum(felem out, const smallfelem in) {
159
- out[0] += in[0];
160
- out[1] += in[1];
161
- out[2] += in[2];
162
- out[3] += in[3];
163
- }
164
-
165
- // felem_scalar sets out = out * scalar
166
- static void felem_scalar(felem out, const uint64_t scalar) {
167
- out[0] *= scalar;
168
- out[1] *= scalar;
169
- out[2] *= scalar;
170
- out[3] *= scalar;
171
- }
172
-
173
- // longfelem_scalar sets out = out * scalar
174
- static void longfelem_scalar(longfelem out, const uint64_t scalar) {
175
- out[0] *= scalar;
176
- out[1] *= scalar;
177
- out[2] *= scalar;
178
- out[3] *= scalar;
179
- out[4] *= scalar;
180
- out[5] *= scalar;
181
- out[6] *= scalar;
182
- out[7] *= scalar;
183
- }
184
-
185
- #define two105m41m9 ((((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9))
186
- #define two105 (((limb)1) << 105)
187
- #define two105m41p9 ((((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9))
188
-
189
- // zero105 is 0 mod p
190
- static const felem zero105 = {two105m41m9, two105, two105m41p9, two105m41p9};
191
-
192
- // smallfelem_neg sets |out| to |-small|
193
- // On exit:
194
- // out[i] < out[i] + 2^105
195
- static void smallfelem_neg(felem out, const smallfelem small) {
196
- // In order to prevent underflow, we subtract from 0 mod p.
197
- out[0] = zero105[0] - small[0];
198
- out[1] = zero105[1] - small[1];
199
- out[2] = zero105[2] - small[2];
200
- out[3] = zero105[3] - small[3];
201
- }
202
-
203
- // felem_diff subtracts |in| from |out|
204
- // On entry:
205
- // in[i] < 2^104
206
- // On exit:
207
- // out[i] < out[i] + 2^105.
208
- static void felem_diff(felem out, const felem in) {
209
- // In order to prevent underflow, we add 0 mod p before subtracting.
210
- out[0] += zero105[0];
211
- out[1] += zero105[1];
212
- out[2] += zero105[2];
213
- out[3] += zero105[3];
214
-
215
- out[0] -= in[0];
216
- out[1] -= in[1];
217
- out[2] -= in[2];
218
- out[3] -= in[3];
219
- }
220
-
221
- #define two107m43m11 \
222
- ((((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11))
223
- #define two107 (((limb)1) << 107)
224
- #define two107m43p11 \
225
- ((((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11))
226
-
227
- // zero107 is 0 mod p
228
- static const felem zero107 = {two107m43m11, two107, two107m43p11, two107m43p11};
229
-
230
- // An alternative felem_diff for larger inputs |in|
231
- // felem_diff_zero107 subtracts |in| from |out|
232
- // On entry:
233
- // in[i] < 2^106
234
- // On exit:
235
- // out[i] < out[i] + 2^107.
236
- static void felem_diff_zero107(felem out, const felem in) {
237
- // In order to prevent underflow, we add 0 mod p before subtracting.
238
- out[0] += zero107[0];
239
- out[1] += zero107[1];
240
- out[2] += zero107[2];
241
- out[3] += zero107[3];
242
-
243
- out[0] -= in[0];
244
- out[1] -= in[1];
245
- out[2] -= in[2];
246
- out[3] -= in[3];
247
- }
248
-
249
- // longfelem_diff subtracts |in| from |out|
250
- // On entry:
251
- // in[i] < 7*2^67
252
- // On exit:
253
- // out[i] < out[i] + 2^70 + 2^40.
254
- static void longfelem_diff(longfelem out, const longfelem in) {
255
- static const limb two70m8p6 =
256
- (((limb)1) << 70) - (((limb)1) << 8) + (((limb)1) << 6);
257
- static const limb two70p40 = (((limb)1) << 70) + (((limb)1) << 40);
258
- static const limb two70 = (((limb)1) << 70);
259
- static const limb two70m40m38p6 = (((limb)1) << 70) - (((limb)1) << 40) -
260
- (((limb)1) << 38) + (((limb)1) << 6);
261
- static const limb two70m6 = (((limb)1) << 70) - (((limb)1) << 6);
262
-
263
- // add 0 mod p to avoid underflow
264
- out[0] += two70m8p6;
265
- out[1] += two70p40;
266
- out[2] += two70;
267
- out[3] += two70m40m38p6;
268
- out[4] += two70m6;
269
- out[5] += two70m6;
270
- out[6] += two70m6;
271
- out[7] += two70m6;
272
-
273
- // in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6
274
- out[0] -= in[0];
275
- out[1] -= in[1];
276
- out[2] -= in[2];
277
- out[3] -= in[3];
278
- out[4] -= in[4];
279
- out[5] -= in[5];
280
- out[6] -= in[6];
281
- out[7] -= in[7];
282
- }
283
-
284
- #define two64m0 ((((limb)1) << 64) - 1)
285
- #define two110p32m0 ((((limb)1) << 110) + (((limb)1) << 32) - 1)
286
- #define two64m46 ((((limb)1) << 64) - (((limb)1) << 46))
287
- #define two64m32 ((((limb)1) << 64) - (((limb)1) << 32))
288
-
289
- // zero110 is 0 mod p.
290
- static const felem zero110 = {two64m0, two110p32m0, two64m46, two64m32};
291
-
292
- // felem_shrink converts an felem into a smallfelem. The result isn't quite
293
- // minimal as the value may be greater than p.
294
- //
295
- // On entry:
296
- // in[i] < 2^109
297
- // On exit:
298
- // out[i] < 2^64.
299
- static void felem_shrink(smallfelem out, const felem in) {
300
- felem tmp;
301
- uint64_t a, b, mask;
302
- int64_t high, low;
303
- static const uint64_t kPrime3Test =
304
- 0x7fffffff00000001ul; // 2^63 - 2^32 + 1
305
-
306
- // Carry 2->3
307
- tmp[3] = zero110[3] + in[3] + ((uint64_t)(in[2] >> 64));
308
- // tmp[3] < 2^110
309
-
310
- tmp[2] = zero110[2] + (uint64_t)in[2];
311
- tmp[0] = zero110[0] + in[0];
312
- tmp[1] = zero110[1] + in[1];
313
- // tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65
314
-
315
- // We perform two partial reductions where we eliminate the high-word of
316
- // tmp[3]. We don't update the other words till the end.
317
- a = tmp[3] >> 64; // a < 2^46
318
- tmp[3] = (uint64_t)tmp[3];
319
- tmp[3] -= a;
320
- tmp[3] += ((limb)a) << 32;
321
- // tmp[3] < 2^79
322
-
323
- b = a;
324
- a = tmp[3] >> 64; // a < 2^15
325
- b += a; // b < 2^46 + 2^15 < 2^47
326
- tmp[3] = (uint64_t)tmp[3];
327
- tmp[3] -= a;
328
- tmp[3] += ((limb)a) << 32;
329
- // tmp[3] < 2^64 + 2^47
330
-
331
- // This adjusts the other two words to complete the two partial
332
- // reductions.
333
- tmp[0] += b;
334
- tmp[1] -= (((limb)b) << 32);
335
-
336
- // In order to make space in tmp[3] for the carry from 2 -> 3, we
337
- // conditionally subtract kPrime if tmp[3] is large enough.
338
- high = tmp[3] >> 64;
339
- // As tmp[3] < 2^65, high is either 1 or 0
340
- high = ~(high - 1);
341
- // high is:
342
- // all ones if the high word of tmp[3] is 1
343
- // all zeros if the high word of tmp[3] if 0
344
- low = tmp[3];
345
- mask = low >> 63;
346
- // mask is:
347
- // all ones if the MSB of low is 1
348
- // all zeros if the MSB of low if 0
349
- low &= bottom63bits;
350
- low -= kPrime3Test;
351
- // if low was greater than kPrime3Test then the MSB is zero
352
- low = ~low;
353
- low >>= 63;
354
- // low is:
355
- // all ones if low was > kPrime3Test
356
- // all zeros if low was <= kPrime3Test
357
- mask = (mask & low) | high;
358
- tmp[0] -= mask & kPrime[0];
359
- tmp[1] -= mask & kPrime[1];
360
- // kPrime[2] is zero, so omitted
361
- tmp[3] -= mask & kPrime[3];
362
- // tmp[3] < 2**64 - 2**32 + 1
363
-
364
- tmp[1] += ((uint64_t)(tmp[0] >> 64));
365
- tmp[0] = (uint64_t)tmp[0];
366
- tmp[2] += ((uint64_t)(tmp[1] >> 64));
367
- tmp[1] = (uint64_t)tmp[1];
368
- tmp[3] += ((uint64_t)(tmp[2] >> 64));
369
- tmp[2] = (uint64_t)tmp[2];
370
- // tmp[i] < 2^64
371
-
372
- out[0] = tmp[0];
373
- out[1] = tmp[1];
374
- out[2] = tmp[2];
375
- out[3] = tmp[3];
376
- }
377
-
378
- // smallfelem_expand converts a smallfelem to an felem
379
- static void smallfelem_expand(felem out, const smallfelem in) {
380
- out[0] = in[0];
381
- out[1] = in[1];
382
- out[2] = in[2];
383
- out[3] = in[3];
384
- }
385
-
386
- // smallfelem_square sets |out| = |small|^2
387
- // On entry:
388
- // small[i] < 2^64
389
- // On exit:
390
- // out[i] < 7 * 2^64 < 2^67
391
- static void smallfelem_square(longfelem out, const smallfelem small) {
392
- limb a;
393
- uint64_t high, low;
394
-
395
- a = ((uint128_t)small[0]) * small[0];
396
- low = a;
397
- high = a >> 64;
398
- out[0] = low;
399
- out[1] = high;
400
-
401
- a = ((uint128_t)small[0]) * small[1];
402
- low = a;
403
- high = a >> 64;
404
- out[1] += low;
405
- out[1] += low;
406
- out[2] = high;
407
-
408
- a = ((uint128_t)small[0]) * small[2];
409
- low = a;
410
- high = a >> 64;
411
- out[2] += low;
412
- out[2] *= 2;
413
- out[3] = high;
414
-
415
- a = ((uint128_t)small[0]) * small[3];
416
- low = a;
417
- high = a >> 64;
418
- out[3] += low;
419
- out[4] = high;
420
-
421
- a = ((uint128_t)small[1]) * small[2];
422
- low = a;
423
- high = a >> 64;
424
- out[3] += low;
425
- out[3] *= 2;
426
- out[4] += high;
427
-
428
- a = ((uint128_t)small[1]) * small[1];
429
- low = a;
430
- high = a >> 64;
431
- out[2] += low;
432
- out[3] += high;
433
-
434
- a = ((uint128_t)small[1]) * small[3];
435
- low = a;
436
- high = a >> 64;
437
- out[4] += low;
438
- out[4] *= 2;
439
- out[5] = high;
440
-
441
- a = ((uint128_t)small[2]) * small[3];
442
- low = a;
443
- high = a >> 64;
444
- out[5] += low;
445
- out[5] *= 2;
446
- out[6] = high;
447
- out[6] += high;
448
-
449
- a = ((uint128_t)small[2]) * small[2];
450
- low = a;
451
- high = a >> 64;
452
- out[4] += low;
453
- out[5] += high;
454
-
455
- a = ((uint128_t)small[3]) * small[3];
456
- low = a;
457
- high = a >> 64;
458
- out[6] += low;
459
- out[7] = high;
460
- }
461
-
462
- //felem_square sets |out| = |in|^2
463
- // On entry:
464
- // in[i] < 2^109
465
- // On exit:
466
- // out[i] < 7 * 2^64 < 2^67.
467
- static void felem_square(longfelem out, const felem in) {
468
- uint64_t small[4];
469
- felem_shrink(small, in);
470
- smallfelem_square(out, small);
471
- }
472
-
473
- // smallfelem_mul sets |out| = |small1| * |small2|
474
- // On entry:
475
- // small1[i] < 2^64
476
- // small2[i] < 2^64
477
- // On exit:
478
- // out[i] < 7 * 2^64 < 2^67.
479
- static void smallfelem_mul(longfelem out, const smallfelem small1,
480
- const smallfelem small2) {
481
- limb a;
482
- uint64_t high, low;
483
-
484
- a = ((uint128_t)small1[0]) * small2[0];
485
- low = a;
486
- high = a >> 64;
487
- out[0] = low;
488
- out[1] = high;
489
-
490
- a = ((uint128_t)small1[0]) * small2[1];
491
- low = a;
492
- high = a >> 64;
493
- out[1] += low;
494
- out[2] = high;
495
-
496
- a = ((uint128_t)small1[1]) * small2[0];
497
- low = a;
498
- high = a >> 64;
499
- out[1] += low;
500
- out[2] += high;
501
-
502
- a = ((uint128_t)small1[0]) * small2[2];
503
- low = a;
504
- high = a >> 64;
505
- out[2] += low;
506
- out[3] = high;
507
-
508
- a = ((uint128_t)small1[1]) * small2[1];
509
- low = a;
510
- high = a >> 64;
511
- out[2] += low;
512
- out[3] += high;
513
-
514
- a = ((uint128_t)small1[2]) * small2[0];
515
- low = a;
516
- high = a >> 64;
517
- out[2] += low;
518
- out[3] += high;
519
-
520
- a = ((uint128_t)small1[0]) * small2[3];
521
- low = a;
522
- high = a >> 64;
523
- out[3] += low;
524
- out[4] = high;
525
-
526
- a = ((uint128_t)small1[1]) * small2[2];
527
- low = a;
528
- high = a >> 64;
529
- out[3] += low;
530
- out[4] += high;
531
-
532
- a = ((uint128_t)small1[2]) * small2[1];
533
- low = a;
534
- high = a >> 64;
535
- out[3] += low;
536
- out[4] += high;
537
-
538
- a = ((uint128_t)small1[3]) * small2[0];
539
- low = a;
540
- high = a >> 64;
541
- out[3] += low;
542
- out[4] += high;
543
-
544
- a = ((uint128_t)small1[1]) * small2[3];
545
- low = a;
546
- high = a >> 64;
547
- out[4] += low;
548
- out[5] = high;
549
-
550
- a = ((uint128_t)small1[2]) * small2[2];
551
- low = a;
552
- high = a >> 64;
553
- out[4] += low;
554
- out[5] += high;
555
-
556
- a = ((uint128_t)small1[3]) * small2[1];
557
- low = a;
558
- high = a >> 64;
559
- out[4] += low;
560
- out[5] += high;
561
-
562
- a = ((uint128_t)small1[2]) * small2[3];
563
- low = a;
564
- high = a >> 64;
565
- out[5] += low;
566
- out[6] = high;
567
-
568
- a = ((uint128_t)small1[3]) * small2[2];
569
- low = a;
570
- high = a >> 64;
571
- out[5] += low;
572
- out[6] += high;
573
-
574
- a = ((uint128_t)small1[3]) * small2[3];
575
- low = a;
576
- high = a >> 64;
577
- out[6] += low;
578
- out[7] = high;
579
- }
580
-
581
- // felem_mul sets |out| = |in1| * |in2|
582
- // On entry:
583
- // in1[i] < 2^109
584
- // in2[i] < 2^109
585
- // On exit:
586
- // out[i] < 7 * 2^64 < 2^67
587
- static void felem_mul(longfelem out, const felem in1, const felem in2) {
588
- smallfelem small1, small2;
589
- felem_shrink(small1, in1);
590
- felem_shrink(small2, in2);
591
- smallfelem_mul(out, small1, small2);
592
- }
593
-
594
- // felem_small_mul sets |out| = |small1| * |in2|
595
- // On entry:
596
- // small1[i] < 2^64
597
- // in2[i] < 2^109
598
- // On exit:
599
- // out[i] < 7 * 2^64 < 2^67
600
- static void felem_small_mul(longfelem out, const smallfelem small1,
601
- const felem in2) {
602
- smallfelem small2;
603
- felem_shrink(small2, in2);
604
- smallfelem_mul(out, small1, small2);
605
- }
606
-
607
- #define two100m36m4 ((((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4))
608
- #define two100 (((limb)1) << 100)
609
- #define two100m36p4 ((((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4))
610
-
611
- // zero100 is 0 mod p
612
- static const felem zero100 = {two100m36m4, two100, two100m36p4, two100m36p4};
613
-
614
- // Internal function for the different flavours of felem_reduce.
615
- // felem_reduce_ reduces the higher coefficients in[4]-in[7].
616
- // On entry:
617
- // out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
618
- // out[1] >= in[7] + 2^32*in[4]
619
- // out[2] >= in[5] + 2^32*in[5]
620
- // out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
621
- // On exit:
622
- // out[0] <= out[0] + in[4] + 2^32*in[5]
623
- // out[1] <= out[1] + in[5] + 2^33*in[6]
624
- // out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
625
- // out[3] <= out[3] + 2^32*in[4] + 3*in[7]
626
- static void felem_reduce_(felem out, const longfelem in) {
627
- int128_t c;
628
- // combine common terms from below
629
- c = in[4] + (in[5] << 32);
630
- out[0] += c;
631
- out[3] -= c;
632
-
633
- c = in[5] - in[7];
634
- out[1] += c;
635
- out[2] -= c;
636
-
637
- // the remaining terms
638
- // 256: [(0,1),(96,-1),(192,-1),(224,1)]
639
- out[1] -= (in[4] << 32);
640
- out[3] += (in[4] << 32);
641
-
642
- // 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)]
643
- out[2] -= (in[5] << 32);
644
-
645
- // 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)]
646
- out[0] -= in[6];
647
- out[0] -= (in[6] << 32);
648
- out[1] += (in[6] << 33);
649
- out[2] += (in[6] * 2);
650
- out[3] -= (in[6] << 32);
651
-
652
- // 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)]
653
- out[0] -= in[7];
654
- out[0] -= (in[7] << 32);
655
- out[2] += (in[7] << 33);
656
- out[3] += (in[7] * 3);
657
- }
658
-
659
- // felem_reduce converts a longfelem into an felem.
660
- // To be called directly after felem_square or felem_mul.
661
- // On entry:
662
- // in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
663
- // in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
664
- // On exit:
665
- // out[i] < 2^101
666
- static void felem_reduce(felem out, const longfelem in) {
667
- out[0] = zero100[0] + in[0];
668
- out[1] = zero100[1] + in[1];
669
- out[2] = zero100[2] + in[2];
670
- out[3] = zero100[3] + in[3];
671
-
672
- felem_reduce_(out, in);
673
-
674
- // out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
675
- // out[1] > 2^100 - 2^64 - 7*2^96 > 0
676
- // out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
677
- // out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
678
- //
679
- // out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
680
- // out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
681
- // out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
682
- // out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101
683
- }
684
-
685
- // felem_reduce_zero105 converts a larger longfelem into an felem.
686
- // On entry:
687
- // in[0] < 2^71
688
- // On exit:
689
- // out[i] < 2^106
690
- static void felem_reduce_zero105(felem out, const longfelem in) {
691
- out[0] = zero105[0] + in[0];
692
- out[1] = zero105[1] + in[1];
693
- out[2] = zero105[2] + in[2];
694
- out[3] = zero105[3] + in[3];
695
-
696
- felem_reduce_(out, in);
697
-
698
- // out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
699
- // out[1] > 2^105 - 2^71 - 2^103 > 0
700
- // out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
701
- // out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
702
- //
703
- // out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
704
- // out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
705
- // out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
706
- // out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106
707
- }
708
-
709
- // subtract_u64 sets *result = *result - v and *carry to one if the
710
- // subtraction underflowed.
711
- static void subtract_u64(uint64_t *result, uint64_t *carry, uint64_t v) {
712
- uint128_t r = *result;
713
- r -= v;
714
- *carry = (r >> 64) & 1;
715
- *result = (uint64_t)r;
716
- }
717
-
718
- // felem_contract converts |in| to its unique, minimal representation. On
719
- // entry: in[i] < 2^109.
720
- static void felem_contract(smallfelem out, const felem in) {
721
- uint64_t all_equal_so_far = 0, result = 0;
722
-
723
- felem_shrink(out, in);
724
- // small is minimal except that the value might be > p
725
-
726
- all_equal_so_far--;
727
- // We are doing a constant time test if out >= kPrime. We need to compare
728
- // each uint64_t, from most-significant to least significant. For each one, if
729
- // all words so far have been equal (m is all ones) then a non-equal
730
- // result is the answer. Otherwise we continue.
731
- for (size_t i = 3; i < 4; i--) {
732
- uint64_t equal;
733
- uint128_t a = ((uint128_t)kPrime[i]) - out[i];
734
- // if out[i] > kPrime[i] then a will underflow and the high 64-bits
735
- // will all be set.
736
- result |= all_equal_so_far & ((uint64_t)(a >> 64));
737
-
738
- // if kPrime[i] == out[i] then |equal| will be all zeros and the
739
- // decrement will make it all ones.
740
- equal = kPrime[i] ^ out[i];
741
- equal--;
742
- equal &= equal << 32;
743
- equal &= equal << 16;
744
- equal &= equal << 8;
745
- equal &= equal << 4;
746
- equal &= equal << 2;
747
- equal &= equal << 1;
748
- equal = ((int64_t)equal) >> 63;
749
-
750
- all_equal_so_far &= equal;
751
- }
752
-
753
- // if all_equal_so_far is still all ones then the two values are equal
754
- // and so out >= kPrime is true.
755
- result |= all_equal_so_far;
756
-
757
- // if out >= kPrime then we subtract kPrime.
758
- uint64_t carry;
759
- subtract_u64(&out[0], &carry, result & kPrime[0]);
760
- subtract_u64(&out[1], &carry, carry);
761
- subtract_u64(&out[2], &carry, carry);
762
- subtract_u64(&out[3], &carry, carry);
763
-
764
- subtract_u64(&out[1], &carry, result & kPrime[1]);
765
- subtract_u64(&out[2], &carry, carry);
766
- subtract_u64(&out[3], &carry, carry);
767
-
768
- subtract_u64(&out[2], &carry, result & kPrime[2]);
769
- subtract_u64(&out[3], &carry, carry);
770
-
771
- subtract_u64(&out[3], &carry, result & kPrime[3]);
772
- }
773
-
774
- // felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
775
- // otherwise.
776
- // On entry:
777
- // small[i] < 2^64
778
- static limb smallfelem_is_zero(const smallfelem small) {
779
- limb result;
780
- uint64_t is_p;
781
-
782
- uint64_t is_zero = small[0] | small[1] | small[2] | small[3];
783
- is_zero--;
784
- is_zero &= is_zero << 32;
785
- is_zero &= is_zero << 16;
786
- is_zero &= is_zero << 8;
787
- is_zero &= is_zero << 4;
788
- is_zero &= is_zero << 2;
789
- is_zero &= is_zero << 1;
790
- is_zero = ((int64_t)is_zero) >> 63;
791
-
792
- is_p = (small[0] ^ kPrime[0]) | (small[1] ^ kPrime[1]) |
793
- (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
794
- is_p--;
795
- is_p &= is_p << 32;
796
- is_p &= is_p << 16;
797
- is_p &= is_p << 8;
798
- is_p &= is_p << 4;
799
- is_p &= is_p << 2;
800
- is_p &= is_p << 1;
801
- is_p = ((int64_t)is_p) >> 63;
802
-
803
- is_zero |= is_p;
804
-
805
- result = is_zero;
806
- result |= ((limb)is_zero) << 64;
807
- return result;
808
- }
809
-
810
- // felem_inv calculates |out| = |in|^{-1}
811
- //
812
- // Based on Fermat's Little Theorem:
813
- // a^p = a (mod p)
814
- // a^{p-1} = 1 (mod p)
815
- // a^{p-2} = a^{-1} (mod p)
816
- static void felem_inv(felem out, const felem in) {
817
- felem ftmp, ftmp2;
818
- // each e_I will hold |in|^{2^I - 1}
819
- felem e2, e4, e8, e16, e32, e64;
820
- longfelem tmp;
821
-
822
- felem_square(tmp, in);
823
- felem_reduce(ftmp, tmp); // 2^1
824
- felem_mul(tmp, in, ftmp);
825
- felem_reduce(ftmp, tmp); // 2^2 - 2^0
826
- felem_assign(e2, ftmp);
827
- felem_square(tmp, ftmp);
828
- felem_reduce(ftmp, tmp); // 2^3 - 2^1
829
- felem_square(tmp, ftmp);
830
- felem_reduce(ftmp, tmp); // 2^4 - 2^2
831
- felem_mul(tmp, ftmp, e2);
832
- felem_reduce(ftmp, tmp); // 2^4 - 2^0
833
- felem_assign(e4, ftmp);
834
- felem_square(tmp, ftmp);
835
- felem_reduce(ftmp, tmp); // 2^5 - 2^1
836
- felem_square(tmp, ftmp);
837
- felem_reduce(ftmp, tmp); // 2^6 - 2^2
838
- felem_square(tmp, ftmp);
839
- felem_reduce(ftmp, tmp); // 2^7 - 2^3
840
- felem_square(tmp, ftmp);
841
- felem_reduce(ftmp, tmp); // 2^8 - 2^4
842
- felem_mul(tmp, ftmp, e4);
843
- felem_reduce(ftmp, tmp); // 2^8 - 2^0
844
- felem_assign(e8, ftmp);
845
- for (size_t i = 0; i < 8; i++) {
846
- felem_square(tmp, ftmp);
847
- felem_reduce(ftmp, tmp);
848
- } // 2^16 - 2^8
849
- felem_mul(tmp, ftmp, e8);
850
- felem_reduce(ftmp, tmp); // 2^16 - 2^0
851
- felem_assign(e16, ftmp);
852
- for (size_t i = 0; i < 16; i++) {
853
- felem_square(tmp, ftmp);
854
- felem_reduce(ftmp, tmp);
855
- } // 2^32 - 2^16
856
- felem_mul(tmp, ftmp, e16);
857
- felem_reduce(ftmp, tmp); // 2^32 - 2^0
858
- felem_assign(e32, ftmp);
859
- for (size_t i = 0; i < 32; i++) {
860
- felem_square(tmp, ftmp);
861
- felem_reduce(ftmp, tmp);
862
- } // 2^64 - 2^32
863
- felem_assign(e64, ftmp);
864
- felem_mul(tmp, ftmp, in);
865
- felem_reduce(ftmp, tmp); // 2^64 - 2^32 + 2^0
866
- for (size_t i = 0; i < 192; i++) {
867
- felem_square(tmp, ftmp);
868
- felem_reduce(ftmp, tmp);
869
- } // 2^256 - 2^224 + 2^192
870
-
871
- felem_mul(tmp, e64, e32);
872
- felem_reduce(ftmp2, tmp); // 2^64 - 2^0
873
- for (size_t i = 0; i < 16; i++) {
874
- felem_square(tmp, ftmp2);
875
- felem_reduce(ftmp2, tmp);
876
- } // 2^80 - 2^16
877
- felem_mul(tmp, ftmp2, e16);
878
- felem_reduce(ftmp2, tmp); // 2^80 - 2^0
879
- for (size_t i = 0; i < 8; i++) {
880
- felem_square(tmp, ftmp2);
881
- felem_reduce(ftmp2, tmp);
882
- } // 2^88 - 2^8
883
- felem_mul(tmp, ftmp2, e8);
884
- felem_reduce(ftmp2, tmp); // 2^88 - 2^0
885
- for (size_t i = 0; i < 4; i++) {
886
- felem_square(tmp, ftmp2);
887
- felem_reduce(ftmp2, tmp);
888
- } // 2^92 - 2^4
889
- felem_mul(tmp, ftmp2, e4);
890
- felem_reduce(ftmp2, tmp); // 2^92 - 2^0
891
- felem_square(tmp, ftmp2);
892
- felem_reduce(ftmp2, tmp); // 2^93 - 2^1
893
- felem_square(tmp, ftmp2);
894
- felem_reduce(ftmp2, tmp); // 2^94 - 2^2
895
- felem_mul(tmp, ftmp2, e2);
896
- felem_reduce(ftmp2, tmp); // 2^94 - 2^0
897
- felem_square(tmp, ftmp2);
898
- felem_reduce(ftmp2, tmp); // 2^95 - 2^1
899
- felem_square(tmp, ftmp2);
900
- felem_reduce(ftmp2, tmp); // 2^96 - 2^2
901
- felem_mul(tmp, ftmp2, in);
902
- felem_reduce(ftmp2, tmp); // 2^96 - 3
903
-
904
- felem_mul(tmp, ftmp2, ftmp);
905
- felem_reduce(out, tmp); // 2^256 - 2^224 + 2^192 + 2^96 - 3
906
- }
907
-
908
- // Group operations
909
- // ----------------
910
- //
911
- // Building on top of the field operations we have the operations on the
912
- // elliptic curve group itself. Points on the curve are represented in Jacobian
913
- // coordinates.
914
-
915
- // point_double calculates 2*(x_in, y_in, z_in)
916
- //
917
- // The method is taken from:
918
- // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
919
- //
920
- // Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
921
- // while x_out == y_in is not (maybe this works, but it's not tested).
922
- static void point_double(felem x_out, felem y_out, felem z_out,
923
- const felem x_in, const felem y_in, const felem z_in) {
924
- longfelem tmp, tmp2;
925
- felem delta, gamma, beta, alpha, ftmp, ftmp2;
926
- smallfelem small1, small2;
927
-
928
- felem_assign(ftmp, x_in);
929
- // ftmp[i] < 2^106
930
- felem_assign(ftmp2, x_in);
931
- // ftmp2[i] < 2^106
932
-
933
- // delta = z^2
934
- felem_square(tmp, z_in);
935
- felem_reduce(delta, tmp);
936
- // delta[i] < 2^101
937
-
938
- // gamma = y^2
939
- felem_square(tmp, y_in);
940
- felem_reduce(gamma, tmp);
941
- // gamma[i] < 2^101
942
- felem_shrink(small1, gamma);
943
-
944
- // beta = x*gamma
945
- felem_small_mul(tmp, small1, x_in);
946
- felem_reduce(beta, tmp);
947
- // beta[i] < 2^101
948
-
949
- // alpha = 3*(x-delta)*(x+delta)
950
- felem_diff(ftmp, delta);
951
- // ftmp[i] < 2^105 + 2^106 < 2^107
952
- felem_sum(ftmp2, delta);
953
- // ftmp2[i] < 2^105 + 2^106 < 2^107
954
- felem_scalar(ftmp2, 3);
955
- // ftmp2[i] < 3 * 2^107 < 2^109
956
- felem_mul(tmp, ftmp, ftmp2);
957
- felem_reduce(alpha, tmp);
958
- // alpha[i] < 2^101
959
- felem_shrink(small2, alpha);
960
-
961
- // x' = alpha^2 - 8*beta
962
- smallfelem_square(tmp, small2);
963
- felem_reduce(x_out, tmp);
964
- felem_assign(ftmp, beta);
965
- felem_scalar(ftmp, 8);
966
- // ftmp[i] < 8 * 2^101 = 2^104
967
- felem_diff(x_out, ftmp);
968
- // x_out[i] < 2^105 + 2^101 < 2^106
969
-
970
- // z' = (y + z)^2 - gamma - delta
971
- felem_sum(delta, gamma);
972
- // delta[i] < 2^101 + 2^101 = 2^102
973
- felem_assign(ftmp, y_in);
974
- felem_sum(ftmp, z_in);
975
- // ftmp[i] < 2^106 + 2^106 = 2^107
976
- felem_square(tmp, ftmp);
977
- felem_reduce(z_out, tmp);
978
- felem_diff(z_out, delta);
979
- // z_out[i] < 2^105 + 2^101 < 2^106
980
-
981
- // y' = alpha*(4*beta - x') - 8*gamma^2
982
- felem_scalar(beta, 4);
983
- // beta[i] < 4 * 2^101 = 2^103
984
- felem_diff_zero107(beta, x_out);
985
- // beta[i] < 2^107 + 2^103 < 2^108
986
- felem_small_mul(tmp, small2, beta);
987
- // tmp[i] < 7 * 2^64 < 2^67
988
- smallfelem_square(tmp2, small1);
989
- // tmp2[i] < 7 * 2^64
990
- longfelem_scalar(tmp2, 8);
991
- // tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67
992
- longfelem_diff(tmp, tmp2);
993
- // tmp[i] < 2^67 + 2^70 + 2^40 < 2^71
994
- felem_reduce_zero105(y_out, tmp);
995
- // y_out[i] < 2^106
996
- }
997
-
998
- // point_double_small is the same as point_double, except that it operates on
999
- // smallfelems.
1000
- static void point_double_small(smallfelem x_out, smallfelem y_out,
1001
- smallfelem z_out, const smallfelem x_in,
1002
- const smallfelem y_in, const smallfelem z_in) {
1003
- felem felem_x_out, felem_y_out, felem_z_out;
1004
- felem felem_x_in, felem_y_in, felem_z_in;
1005
-
1006
- smallfelem_expand(felem_x_in, x_in);
1007
- smallfelem_expand(felem_y_in, y_in);
1008
- smallfelem_expand(felem_z_in, z_in);
1009
- point_double(felem_x_out, felem_y_out, felem_z_out, felem_x_in, felem_y_in,
1010
- felem_z_in);
1011
- felem_shrink(x_out, felem_x_out);
1012
- felem_shrink(y_out, felem_y_out);
1013
- felem_shrink(z_out, felem_z_out);
1014
- }
1015
-
1016
- // p256_copy_conditional copies in to out iff mask is all ones.
1017
- static void p256_copy_conditional(felem out, const felem in, limb mask) {
1018
- for (size_t i = 0; i < NLIMBS; ++i) {
1019
- const limb tmp = mask & (in[i] ^ out[i]);
1020
- out[i] ^= tmp;
1021
- }
1022
- }
1023
-
1024
- // copy_small_conditional copies in to out iff mask is all ones.
1025
- static void copy_small_conditional(felem out, const smallfelem in, limb mask) {
1026
- const uint64_t mask64 = mask;
1027
- for (size_t i = 0; i < NLIMBS; ++i) {
1028
- out[i] = ((limb)(in[i] & mask64)) | (out[i] & ~mask);
1029
- }
1030
- }
1031
-
1032
- // point_add calcuates (x1, y1, z1) + (x2, y2, z2)
1033
- //
1034
- // The method is taken from:
1035
- // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
1036
- // adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
1037
- //
1038
- // This function includes a branch for checking whether the two input points
1039
- // are equal, (while not equal to the point at infinity). This case never
1040
- // happens during single point multiplication, so there is no timing leak for
1041
- // ECDH or ECDSA signing.
1042
- static void point_add(felem x3, felem y3, felem z3, const felem x1,
1043
- const felem y1, const felem z1, const int mixed,
1044
- const smallfelem x2, const smallfelem y2,
1045
- const smallfelem z2) {
1046
- felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
1047
- longfelem tmp, tmp2;
1048
- smallfelem small1, small2, small3, small4, small5;
1049
- limb x_equal, y_equal, z1_is_zero, z2_is_zero;
1050
-
1051
- felem_shrink(small3, z1);
1052
-
1053
- z1_is_zero = smallfelem_is_zero(small3);
1054
- z2_is_zero = smallfelem_is_zero(z2);
1055
-
1056
- // ftmp = z1z1 = z1**2
1057
- smallfelem_square(tmp, small3);
1058
- felem_reduce(ftmp, tmp);
1059
- // ftmp[i] < 2^101
1060
- felem_shrink(small1, ftmp);
1061
-
1062
- if (!mixed) {
1063
- // ftmp2 = z2z2 = z2**2
1064
- smallfelem_square(tmp, z2);
1065
- felem_reduce(ftmp2, tmp);
1066
- // ftmp2[i] < 2^101
1067
- felem_shrink(small2, ftmp2);
1068
-
1069
- felem_shrink(small5, x1);
1070
-
1071
- // u1 = ftmp3 = x1*z2z2
1072
- smallfelem_mul(tmp, small5, small2);
1073
- felem_reduce(ftmp3, tmp);
1074
- // ftmp3[i] < 2^101
1075
-
1076
- // ftmp5 = z1 + z2
1077
- felem_assign(ftmp5, z1);
1078
- felem_small_sum(ftmp5, z2);
1079
- // ftmp5[i] < 2^107
1080
-
1081
- // ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2
1082
- felem_square(tmp, ftmp5);
1083
- felem_reduce(ftmp5, tmp);
1084
- // ftmp2 = z2z2 + z1z1
1085
- felem_sum(ftmp2, ftmp);
1086
- // ftmp2[i] < 2^101 + 2^101 = 2^102
1087
- felem_diff(ftmp5, ftmp2);
1088
- // ftmp5[i] < 2^105 + 2^101 < 2^106
1089
-
1090
- // ftmp2 = z2 * z2z2
1091
- smallfelem_mul(tmp, small2, z2);
1092
- felem_reduce(ftmp2, tmp);
1093
-
1094
- // s1 = ftmp2 = y1 * z2**3
1095
- felem_mul(tmp, y1, ftmp2);
1096
- felem_reduce(ftmp6, tmp);
1097
- // ftmp6[i] < 2^101
1098
- } else {
1099
- // We'll assume z2 = 1 (special case z2 = 0 is handled later).
1100
-
1101
- // u1 = ftmp3 = x1*z2z2
1102
- felem_assign(ftmp3, x1);
1103
- // ftmp3[i] < 2^106
1104
-
1105
- // ftmp5 = 2z1z2
1106
- felem_assign(ftmp5, z1);
1107
- felem_scalar(ftmp5, 2);
1108
- // ftmp5[i] < 2*2^106 = 2^107
1109
-
1110
- // s1 = ftmp2 = y1 * z2**3
1111
- felem_assign(ftmp6, y1);
1112
- // ftmp6[i] < 2^106
1113
- }
1114
-
1115
- // u2 = x2*z1z1
1116
- smallfelem_mul(tmp, x2, small1);
1117
- felem_reduce(ftmp4, tmp);
1118
-
1119
- // h = ftmp4 = u2 - u1
1120
- felem_diff_zero107(ftmp4, ftmp3);
1121
- // ftmp4[i] < 2^107 + 2^101 < 2^108
1122
- felem_shrink(small4, ftmp4);
1123
-
1124
- x_equal = smallfelem_is_zero(small4);
1125
-
1126
- // z_out = ftmp5 * h
1127
- felem_small_mul(tmp, small4, ftmp5);
1128
- felem_reduce(z_out, tmp);
1129
- // z_out[i] < 2^101
1130
-
1131
- // ftmp = z1 * z1z1
1132
- smallfelem_mul(tmp, small1, small3);
1133
- felem_reduce(ftmp, tmp);
1134
-
1135
- // s2 = tmp = y2 * z1**3
1136
- felem_small_mul(tmp, y2, ftmp);
1137
- felem_reduce(ftmp5, tmp);
1138
-
1139
- // r = ftmp5 = (s2 - s1)*2
1140
- felem_diff_zero107(ftmp5, ftmp6);
1141
- // ftmp5[i] < 2^107 + 2^107 = 2^108
1142
- felem_scalar(ftmp5, 2);
1143
- // ftmp5[i] < 2^109
1144
- felem_shrink(small1, ftmp5);
1145
- y_equal = smallfelem_is_zero(small1);
1146
-
1147
- if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
1148
- point_double(x3, y3, z3, x1, y1, z1);
1149
- return;
1150
- }
1151
-
1152
- // I = ftmp = (2h)**2
1153
- felem_assign(ftmp, ftmp4);
1154
- felem_scalar(ftmp, 2);
1155
- // ftmp[i] < 2*2^108 = 2^109
1156
- felem_square(tmp, ftmp);
1157
- felem_reduce(ftmp, tmp);
1158
-
1159
- // J = ftmp2 = h * I
1160
- felem_mul(tmp, ftmp4, ftmp);
1161
- felem_reduce(ftmp2, tmp);
1162
-
1163
- // V = ftmp4 = U1 * I
1164
- felem_mul(tmp, ftmp3, ftmp);
1165
- felem_reduce(ftmp4, tmp);
1166
-
1167
- // x_out = r**2 - J - 2V
1168
- smallfelem_square(tmp, small1);
1169
- felem_reduce(x_out, tmp);
1170
- felem_assign(ftmp3, ftmp4);
1171
- felem_scalar(ftmp4, 2);
1172
- felem_sum(ftmp4, ftmp2);
1173
- // ftmp4[i] < 2*2^101 + 2^101 < 2^103
1174
- felem_diff(x_out, ftmp4);
1175
- // x_out[i] < 2^105 + 2^101
1176
-
1177
- // y_out = r(V-x_out) - 2 * s1 * J
1178
- felem_diff_zero107(ftmp3, x_out);
1179
- // ftmp3[i] < 2^107 + 2^101 < 2^108
1180
- felem_small_mul(tmp, small1, ftmp3);
1181
- felem_mul(tmp2, ftmp6, ftmp2);
1182
- longfelem_scalar(tmp2, 2);
1183
- // tmp2[i] < 2*2^67 = 2^68
1184
- longfelem_diff(tmp, tmp2);
1185
- // tmp[i] < 2^67 + 2^70 + 2^40 < 2^71
1186
- felem_reduce_zero105(y_out, tmp);
1187
- // y_out[i] < 2^106
1188
-
1189
- copy_small_conditional(x_out, x2, z1_is_zero);
1190
- p256_copy_conditional(x_out, x1, z2_is_zero);
1191
- copy_small_conditional(y_out, y2, z1_is_zero);
1192
- p256_copy_conditional(y_out, y1, z2_is_zero);
1193
- copy_small_conditional(z_out, z2, z1_is_zero);
1194
- p256_copy_conditional(z_out, z1, z2_is_zero);
1195
- felem_assign(x3, x_out);
1196
- felem_assign(y3, y_out);
1197
- felem_assign(z3, z_out);
1198
- }
1199
-
1200
- // point_add_small is the same as point_add, except that it operates on
1201
- // smallfelems.
1202
- static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
1203
- smallfelem x1, smallfelem y1, smallfelem z1,
1204
- smallfelem x2, smallfelem y2, smallfelem z2) {
1205
- felem felem_x3, felem_y3, felem_z3;
1206
- felem felem_x1, felem_y1, felem_z1;
1207
- smallfelem_expand(felem_x1, x1);
1208
- smallfelem_expand(felem_y1, y1);
1209
- smallfelem_expand(felem_z1, z1);
1210
- point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0, x2,
1211
- y2, z2);
1212
- felem_shrink(x3, felem_x3);
1213
- felem_shrink(y3, felem_y3);
1214
- felem_shrink(z3, felem_z3);
1215
- }
1216
-
1217
- // Base point pre computation
1218
- // --------------------------
1219
- //
1220
- // Two different sorts of precomputed tables are used in the following code.
1221
- // Each contain various points on the curve, where each point is three field
1222
- // elements (x, y, z).
1223
- //
1224
- // For the base point table, z is usually 1 (0 for the point at infinity).
1225
- // This table has 2 * 16 elements, starting with the following:
1226
- // index | bits | point
1227
- // ------+---------+------------------------------
1228
- // 0 | 0 0 0 0 | 0G
1229
- // 1 | 0 0 0 1 | 1G
1230
- // 2 | 0 0 1 0 | 2^64G
1231
- // 3 | 0 0 1 1 | (2^64 + 1)G
1232
- // 4 | 0 1 0 0 | 2^128G
1233
- // 5 | 0 1 0 1 | (2^128 + 1)G
1234
- // 6 | 0 1 1 0 | (2^128 + 2^64)G
1235
- // 7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
1236
- // 8 | 1 0 0 0 | 2^192G
1237
- // 9 | 1 0 0 1 | (2^192 + 1)G
1238
- // 10 | 1 0 1 0 | (2^192 + 2^64)G
1239
- // 11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
1240
- // 12 | 1 1 0 0 | (2^192 + 2^128)G
1241
- // 13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
1242
- // 14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
1243
- // 15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
1244
- // followed by a copy of this with each element multiplied by 2^32.
1245
- //
1246
- // The reason for this is so that we can clock bits into four different
1247
- // locations when doing simple scalar multiplies against the base point,
1248
- // and then another four locations using the second 16 elements.
1249
- //
1250
- // Tables for other points have table[i] = iG for i in 0 .. 16.
1251
-
1252
- // g_pre_comp is the table of precomputed base points
1253
- static const smallfelem g_pre_comp[2][16][3] = {
1254
- {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
1255
- {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
1256
- 0x6b17d1f2e12c4247},
1257
- {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
1258
- 0x4fe342e2fe1a7f9b},
1259
- {1, 0, 0, 0}},
1260
- {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
1261
- 0x0fa822bc2811aaa5},
1262
- {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
1263
- 0xbff44ae8f5dba80d},
1264
- {1, 0, 0, 0}},
1265
- {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
1266
- 0x300a4bbc89d6726f},
1267
- {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
1268
- 0x72aac7e0d09b4644},
1269
- {1, 0, 0, 0}},
1270
- {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
1271
- 0x447d739beedb5e67},
1272
- {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
1273
- 0x2d4825ab834131ee},
1274
- {1, 0, 0, 0}},
1275
- {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
1276
- 0xef9519328a9c72ff},
1277
- {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
1278
- 0x611e9fc37dbb2c9b},
1279
- {1, 0, 0, 0}},
1280
- {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
1281
- 0x550663797b51f5d8},
1282
- {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
1283
- 0x157164848aecb851},
1284
- {1, 0, 0, 0}},
1285
- {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
1286
- 0xeb5d7745b21141ea},
1287
- {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
1288
- 0xeafd72ebdbecc17b},
1289
- {1, 0, 0, 0}},
1290
- {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
1291
- 0xa6d39677a7849276},
1292
- {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
1293
- 0x674f84749b0b8816},
1294
- {1, 0, 0, 0}},
1295
- {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
1296
- 0x4e769e7672c9ddad},
1297
- {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
1298
- 0x42b99082de830663},
1299
- {1, 0, 0, 0}},
1300
- {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
1301
- 0x78878ef61c6ce04d},
1302
- {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
1303
- 0xb6cb3f5d7b72c321},
1304
- {1, 0, 0, 0}},
1305
- {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
1306
- 0x0c88bc4d716b1287},
1307
- {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
1308
- 0xdd5ddea3f3901dc6},
1309
- {1, 0, 0, 0}},
1310
- {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
1311
- 0x68f344af6b317466},
1312
- {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
1313
- 0x31b9c405f8540a20},
1314
- {1, 0, 0, 0}},
1315
- {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
1316
- 0x4052bf4b6f461db9},
1317
- {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
1318
- 0xfecf4d5190b0fc61},
1319
- {1, 0, 0, 0}},
1320
- {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
1321
- 0x1eddbae2c802e41a},
1322
- {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
1323
- 0x43104d86560ebcfc},
1324
- {1, 0, 0, 0}},
1325
- {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
1326
- 0xb48e26b484f7a21c},
1327
- {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
1328
- 0xfac015404d4d3dab},
1329
- {1, 0, 0, 0}}},
1330
- {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
1331
- {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
1332
- 0x7fe36b40af22af89},
1333
- {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
1334
- 0xe697d45825b63624},
1335
- {1, 0, 0, 0}},
1336
- {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
1337
- 0x4a5b506612a677a6},
1338
- {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
1339
- 0xeb13461ceac089f1},
1340
- {1, 0, 0, 0}},
1341
- {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
1342
- 0x0781b8291c6a220a},
1343
- {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
1344
- 0x690cde8df0151593},
1345
- {1, 0, 0, 0}},
1346
- {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
1347
- 0x8a535f566ec73617},
1348
- {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
1349
- 0x0455c08468b08bd7},
1350
- {1, 0, 0, 0}},
1351
- {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
1352
- 0x06bada7ab77f8276},
1353
- {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
1354
- 0x5b476dfd0e6cb18a},
1355
- {1, 0, 0, 0}},
1356
- {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
1357
- 0x3e29864e8a2ec908},
1358
- {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
1359
- 0x239b90ea3dc31e7e},
1360
- {1, 0, 0, 0}},
1361
- {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
1362
- 0x820f4dd949f72ff7},
1363
- {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
1364
- 0x140406ec783a05ec},
1365
- {1, 0, 0, 0}},
1366
- {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
1367
- 0x68f6b8542783dfee},
1368
- {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
1369
- 0xcbe1feba92e40ce6},
1370
- {1, 0, 0, 0}},
1371
- {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
1372
- 0xd0b2f94d2f420109},
1373
- {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
1374
- 0x971459828b0719e5},
1375
- {1, 0, 0, 0}},
1376
- {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
1377
- 0x961610004a866aba},
1378
- {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
1379
- 0x7acb9fadcee75e44},
1380
- {1, 0, 0, 0}},
1381
- {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
1382
- 0x24eb9acca333bf5b},
1383
- {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
1384
- 0x69f891c5acd079cc},
1385
- {1, 0, 0, 0}},
1386
- {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
1387
- 0xe51f547c5972a107},
1388
- {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
1389
- 0x1c309a2b25bb1387},
1390
- {1, 0, 0, 0}},
1391
- {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
1392
- 0x20b87b8aa2c4e503},
1393
- {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
1394
- 0xf5c6fa49919776be},
1395
- {1, 0, 0, 0}},
1396
- {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
1397
- 0x1ed7d1b9332010b9},
1398
- {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
1399
- 0x3a2b03f03217257a},
1400
- {1, 0, 0, 0}},
1401
- {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
1402
- 0x15fee545c78dd9f6},
1403
- {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
1404
- 0x4ab5b6b2b8753f81},
1405
- {1, 0, 0, 0}}}};
1406
-
1407
- // select_point selects the |idx|th point from a precomputation table and
1408
- // copies it to out.
1409
- static void select_point(const uint64_t idx, size_t size,
1410
- const smallfelem pre_comp[/*size*/][3],
1411
- smallfelem out[3]) {
1412
- uint64_t *outlimbs = &out[0][0];
1413
- OPENSSL_memset(outlimbs, 0, 3 * sizeof(smallfelem));
1414
-
1415
- for (size_t i = 0; i < size; i++) {
1416
- const uint64_t *inlimbs = (const uint64_t *)&pre_comp[i][0][0];
1417
- uint64_t mask = i ^ idx;
1418
- mask |= mask >> 4;
1419
- mask |= mask >> 2;
1420
- mask |= mask >> 1;
1421
- mask &= 1;
1422
- mask--;
1423
- for (size_t j = 0; j < NLIMBS * 3; j++) {
1424
- outlimbs[j] |= inlimbs[j] & mask;
1425
- }
1426
- }
1427
- }
1428
-
1429
- // get_bit returns the |i|th bit in |in|
1430
- static char get_bit(const felem_bytearray in, int i) {
1431
- if (i < 0 || i >= 256) {
1432
- return 0;
1433
- }
1434
- return (in[i >> 3] >> (i & 7)) & 1;
1435
- }
1436
-
1437
- // Interleaved point multiplication using precomputed point multiples: The
1438
- // small point multiples 0*P, 1*P, ..., 17*P are in p_pre_comp, the scalar
1439
- // in p_scalar, if non-NULL. If g_scalar is non-NULL, we also add this multiple
1440
- // of the generator, using certain (large) precomputed multiples in g_pre_comp.
1441
- // Output point (X, Y, Z) is stored in x_out, y_out, z_out.
1442
- static void batch_mul(felem x_out, felem y_out, felem z_out,
1443
- const uint8_t *p_scalar, const uint8_t *g_scalar,
1444
- const smallfelem p_pre_comp[17][3]) {
1445
- felem nq[3], ftmp;
1446
- smallfelem tmp[3];
1447
- uint64_t bits;
1448
- uint8_t sign, digit;
1449
-
1450
- // set nq to the point at infinity
1451
- OPENSSL_memset(nq, 0, 3 * sizeof(felem));
1452
-
1453
- // Loop over both scalars msb-to-lsb, interleaving additions of multiples
1454
- // of the generator (two in each of the last 32 rounds) and additions of p
1455
- // (every 5th round).
1456
-
1457
- int skip = 1; // save two point operations in the first round
1458
- size_t i = p_scalar != NULL ? 255 : 31;
1459
- for (;;) {
1460
- // double
1461
- if (!skip) {
1462
- point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
1463
- }
1464
-
1465
- // add multiples of the generator
1466
- if (g_scalar != NULL && i <= 31) {
1467
- // first, look 32 bits upwards
1468
- bits = get_bit(g_scalar, i + 224) << 3;
1469
- bits |= get_bit(g_scalar, i + 160) << 2;
1470
- bits |= get_bit(g_scalar, i + 96) << 1;
1471
- bits |= get_bit(g_scalar, i + 32);
1472
- // select the point to add, in constant time
1473
- select_point(bits, 16, g_pre_comp[1], tmp);
1474
-
1475
- if (!skip) {
1476
- point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
1477
- tmp[0], tmp[1], tmp[2]);
1478
- } else {
1479
- smallfelem_expand(nq[0], tmp[0]);
1480
- smallfelem_expand(nq[1], tmp[1]);
1481
- smallfelem_expand(nq[2], tmp[2]);
1482
- skip = 0;
1483
- }
1484
-
1485
- // second, look at the current position
1486
- bits = get_bit(g_scalar, i + 192) << 3;
1487
- bits |= get_bit(g_scalar, i + 128) << 2;
1488
- bits |= get_bit(g_scalar, i + 64) << 1;
1489
- bits |= get_bit(g_scalar, i);
1490
- // select the point to add, in constant time
1491
- select_point(bits, 16, g_pre_comp[0], tmp);
1492
- point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */, tmp[0],
1493
- tmp[1], tmp[2]);
1494
- }
1495
-
1496
- // do other additions every 5 doublings
1497
- if (p_scalar != NULL && i % 5 == 0) {
1498
- bits = get_bit(p_scalar, i + 4) << 5;
1499
- bits |= get_bit(p_scalar, i + 3) << 4;
1500
- bits |= get_bit(p_scalar, i + 2) << 3;
1501
- bits |= get_bit(p_scalar, i + 1) << 2;
1502
- bits |= get_bit(p_scalar, i) << 1;
1503
- bits |= get_bit(p_scalar, i - 1);
1504
- ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
1505
-
1506
- // select the point to add or subtract, in constant time.
1507
- select_point(digit, 17, p_pre_comp, tmp);
1508
- smallfelem_neg(ftmp, tmp[1]); // (X, -Y, Z) is the negative
1509
- // point
1510
- copy_small_conditional(ftmp, tmp[1], (((limb)sign) - 1));
1511
- felem_contract(tmp[1], ftmp);
1512
-
1513
- if (!skip) {
1514
- point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 0 /* mixed */,
1515
- tmp[0], tmp[1], tmp[2]);
1516
- } else {
1517
- smallfelem_expand(nq[0], tmp[0]);
1518
- smallfelem_expand(nq[1], tmp[1]);
1519
- smallfelem_expand(nq[2], tmp[2]);
1520
- skip = 0;
1521
- }
1522
- }
1523
-
1524
- if (i == 0) {
1525
- break;
1526
- }
1527
- --i;
1528
- }
1529
- felem_assign(x_out, nq[0]);
1530
- felem_assign(y_out, nq[1]);
1531
- felem_assign(z_out, nq[2]);
1532
- }
1533
-
1534
- // OPENSSL EC_METHOD FUNCTIONS
1535
-
1536
- // Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
1537
- // (X/Z^2, Y/Z^3).
1538
- static int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
1539
- const EC_POINT *point,
1540
- BIGNUM *x, BIGNUM *y,
1541
- BN_CTX *ctx) {
1542
- felem z1, z2, x_in, y_in;
1543
- smallfelem x_out, y_out;
1544
- longfelem tmp;
1545
-
1546
- if (EC_POINT_is_at_infinity(group, point)) {
1547
- OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
1548
- return 0;
1549
- }
1550
- if (!BN_to_felem(x_in, &point->X) ||
1551
- !BN_to_felem(y_in, &point->Y) ||
1552
- !BN_to_felem(z1, &point->Z)) {
1553
- return 0;
1554
- }
1555
- felem_inv(z2, z1);
1556
- felem_square(tmp, z2);
1557
- felem_reduce(z1, tmp);
1558
-
1559
- if (x != NULL) {
1560
- felem_mul(tmp, x_in, z1);
1561
- felem_reduce(x_in, tmp);
1562
- felem_contract(x_out, x_in);
1563
- if (!smallfelem_to_BN(x, x_out)) {
1564
- OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1565
- return 0;
1566
- }
1567
- }
1568
-
1569
- if (y != NULL) {
1570
- felem_mul(tmp, z1, z2);
1571
- felem_reduce(z1, tmp);
1572
- felem_mul(tmp, y_in, z1);
1573
- felem_reduce(y_in, tmp);
1574
- felem_contract(y_out, y_in);
1575
- if (!smallfelem_to_BN(y, y_out)) {
1576
- OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1577
- return 0;
1578
- }
1579
- }
1580
-
1581
- return 1;
1582
- }
1583
-
1584
- static int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
1585
- const EC_SCALAR *g_scalar,
1586
- const EC_POINT *p,
1587
- const EC_SCALAR *p_scalar, BN_CTX *ctx) {
1588
- int ret = 0;
1589
- BN_CTX *new_ctx = NULL;
1590
- BIGNUM *x, *y, *z, *tmp_scalar;
1591
- smallfelem p_pre_comp[17][3];
1592
- smallfelem x_in, y_in, z_in;
1593
- felem x_out, y_out, z_out;
1594
-
1595
- if (ctx == NULL) {
1596
- ctx = new_ctx = BN_CTX_new();
1597
- if (ctx == NULL) {
1598
- return 0;
1599
- }
1600
- }
1601
-
1602
- BN_CTX_start(ctx);
1603
- if ((x = BN_CTX_get(ctx)) == NULL ||
1604
- (y = BN_CTX_get(ctx)) == NULL ||
1605
- (z = BN_CTX_get(ctx)) == NULL ||
1606
- (tmp_scalar = BN_CTX_get(ctx)) == NULL) {
1607
- goto err;
1608
- }
1609
-
1610
- if (p != NULL && p_scalar != NULL) {
1611
- // We treat NULL scalars as 0, and NULL points as points at infinity, i.e.,
1612
- // they contribute nothing to the linear combination.
1613
- OPENSSL_memset(&p_pre_comp, 0, sizeof(p_pre_comp));
1614
- // Precompute multiples.
1615
- if (!BN_to_felem(x_out, &p->X) ||
1616
- !BN_to_felem(y_out, &p->Y) ||
1617
- !BN_to_felem(z_out, &p->Z)) {
1618
- goto err;
1619
- }
1620
- felem_shrink(p_pre_comp[1][0], x_out);
1621
- felem_shrink(p_pre_comp[1][1], y_out);
1622
- felem_shrink(p_pre_comp[1][2], z_out);
1623
- for (size_t j = 2; j <= 16; ++j) {
1624
- if (j & 1) {
1625
- point_add_small(p_pre_comp[j][0], p_pre_comp[j][1],
1626
- p_pre_comp[j][2], p_pre_comp[1][0],
1627
- p_pre_comp[1][1], p_pre_comp[1][2],
1628
- p_pre_comp[j - 1][0], p_pre_comp[j - 1][1],
1629
- p_pre_comp[j - 1][2]);
1630
- } else {
1631
- point_double_small(p_pre_comp[j][0], p_pre_comp[j][1],
1632
- p_pre_comp[j][2], p_pre_comp[j / 2][0],
1633
- p_pre_comp[j / 2][1], p_pre_comp[j / 2][2]);
1634
- }
1635
- }
1636
- }
1637
-
1638
- batch_mul(x_out, y_out, z_out,
1639
- (p != NULL && p_scalar != NULL) ? p_scalar->bytes : NULL,
1640
- g_scalar != NULL ? g_scalar->bytes : NULL,
1641
- (const smallfelem(*)[3]) & p_pre_comp);
1642
-
1643
- // reduce the output to its unique minimal representation
1644
- felem_contract(x_in, x_out);
1645
- felem_contract(y_in, y_out);
1646
- felem_contract(z_in, z_out);
1647
- if (!smallfelem_to_BN(x, x_in) ||
1648
- !smallfelem_to_BN(y, y_in) ||
1649
- !smallfelem_to_BN(z, z_in)) {
1650
- OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1651
- goto err;
1652
- }
1653
- ret = ec_point_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
1654
-
1655
- err:
1656
- BN_CTX_end(ctx);
1657
- BN_CTX_free(new_ctx);
1658
- return ret;
1659
- }
1660
-
1661
- DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp256_method) {
1662
- out->group_init = ec_GFp_simple_group_init;
1663
- out->group_finish = ec_GFp_simple_group_finish;
1664
- out->group_set_curve = ec_GFp_simple_group_set_curve;
1665
- out->point_get_affine_coordinates =
1666
- ec_GFp_nistp256_point_get_affine_coordinates;
1667
- out->mul = ec_GFp_nistp256_points_mul;
1668
- out->field_mul = ec_GFp_simple_field_mul;
1669
- out->field_sqr = ec_GFp_simple_field_sqr;
1670
- out->field_encode = NULL;
1671
- out->field_decode = NULL;
1672
- };
1673
-
1674
- #endif // 64_BIT && !WINDOWS