mirror of
https://github.com/status-im/nim-bncurve.git
synced 2025-02-17 07:26:31 +00:00
fix styles
This commit is contained in:
parent
d2935bc074
commit
edc8371274
@ -51,7 +51,7 @@ proc isZero*(a: BNU256): bool {.inline, noinit.} =
|
||||
## Check if integer ``a`` is zero.
|
||||
(a[0] == 0'u64) and (a[1] == 0'u64) and (a[2] == 0'u64) and (a[3] == 0'u64)
|
||||
|
||||
proc setBit*(a: var openarray[uint64], n: int,
|
||||
proc setBit*(a: var openArray[uint64], n: int,
|
||||
to: bool): bool {.inline, noinit.} =
|
||||
## Set bit of integer ``a`` at position ``n`` to value ``to``.
|
||||
if n >= 256:
|
||||
@ -62,7 +62,7 @@ proc setBit*(a: var openarray[uint64], n: int,
|
||||
a[part] = a[part] and not(1'u64 shl index) or (value shl index)
|
||||
result = true
|
||||
|
||||
proc getBit*(a: openarray[uint64], n: int): bool {.inline, noinit.} =
|
||||
proc getBit*(a: openArray[uint64], n: int): bool {.inline, noinit.} =
|
||||
## Get value of bit at position ``n`` in integer ``a``.
|
||||
let part = n shr 6
|
||||
let bit = n - (part shl 6)
|
||||
@ -141,7 +141,7 @@ proc subNoBorrow*(a: var BNU256, b: BNU256) {.inline.} =
|
||||
a[3] = sbb(a[3], b[3], borrow)
|
||||
doAssert(borrow == 0)
|
||||
|
||||
proc macDigit(acc: var openarray[uint64], pos: int, b: openarray[uint64],
|
||||
proc macDigit(acc: var openArray[uint64], pos: int, b: openArray[uint64],
|
||||
c: uint64) =
|
||||
proc macWithCarry(a, b, c: uint64, carry: var uint64): uint64 {.noinit.} =
|
||||
var
|
||||
@ -154,7 +154,7 @@ proc macDigit(acc: var openarray[uint64], pos: int, b: openarray[uint64],
|
||||
splitU64(blo * clo + alo + carrylo, xhi, xlo)
|
||||
splitU64(blo * chi, yhi, ylo)
|
||||
splitU64(bhi * clo, zhi, zlo)
|
||||
splitU64(x_hi + y_lo + z_lo + a_hi + carry_hi, rhi, rlo)
|
||||
splitU64(xhi + ylo + zlo + ahi + carryhi, rhi, rlo)
|
||||
carry = (bhi * chi) + rhi + yhi + zhi
|
||||
result = combineU64(rlo, xlo)
|
||||
|
||||
@ -277,7 +277,7 @@ proc into*(t: typedesc[BNU512], c1: BNU256,
|
||||
break
|
||||
doAssert(carry == 0'u64)
|
||||
|
||||
proc fromBytes*(dst: var BNU256, src: openarray[byte]): bool =
|
||||
proc fromBytes*(dst: var BNU256, src: openArray[byte]): bool =
|
||||
## Create 256bit integer from big-endian bytes representation ``src``.
|
||||
## Returns ``true`` if ``dst`` was successfully initialized, ``false``
|
||||
## otherwise.
|
||||
@ -292,7 +292,7 @@ proc fromBytes*(dst: var BNU256, src: openarray[byte]): bool =
|
||||
bigEndian64(addr dst[3], addr buffer[0 * sizeof(uint64)])
|
||||
result = true
|
||||
|
||||
proc fromBytes*(dst: var BNU512, src: openarray[byte]): bool =
|
||||
proc fromBytes*(dst: var BNU512, src: openArray[byte]): bool =
|
||||
## Create 512bit integer form big-endian bytes representation ``src``.
|
||||
## Returns ``true`` if ``dst`` was successfully initialized, ``false``
|
||||
## otherwise.
|
||||
@ -318,7 +318,7 @@ proc fromHexString*(dst: var BNU256, src: string): bool {.inline, noinit.} =
|
||||
## otherwise.
|
||||
result = dst.fromBytes(fromHex(src))
|
||||
|
||||
proc toBytes*(src: BNU256, dst: var openarray[byte]): bool {.noinit.} =
|
||||
proc toBytes*(src: BNU256, dst: var openArray[byte]): bool {.noinit.} =
|
||||
## Convert 256bit integer ``src`` to big-endian bytes representation.
|
||||
## Return ``true`` if ``dst`` was successfully set, ``false`` otherwise.
|
||||
if len(dst) < 4 * sizeof(uint64):
|
||||
@ -329,7 +329,7 @@ proc toBytes*(src: BNU256, dst: var openarray[byte]): bool {.noinit.} =
|
||||
bigEndian64(addr dst[3 * sizeof(uint64)], unsafeAddr src[0])
|
||||
result = true
|
||||
|
||||
proc toBytes*(src: BNU512, dst: var openarray[byte]): bool {.noinit.} =
|
||||
proc toBytes*(src: BNU512, dst: var openArray[byte]): bool {.noinit.} =
|
||||
## Convert 512bit integer ``src`` to big-endian bytes representation.
|
||||
## Return ``true`` if ``dst`` was successfully set, ``false`` otherwise.
|
||||
if len(dst) < 8 * sizeof(uint64):
|
||||
|
@ -114,7 +114,7 @@ template fieldImplementation(finame, fimodulus, firsquared, fircubed,
|
||||
result = finame(num)
|
||||
mul(BNU256(result), BNU256(firsquared), BNU256(fimodulus), fiinv)
|
||||
|
||||
proc fromBytes*(dst: var finame, src: openarray[byte]): bool {.noinit.} =
|
||||
proc fromBytes*(dst: var finame, src: openArray[byte]): bool {.noinit.} =
|
||||
## Create integer FR/FQ from big-endian bytes representation ``src``.
|
||||
## Returns ``true`` if ``dst`` was successfully initialized, ``false``
|
||||
## otherwise.
|
||||
@ -126,7 +126,7 @@ template fieldImplementation(finame, fimodulus, firsquared, fircubed,
|
||||
dst = optr.get()
|
||||
result = true
|
||||
|
||||
proc fromBytes2*(dst: var finame, src: openarray[byte]): bool {.noinit.} =
|
||||
proc fromBytes2*(dst: var finame, src: openArray[byte]): bool {.noinit.} =
|
||||
## Create integer FR/FQ from big-endian bytes representation ``src`` in
|
||||
## Ethereum way (without modulo check).
|
||||
## Returns ``true`` if ``dst`` was successfully initialized, ``false``
|
||||
@ -138,7 +138,7 @@ template fieldImplementation(finame, fimodulus, firsquared, fircubed,
|
||||
result = true
|
||||
|
||||
proc toBytes*(src: finame,
|
||||
dst: var openarray[byte]): bool {.noinit, inline.} =
|
||||
dst: var openArray[byte]): bool {.noinit, inline.} =
|
||||
## Encode integer FP/FQ to big-endian bytes representation ``dst``.
|
||||
## Returns ``true`` if integer was successfully serialized, ``false``
|
||||
## otherwise.
|
||||
|
@ -14,20 +14,20 @@ import fq6, fq2, fp, arith
|
||||
const frobeniusCoeffsC1: array[4, FQ2] = [
|
||||
FQ2.one(),
|
||||
FQ2(
|
||||
c0: Fq([12653890742059813127'u64, 14585784200204367754'u64,
|
||||
c0: FQ([12653890742059813127'u64, 14585784200204367754'u64,
|
||||
1278438861261381767'u64, 212598772761311868'u64]),
|
||||
c1: Fq([11683091849979440498'u64, 14992204589386555739'u64,
|
||||
c1: FQ([11683091849979440498'u64, 14992204589386555739'u64,
|
||||
15866167890766973222'u64, 1200023580730561873'u64])
|
||||
),
|
||||
FQ2(
|
||||
c0: Fq([14595462726357228530'u64, 17349508522658994025'u64,
|
||||
c0: FQ([14595462726357228530'u64, 17349508522658994025'u64,
|
||||
1017833795229664280'u64, 299787779797702374'u64]),
|
||||
c1: Fq.zero()
|
||||
c1: FQ.zero()
|
||||
),
|
||||
FQ2(
|
||||
c0: Fq([3914496794763385213'u64, 790120733010914719'u64,
|
||||
c0: FQ([3914496794763385213'u64, 790120733010914719'u64,
|
||||
7322192392869644725'u64, 581366264293887267'u64]),
|
||||
c1: Fq([12817045492518885689'u64, 4440270538777280383'u64,
|
||||
c1: FQ([12817045492518885689'u64, 4440270538777280383'u64,
|
||||
11178533038884588256'u64, 2767537931541304486'u64])
|
||||
)
|
||||
]
|
||||
|
@ -124,7 +124,7 @@ proc `==`*(x: FQ2, y: FQ2): bool =
|
||||
proc mulByNonresidue*(x: FQ2): FQ2 =
|
||||
result = x * FQ2NonResidue
|
||||
|
||||
proc fromBytes*(dst: var FQ2, src: openarray[byte]): bool {.noinit.} =
|
||||
proc fromBytes*(dst: var FQ2, src: openArray[byte]): bool {.noinit.} =
|
||||
## Create 512bit integer FQ2 from big-endian bytes representation ``src``.
|
||||
## Returns ``true`` if ``dst`` was successfully initialized, ``false``
|
||||
## otherwise.
|
||||
@ -140,7 +140,7 @@ proc fromBytes*(dst: var FQ2, src: openarray[byte]): bool {.noinit.} =
|
||||
dst = init(c0o.get(), c1o.get())
|
||||
result = true
|
||||
|
||||
proc fromBytes2*(dst: var FQ2, src: openarray[byte]): bool {.noinit.} =
|
||||
proc fromBytes2*(dst: var FQ2, src: openArray[byte]): bool {.noinit.} =
|
||||
## Create integer FQ2 from big-endian bytes representation ``src`` in
|
||||
## Ethereum way.
|
||||
## Returns ``true`` if ``dst`` was successfully initialized, ``false``
|
||||
@ -151,7 +151,7 @@ proc fromBytes2*(dst: var FQ2, src: openarray[byte]): bool {.noinit.} =
|
||||
result = true
|
||||
|
||||
proc toBytes*(src: FQ2,
|
||||
dst: var openarray[byte]): bool {.noinit, inline.} =
|
||||
dst: var openArray[byte]): bool {.noinit, inline.} =
|
||||
## Encode 512bit integer FQ2 to big-endian bytes representation ``dst``.
|
||||
## Returns ``true`` if integer was successfully serialized, ``false``
|
||||
## otherwise.
|
||||
|
@ -14,20 +14,20 @@ import fq2, fp, arith
|
||||
const frobeniusCoeffsC1: array[4, FQ2] = [
|
||||
FQ2.one(),
|
||||
FQ2(
|
||||
c0: Fq([13075984984163199792'u64, 3782902503040509012'u64,
|
||||
c0: FQ([13075984984163199792'u64, 3782902503040509012'u64,
|
||||
8791150885551868305'u64, 1825854335138010348'u64]),
|
||||
c1: Fq([7963664994991228759'u64, 12257807996192067905'u64,
|
||||
c1: FQ([7963664994991228759'u64, 12257807996192067905'u64,
|
||||
13179524609921305146'u64, 2767831111890561987'u64])
|
||||
),
|
||||
FQ2(
|
||||
c0: Fq([3697675806616062876'u64, 9065277094688085689'u64,
|
||||
c0: FQ([3697675806616062876'u64, 9065277094688085689'u64,
|
||||
6918009208039626314'u64, 2775033306905974752'u64]),
|
||||
c1: Fq.zero()
|
||||
c1: FQ.zero()
|
||||
),
|
||||
FQ2(
|
||||
c0: Fq([14532872967180610477'u64, 12903226530429559474'u64,
|
||||
c0: FQ([14532872967180610477'u64, 12903226530429559474'u64,
|
||||
1868623743233345524'u64, 2316889217940299650'u64]),
|
||||
c1: Fq([12447993766991532972'u64, 4121872836076202828'u64,
|
||||
c1: FQ([12447993766991532972'u64, 4121872836076202828'u64,
|
||||
7630813605053367399'u64, 740282956577754197'u64])
|
||||
)
|
||||
]
|
||||
@ -35,20 +35,20 @@ const frobeniusCoeffsC1: array[4, FQ2] = [
|
||||
const frobeniusCoeffsC2: array[4, FQ2] = [
|
||||
FQ2.one(),
|
||||
FQ2(
|
||||
c0: Fq([8314163329781907090'u64, 11942187022798819835'u64,
|
||||
c0: FQ([8314163329781907090'u64, 11942187022798819835'u64,
|
||||
11282677263046157209'u64, 1576150870752482284'u64]),
|
||||
c1: Fq([6763840483288992073'u64, 7118829427391486816'u64,
|
||||
c1: FQ([6763840483288992073'u64, 7118829427391486816'u64,
|
||||
4016233444936635065'u64, 2630958277570195709'u64])
|
||||
),
|
||||
FQ2(
|
||||
c0: Fq([8183898218631979349'u64, 12014359695528440611'u64,
|
||||
c0: FQ([8183898218631979349'u64, 12014359695528440611'u64,
|
||||
12263358156045030468'u64, 3187210487005268291'u64]),
|
||||
c1: Fq.zero()
|
||||
c1: FQ.zero()
|
||||
),
|
||||
FQ2(
|
||||
c0: Fq([4938922280314430175'u64, 13823286637238282975'u64,
|
||||
c0: FQ([4938922280314430175'u64, 13823286637238282975'u64,
|
||||
15589480384090068090'u64, 481952561930628184'u64]),
|
||||
c1: Fq([3105754162722846417'u64, 11647802298615474591'u64,
|
||||
c1: FQ([3105754162722846417'u64, 11647802298615474591'u64,
|
||||
13057042392041828081'u64, 1660844386505564338'u64])
|
||||
)
|
||||
]
|
||||
|
@ -393,7 +393,7 @@ proc init*(p: var AffinePoint[G2], x: FQ2, y: FQ2): bool {.inline.} =
|
||||
p.y = y
|
||||
result = true
|
||||
|
||||
proc toBytes*[T: G1|G2](p: AffinePoint[T], dst: var openarray[byte]): bool =
|
||||
proc toBytes*[T: G1|G2](p: AffinePoint[T], dst: var openArray[byte]): bool =
|
||||
## Encode affine point coordinates (x, y) to big-endian bytes representation
|
||||
## ``dst``.
|
||||
## Returns ``true`` if coordinates was successfully serialized, ``false``
|
||||
@ -409,7 +409,7 @@ proc toBytes*[T: G1|G2](p: AffinePoint[T], dst: var openarray[byte]): bool =
|
||||
if p.y.toBytes(toOpenArray(dst, 64, 127)):
|
||||
result = true
|
||||
|
||||
proc fromBytes*[T: G1|G2](p: var AffinePoint[T], src: openarray[byte]): bool =
|
||||
proc fromBytes*[T: G1|G2](p: var AffinePoint[T], src: openArray[byte]): bool =
|
||||
## Decode affine point coordinates (x, y) from big endian bytes representation
|
||||
## ``src``.
|
||||
## Returns ``true`` if coordinates was successfully serialized, ``false``
|
||||
@ -469,7 +469,7 @@ proc toHexString*[T: G1|G2](p: AffinePoint[T],
|
||||
result = toHex(buffer, lowercase)
|
||||
|
||||
proc toBytes*[T: G1|G2](p: Point[T],
|
||||
dst: var openarray[byte]): bool {.inline.} =
|
||||
dst: var openArray[byte]): bool {.inline.} =
|
||||
## Encode point coordinates (x, y, z) to big-endian bytes representation
|
||||
## ``dst``.
|
||||
## Returns ``true`` if coordinates was successfully serialized, ``false``
|
||||
@ -492,7 +492,7 @@ proc toBytes*[T: G1|G2](p: Point[T],
|
||||
result = apo.get().toBytes(toOpenArray(dst, 1, outputSize))
|
||||
|
||||
proc fromBytes*[T: G1|G2](p: var Point[T],
|
||||
src: openarray[byte]): bool {.inline.} =
|
||||
src: openArray[byte]): bool {.inline.} =
|
||||
## Decode affine point coordinates (x, y, z) from big endian bytes
|
||||
## representation ``src``.
|
||||
## Returns ``true`` if coordinates was successfully serialized, ``false``
|
||||
|
@ -178,7 +178,7 @@ when isMainModule:
|
||||
0x43e1f593f0000001'u64, 0x2833e84879b97091'u64,
|
||||
0xb85045b68181585d'u64, 0x30644e72e131a029'u64
|
||||
]
|
||||
var c0, c2: BNU256
|
||||
var c0: BNU256
|
||||
var c1: Option[BNU256]
|
||||
c1 = a.divrem(moduloFr, c0)
|
||||
check:
|
||||
|
@ -5,7 +5,7 @@ import nimcrypto/utils
|
||||
type
|
||||
ValidationError = object of Exception
|
||||
|
||||
proc getPoint[T: G1|G2](t: typedesc[T], data: openarray[byte]): Point[T] =
|
||||
proc getPoint[T: G1|G2](t: typedesc[T], data: openArray[byte]): Point[T] =
|
||||
when T is G1:
|
||||
const nextOffset = 32
|
||||
var px, py: FQ
|
||||
@ -24,11 +24,11 @@ proc getPoint[T: G1|G2](t: typedesc[T], data: openarray[byte]): Point[T] =
|
||||
raise newException(ValidationError, "Point is not on curve")
|
||||
result = ap.toJacobian()
|
||||
|
||||
proc getFR(data: openarray[byte]): FR =
|
||||
proc getFR(data: openArray[byte]): FR =
|
||||
if not result.fromBytes2(data):
|
||||
raise newException(ValidationError, "Could not get FR value")
|
||||
|
||||
proc bn256ecAdd*(data: openarray[byte]): array[64, byte] =
|
||||
proc bn256ecAdd*(data: openArray[byte]): array[64, byte] =
|
||||
var input: array[128, byte]
|
||||
# Padding data
|
||||
let msglen = len(data)
|
||||
@ -43,7 +43,7 @@ proc bn256ecAdd*(data: openarray[byte]): array[64, byte] =
|
||||
# we can discard here because we supply proper buffer
|
||||
discard p.toBytes(result)
|
||||
|
||||
proc bn256ecMul*(data: openarray[byte]): array[64, byte] =
|
||||
proc bn256ecMul*(data: openArray[byte]): array[64, byte] =
|
||||
var input: array[96, byte]
|
||||
|
||||
# Padding data
|
||||
@ -60,7 +60,7 @@ proc bn256ecMul*(data: openarray[byte]): array[64, byte] =
|
||||
# we can discard here because we supply buffer of proper size
|
||||
discard p.toBytes(result)
|
||||
|
||||
proc bn256ecPairing*(data: openarray[byte]): array[32, byte] =
|
||||
proc bn256ecPairing*(data: openArray[byte]): array[32, byte] =
|
||||
let msglen = len(data)
|
||||
if msglen mod 192 != 0:
|
||||
raise newException(ValidationError, "Invalid input length")
|
||||
|
Loading…
x
Reference in New Issue
Block a user