Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions bncurve/arith.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import options, endians
import nimcrypto/[utils, sysrand]
export options
import options, endians, types
import nimcrypto/utils
export options, types

{.deadCodeElim: on.}

type
BNU256* = array[4, uint64]
BNU512* = array[8, uint64]

proc setRandom*(a: var BNU512) {.inline.} =
## Set value of integer ``a`` to random value.
let ret = randomBytes(a)
doAssert(ret == 8)

proc random*(t: typedesc[BNU512]): BNU512 {.inline, noinit.} =
## Return random 512bit integer.
setRandom(result)

proc setZero*(a: var BNU256) {.inline.} =
## Set value of integer ``a`` to zero.
Expand Down Expand Up @@ -364,15 +352,6 @@ proc `$`*(src: BNU512): string =
## Return hexadecimal string representation of integer ``src``.
result = toString(src, false)

proc setRandom*(a: var BNU256, modulo: BNU256) {.noinit, inline.} =
## Set value of integer ``a`` to random value (mod ``modulo``).
var r = BNU512.random()
discard divrem(r, modulo, a)

proc random*(t: typedesc[BNU256], modulo: BNU256): BNU256 {.noinit, inline.} =
## Return random 256bit integer (mod ``modulo``).
result.setRandom(modulo)

proc invert*(a: var BNU256, modulo: BNU256) =
## Turn integer ``a`` into its multiplicative inverse (mod ``modulo``).
var u = a
Expand Down
4 changes: 2 additions & 2 deletions bncurve/fields.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import options, arith, fp, fq2, fq6, fq12
export options, arith, fp, fq2, fq6, fq12
import options, arith, fp, fq2, fq6, fq12, types
export options, arith, fp, fq2, fq6, fq12, types
13 changes: 1 addition & 12 deletions bncurve/fp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import arith, options

import options, arith, types
{.deadCodeElim: on.}

template fieldImplementation(finame, fimodulus, firsquared, fircubed,
fionep, fiinv: untyped): untyped {.dirty.} =
type finame* = distinct BNU256

proc setZero*(dst: var finame) {.noinit, inline.} =
## Set ``zero`` representation in Fp to ``dst``.
Expand All @@ -38,15 +36,6 @@ template fieldImplementation(finame, fimodulus, firsquared, fircubed,
## Return ``Fp`` modulus.
result = fimodulus

proc setRandom*(dst: var finame) {.noinit, inline.} =
## Set ``dst`` to random value
var a = BNU256.random(fimodulus)
dst = finame(a)

proc random*(t: typedesc[finame]): finame {.noinit, inline.} =
## Return random ``Fp``.
result.setRandom()

proc `+`*(x, y: finame): finame {.noinit, inline.} =
## Return result of ``x + y``.
result = x
Expand Down
11 changes: 1 addition & 10 deletions bncurve/fq12.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# This file may not be copied, modified, or distributed except according to
# those terms.
import options
import fq6, fq2, fp, arith
import fq6, fq2, fp, arith, types

{.deadCodeElim: on.}

Expand All @@ -32,11 +32,6 @@ const frobeniusCoeffsC1: array[4, FQ2] = [
)
]

type
FQ12* = object
c0*: FQ6
c1*: FQ6

proc init*(c0, c1: FQ6): FQ12 {.inline, noinit.} =
result.c0 = c0
result.c1 = c1
Expand All @@ -49,10 +44,6 @@ proc one*(t: typedesc[FQ12]): FQ12 {.inline, noinit.} =
result.c0 = FQ6.one()
result.c1 = FQ6.zero()

proc random*(t: typedesc[FQ12]): FQ12 {.inline, noinit.} =
result.c0 = FQ6.random()
result.c1 = FQ6.random()

proc isZero*(x: FQ12): bool {.inline, noinit.} =
result = (x.c0.isZero() and x.c1.isZero())

Expand Down
11 changes: 1 addition & 10 deletions bncurve/fq2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
# This file may not be copied, modified, or distributed except according to
# those terms.
import options
import fp, arith

import fp, arith, types
{.deadCodeElim: on.}

type
FQ2* = object
c0*: FQ
c1*: FQ

const
FQNonResidue = FQ([
Expand Down Expand Up @@ -45,10 +40,6 @@ proc one*(t: typedesc[FQ2]): FQ2 {.inline, noinit.} =
result.c0 = FQ.one()
result.c1 = FQ.zero()

proc random*(t: typedesc[FQ2]): FQ2 {.inline, noinit.} =
result.c0 = FQ.random()
result.c1 = FQ.random()

proc isZero*(x: FQ2): bool {.inline, noinit.} =
result = (x.c0.isZero() and x.c1.isZero())

Expand Down
13 changes: 1 addition & 12 deletions bncurve/fq6.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# This file may not be copied, modified, or distributed except according to
# those terms.
import options
import fq2, fp, arith
import fq2, fp, arith, types

{.deadCodeElim: on.}

Expand Down Expand Up @@ -53,12 +53,6 @@ const frobeniusCoeffsC2: array[4, FQ2] = [
)
]

type
FQ6* = object
c0*: FQ2
c1*: FQ2
c2*: FQ2

proc init*(c0, c1, c2: FQ2): FQ6 {.inline, noinit.} =
result.c0 = c0
result.c1 = c1
Expand All @@ -74,11 +68,6 @@ proc one*(t: typedesc[FQ6]): FQ6 {.inline, noinit.} =
result.c1 = FQ2.zero()
result.c2 = FQ2.zero()

proc random*(t: typedesc[FQ6]): FQ6 {.inline, noinit.} =
result.c0 = FQ2.random()
result.c1 = FQ2.random()
result.c2 = FQ2.random()

proc isZero*(x: FQ6): bool {.inline, noinit.} =
result = (x.c0.isZero() and x.c1.isZero() and x.c2.isZero())

Expand Down
Loading