aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-07-25 22:13:39 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-07-25 22:13:39 +0200
commit33fe06c71019b826e47d3fdcf0f217ee9f32b5b1 (patch)
treeb8816cfe8731dda302a0ac8d25026cf8dcad2af7
parentfd50c91473eda09db01c63421fbb7084ed896494 (diff)
Notes on elliptic curves
-rw-r--r--docs/journal/pages/elliptic-curves.md20
-rw-r--r--lib/curve.ml45
2 files changed, 65 insertions, 0 deletions
diff --git a/docs/journal/pages/elliptic-curves.md b/docs/journal/pages/elliptic-curves.md
new file mode 100644
index 0000000..8e9695f
--- /dev/null
+++ b/docs/journal/pages/elliptic-curves.md
@@ -0,0 +1,20 @@
+> [Index](../README.md)
+
+# Elliptic Curves
+
+We are implementing PLONK over the BN254 curve. Baretto-Naehrig curves are a type of elliptic curve that are designed to be pairing friendly, which we need for PLONK.
+
+Implementing elliptic curves is actually easier than it sounds. Elements are `(x, y)` coordinate pairs, where each of the coordinates is a field element from the prime field we previously implemented. Baretto-Naehrig curves specifically have equations of the form `y^2 = x^3 + b` for some `b ∈ Fp`. The BN254 curve we are implementing has `b = 3`. Note that there is another Barreto-Naehrig curve which is also sometimes called, BN254 with has `b = 2`, this is not our curve.
+
+Curves are abelian groups, so we can implement addition and scalar multiplication (which is just repeated addition). While the addition formula looks complicated, it actually isn't that hard to derive. Lets start with the basic idea of point addition on an elliptic curve: if you draw a straight line that passes through 3 distinct points `P`, `Q` and `R` on the curve, then `P + Q + R = 0` (where 0 is some imaginary point representing a "zero" value, i.e. additive identity, and sometimes referred to as the point at infinity). From basic algebra, we know that the slope of the line between two points is `m = (y_P - y_Q) / (x_P - x_Q)`. Also, because `R` is on the same line, we have `y_R = y_P + m(x_R - x_P)` (and equivalently `y_R = y_Q + m(x_R - x_Q)`). Figuring out how to compute `x_R` is a little more complicated (it involves cubic equations) so lets just share the formula: `x_R = m^2 - x_P - x_Q`.
+
+The same principle can be extrapolated to doubling a point, which is a special case where `P = Q` in the example above, and the line would be the tangent to the curve passing by `P`. The formula of `x_R` and `y_R` are the same, but the slope is computed as the tangent `m = 3x_P^2 / 2y_P` (derivatives!).
+
+ It turns out that the equations for elliptic curves over real numbers also apply to finite fields, so yay! With this, we can implement the basic operations of a group over elliptic curve.
+
+# Resources
+
+- [A From-Scratch Tour of Bitcoin in Python](http://karpathy.github.io/2021/06/21/blockchain/)
+- [Elliptic Curve Cryptography: a Gentle Introduction](https://andrea.corbellini.name/2015/05/17/elliptic-curve-cryptography-a-gentle-introduction/)
+
+> [Index](../README.md)
diff --git a/lib/curve.ml b/lib/curve.ml
new file mode 100644
index 0000000..896cce1
--- /dev/null
+++ b/lib/curve.ml
@@ -0,0 +1,45 @@
+(** Elliptic Curve. *)
+
+(** Module signature for an elliptic curve. *)
+module type S = sig
+ type t
+ (** A type representing an element in an elliptic curve. *)
+
+ val inf : t
+ (** The point at infinity or additive identity. *)
+
+ val g : t
+ (** The generator point. *)
+
+ val n : Z.t
+ (** The order of the curve. *)
+
+ val equal : t -> t -> bool
+ (** [equal a b] returns [true] if [a] is equal to [b]. *)
+
+ val neg : t -> t
+ (** [neg a] is the additive inverse of [a]. [add a (neg a) = zero] for all
+ [a]. *)
+
+ val add : t -> t -> t
+ (** [add a b] is the sum of [a] plus [b] *)
+
+ val sub : t -> t -> t
+ (** [sub a b] is the difference of [a] minus [b]. It is equivalent to
+ [add a (neg b)]. *)
+
+ val scalar_mul : Z.t -> t -> t
+ (** [scalar_mul k a] is the scalar multiplication of [a] by [k]. *)
+
+ val ( ~- ) : t -> t
+ (** [neg]. *)
+
+ val ( + ) : t -> t -> t
+ (** [add]. *)
+
+ val ( - ) : t -> t -> t
+ (** [sub]. *)
+
+ val ( $* ) : Z.t -> t -> t
+ (** [scalar_mul]. *)
+end