From fc02e12f2e865c9f9b1d9b035c45527d63737b8d Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Wed, 3 Oct 2018 11:29:31 +0100 Subject: [PATCH] makes p751 consistent with p503 formatting, moves constant values to consts.go, etc. --- p751/arith_generic.go | 2 +- p751/consts.go | 44 ++++++++++++++++++++++++++++++++----------- p751/field_ops.go | 24 +++-------------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/p751/arith_generic.go b/p751/arith_generic.go index 89ceb87..17e2941 100644 --- a/p751/arith_generic.go +++ b/p751/arith_generic.go @@ -1,4 +1,4 @@ -// +build noasm arm64 arm +// +build noasm !amd64 package p751 diff --git a/p751/consts.go b/p751/consts.go index bf539a8..e49918e 100644 --- a/p751/consts.go +++ b/p751/consts.go @@ -166,26 +166,48 @@ var P751_BobIsogenyStrategy = [strategySizeB]uint32{ 0x03, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01} +// Used internally by this package. Not consts as Go doesn't allow arrays to be consts +// ------------------------------- + +// p751 +var p751 = FpElement{ + 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, + 0xffffffffffffffff, 0xffffffffffffffff, 0xeeafffffffffffff, + 0xe3ec968549f878a8, 0xda959b1a13f7cc76, 0x084e9867d6ebe876, + 0x8562b5045cb25748, 0x0e12909f97badc66, 0x00006fe5d541f71c} + +// 2*p751 +var p751x2 = FpElement{ + 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xDD5FFFFFFFFFFFFF, + 0xC7D92D0A93F0F151, 0xB52B363427EF98ED, 0x109D30CFADD7D0ED, + 0x0AC56A08B964AE90, 0x1C25213F2F75B8CD, 0x0000DFCBAA83EE38} + +// p751 + 1 +var p751p1 = FpElement{ + 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, + 0x0000000000000000, 0x0000000000000000, 0xeeb0000000000000, + 0xe3ec968549f878a8, 0xda959b1a13f7cc76, 0x084e9867d6ebe876, + 0x8562b5045cb25748, 0x0e12909f97badc66, 0x00006fe5d541f71c} + +// R^2 = (2^768)^2 mod p +var p751R2 = FpElement{ + 2535603850726686808, 15780896088201250090, 6788776303855402382, + 17585428585582356230, 5274503137951975249, 2266259624764636289, + 11695651972693921304, 13072885652150159301, 4908312795585420432, + 6229583484603254826, 488927695601805643, 72213483953973} + +// 1*R mod p var P751_OneFp2 = Fp2Element{ A: FpElement{ 0x249ad, 0x0, 0x0, 0x0, 0x0, 0x8310000000000000, 0x5527b1e4375c6c66, 0x697797bf3f4f24d0, 0xc89db7b2ac5c4e2e, 0x4ca4b439d2076956, 0x10f7926c7512c7e9, 0x2d5b24bce5e2}, - B: FpElement{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, } +// 1/2 * R mod p var P751_HalfFp2 = Fp2Element{ A: FpElement{ 0x00000000000124D6, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0xB8E0000000000000, 0x9C8A2434C0AA7287, 0xA206996CA9A378A3, 0x6876280D41A41B52, 0xE903B49F175CE04F, 0x0F8511860666D227, 0x00004EA07CFF6E7F}, - B: FpElement{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, } - -// (2^768)^2 mod p -// This can't be a constant because Go doesn't allow array constants, so try -// not to modify it. -var montgomeryRsq = FpElement{ - 2535603850726686808, 15780896088201250090, 6788776303855402382, - 17585428585582356230, 5274503137951975249, 2266259624764636289, - 11695651972693921304, 13072885652150159301, 4908312795585420432, - 6229583484603254826, 488927695601805643, 72213483953973} diff --git a/p751/field_ops.go b/p751/field_ops.go index 5bc24d1..12796c6 100644 --- a/p751/field_ops.go +++ b/p751/field_ops.go @@ -4,24 +4,6 @@ import . "github.com/cloudflare/p751sidh/internal/isogeny" // 2*p751 var ( - // p751 - p751 = FpElement{ - 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, - 0xffffffffffffffff, 0xffffffffffffffff, 0xeeafffffffffffff, - 0xe3ec968549f878a8, 0xda959b1a13f7cc76, 0x084e9867d6ebe876, - 0x8562b5045cb25748, 0x0e12909f97badc66, 0x00006fe5d541f71c} - // p751 + 1 - p751p1 = FpElement{ - 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, - 0x0000000000000000, 0x0000000000000000, 0xeeb0000000000000, - 0xe3ec968549f878a8, 0xda959b1a13f7cc76, 0x084e9867d6ebe876, - 0x8562b5045cb25748, 0x0e12909f97badc66, 0x00006fe5d541f71c} - // 2*p751 - p751x2 = FpElement{ - 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xDD5FFFFFFFFFFFFF, - 0xC7D92D0A93F0F151, 0xB52B363427EF98ED, 0x109D30CFADD7D0ED, - 0x0AC56A08B964AE90, 0x1C25213F2F75B8CD, 0x0000DFCBAA83EE38} ) //------------------------------------------------------------------------------ @@ -166,9 +148,9 @@ func (fp751Ops) ToMontgomery(x *Fp2Element) { var aRR FpElementX2 // convert to montgomery domain - fp751Mul(&aRR, &x.A, &montgomeryRsq) // = a*R*R - fp751MontgomeryReduce(&x.A, &aRR) // = a*R mod p - fp751Mul(&aRR, &x.B, &montgomeryRsq) + fp751Mul(&aRR, &x.A, &p751R2) // = a*R*R + fp751MontgomeryReduce(&x.A, &aRR) // = a*R mod p + fp751Mul(&aRR, &x.B, &p751R2) fp751MontgomeryReduce(&x.B, &aRR) }