Project

General

Profile

Bug #7242 ยป dhtest.rb

test case code - mhall (Matthew Hall), 10/30/2012 07:34 AM

 
1
#!/usr/local/ruby192/bin/ruby
2

    
3
require 'yaml'
4

    
5
class Integer
6
    # Compute self ^ e mod m
7
    def mod_exp e, m
8
        result = 1
9
        b = self
10
        while e > 0
11
            result = (result * b) % m if e[0] == 1
12
            e = e >> 1
13
            b = (b * b) % m
14
        end
15
        return result
16
    end
17

    
18
    # A roundabout, slow but fun way of counting bits.
19
    def bits_set
20
        ("%b" % self).count('1')
21
    end
22
end
23

    
24
def valid e
25
    e and e.between?(2, self.p-2) and e.bits_set > 1
26
end
27

    
28
yaml = YAML.load_file("#{ENV['HOME']}/dhtest.yaml")
29

    
30
P  = yaml['P']
31
G  = yaml['G']
32
Q  = yaml['Q']
33

    
34
CX = yaml['CX']
35
SX = yaml['SX']
36

    
37
CE = yaml['CE']
38
SE = yaml['SE']
39

    
40
CS = yaml['CS']
41
SS = yaml['SS']
42

    
43
#c.generate
44
#s.generate
45
#
46
CE2 = G.mod_exp(CX, P)
47
SE2 = G.mod_exp(SX, P)
48

    
49
ce_ok = CE == CE2
50
se_ok = SE == SE2
51

    
52
puts "CE #{CE2}"
53
puts
54
puts "SE #{SE2}"
55
puts
56
puts "CE_OK #{ce_ok} SE_OK #{se_ok}"
57
puts
58

    
59
#kc = c.secret(s.e)
60
#ks = s.secret(c.e)
61
#
62
CS2 = SE2.mod_exp(CX, P)
63
SS2 = CE2.mod_exp(SX, P)
64

    
65
cs_ok = CS == CS2
66
ss_ok = SS == SS2
67

    
68
puts "CS #{CS2}"
69
puts
70
puts "SS #{SS2}"
71
puts
72
puts "CS_OK #{cs_ok} SS_OK #{ss_ok}"
73
puts
74

    
75
dh_ok = CS2.to_s == SS2.to_s
76

    
77
if dh_ok
78
    puts "result: OK"
79
    exit 0
80
else
81
    puts "result: BAD"
82
    exit 1
83
end