add Abelian Group concept

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-25 14:10:59 +01:00
parent 2aa33ea226
commit e6c7b3e52a
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
3 changed files with 144 additions and 26 deletions

View File

@ -91,12 +91,12 @@ func toBig*(src: Fp): auto {.noInit.} =
func setZero*(a: var Fp) = func setZero*(a: var Fp) =
## Set ``a`` to zero ## Set ``a`` to zero
a.setZero() a.mres.setZero()
func setOne*(a: var Fp) = func setOne*(a: var Fp) =
## Set ``a`` to one ## Set ``a`` to one
# Note: we need 1 in Montgomery residue form # Note: we need 1 in Montgomery residue form
a = Fp.C.getMontyOne() a.mres = Fp.C.getMontyOne()
func `+=`*(a: var Fp, b: Fp) = func `+=`*(a: var Fp, b: Fp) =
## In-place addition modulo p ## In-place addition modulo p

View File

@ -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)

View File

@ -43,7 +43,8 @@
import import
../arithmetic/finite_fields, ../arithmetic/finite_fields,
../config/curves ../config/curves,
./abelian_groups
type type
Fp2[C: static Curve] = object Fp2[C: static Curve] = object
@ -55,29 +56,7 @@ type
## ##
## This requires 𝑖² = -1 to not ## This requires 𝑖² = -1 to not
## be a square (mod p) ## be a square (mod p)
c0, c1: Fp[Curve] c0*, c1*: Fp[C]
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
func square*(a: Fp2): Fp2 {.noInit.} = func square*(a: Fp2): Fp2 {.noInit.} =
## Return a^2 in 𝔽p2 ## Return a^2 in 𝔽p2