diff --git a/constantine/arithmetic/finite_fields.nim b/constantine/arithmetic/finite_fields.nim index 29585e6..e15675a 100644 --- a/constantine/arithmetic/finite_fields.nim +++ b/constantine/arithmetic/finite_fields.nim @@ -91,12 +91,12 @@ func toBig*(src: Fp): auto {.noInit.} = func setZero*(a: var Fp) = ## Set ``a`` to zero - a.setZero() + a.mres.setZero() func setOne*(a: var Fp) = ## Set ``a`` to one # Note: we need 1 in Montgomery residue form - a = Fp.C.getMontyOne() + a.mres = Fp.C.getMontyOne() func `+=`*(a: var Fp, b: Fp) = ## In-place addition modulo p diff --git a/constantine/tower_field_extensions/abelian_groups.nim b/constantine/tower_field_extensions/abelian_groups.nim new file mode 100644 index 0000000..62cb3ac --- /dev/null +++ b/constantine/tower_field_extensions/abelian_groups.nim @@ -0,0 +1,139 @@ +# Constantine +# Copyright (c) 2018-2019 Status Research & Development GmbH +# Copyright (c) 2020-Present Mamy André-Ratsimbazafy +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +import + ../config/curves, + ../arithmetic/finite_fields + +# ############################################################ +# +# Algebraic concepts +# +# ############################################################ +# Too heavy on the Nim compiler, we just rely on generic instantiation +# to complain if the base field procedures don't exist. + +# type +# AbelianGroup* {.explain.} = concept a, b, var mA, var mR +# setZero(mA) +# setOne(mA) +# `+=`(mA, b) +# `-=`(mA, b) +# double(mR, a) +# sum(mR, a, b) +# diff(mR, a, b) + +# ############################################################ +# +# Quadratic Extension fields +# +# ############################################################ + +type + QuadExtAddGroup*[T] = concept x + ## Quadratic extension fields - Abelian Additive Group concept + x.c0 is T + x.c1 is T + +func setZero*(a: var QuadExtAddGroup) = + ## Set ``a`` to zero in the extension field + ## Coordinates 0 + 0 𝛼 + ## with 𝛼 the solution of f(x) = x² - µ = 0 + a.c0.setZero() + a.c1.setZero() + +func setOne*(a: var QuadExtAddGroup) = + ## Set ``a`` to one in the extension field + ## Coordinates 1 + 0 𝛼 + ## with 𝛼 the solution of f(x) = x² - µ = 0 + a.c0.setOne() + a.c1.setZero() + +func `+=`*(a: var QuadExtAddGroup, b: QuadExtAddGroup) = + ## Addition in the extension field + a.c0 += b.c0 + a.c1 += b.c1 + +func `-=`*(a: var QuadExtAddGroup, b: QuadExtAddGroup) = + ## Substraction in the extension field + a.c0 -= b.c0 + a.c1 -= b.c1 + +func double*(r: var QuadExtAddGroup, a: QuadExtAddGroup) = + ## Double in the extension field + r.c0.double(a.c0) + r.c1.double(a.c1) + +func sum*(r: var QuadExtAddGroup, a, b: QuadExtAddGroup) = + ## Sum ``a`` and ``b`` into r + r.c0.sum(a.c0, b.c0) + r.c1.sum(a.c1, b.c1) + +func diff*(r: var QuadExtAddGroup, a, b: QuadExtAddGroup) = + ## Difference of ``a`` by `b`` into r + r.c0.diff(a.c0, b.c0) + r.c1.diff(a.c1, b.c1) + +# ############################################################ +# +# Cubic Extension fields +# +# ############################################################ + +type + CubicExtAddGroup*[T] = concept x + ## Cubic extension fields - Abelian Additive Group concept + x.c0 is T + x.c1 is T + x.c2 is T + +func setZero*(a: var CubicExtAddGroup) = + ## Set ``a`` to zero in the extension field + ## Coordinates 0 + 0 w + 0 w² + ## with w the solution of f(x) = x³ - µ = 0 + a.c0.setZero() + a.c1.setZero() + a.c2.setZero() + +func setOne*(a: var CubicExtAddGroup) = + ## Set ``a`` to one in the extension field + ## Coordinates 1 + 0 w + 0 w² + ## with w the solution of f(x) = x³ - µ = 0 + a.c0.setOne() + a.c1.setZero() + a.c2.setZero() + +func `+=`*(a: var CubicExtAddGroup, b: CubicExtAddGroup) = + ## Addition in the extension field + a.c0 += b.c0 + a.c1 += b.c1 + a.c2 += b.c2 + +func `-=`*(a: var CubicExtAddGroup, b: CubicExtAddGroup) = + ## Substraction in the extension field + a.c0 -= b.c0 + a.c1 -= b.c1 + a.c2 -= b.c2 + +func double*(r: var CubicExtAddGroup, a: CubicExtAddGroup) = + ## Double in the extension field + r.c0.double(a.c0) + r.c1.double(a.c1) + r.c2.double(a.c2) + +func sum*(r: var CubicExtAddGroup, a, b: CubicExtAddGroup) = + ## Sum ``a`` and ``b`` into r + r.c0.sum(a.c0, b.c0) + r.c1.sum(a.c1, b.c1) + r.c2.sum(a.c2, b.c2) + +func diff*(r: var CubicExtAddGroup, a, b: CubicExtAddGroup) = + ## Difference of ``a`` by `b`` into r + r.c0.diff(a.c0, b.c0) + r.c1.diff(a.c1, b.c1) + r.c2.diff(a.c2, b.c2) diff --git a/constantine/tower_field_extensions/fp2_complex.nim b/constantine/tower_field_extensions/fp2_complex.nim index 223aca9..33c9fac 100644 --- a/constantine/tower_field_extensions/fp2_complex.nim +++ b/constantine/tower_field_extensions/fp2_complex.nim @@ -43,7 +43,8 @@ import ../arithmetic/finite_fields, - ../config/curves + ../config/curves, + ./abelian_groups type Fp2[C: static Curve] = object @@ -55,29 +56,7 @@ type ## ## This requires 𝑖² = -1 to not ## be a square (mod p) - c0, c1: Fp[Curve] - -func setZero*(a: var Fp2) = - ## Set ``a`` to zero in 𝔽p2 - ## Coordinates 0 + 0𝑖 - a.c0.setZero() - a.c1.setZero() - -func setOne*(a: var Fp2) = - ## Set ``a`` to one in 𝔽p2 - ## Coordinates 1 + 0𝑖 - a.c0.setOne() - a.c1.setZero() - -func `+=`*(a: var Fp2, b: Fp2) = - ## Addition over 𝔽p2 - a.c0 += b.c0 - a.c1 += b.c1 - -func `-=`*(a: var Fp2, b: Fp2) = - ## Substraction over 𝔽p2 - a.c0 -= b.c0 - a.c1 -= b.c1 + c0*, c1*: Fp[C] func square*(a: Fp2): Fp2 {.noInit.} = ## Return a^2 in 𝔽p2