__brand: 'p1p1' = 'p1p1'
}
-/**
- * Base class for a point in projective space.
- */
-class ProjectivePoint extends Point {
- __class: 'p' = 'p'
-}
-
/**
* Projective coordinates.
*/
-export class p2 extends ProjectivePoint {
+export class p2 extends Point {
__brand: 'p2' = 'p2'
}
/**
* Extended projective coordinates.
*/
-export class p3 extends ProjectivePoint {
+export class p3 extends Point {
T: FieldElement = fe()
__brand: 'p3' = 'p3'
}
}
/**
- * Shared temporary variables used to hold intermediate results when doubling.
+ * Shared temporary variable used to hold intermediate results when doubling.
*/
const t = new p1p1()
-const t2 = new p2()
/**
* `r = 2p`
* Z = Y₁² - X₁²
* T = 2Z₁² - (Y₁²-X₁²)
* ```
+ *
+ * The order of operations here are specific, and changing them can break other
+ * functions (see `p1p1_dbl_to_p1p1` for more information).
*/
-function dbl_to_p1p1 (r: p1p1, p: ProjectivePoint): void {
- fe_add(r.X, p.X, p.Y) // X = X₁+Y₁
- fe_sq_pair_dbl(r.X, r.X, r.T, p.Z) // X = (X₁+Y₁)², T = 2Z₁²
- fe_sq_pair(r.Z, p.X, r.Y, p.Y) // Z = X₁², Y = Y₁²
+function dbl_to_p1p1 (r: p1p1, X: FieldElement, Y: FieldElement, Z: FieldElement): void {
+ fe_add(r.X, X, Y) // X = X₁+Y₁
+ fe_sq_pair_dbl(r.X, r.X, r.T, Z) // X = (X₁+Y₁)², T = 2Z₁²
+ fe_sq_pair(r.Z, X, r.Y, Y) // Z = X₁², Y = Y₁²
fe_addsub(r.Y, r.Z, r.Y, r.Z) // Y = Y₁²+X₁², Z = Y₁²-X₁²
fe_sub(r.X, r.X, r.Y) // X = (X₁+Y₁)² - (Y₁²+X₁²)
fe_sub(r.T, r.T, r.Z) // T = 2Z₁² - (Y₁²-X₁²)
* `r = 2p`
*
* ```
- * X = X₁T₁
- * Y = Y₁Z₁
- * Z = Z₁T₁
+ * X = (X₁T₁ + Y₁Z₁)² - (Y₁²Z₁² + X₁²T₁²)
+ * Y = Y₁²Z₁² + X₁²T₁²
+ * Z = Y₁²Z₁² - X₁²T₁²
+ * T = 2Z₁²T₁² - (Y₁²Z₁² - X₁²T₁²)
* ```
*
- * then
- *
- * ```
- * X = (X₁+Y₁)² - (Y₁²+X₁²)
- * Y = Y₁² + X₁²
- * Z = Y₁² - X₁²
- * T = 2Z₁² - (Y₁²-X₁²)
- * ```
+ * Converts to projective coordinates in place instead of calling `p1p1_to_p2`
+ * so that `dbl_to_p1p1` overwrites each component only by squaring it after its
+ * final read. This allows `r` and `p` to reference the same underlying object
+ * without aliasing issues. This works here and not for other functions because
+ * it condenses 4 coordinates to 3 and leaves 1 available for scratch space so
+ * that `X` can be used to compute `X₂ + Y₂`. This depends on the order of
+ * operations in `dbl_to_p1p1`, so a change there requires a change here.
*/
export function p1p1_dbl_to_p1p1 (r: p1p1, p: p1p1): void {
- p1p1_to_p2(t2, p)
- dbl_to_p1p1(r, t2)
- p2_0(t2)
+ fe_mul(r.Y, p.Y, p.Z) // Y = Y₂ = Y₁Z₁
+ fe_mul_pair(r.Z, p.X, p.T, r.T, p.Z, p.T) // Z = X₂ = X₁T₁, T = Z₂ = Z₁T₁
+ dbl_to_p1p1(r, r.Z, r.Y, r.T)
}
/**
* ```
*/
export function p2_dbl_to_p2 (r: p2, p: p2): void {
- dbl_to_p1p1(t, p)
+ dbl_to_p1p1(t, p.X, p.Y, p.Z)
p1p1_to_p2(r, t)
- p1p1_0(t)
}
/**
* ```
*/
export function p2_dbl_to_p3 (r: p3, p: p2): void {
- dbl_to_p1p1(t, p)
+ dbl_to_p1p1(t, p.X, p.Y, p.Z)
p1p1_to_p3(r, t)
p1p1_0(t)
}
* X = ((X₁+Y₁)² - (Y₁²+X₁²))(2Z₁² - (Y₁²-X₁²))
* Y = (Y₁² + X₁²)(Y₁² - X₁²)
* Z = (Y₁² - X₁²)(2Z₁² - (Y₁²-X₁²))
- * ```
+ * ```
*/
export function p3_dbl_to_p2 (r: p2, p: p3): void {
- dbl_to_p1p1(t, p)
+ dbl_to_p1p1(t, p.X, p.Y, p.Z)
p1p1_to_p2(r, t)
p1p1_0(t)
}
* X = ((X₁+Y₁)² - (Y₁²+X₁²))(2Z₁² - (Y₁²-X₁²))
* Y = (Y₁² + X₁²)(Y₁² - X₁²)
* Z = (Y₁² - X₁²)(2Z₁² - (Y₁²-X₁²))
-* T = ((X₁+Y₁)² - (Y₁²+X₁²))(Y₁² + X₁²)
+ * T = ((X₁+Y₁)² - (Y₁²+X₁²))(Y₁² + X₁²)
* ```
*/
export function p3_dbl_to_p3 (r: p3, p: p3): void {
- dbl_to_p1p1(t, p)
+ dbl_to_p1p1(t, p.X, p.Y, p.Z)
p1p1_to_p3(r, t)
p1p1_0(t)
}