Skip to content

Commit 7c9074a

Browse files
authored
Examples: Fix compiling issues and imports, closes #69 (#73)
1 parent 29d9761 commit 7c9074a

13 files changed

+154
-167
lines changed

examples/elliptic.nim

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,42 @@
1-
# By Cyther606: http://forum.nimrod-lang.org/t/522
1+
# By Cyther606: https://forum.nim-lang.org/t/522
22
# Adapted from: https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart4-PrivateKeyToPublicKey.py
3-
import bigints, math, strutils
3+
import bigints
4+
import std/[math, strutils]
45

5-
proc `^`(base: int; exp: int): BigInt =
6-
let base = base.initBigInt
7-
var exp = exp
8-
result = 1.initBigInt
9-
while exp > 0:
10-
result *= base
11-
dec(exp)
6+
const
7+
one = 1.initBigInt
8+
two = 2.initBigInt
9+
zero = 0.initBigInt
1210

11+
proc `^`(base: int; exp: int): BigInt = pow(base.initBigInt, exp)
12+
13+
# Specs of the Bitcoin's curve - secp256k1
1314
let
14-
Pcurve: BigInt = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
15-
N = initBigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
16-
Acurve = 0.initBigInt
17-
Bcurve = 7.initBigInt
15+
primeCurve: BigInt = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - one
16+
numberPoints = initBigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
17+
Acurve = zero # with Bcurve = 7, coefficients in the elliptic curve equation y^2 = x^3 + Acurve * x + Bcurve
1818
Gx = initBigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240")
1919
Gy = initBigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424")
2020
Gpoint = (Gx, Gy)
2121
privKey = initBigInt("A0DC65FFCA799873CBEA0AC274015B9526505DAAAED385155425F7337704883E", 16)
2222

23-
proc modinv(a: BigInt): BigInt =
24-
var
25-
lm = 1.initBigInt
26-
hm = 0.initBigInt
27-
lowm = a mod Pcurve
28-
highm = Pcurve
29-
while lowm > 1:
30-
let
31-
ratio = highm div lowm
32-
nm = hm - (lm * ratio)
33-
temp = highm - (lowm * ratio)
34-
hm = nm
35-
highm = temp
36-
swap hm, lm
37-
swap highm, lowm
38-
result = lm mod Pcurve
39-
4023
proc ecAdd(a: tuple, b: tuple): (BigInt, BigInt) =
4124
let
42-
lamAdd = ((b[1] - a[1]) * modinv(b[0] - a[0])) mod Pcurve
43-
x = (lamAdd * lamAdd - a[0] - b[0]) mod Pcurve
44-
y = (lamAdd * (a[0] - x) - a[1]) mod Pcurve
25+
lamAdd = ((b[1] - a[1]) * invmod((b[0] - a[0]), primeCurve)) mod primeCurve
26+
x = (lamAdd * lamAdd - a[0] - b[0]) mod primeCurve
27+
y = (lamAdd * (a[0] - x) - a[1]) mod primeCurve
4528
result = (x, y)
4629

4730
proc ecDouble(a: tuple): (BigInt, BigInt) =
4831
var
49-
lam = ((3.initBigInt * a[0] * a[0] + Acurve) * modinv(2.initBigInt * a[1]))
50-
x = ((lam * lam) - (2.initBigInt * a[0])) mod Pcurve
51-
y = (lam * (a[0] - x) - a[1]) mod Pcurve
52-
lam = lam mod Pcurve
32+
lam = ((3.initBigInt * a[0] * a[0] + Acurve) * invmod(2.initBigInt * a[1], primeCurve))
33+
x = ((lam * lam) - (2.initBigInt * a[0])) mod primeCurve
34+
y = (lam * (a[0] - x) - a[1]) mod primeCurve
35+
lam = lam mod primeCurve
5336
result = (x, y)
5437

5538
proc ecMultiply(genPoint: tuple, scalarHex: BigInt): (BigInt, BigInt) =
56-
if scalarHex == 0 or scalarHex >= N:
39+
if scalarHex == zero or scalarHex >= numberPoints:
5740
raise newException(Exception, "Invalid Scalar/Private Key")
5841
var
5942
scalarBin = scalarHex.toString(base = 2)
@@ -80,7 +63,7 @@ proc main() =
8063
echo "04", publicKey[0].toString(base = 16).align(64, '0'), publicKey[1].toString(base = 16).align(64, '0')
8164
echo ""
8265
echo "the official Public Key - compressed:"
83-
echo if publicKey[1] mod 2 == 1: "03" & publicKey[0].toString(base = 16).align(64, '0')
66+
echo if publicKey[1] mod two == one: "03" & publicKey[0].toString(base = 16).align(64, '0')
8467
else: "02" & publicKey[0].toString(base = 16).align(64, '0')
8568

8669
main()

examples/nim.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
path = "$projectPath/../src"

examples/pidigits.nim

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
# Translation of http://benchmarksgame.alioth.debian.org/u32/program.php?test=pidigits&lang=go&id=4
1+
# This program prints as much pi digits as the user indicates
2+
# with the first command line argument
3+
# This program is an extension of the solution for https://rosettacode.org/wiki/Pi
4+
# translated from former website http://benchmarksgame.alioth.debian.org
25

3-
import os, strutils, unsigned, bigints
6+
import std/[os, strutils, options]
7+
import bigints
8+
9+
const
10+
zero = 0.initBigInt
11+
one = 1.initBigInt
12+
two = 2.initBigInt
13+
ten = 10.initBigInt
14+
let
15+
mask = (one shl 32) - one
416

517
var
6-
tmp1, tmp2, tmp3, acc, k, dd = initBigInt(0)
7-
den, num, k2 = initBigInt(1)
18+
tmp1, tmp2, tmp3, acc, k = zero
19+
den, num, k2 = one
820

921
proc extractDigit(): int32 =
1022
if num > acc:
@@ -20,35 +32,49 @@ proc extractDigit(): int32 =
2032
if tmp2 >= den:
2133
return -1
2234

23-
result = int32(tmp1.limbs[0])
35+
result = get(toSignedInt[int32](tmp1 and mask))
2436

2537
proc eliminateDigit(d: int32) =
26-
acc -= den * d
27-
acc *= 10
28-
num *= 10
38+
acc -= den * d.initBigInt
39+
acc *= ten
40+
num *= ten
2941

3042
proc nextTerm() =
31-
k += 1
32-
k2 += 2
43+
k += one
44+
k2 += two
3345
tmp1 = num shl 1
3446
acc += tmp1
3547
acc *= k2
3648
den *= k2
3749
num *= k
3850

39-
let n = parseInt(paramStr(1))
51+
proc findPiDigit(): int32 =
52+
result = -1
53+
while result < 0:
54+
nextTerm()
55+
result = extractDigit()
56+
4057
var i = 0
58+
if paramCount() == 0:
59+
# prints an infinite amount of pi digits
60+
while true:
61+
var d: int32 = findPiDigit()
62+
stdout.write chr(ord('0') + d)
63+
inc i
64+
if i == 40:
65+
echo ""
66+
i = 0
67+
eliminateDigit(d)
4168

42-
while i < n:
43-
var d: int32 = -1
44-
while d < 0:
45-
nextTerm()
46-
d = extractDigit()
69+
let n = parseInt(paramStr(1))
4770

71+
if n <= 0:
72+
quit("The number you entered is negative. Please specify a strictly positive number")
73+
74+
while i < n:
75+
var d: int32 = findPiDigit()
4876
stdout.write(chr(ord('0') + d))
4977
inc(i)
50-
if i mod 10 == 0:
78+
if i mod 40 == 0:
5179
echo "\t:", i
52-
if i >= n:
53-
break
5480
eliminateDigit(d)

examples/rc_combperm.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# Solution for http://rosettacode.org/wiki/Combinations_and_permutations
1+
# Solution for https://rosettacode.org/wiki/Combinations_and_permutations
22
import bigints
33

44
proc perm(n, k: int32): BigInt =
5-
result = initBigInt 1
5+
result = initBigInt(1)
66
var
7-
k = n - k
8-
n = n
7+
k = initBigInt(n - k)
8+
n = initBigInt(n)
99
while n > k:
1010
result *= n
1111
dec n
1212

1313
proc comb(n, k: int32): BigInt =
1414
result = perm(n, k)
15-
var k = k
16-
while k > 0:
15+
var k = initBigInt(k)
16+
while k > 0.initBigInt:
1717
result = result div k
1818
dec k
1919

examples/rc_godtheinteger.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Solution for http://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python
1+
# Solution for https://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python
22
import bigints
33

44
var cache = @[@[1.initBigInt]]
@@ -14,14 +14,15 @@ proc cumu(n: int): seq[BigInt] =
1414
proc row(n: int): seq[BigInt] =
1515
let r = cumu n
1616
result = @[]
17-
for i in 0 .. <n:
17+
for i in 0 ..< n:
1818
result.add r[i+1] - r[i]
1919

2020
echo "rows:"
2121
for x in 1..10:
2222
echo row x
2323

2424
echo "sums:"
25-
for x in [23, 123, 1234, 12345]:
25+
# for 12345 this implementation is too slow, for a faster implementation see rc_godtheinteger2.nim
26+
for x in [23, 123, 1234]:
2627
let c = cumu(x)
2728
echo x, " ", c[c.high]

examples/rc_godtheinteger2.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Solution for http://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python
1+
# Solution for https://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python
22
import bigints
33

44
var p = @[1.initBigInt]

examples/rc_hammingnumbers.nim

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
# Translation of http://rosettacode.org/wiki/Hamming_numbers#Another_implementation_of_same_approach
1+
# Translation of https://rosettacode.org/wiki/Hamming_numbers#Another_implementation_of_same_approach
2+
import bigints
23

3-
import bigints, math
4+
const
5+
two = initBigInt(2)
6+
three = initBigInt(3)
7+
five = initBigInt(5)
48

59
proc hamming(limit: int): BigInt =
610
var
711
h = newSeq[BigInt](limit)
8-
x2 = initBigInt(2)
9-
x3 = initBigInt(3)
10-
x5 = initBigInt(5)
12+
x2 = two
13+
x3 = three
14+
x5 = five
1115
i, j, k = 0
1216
for i in 0..h.high: h[i] = initBigInt(1)
1317
14-
for n in 1 .. < limit:
18+
for n in 1 ..< limit:
1519
h[n] = min([x2, x3, x5])
1620
if x2 == h[n]:
1721
inc i
18-
x2 = h[i] * 2
22+
x2 = h[i] * two
1923
if x3 == h[n]:
2024
inc j
21-
x3 = h[j] * 3
25+
x3 = h[j] * three
2226
if x5 == h[n]:
2327
inc k
24-
x5 = h[k] * 5
28+
x5 = h[k] * five
2529

2630
result = h[h.high]
2731

examples/rc_integersequence.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# This example includes an infinite loop that has to be interrupted by Ctrl+C
12
import bigints
23

4+
const one = 1.initBigInt
35
var i = 0.initBigInt
46
while true:
5-
i += 1
7+
i += one
68
echo i

examples/rc_leftfactorials.nim

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1-
import iterutils, bigints
1+
import bigints
22

3-
proc lfact: iterator: BigInt =
4-
result = iterator: BigInt =
5-
yield 0.initBigInt
6-
var
7-
fact = 1.initBigInt
8-
sum = 0.initBigInt
9-
n = 1.initBigInt
10-
while true:
11-
sum += fact
12-
fact *= n
13-
n += 1
14-
yield sum
3+
const
4+
one = 1.initBigInt
5+
zero = 0.initBigInt
156

16-
echo "first 11:"
17-
for i in lfact().slice(last = 10):
18-
echo " ", i
7+
iterator lfact: BigInt =
8+
yield zero
9+
var
10+
fact = one
11+
sum = zero
12+
n = one
13+
while true:
14+
sum += fact
15+
fact *= n
16+
n += one
17+
yield sum
1918

20-
echo "20 through 110 (inclusive) by tens:"
21-
for i in lfact().slice(20, 110, 10):
22-
echo " ", i
19+
var i = 0
20+
for n in lfact():
21+
if i == 0:
22+
echo "first 11:"
23+
if i == 20:
24+
echo "20 through 110 (inclusive) by tens:"
25+
if i == 1000:
26+
echo "Digits in 1,000 through 10,000 (inclusive) by thousands:"
27+
28+
if i <= 10:
29+
echo i, ": ", n
30+
elif i <= 110 and i mod 10 == 0:
31+
echo i, ": ", n
32+
elif i >= 1000 and i <= 10_000 and i mod 1000 == 0:
33+
echo i, ": ", ($n).len
34+
elif i > 10_000:
35+
break
36+
inc i
2337

24-
echo "Digits in 1,000 through 10,000 (inclusive) by thousands:"
25-
for i in lfact().slice(1_000, 10_000, 1_000):
26-
echo " ", ($i).len

examples/rc_paraffins.nim

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
# Solution for http://rosettacode.org/wiki/Paraffins
1+
# Solution for https://rosettacode.org/wiki/Paraffins
22
import bigints
33

44
const
55
nMax: int32 = 250
66
nBranches: int32 = 4
77

8+
const
9+
one = 1.initBigInt
10+
zero = 0.initBigInt
11+
812
var rooted, unrooted: array[nMax + 1, BigInt]
9-
rooted[0..1] = [1.initBigInt, 1.initBigInt]
10-
unrooted[0..1] = [1.initBigInt, 1.initBigInt]
13+
rooted[0..1] = [one, one]
14+
unrooted[0..1] = [one, one]
1115
for i in 2 .. nMax:
12-
rooted[i] = 0.initBigInt
13-
unrooted[i] = 0.initBigInt
16+
rooted[i] = zero
17+
unrooted[i] = zero
1418

1519
proc choose(m: BigInt, k: int32): BigInt =
1620
result = m
1721
if k == 1: return
18-
for i in 1 .. < k:
19-
result = result * (m + i) div (i + 1)
22+
for i in 1 ..< k:
23+
result = result * (m + i.initBigInt) div (i + 1).initBigInt
2024

2125
proc tree(br, n, l, sum: int32, cnt: BigInt) =
2226
var s: int32 = 0
@@ -33,8 +37,9 @@ proc tree(br, n, l, sum: int32, cnt: BigInt) =
3337
tree b, m, l, s, c
3438

3539
proc bicenter(s: int32) =
40+
var s = s
3641
if (s and 1) == 0:
37-
unrooted[s] += rooted[s div 2] * (rooted[s div 2] + 1) div 2
42+
unrooted[s] += rooted[s div 2] * (rooted[s div 2] + 1.initBigInt) div 2.initBigInt
3843

3944
for n in 1 .. nMax:
4045
tree 0, n, n, 1, 1.initBigInt

0 commit comments

Comments
 (0)