Implement to Montgomery domain conversion

This commit is contained in:
mratsim 2018-12-02 19:28:42 +01:00
parent 22d8bc218d
commit 27e2d2d2bc
3 changed files with 13 additions and 5 deletions

View File

@ -6,8 +6,9 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
This library provides constant time finite field primitives.
The main use will be for implementation of elliptic curve cryptography
This library provides constant time finite field primitives for implementation of elliptic curve cryptography.
Warning ⚠️: The library is in development state.
## Installation

View File

@ -160,14 +160,14 @@ template scaleadd_impl(a: var Fp, c: Limb) =
sub(a, Fp.P, tooBig)
func scaleadd*(a: var Fp, c: Limb) =
## Scale-accumulate
## Scale-accumulate modulo P
##
## With a word W = 2^LimbBitSize and a field Fp
## Does a <- a * W + c (mod p)
scaleadd_impl(a, c)
func scaleadd*(a: var Fp, c: static Limb) =
## Scale-accumulate
## Scale-accumulate modulo P
##
## With a word W = 2^LimbBitSize and a field Fp
## Does a <- a * W + c (mod p)

View File

@ -69,5 +69,12 @@ func montyMagic*(M: static BigInt): static Limb =
for _ in static(0 ..< k):
result *= 2 + M * result # x' = x(2 + ax) (`+` to avoid negating at the end)
# func toMonty*[P: static BigInt](a: Fp[P], montyMagic: Limb): Montgomery[P] =
func toMonty*[P: static BigInt](a: Fp[P]): Montgomery[P] =
## Convert a big integer over Fp to it's montgomery representation
## over Fp.
## i.e. Does "a * (2^LimbSize)^W (mod p), where W is the number
## of words needed to represent p in base 2^LimbSize
result = a
for i in static(countdown(P.limbs.high, 0)):
scaleadd(result, 0)