mirror of
https://github.com/logos-storage/constantine.git
synced 2026-01-02 13:13:07 +00:00
Remove sanity checks 'when isMainModule' superceded by comprehensive tests
This commit is contained in:
parent
928f515582
commit
53a5729442
@ -284,140 +284,3 @@ macro negmod_gen[N: static int](R: var Limbs[N], A, m: Limbs[N]): untyped =
|
||||
func negmod_asm*(r: var Limbs, a, m: Limbs) =
|
||||
## Constant-time modular negation
|
||||
negmod_gen(r, a, m)
|
||||
|
||||
# Sanity checks
|
||||
# ----------------------------------------------------------
|
||||
|
||||
when isMainModule:
|
||||
import ../../config/type_bigint, algorithm, strutils
|
||||
|
||||
proc mainAdd() =
|
||||
var a = [SecretWord 0xE3DF60E8F6D0AF9A'u64, SecretWord 0x7B2665C2258A7625'u64, SecretWord 0x68FC9A1D0977C8E0'u64, SecretWord 0xF3DC61ED7DE76883'u64]
|
||||
var b = [SecretWord 0x78E9C2EF58BB6B78'u64, SecretWord 0x547F65BD19014254'u64, SecretWord 0x556A115819EAD4B5'u64, SecretWord 0x8CA844A546935DC3'u64]
|
||||
var m = [SecretWord 0xFFFFFFFF00000001'u64, SecretWord 0x0000000000000000'u64, SecretWord 0x00000000FFFFFFFF'u64, SecretWord 0xFFFFFFFFFFFFFFFF'u64]
|
||||
var s = "0x5cc923d94f8c1b11cfa5cb7f3e8bb879be66ab7423629d968084a692c47ac647"
|
||||
|
||||
a.reverse()
|
||||
b.reverse()
|
||||
m.reverse()
|
||||
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a: ", a.toHex()
|
||||
debugecho " b: ", b.toHex()
|
||||
debugecho " m: ", m.toHex()
|
||||
addmod_asm(a, a, b, m, spareBits = 0)
|
||||
debugecho "after:"
|
||||
debugecho " a: ", a.toHex().tolower
|
||||
debugecho " s: ", s
|
||||
debugecho " ok: ", a.toHex().tolower == s
|
||||
|
||||
a = [SecretWord 0x00935a991ca215a6'u64, SecretWord 0x5fbdac6294679337'u64, SecretWord 0x1e41793877b80f12'u64, SecretWord 0x5724cd93cb32932d'u64]
|
||||
b = [SecretWord 0x19dd4ecfda64ef80'u64, SecretWord 0x92deeb1532169c3d'u64, SecretWord 0x69ce4ee28421cd30'u64, SecretWord 0x4d90ab5a40295321'u64]
|
||||
m = [SecretWord 0x2523648240000001'u64, SecretWord 0xba344d8000000008'u64, SecretWord 0x6121000000000013'u64, SecretWord 0xa700000000000013'u64]
|
||||
s = "0x1a70a968f7070526f29c9777c67e2f74880fc81afbd9dc42a4b578ee0b5be64e"
|
||||
|
||||
a.reverse()
|
||||
b.reverse()
|
||||
m.reverse()
|
||||
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a: ", a.toHex()
|
||||
debugecho " b: ", b.toHex()
|
||||
debugecho " m: ", m.toHex()
|
||||
addmod_asm(a, a, b, m, spareBits = 0)
|
||||
debugecho "after:"
|
||||
debugecho " a: ", a.toHex().tolower
|
||||
debugecho " s: ", s
|
||||
debugecho " ok: ", a.toHex().tolower == s
|
||||
|
||||
a = [SecretWord 0x1c7d810f37fc6e0b'u64, SecretWord 0xb91aba4ce339cea3'u64, SecretWord 0xd9f5571ccc4dfd1a'u64, SecretWord 0xf5906ee9df91f554'u64]
|
||||
b = [SecretWord 0x18394ffe94874c9f'u64, SecretWord 0x6e8a8ad032fc5f15'u64, SecretWord 0x7533a2b46b7e9530'u64, SecretWord 0x2849996b4bb61b48'u64]
|
||||
m = [SecretWord 0x2523648240000001'u64, SecretWord 0xba344d8000000008'u64, SecretWord 0x6121000000000013'u64, SecretWord 0xa700000000000013'u64]
|
||||
s = "0x0f936c8b8c83baa96d70f79d16362db0ee07f9d137cc923776da08552b481089"
|
||||
|
||||
a.reverse()
|
||||
b.reverse()
|
||||
m.reverse()
|
||||
|
||||
debugecho "--------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a: ", a.toHex()
|
||||
debugecho " b: ", b.toHex()
|
||||
debugecho " m: ", m.toHex()
|
||||
addmod_asm(a, a, b, m, spareBits = 0)
|
||||
debugecho "after:"
|
||||
debugecho " a: ", a.toHex().tolower
|
||||
debugecho " s: ", s
|
||||
debugecho " ok: ", a.toHex().tolower == s
|
||||
|
||||
a = [SecretWord 0xe9d55643'u64, SecretWord 0x580ec4cc3f91cef3'u64, SecretWord 0x11ecbb7d35b36449'u64, SecretWord 0x35535ca31c5dc2ba'u64]
|
||||
b = [SecretWord 0x97f7ed94'u64, SecretWord 0xbad96eb98204a622'u64, SecretWord 0xbba94400f9a061d6'u64, SecretWord 0x60d3521a0d3dd9eb'u64]
|
||||
m = [SecretWord 0xffffffff'u64, SecretWord 0xffffffffffffffff'u64, SecretWord 0xffffffff00000000'u64, SecretWord 0x0000000000000001'u64]
|
||||
s = "0x0000000081cd43d812e83385c1967515cd95ff7f2f53c61f9626aebd299b9ca4"
|
||||
|
||||
a.reverse()
|
||||
b.reverse()
|
||||
m.reverse()
|
||||
|
||||
debugecho "--------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a: ", a.toHex()
|
||||
debugecho " b: ", b.toHex()
|
||||
debugecho " m: ", m.toHex()
|
||||
addmod_asm(a, a, b, m, spareBits = 0)
|
||||
debugecho "after:"
|
||||
debugecho " a: ", a.toHex().tolower
|
||||
debugecho " s: ", s
|
||||
debugecho " ok: ", a.toHex().tolower == s
|
||||
|
||||
mainAdd()
|
||||
|
||||
proc mainSub() =
|
||||
var a = [SecretWord 0xf9c32e89b80b17bd'u64, SecretWord 0xdbd3069d4ca0e1c3'u64, SecretWord 0x980d4c70d39d5e17'u64, SecretWord 0xd9f0252845f18c3a'u64]
|
||||
var b = [SecretWord 0x215075604bfd64de'u64, SecretWord 0x36dc488149fc5d3e'u64, SecretWord 0x91fff665385d20fd'u64, SecretWord 0xe980a5a203b43179'u64]
|
||||
var m = [SecretWord 0xFFFFFFFFFFFFFFFF'u64, SecretWord 0xFFFFFFFFFFFFFFFF'u64, SecretWord 0xFFFFFFFFFFFFFFFF'u64, SecretWord 0xFFFFFFFEFFFFFC2F'u64]
|
||||
var s = "0xd872b9296c0db2dfa4f6be1c02a48485060d560b9b403d19f06f7f86423d5ac1"
|
||||
|
||||
a.reverse()
|
||||
b.reverse()
|
||||
m.reverse()
|
||||
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a: ", a.toHex()
|
||||
debugecho " b: ", b.toHex()
|
||||
debugecho " m: ", m.toHex()
|
||||
submod_asm(a, a, b, m, spareBits = 0)
|
||||
debugecho "after:"
|
||||
debugecho " a: ", a.toHex().tolower
|
||||
debugecho " s: ", s
|
||||
debugecho " ok: ", a.toHex().tolower == s
|
||||
|
||||
mainSub()
|
||||
|
||||
proc mainSubOutplace() =
|
||||
var a = [SecretWord 0xf9c32e89b80b17bd'u64, SecretWord 0xdbd3069d4ca0e1c3'u64, SecretWord 0x980d4c70d39d5e17'u64, SecretWord 0xd9f0252845f18c3a'u64]
|
||||
var b = [SecretWord 0x215075604bfd64de'u64, SecretWord 0x36dc488149fc5d3e'u64, SecretWord 0x91fff665385d20fd'u64, SecretWord 0xe980a5a203b43179'u64]
|
||||
var m = [SecretWord 0xFFFFFFFFFFFFFFFF'u64, SecretWord 0xFFFFFFFFFFFFFFFF'u64, SecretWord 0xFFFFFFFFFFFFFFFF'u64, SecretWord 0xFFFFFFFEFFFFFC2F'u64]
|
||||
var s = "0xd872b9296c0db2dfa4f6be1c02a48485060d560b9b403d19f06f7f86423d5ac1"
|
||||
|
||||
a.reverse()
|
||||
b.reverse()
|
||||
m.reverse()
|
||||
|
||||
var r: typeof(a)
|
||||
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a: ", a.toHex()
|
||||
debugecho " b: ", b.toHex()
|
||||
debugecho " m: ", m.toHex()
|
||||
submod_asm(r, a, b, m, spareBits = 0)
|
||||
debugecho "after:"
|
||||
debugecho " r: ", r.toHex().tolower
|
||||
debugecho " s: ", s
|
||||
debugecho " ok: ", r.toHex().tolower == s
|
||||
|
||||
mainSubOutplace()
|
||||
|
||||
@ -612,92 +612,3 @@ func square_asm_adx*[rLen, aLen: static int](r: var Limbs[rLen], a: Limbs[aLen])
|
||||
## Multi-precision Squaring
|
||||
## Assumes r doesn't alias a
|
||||
square_asm_adx_inline(r, a)
|
||||
|
||||
|
||||
# Sanity checks
|
||||
# ----------------------------------------------------------
|
||||
|
||||
when isMainModule:
|
||||
import
|
||||
../../../platforms/abstractions,
|
||||
../../config/type_bigint,
|
||||
../../arithmetic/limbs
|
||||
|
||||
type SW = SecretWord
|
||||
|
||||
# 4 limbs
|
||||
# --------------------------------
|
||||
|
||||
proc mainSqr1() =
|
||||
var a = [SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64]
|
||||
|
||||
var a2x, expected: Limbs[8]
|
||||
|
||||
a2x.square_asm_adx(a)
|
||||
expected.mul_asm_adx(a, a)
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a : ", a.toString()
|
||||
debugecho "after:"
|
||||
debugecho " a2x: ", a2x.toString()
|
||||
debugecho " ref: ", expected.toString()
|
||||
|
||||
doAssert bool(a2x == expected)
|
||||
|
||||
proc mainSqr2() =
|
||||
var a = [SW 0x2'u64, SW 0x1'u64, SW 0x1'u64, SW 0x2'u64]
|
||||
|
||||
var a2x, expected: Limbs[8]
|
||||
|
||||
a2x.square_asm_adx(a)
|
||||
expected.mul_asm_adx(a, a)
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a : ", a.toString()
|
||||
debugecho "after:"
|
||||
debugecho " a2x: ", a2x.toString()
|
||||
debugecho " ref: ", expected.toString()
|
||||
|
||||
doAssert bool(a2x == expected)
|
||||
|
||||
mainSqr1()
|
||||
mainSqr2()
|
||||
|
||||
# 6 limbs
|
||||
# --------------------------------
|
||||
|
||||
|
||||
proc mainSqr3() =
|
||||
var a = [SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64, SW 0xFFFF_FFFF_FFFF_FFFF'u64]
|
||||
|
||||
var a2x, expected: Limbs[12]
|
||||
|
||||
a2x.square_asm_adx(a)
|
||||
expected.mul_asm_adx(a, a)
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a : ", a.toString()
|
||||
debugecho "after:"
|
||||
debugecho " a2x: ", a2x.toString()
|
||||
debugecho " ref: ", expected.toString()
|
||||
|
||||
doAssert bool(a2x == expected)
|
||||
|
||||
proc mainSqr4() =
|
||||
var a = [SW 0x1'u64, SW 0x2'u64, SW 0x2'u64, SW 0x2'u64, SW 0x1'u64, SW 0x1'u64,]
|
||||
|
||||
var a2x, expected: Limbs[12]
|
||||
|
||||
a2x.square_asm_adx(a)
|
||||
expected.mul_asm_adx(a, a)
|
||||
debugecho "--------------------------------"
|
||||
debugecho "before:"
|
||||
debugecho " a : ", a.toString()
|
||||
debugecho "after:"
|
||||
debugecho " a2x: ", a2x.toString()
|
||||
debugecho " ref: ", expected.toString()
|
||||
|
||||
doAssert bool(a2x == expected)
|
||||
|
||||
mainSqr3()
|
||||
mainSqr4()
|
||||
|
||||
@ -283,88 +283,3 @@ func fromMont_asm*(r: var Limbs, a, M: Limbs, m0ninv: BaseType) =
|
||||
block: # Map from [0, 2p) to [0, p)
|
||||
var workspace{.noInit.}: typeof(r)
|
||||
r.finalSub_gen(t, M, workspace, mayCarry = false)
|
||||
|
||||
# Sanity checks
|
||||
# ----------------------------------------------------------
|
||||
|
||||
when isMainModule:
|
||||
import
|
||||
../../config/[type_bigint, common],
|
||||
../../arithmetic/limbs
|
||||
|
||||
type SW = SecretWord
|
||||
|
||||
# TODO: Properly handle low number of limbs
|
||||
|
||||
func redc2xMont_Comba[N: static int](
|
||||
r: var array[N, SecretWord],
|
||||
a: array[N*2, SecretWord],
|
||||
M: array[N, SecretWord],
|
||||
m0ninv: BaseType) =
|
||||
## Montgomery reduce a double-precision bigint modulo M
|
||||
# We use Product Scanning / Comba multiplication
|
||||
var t, u, v = Zero
|
||||
var carry: Carry
|
||||
var z: typeof(r) # zero-init, ensure on stack and removes in-place problems in tower fields
|
||||
staticFor i, 0, N:
|
||||
staticFor j, 0, i:
|
||||
mulAcc(t, u, v, z[j], M[i-j])
|
||||
|
||||
addC(carry, v, v, a[i], Carry(0))
|
||||
addC(carry, u, u, Zero, carry)
|
||||
addC(carry, t, t, Zero, carry)
|
||||
|
||||
z[i] = v * SecretWord(m0ninv)
|
||||
mulAcc(t, u, v, z[i], M[0])
|
||||
v = u
|
||||
u = t
|
||||
t = Zero
|
||||
|
||||
staticFor i, N, 2*N-1:
|
||||
staticFor j, i-N+1, N:
|
||||
mulAcc(t, u, v, z[j], M[i-j])
|
||||
|
||||
addC(carry, v, v, a[i], Carry(0))
|
||||
addC(carry, u, u, Zero, carry)
|
||||
addC(carry, t, t, Zero, carry)
|
||||
|
||||
z[i-N] = v
|
||||
|
||||
v = u
|
||||
u = t
|
||||
t = Zero
|
||||
|
||||
addC(carry, z[N-1], v, a[2*N-1], Carry(0))
|
||||
|
||||
# Final substraction
|
||||
discard z.csub(M, SecretBool(carry) or not(z < M))
|
||||
r = z
|
||||
|
||||
|
||||
proc main2L() =
|
||||
let M = [SW 0xFFFFFFFF_FFFFFFFF'u64, SW 0x7FFFFFFF_FFFFFFFF'u64]
|
||||
|
||||
# a²
|
||||
let adbl_sqr = [SW 0xFF677F6000000001'u64, SW 0xD79897153FA818FD'u64, SW 0x68BFF63DE35C5451'u64, SW 0x2D243FE4B480041F'u64]
|
||||
# (-a)²
|
||||
let nadbl_sqr = [SW 0xFECEFEC000000004'u64, SW 0xAE9896D43FA818FB'u64, SW 0x690C368DE35C5450'u64, SW 0x01A4400534800420'u64]
|
||||
|
||||
var a_sqr{.noInit.}, na_sqr{.noInit.}: Limbs[2]
|
||||
var a_sqr_comba{.noInit.}, na_sqr_comba{.noInit.}: Limbs[2]
|
||||
|
||||
a_sqr.redcMont_asm(adbl_sqr, M, 1, spareBits = 0, skipFinalSub = false)
|
||||
na_sqr.redcMont_asm(nadbl_sqr, M, 1, spareBits = 0, skipFinalSub = false)
|
||||
a_sqr_comba.redc2xMont_Comba(adbl_sqr, M, 1)
|
||||
na_sqr_comba.redc2xMont_Comba(nadbl_sqr, M, 1)
|
||||
|
||||
debugecho "--------------------------------"
|
||||
debugecho "after:"
|
||||
debugecho " a_sqr: ", a_sqr.toString()
|
||||
debugecho " na_sqr: ", na_sqr.toString()
|
||||
debugecho " a_sqr_comba: ", a_sqr_comba.toString()
|
||||
debugecho " na_sqr_comba: ", na_sqr_comba.toString()
|
||||
|
||||
doAssert bool(a_sqr == na_sqr)
|
||||
doAssert bool(a_sqr == a_sqr_comba)
|
||||
|
||||
main2L()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user