Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
06dc8538
提交
06dc8538
authored
5月 19, 2016
作者:
Thomas Mueller Graf
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Supplemental hash function for 64-bit numbers
上级
61873c88
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
87 行增加
和
56 行删除
+87
-56
CalculateHashConstantLong.java
h2/src/test/org/h2/test/store/CalculateHashConstantLong.java
+87
-56
没有找到文件。
h2/src/test/org/h2/test/store/CalculateHashConstantLong.java
浏览文件 @
06dc8538
...
...
@@ -34,7 +34,7 @@ public class CalculateHashConstantLong implements Runnable {
private
final
AES
aes
=
new
AES
();
{
aes
.
setKey
(
"Hello W
elt
Hallo Welt"
.
getBytes
());
aes
.
setKey
(
"Hello W
orld
Hallo Welt"
.
getBytes
());
}
private
final
byte
[]
data
=
new
byte
[
16
];
...
...
@@ -54,6 +54,39 @@ public class CalculateHashConstantLong implements Runnable {
for
(
int
i
=
0
;
i
<
randomValues
.
length
;
i
++)
{
randomValues
[
i
]
=
r
.
nextInt
();
}
printQuality
(
new
CalculateHashConstantLong
()
{
@Override
public
long
hash
(
long
x
)
{
return
secureHash
(
x
);
}
@Override
public
String
toString
()
{
return
"AES"
;
}
},
randomValues
);
// Quality of AES
// Dependencies: 15715..16364
// Avalanche: 31998
// AvalancheSum: 3199841
// Effect: 49456..50584
printQuality
(
new
CalculateHashConstantLong
()
{
@Override
public
long
hash
(
long
x
)
{
x
=
(
x
^
(
x
>>>
30
))
*
0xbf58476d1ce4e5b9
L
;
x
=
(
x
^
(
x
>>>
27
))
*
0x94d049bb133111eb
L
;
return
x
^
(
x
>>>
31
);
}
@Override
public
String
toString
()
{
return
"Test"
;
}
},
randomValues
);
// Quality of Test
// Dependencies: 14693..16502
// Avalanche: 31996
// AvalancheSum: 3199679
// Effect: 49437..50537
Thread
[]
threads
=
new
Thread
[
8
];
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
...
...
@@ -67,28 +100,8 @@ public class CalculateHashConstantLong implements Runnable {
int
finalCount
=
10000
;
long
[]
randomValues
=
getRandomValues
(
finalCount
,
10
);
System
.
out
.
println
();
System
.
out
.
println
(
"AES:"
);
CalculateHashConstantLong
test
=
new
CalculateHashConstantLong
();
CalculateHashConstantLong
test
;
int
[]
minMax
;
int
av
=
0
;
test
=
new
CalculateHashConstantLong
()
{
@Override
public
long
hash
(
long
x
)
{
return
secureHash
(
x
);
}
};
minMax
=
test
.
getDependencies
(
test
,
randomValues
);
System
.
out
.
println
(
"Dependencies: "
+
minMax
[
0
]
+
".."
+
minMax
[
1
]);
av
=
0
;
for
(
int
j
=
0
;
j
<
100
;
j
++)
{
av
+=
test
.
getAvalanche
(
test
,
randomValues
[
j
]);
}
System
.
out
.
println
(
"Avalanche: "
+
(
av
/
100
));
System
.
out
.
println
(
"AvalancheSum: "
+
av
);
minMax
=
test
.
getEffect
(
test
,
finalCount
*
10
,
11
);
System
.
out
.
println
(
"Effect: "
+
minMax
[
0
]
+
".."
+
minMax
[
1
]);
test
=
new
CalculateHashConstantLong
();
long
best
=
0
;
int
dist
=
Integer
.
MAX_VALUE
;
...
...
@@ -99,7 +112,7 @@ public class CalculateHashConstantLong implements Runnable {
minMax
=
test
.
getDependencies
(
test
,
randomValues
);
System
.
out
.
println
(
"Dependencies: "
+
minMax
[
0
]
+
".."
+
minMax
[
1
]);
int
d
=
minMax
[
1
]
-
minMax
[
0
];
av
=
0
;
int
av
=
0
;
for
(
int
j
=
0
;
j
<
100
;
j
++)
{
av
+=
test
.
getAvalanche
(
test
,
randomValues
[
j
]);
}
...
...
@@ -119,6 +132,24 @@ public class CalculateHashConstantLong implements Runnable {
System
.
out
.
println
(
"Collisions: "
+
collisions
);
}
static
void
printQuality
(
CalculateHashConstantLong
test
,
long
[]
randomValues
)
{
int
finalCount
=
randomValues
.
length
*
10
;
System
.
out
.
println
(
"Quality of "
+
test
);
int
[]
minMax
;
int
av
=
0
;
minMax
=
test
.
getDependencies
(
test
,
randomValues
);
System
.
out
.
println
(
"Dependencies: "
+
minMax
[
0
]
+
".."
+
minMax
[
1
]);
av
=
0
;
for
(
int
j
=
0
;
j
<
100
;
j
++)
{
av
+=
test
.
getAvalanche
(
test
,
randomValues
[
j
]);
}
System
.
out
.
println
(
"Avalanche: "
+
(
av
/
100
));
System
.
out
.
println
(
"AvalancheSum: "
+
av
);
minMax
=
test
.
getEffect
(
test
,
finalCount
*
10
,
11
);
System
.
out
.
println
(
"Effect: "
+
minMax
[
0
]
+
".."
+
minMax
[
1
]);
System
.
out
.
println
(
"ok="
+
test
.
testCandidate
());
}
/**
* Store a random file to be analyzed by the Diehard test.
*/
...
...
@@ -169,40 +200,42 @@ public class CalculateHashConstantLong implements Runnable {
}
long
i
=
(
currentHigh
<<
48
)
|
((
long
)
low
<<
32
)
|
(
currentHigh
<<
16
)
|
low
;
constant
=
i
;
if
(!
testCandidate
())
{
continue
;
}
System
.
out
.
println
(
Long
.
toHexString
(
i
)
+
" hit "
+
i
);
candidates
.
add
(
i
);
}
}
private
boolean
testCandidate
()
{
// after one bit changes in the input,
// on average 32 bits of the output change
int
av
=
getAvalanche
(
this
,
0
);
if
(
Math
.
abs
(
av
-
28057
)
>
1000
)
{
continu
e
;
if
(
Math
.
abs
(
av
-
32000
)
>
1000
)
{
return
fals
e
;
}
av
=
getAvalanche
(
this
,
0xffffffffffffffff
L
);
if
(
Math
.
abs
(
av
-
28057
)
>
1000
)
{
continu
e
;
if
(
Math
.
abs
(
av
-
32000
)
>
1000
)
{
return
fals
e
;
}
long
es
=
getEffectSquare
(
this
,
randomValues
);
if
(
es
>
54379333
)
{
continue
;
if
(
es
>
1100000
)
{
System
.
out
.
println
(
"fail at a "
+
es
);
return
false
;
}
int
[]
minMax
=
getEffect
(
this
,
10000
,
1
);
if
(!
isWithin
(
1500
,
10000
,
minMax
))
{
continue
;
if
(!
isWithin
(
4700
,
5300
,
minMax
))
{
System
.
out
.
println
(
"fail at b "
+
minMax
[
0
]
+
" "
+
minMax
[
1
]);
return
false
;
}
minMax
=
getDependencies
(
this
,
randomValues
);
System
.
out
.
println
(
i
+
" minMax "
+
minMax
[
0
]
+
" "
+
minMax
[
1
]);
// aes: 117693..162049
if
(!
isWithin
(
13063
,
21744
,
minMax
))
{
continue
;
}
minMax
=
getEffect
(
this
,
100000
,
3
);
System
.
out
.
println
(
i
+
" minMax2 "
+
minMax
[
0
]
+
" "
+
minMax
[
1
]);
if
(!
isWithin
(
15143
,
82527
,
minMax
))
{
continue
;
}
System
.
out
.
println
(
Long
.
toHexString
(
i
)
+
" hit av:"
+
av
+
" bits:"
+
Long
.
bitCount
(
i
)
+
" es:"
+
es
+
" prime:"
+
BigInteger
.
valueOf
(
i
).
isProbablePrime
(
15
));
candidates
.
add
(
i
);
if
(!
isWithin
(
14500
,
17000
,
minMax
))
{
System
.
out
.
println
(
"fail at c "
+
minMax
[
0
]
+
" "
+
minMax
[
1
]);
return
false
;
}
return
true
;
}
long
getCollisionCount
()
{
...
...
@@ -316,7 +349,7 @@ System.out.println(i + " minMax2 " + minMax[0] + " " + minMax[1]);
}
/**
* Calculate if the
all
bit changes (that an output bit changes if an input
* Calculate if the bit changes (that an output bit changes if an input
* bit is changed) are within a certain range.
*
* @param h the hash object
...
...
@@ -360,7 +393,6 @@ System.out.println(i + " minMax2 " + minMax[0] + " " + minMax[1]);
* @return the output
*/
long
hash
(
long
x
)
{
// return secureHash(x);
x
=
((
x
>>>
32
)
^
x
)
*
constant
;
x
=
((
x
>>>
32
)
^
x
)
*
constant
;
x
=
(
x
>>>
32
)
^
x
;
...
...
@@ -374,7 +406,6 @@ System.out.println(i + " minMax2 " + minMax[0] + " " + minMax[1]);
* @return the output
*/
long
secureHash
(
long
x
)
{
Arrays
.
fill
(
data
,
(
byte
)
0
);
writeLong
(
data
,
0
,
x
);
aes
.
encrypt
(
data
,
0
,
16
);
return
readLong
(
data
,
0
);
...
...
@@ -393,7 +424,7 @@ System.out.println(i + " minMax2 " + minMax[0] + " " + minMax[1]);
}
private
static
long
readLong
(
byte
[]
buff
,
int
pos
)
{
return
(((
long
)
readInt
(
buff
,
pos
))
<<
32
)
|
readInt
(
buff
,
pos
+
4
);
return
(((
long
)
readInt
(
buff
,
pos
))
<<
32
)
|
(
readInt
(
buff
,
pos
+
4
)
&
0xffffffff
L
);
}
private
static
int
readInt
(
byte
[]
buff
,
int
pos
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论