cwwppb
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 2023q1 Homework1 (quiz1) contributed by < [chiwawachiwawa](https://github.com/Chiwawachiwawa) > :::danger 注意書寫規範,中英文間用一個空白字元區隔。依循[資訊科技詞彙翻譯](https://hackmd.io/@sysprog/it-vocabulary)指示,調整用語。 :notes: jserv ::: :::info 好的! ::: [kernel_code](https://github.com/torvalds/linux/blob/master/include/linux/list.h) ## 作業要求 - [ ] 修改 `queue.[ch]` 以完成 `make test` 自動評分系統的所有項目 - [ ] 研讀 Linux 核心原始程式碼的 [lib/list_sort.c](https://github.com/torvalds/linux/blob/master/lib/list_sort.c) 並嘗試引入到 lab0-c 專案,比較你自己實作的 merge sort 和 Linux 核心程式碼之間效能落差 - [ ] 確保 `qtest` 提供的 `web` 命令可以搭配上述佇列使用 - [ ] 新增命令 `shuffle`, 參考 [Fisher–Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) - [ ] 在 `qtest` 中執行 `option entropy 1` 並搭配 `ih RAND 10` - [ ] 研讀論文〈[Dude, is my code constant time?](https://eprint.iacr.org/2016/1123.pdf)〉,解釋本程式的 “simulation” 模式是如何透過以實驗而非理論分析,達到驗證時間複雜度,需要解釋 [Student’s t-distribution](https://en.wikipedia.org/wiki/Student%27s_t-distribution) 及程式實作的原理。 ## 開發環境 ## 更改 queue.c ### 1.new ```c struct list_head *q_new() { struct list_head *queue_head = malloc(sizeof(struct list_head)); if (!queue_head) return NULL; INIT_LIST_HEAD(queue_head); return queue_head; } ``` :::danger 依循 [CONTRIBUTING.md](https://github.com/sysprog21/lab0-c/blob/master/CONTRIBUTING.md) 規範的程式碼風格,包含本筆記列出的程式碼。 :notes: jserv ::: <s>[malloc():](https://legacy.cplusplus.com/reference/cstdlib/malloc/?kw=malloc)</s> [malloc(3)](https://man7.org/linux/man-pages/man3/malloc.3.html) > man page 視為第一手材料。 > :notes: jserv > 好的!會注意 > :dog: chiwawa ### 2.free [free():](http://tw.gitbook.net/c_standard_library/c_function_free.html) ```c void q_free(struct list_head *l) { if(!l) return; element_t *q_entry , *enable; list_for_each_entry_safe(q_entry, enable, l, list) { q_release_element(q_entry); } free(l); } ``` ### 3.q_insert_head ```c bool q_insert_head(struct list_head *head, char *s)//testing { if(!head) return false; element_t *insert_node = malloc(sizeof(element_t)); if(!insert_node) return false; size_t strsize = strlen(s) + 1; insert_node->value = malloc(strsize * sizeof(char)); if(!insert_node->value){ free(insert_node); return false; } memcpy(insert_node->value, s, strsize); list_add(&insert_node->list, head); return true; } ``` ### 4.q_insert_tail ```c bool q_insert_tail(struct list_head *head, char *s)//**testing { if(!head) return false; element_t *insert_node = malloc(sizeof(element_t)); if(!insert_node) return false; size_t strsize = strlen(s) + 1; insert_node->value = malloc(strsize * sizeof(char)); if(!insert_node->value){ free(insert_node); return false; } memcpy(insert_node->value, s, strsize); list_add_tail(&insert_node->list, head); return true; } ``` ### 5.q_remove_head ```c element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize) { if(!head || list_empty(head)) return NULL; element_t *rmtarget = list_last_entry(head, element_t, list); list_del(&rmtarget->list); if(sp){ size_t len = strlen(rmtarget->value) + 1; len = (bufsize - 1) > len ? len : (bufsize - 1); strncpy(sp, rmtarget->value, len); sp[len] = '\0'; } return rmtarget; } ``` ### 6.q_remove_tail ```c element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize) { if(!head||list_empty(head)) return NULL; element_t *element = list_last_entry(head, element_t, list); if (sp) { strncpy(sp, element->value, bufsize - 1); sp[bufsize - 1] = '\0'; } list_del(&element->list); return element; } ``` ### 7.q_size 這邊我採用的是經典的逐個走訪 ```c int q_size(struct list_head *head) { if(!head) return 0; int count = 0; struct list_head *node; list_for_each (node, head) count++; return count; } ``` ### 8.q_delete_mid 我用的方法為上課時提到的***快慢法*** :::danger 注意用語! :notes: jserv ::: :::info done! ::: ``` bool q_delete_mid(struct list_head *head) { if(!head || list_empty(head)) return false; struct list_head *s = head->next, *f = head->next; while (f != head && f != head->prev){ s = s->next; f = f->next->next; } element_t *rmelement = list_entry(s, element_t, list); list_del(&rmelement->list); q_release_element(rmelement); return true; } ``` ### 9.q_delete_dup 我想要嘗試使用 hash 來實作,此方法為暫時的權宜之計 ```c bool q_delete_dup(struct list_head *head) { if (!head || list_empty(head)) return false; element_t *imm_node, *next_node; bool dupornot = false; list_for_each_entry_safe(imm_node, next_node, head, list){ if(imm_node->list.next != head && strcmp(imm_node->value, next_node->value) == 0){ list_del(&imm_node->list); q_release_element(imm_node); dupornot = true; } else if(dupornot){ list_del(&imm_node->list); q_release_element(imm_node); dupornot = false; } } return true; } ``` ### 10.q_swap** ``` void q_swap(struct list_head *head) if (head && !list_empty(head)){ struct list_head *target = head->next; while (target != head && target->next != head){ struct list_head *temp = target; list_move(target, temp->next); target = target->next; } } return; } ``` ### 11.q_reverse 無限迴圈 ```c void q_reverse(struct list_head *head) { if (!head||list_empty) return NULL; struct list_head *imm_node, *enable_node; list_for_each_entry_safe(imm_node, enable_node, head);{ list_move(head, imm_node); } } ``` fixed ```c void q_reverse(struct list_head *head) { if (!head || list_empty(head)) return; struct list_head *n, *s, *t; list_for_each_safe (n, s, head) { t = n->next; n->next = n->prev; n->prev = t; } t = head->next; head->next = head->prev; head->prev = t; } ``` ### 12.q_reversek :::danger 不用張貼完整程式碼,開發紀錄著重於你的想法、考量因素,還有相關的分析。僅列出佐證用的程式碼即可。 :notes: jserv ::: :::info 好的!會注意。 ::: 利用了上面的revers來解決,每K個元素作一次revers,利用一個counter來實作。 fixed ```c /* Reverse the nodes of the list k at a time */ void q_reverseK(struct list_head *head, int k) { if(!head || list_empty(head)) return; struct list_head *n, *s, iter, *fake_head = head; int counter = 0; INIT_LIST_HEAD(&iter); list_for_each_safe(n, s, head){ counter++; if(counter == k){ list_cut_position(&iter, fake_head, n); q_reverse(&iter); list_splice_init(&iter, fake_head); counter = 0; fake_head = s->prev; } } } ``` ### 13.q_sort(testing...) 採用分而治之的方式來解,採取 Doubly-linked List 結構,首先我們找到中心點,並將我們的 doubly-linked List 分為兩段,並且利用地回進行排序,再透過 list_cut_position(&r, head, mid) 進行切割,然後利用 list_splice_init(head, &l) 來將沒有head的那一端定義一個新的head出來,最後利用while來進行比較,將剩餘的 element 轉移到尾部。 ```c void q_sort(struct list_head *head){ if(!head || list_empty(head) || list_is_singular(head)) return; struct list_head *middle; { struct list_head *l, *r; l = head->prev; r = head->next; while (r != l && r->next != l) { r = r->next; l = l->prev; } middle = r; } LIST_HEAD(r); LIST_HEAD(l); list_cut_position(&r, head, mid); list_splice_init(head, &l); q_sort(&r); q_sort(&l); while (!list_empty(&r) && !list_empty(&l)) { if (strcmp(list_first_entry(&r, element_t, list)->value, list_first_entry(&l, element_t, list)->value)<= 0) { list_move_tail(r.next, head); } else{ list_move_tail(l.next, head); } } list_splice_tail(&r, head); list_splice_tail(&l, head); } ``` ### 14.q_descend ```c int q_descend(struct list_head *head) { struct list_head *c = head->prev, *n = head->prev->prev; while(n != head){ if(strcmp(list_entry(n, element_t, list)->value,list_entry(c, element_t, list)->value) < 0){ list_del(n); } else{ c = n; } n = n->prev; } return q_size(head); } ``` ### 15.q_merge 在參考 [alanjian85](https://hackmd.io/@alanjian85/lab0-2023) 同學的實作後,我想要重新寫我的 `q_merge` (我一開始認為不能新增其他的函式)。 ```c int q_merge(struct list_head *head) { // https://leetcode.com/problems/merge-k-sorted-lists/ if (!head || list_empty(head)) return 0; int count = 0; struct list_head *first_head, temp_head; first_head = list_entry(head->next, queue_contex_t, chain)->q; INIT_LIST_HEAD(&temp_head); for (;;) { element_t *min_num = NULL; queue_contex_t *entry, *min_content; list_for_each_entry (entry, head, chain) { if (entry->q == NULL || list_empty(entry->q)) continue; element_t *element = list_first_entry(entry->q, element_t, list); if (!min_num || strcmp(element->value, min_num->value) < 0) { min_num = element; min_content = entry; } } if (!min_num) break; list_move_tail(&min_num->list, &temp_head); count++; if (min_content->q != first_head && list_empty(min_content->q)) min_content->q = NULL; } list_splice(&temp_head, first_head); return count; } ``` ``` ``` :::danger 文字訊息「不要」用螢幕截圖來展現,尊重視障朋友的閱讀權益。 :notes: jserv > 好的! ::: --- ## 論文閱讀[Dude, is my code constant time?](https://eprint.iacr.org/2016/1123.pdf) ### 什麼是 Student's t-distribution ? t分佈的機率密度函數( probability density function )包含一個自由度( degrees of freedom )參數,自由度是樣本量 n 減去 1 。自由度越大, t 分佈越接近正態分佈。 t 分佈的機率密度函數是對稱的,且其平均值為 0。 t 分佈在機率密度函數曲線下的面積為 1 ,可以使用t表或統計軟件計算 t 分佈的機率。 t 分佈在統計學中有廣泛應用,例如用於小樣本量的假設檢定,或計算置信區間等。 ### [dudect](https://) Dudect 是一種偵測雙重執行( Double Execution )攻擊的工具。 本程式利用了統計的方式來進行檢測我們的程式是否存在擁有倍以上手法攻擊的漏洞。 ### 統計學在本作業之中的應用 對於本程式的應用,我們在執行 make test 時,可以看到以下訊息: ``` Testing if time complexity of XXX is constant meas:not enough measurements (XXXXX still to go) ``` 這就是本作業再進行"隨機的(目前暫定為,等等我們會探討)"檢驗,利用 dudect 來測量我們這些程式是否為 constent time 在這其中,我發現了一個非常有趣的現象(見下一節) ## 隨機性 我在實作的過程中,發現了一個現象,在我們不改變 queue[ch] 兩個檔案的前提之下,每次測驗的分數,雖然看似相同,但是有少數出現較高或是較低的現象(如圖下所見) 1.第一次出現了 94 分 2.但我在之後的測試(以及之前多次的測試)皆為上述的 89 分 但總體上,我發現這個現象只會發生比預期分數還高的情形,比預期分數還低的情況我還沒遇到過,為了測試這個猜想,我用我的電腦測試了 30 次,其中出現了 94 分的情形有 2 次,其餘 28 次皆為 89 分,這個情形令我十分好奇,因此我想了解其中的原因。 3.首先,我們打開專案中的檔案,有關隨機性的部分 `random.[ch]` 。 很明顯,我根本看不懂,這時候,我想到利用chatGPT能否給我需要的資訊來參考。 由 chatGPT 的提示可以知道,作業中的檔案是為 Unix-like 平台定義了 randombytes() 實作,而有了他的提示之後,我發現我可以讀 [gcc/libstdc++-v3/include/bits/random.h](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/random.h),透過它來了解我們的程式是如何產出「隨機性」。 因為這段程式有 6000 多行,這代表又需要我們的 chatGPT 上場了。 ## True Random Number Generator * `random_device` 是 C++ STL 中提供的一種用於產生真正的隨機數的類。它的行為會根據系統而有所不同,但通常會使用優質熵源(如硬件噪聲)來生成隨機數。**(來自 chatGPT )**。這種情況下,是可以產生"真正的"隨機數的,而在察看了原文件後,我發現作品中使用 randombytes_linux_randombytes_getrandom() 來生成亂數,這樣就代表,其實我們在作業中得到的亂數是真正的亂數,而非是經過數學運算的,這也是 dudect 派上用場的原因之一,因為這個專案他強調的是不需要我們硬體的設計(在文章開頭有提到),就可以拿來判別是否為 constent time (他利用的是統計學), ### getrandom() Linux kernel 中有一些驅動程式會使用硬體 TRNG 來生成隨機數,當需要隨機數時,Linux kernel 會檢查這些裝置是否可用,並且使用它們生成隨機數。 **以下這段來自chatGPT** `除了硬體 TRNG 外,還有一些使用軟體技術來實作 TRNG 的方法。例如,Linux kernel 中有一個名為 "entropy pool" 的子系統,該子系統收集系統中不可預測的事件,例如中斷、鍵盤輸入和網絡數據包等。這些事件被稱為 "熵源",它們被使用來產生隨機數。這個子系統可以防止軟體 PRNG 因缺乏熵而產生可預測的隨機數。` [如何實作一個TRNG](https://blog.csdn.net/swithin_he/article/details/80795421?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167692703716800225566406%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167692703716800225566406&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-80795421-null-null.142^v73^control_1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=TRNG%20verilog&spm=1018.2226.3001.4187) CSDN ## Pseudo Random Number Generator 待更新。 ## 針對成績變動後經過討論後的結果 對專案的缺陷部分進行 trace

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    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.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully