Skip to content

Commit 608fd3b

Browse files
committed
Add some script to test the performance of different hash types
1 parent 4851516 commit 608fd3b

File tree

8 files changed

+107
-9
lines changed

8 files changed

+107
-9
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
# quantum-resistant-lock-script
22
Qantum resistant lock script on CKB, using SPHINCS+
3+
4+
5+
## Performance
6+
Use items for tests/sphincsplus/optimization/run-all-optimization.sh
7+
8+
| | 128s bit | 256s bit |
9+
| ------------- | ---------- | --------- |
10+
| pubkey size | 32 | 64 |
11+
| sign size | 7888 | 29824 |
12+
| shake simple | 16.9M | 37.1M |
13+
| shake robust | 34.3M | 73.2M |
14+
| sha2 simple | 10.7M | 24.7M |
15+
| sha2 robust | 22.5M | 60.4M |
16+
| haraka simple | 27.5M | 60.4M |
17+
| haraka robust | 45.7M | 102.8M |

tests/sphincsplus/optimization/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LD := $(TARGET)gcc
55
PARAMS = sphincs-shake-256f
66
THASH = robust
77

8-
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -nostdlib -Wno-nonnull-compare -DCKB_VM -DCKB_DECLARATION_ONLY
8+
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -nostdlib -Wno-nonnull-compare -DCKB_VM -DCKB_DECLARATION_ONLY -g -DCKB_C_STDLIB_PRINTF
99
LDFLAGS := -fdata-sections -ffunction-sections
1010

1111
# Using a new version of gcc will have a warning of ckb-c-stdlib
@@ -72,12 +72,13 @@ BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:7601a814be2595ad471288fe
7272
all: build/verify
7373

7474
all-via-docker:
75-
docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make"
75+
cd ../../../ && docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code/tests/sphincsplus/optimization && make PARAMS=$(PARAMS) THASH=$(THASH)"
7676

7777
build/verify: optimization-sphincsplus.c $(SOURCES) $(HEADERS)
7878
$(CC) $(CFLAGS) -o $@ $(SOURCES) $<
7979

8080
run: build/verify
81+
export RUST_LOG=debug
8182
ckb-debugger --bin $< --max-cycles=10000000000
8283

8384
FLAME_GRAPH_DIR := ~/code/tmp/FlameGraph/

tests/sphincsplus/optimization/optimization-sphincsplus.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ int main() {
2323
return 2;
2424
}
2525

26-
printf("Done");
26+
printf("PubKey size: %d, Sign size: %d\n", sizeof(G_TEST_DATA_PUB_KEY),
27+
sizeof(G_TEST_DATA_SIGN));
28+
// printf("Done");
2729
return 0;
2830
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
workdir=$(
2+
cd $(dirname $0)
3+
pwd
4+
)
5+
6+
cd $workdir
7+
8+
HASH_NAMES="shake sha2 haraka"
9+
HASH_SIZES="128 256"
10+
HASH_OPTIONS="s"
11+
THASHS="simple robust"
12+
for HASH_NAME in ${HASH_NAMES[@]}; do
13+
for HASH_SIZE in ${HASH_SIZES[@]}; do
14+
for HASH_OPTION in ${HASH_OPTIONS[@]}; do
15+
for THASH in ${THASHS[@]}; do
16+
PARAMS="sphincs-$HASH_NAME-$HASH_SIZE$HASH_OPTION"
17+
echo "-----------------------------------------------"
18+
echo $PARAMS $THASH
19+
20+
make clean > /dev/null
21+
make all-via-docker PARAMS=$PARAMS THASH=$THASH > /dev/null
22+
ckb-debugger --bin build/verify --max-cycles=10000000000
23+
24+
if (($? == 0)); then
25+
echo "success"
26+
else
27+
exit 1
28+
fi
29+
30+
done
31+
done
32+
done
33+
done

tests/sphincsplus_rust/examples/run_base.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub const MAX_CYCLES: u64 = std::u64::MAX;
1111

1212
fn main() {
1313
let mut config = TestConfig::new();
14+
config.print_time = true;
1415

1516
let mut dummy = DummyDataLoader::new();
1617

@@ -23,5 +24,5 @@ fn main() {
2324
verifier.set_debug_printer(debug_printer);
2425
let verify_result = verifier.verify(MAX_CYCLES);
2526
let res = verify_result.expect("pass verification");
26-
println!("cycles: {}", res);
27+
println!("cycles: {} ({:.2?}M)", res, (res as f64) / 1024.0 / 1024.0);
2728
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
if [ ! -n "$1" ] ;then
2+
HASH_NAME="shake"
3+
HASH_SIZE="128"
4+
THASH="robust"
5+
HASH_OPTION="s"
6+
else
7+
HASH_NAME=$1
8+
HASH_SIZE=$2
9+
THASH=$3
10+
HASH_OPTION=$4
11+
fi
12+
PARAMS="sphincs-$HASH_NAME-$HASH_SIZE$HASH_OPTION"
13+
14+
#!/bin/bash
15+
workdir=$(
16+
cd $(dirname $0)/../../
17+
pwd
18+
)
19+
20+
cd $workdir
21+
22+
rm -rf build/*
23+
mkdir -p build
24+
25+
make all-via-docker PARAMS=$PARAMS THASH=$THASH > /dev/null
26+
if (($? != 0)); then
27+
exit 1
28+
fi
29+
30+
cd tests/sphincsplus_rust
31+
cargo clean > /dev/null
32+
cargo build --examples --no-default-features --features "$HASH_NAME hash_$HASH_SIZE hash_options_$HASH_OPTION thashes_$THASH" > /dev/null 2>&1
33+
./target/debug/examples/run_base
34+
if (($? != 0)); then
35+
exit 1
36+
fi

tests/sphincsplus_rust/run_rust.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
if [ ! -n "$1" ] ;then
2-
PARAMS="sphincs-shake-256f"
2+
HASH_NAME="shake"
3+
HASH_SIZE="256"
34
THASH="robust"
5+
HASH_OPTION="f"
46
else
57
HASH_NAME=$1
68
HASH_SIZE=$2
79
THASH=$3
810
HASH_OPTION=$4
9-
PARAMS="sphincs-$HASH_NAME-$HASH_SIZE$HASH_OPTION"
1011
fi
12+
PARAMS="sphincs-$HASH_NAME-$HASH_SIZE$HASH_OPTION"
1113

1214
#!/bin/bash
1315
workdir=$(
@@ -29,7 +31,7 @@ fi
2931

3032
cd tests/sphincsplus_rust
3133
cargo clean
32-
cargo test
34+
cargo test --no-default-features --features "$HASH_NAME hash_$HASH_SIZE hash_options_$HASH_OPTION thashes_$THASH"
3335
if (($? == 0)); then
3436
echo "success"
3537
else

tests/sphincsplus_rust/src/utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct TestConfig {
2828
pub pubkey_error: bool,
2929
pub message_error: bool,
3030
rng: ThreadRng,
31+
pub print_time: bool,
3132
}
3233

3334
impl TestConfig {
@@ -38,6 +39,7 @@ impl TestConfig {
3839
pubkey_error: false,
3940
message_error: false,
4041
rng: thread_rng(),
42+
print_time: false,
4143
}
4244
}
4345

@@ -187,14 +189,20 @@ pub fn sign_tx_by_input_group(
187189
if config.message_error {
188190
config.rng.fill(&mut message);
189191
}
190-
// let start = std::time::Instant::now();
192+
let start = std::time::Instant::now();
191193
let sign = Bytes::from(if config.sign_error {
192194
config.gen_rand_buf(config.key.get_sign_len())
193195
} else {
194196
config.key.sign(&message)
195197
});
198+
if config.print_time {
199+
println!(
200+
"sign time(native): {} us ({:.2?}s)",
201+
start.elapsed().as_micros(),
202+
start.elapsed().as_secs_f32()
203+
);
204+
}
196205

197-
// println!("sign time(native): {} us", start.elapsed().as_micros());
198206
sign
199207
};
200208

0 commit comments

Comments
 (0)