or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
# dudect/src/dudect.h
dudect/src/dudect.h
dudect_config_t
存儲測試設定
ttest_ctx_t
存儲 t-test 變數
dudect_ctx_t
存儲測試數據
dudect_state_t
dudect_init
初始化 ctx,並分配記憶體
初始化 Welch’s t-test 統計數據
dudect 會篩選執行時間的前 DUDECT_NUMBER_PERCENTILES 個最快的數據,避免 OS 影響
assert() 確保記憶體分配成功,避免 malloc() 失敗
return 0 表示 dudect_init() 初始化成功
為甚麼使用 calloc 而不是 malloc 呢?
calloc v.s malloc
If allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any object type with fundamental alignment.
If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage).
calloc is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.
A previous call to free, free_sized, and free_aligned_sized(since C23) or realloc that deallocates a region of memory synchronizes-with a call to calloc that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory by calloc. There is a single total order of all allocation and deallocation functions operating on each particular region of memory.
dudect_main
Welch’s t-test 統計測試
這是 Welford 方法 的實作,它可以在線更新均值和變異數。
x 是執行時間,根據它的 class (clazz=0 or 1),更新 mean 和 variance。
計算 Welch’s t-value 來比較兩組數據的均值是否有顯著差異。
測量函式執行時間
更新統計數據
這裡會丟棄前 10 次測試數據 (warming-up 階段),然後將執行時間存入 t-test。
結果報告
主函式
examples/simple/example.c
這個 example.c 程式使用 dudect 來測試 check_tag() 函式是否為 constant-time,也就是 不會根據輸入值影響執行時間。
example.c 的核心目標
模擬密鑰
run_test - 執行測試
dudect_init(&ctx, &config);
ctx 是 dudect 測試的上下文 (context) 它儲存 所有的測試數據、執行時間記錄、t-test 統計數據。run_test() 是 執行 constant-time 測試的主函式,回傳 0 或 1:
進入點
(void)argc;
和(void)argv;
這部分不影響程式的執行邏輯,只是為了讓編譯器知道這些變數是「刻意不使用」,而不是「程式設計師忘記使用」。若沒有這兩行可能會產生像是
warning: unused parameter ‘argc’ [-Wunused-parameter]
的警告且 如果程式的未來版本可能會接受命令列參數,那麼保留 argc 和 argv 是個 良好的習慣
example.c 主要使用的 dudect.h 函式
dudect_init()
dudect.h
dudect_main()
dudect.h
dudect_free()
dudect.h
randombytes
dudect.h
randombit()
dudect.h
web server
select (blocking)
可以把 select 想成是 : "作業系統,我現在要監控這幾個 fd,如果有任何一個可以用了(ready),請叫醒我!"
把 server_fd 設成 non-blocking 模式(進階)
int flags = fcntl(server_fd, F_GETFL, 0);
fcntl(server_fd, F_SETFL, flags | O_NONBLOCK);
這樣即使沒人來連線,accept() 也不會阻塞,而是直接回傳 -1 並設置 errno = EAGAIN 或 EWOULDBLOCK。