AES-GCMã®ä¹ç®ãå®è£ ãã
1. ã¯ããã«
å
±ééµæå·ã§ããAESã®ã
ãªãããããããªæ¹å¼ã«GCMã¨ãããã®ãããã¾ãã
ããã¤ã®ä¹ç®ã ããå®è£
ãã¾ãã
æåã«è¨ã£ã¦ããã¾ããçè«ã¯èª¬æãã¾ãããã¨ãããã§ãã¾ããã
ç¥ããªããã
ããã§èª¬æããã®ã¯ãã©ããã£ã¦å®è£
ãããã§ãã
ç¹ã«NISTãé
å¸ãã¦ããPDFã®ä¹ç®ã®æé©åã詳ããè¦ã¦ããã¾ãã
ãã¨ã§è©³ããè¨ãã¾ãããå
ã«è¨ã£ã¦ãããªãã¨ãã£ãããã人ããããããããªãã®ã§ã
ããã ãã¯å
è¡ãã¦æ¸ãã¦ããã¾ãã
ä½æããä¹ç®ã®æ¹æ³ã¯ä¸è¨ã®éãã
- No tables
- Simple, 8-bit tables
- Simple, 4-bit tables
- Simple, 1-bit tablesãï¼PDFã«ãªããã®ï¼
次ã®ãã¤ã¯ä½ãã¾ããã
- Shoupâs, 8-bit tables
- Shoupâs, 4-bit tables
åèã¨ãã¦Common Lispã®å®æåãä¸è¨ã«ãªã³ã¯ãã¦ããã¾ãã
AES, CCM, GCMã使ãã¾ãã
https://github.com/nptcl/fixed/blob/main/aes/aes.lisp
1.1. ä»æ§æ¸
ã¾ãã¯ä»æ§æ¸ããã
ãã¶ããããå
ã ã¨æãã¾ãã
- Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
ããä¸ã¤ãããããã¤ã¯ä½ãªãã ï¼
æ¤ç´¢ã§åºã¦ãããã©NISTå
ã§ãªã³ã¯å¼µã£ã¦ãã¨ãããè¦ã¤ãããªãã£ãã
- The Galois/Counter Mode of Operation (GCM)
https://csrc.nist.rip/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
ãã®ä¸ã®æ¹ã®PDFãæé«ãªãã§ãã
å®è£
ããã¨ããè¦ç¹ã§è¨ããã¦ãããã¾ããã
ä¸ã®æ¹ã¯åèç¨åº¦ã§ã
ä¸ã®æ¹ãè¦ãªããä½ã£ãæ¹ãããã¨æãã¾ãã
ä¸ã¯ãencrypt, ghash, decryptããããã
æ°å¼ã§è¡¨ãããã®ãããã®ã§ããã
ããããããè¯ãã£ãã§ãã
ä¹ç®ä»¥å¤ã¯ãã®å¼ã ãã§ä½ãã¾ãã
ãããããã ãè¦ãã»ããããããããã§ãã
ä¹ç®ã¯ã©ã¡ãã®PDFãè¦ã¦ãä½ããã¨æãã¾ãã
ããããã¯ä¸ã®æ¹ã®PDFã§ãã
ããã«ä¸ã®PDFã¯ãä¹ç®ã®æé©åã¾ã§è¨åããã¦ãã¾ãã
ããã¤ãã¡ããã¨ããããã¨ããã®ãããã§ã®ç®çã§ãã
2. ã¬ãã¢ä½ã®æ¼ç®
ã¬ãã¢ä½ã¨ã¯æéä½ã®äºã ããã§ãã
GCMã§ã¯ã®æéä½ãæ±ãããã§ã
ã¨æ¸ãããã§ãã
GCMã§ã¯ãªããç´ ã®AESã§ã¯ã使ããã¦ããã®ãè¦ãã¦ãã¾ãã
æ¥åæ²ç·ã§ã¯ããããç´ æ°ã®æéä½ããã£ã¦ãã¾ããã
ãããä»åã¯ã§ããã
ç´ æ°ã§ã¯ãªãã®ã§æ¥åæ²ç·ã®æã¨ã¯ã ãã¶äºæ
ãéãã¾ãã
åã«ã§ã²ãããå²ã£ã¦ããã ãã§ã¯ãã¡ã¿ããã§ãã
ããã«æ°ãä»ãã®ã«æéãããã£ã¦ãã¾ã£ãã
ã¾ãæ®éã®å¤ã¨ãã¬ãã¢ä½ã®å¤ã®å¯¾å¿ãèãã¾ãã
ã©ã¡ãã128bitã®æ´æ°ãæ±ãã¾ããã
ãããã®ãªã¼ãã¼ãé転ãã¾ãã
æ®éã®æ´æ°ã®æä¸ä½1bitã¯ãã¬ãã¢ä½ã§ã¯æä¸ä½1bitã«ãªãã¾ãã
ãªãã§ãããªãã¨ã«ãªãã®ãã¯ç¥ãã¾ãããã
å®è£
ãããªãã¡ããã¨è¦ãã¦ãããªããã°ããã¾ããã
è¦ãã¦ããã¦ã»ããã®ã§è©³ããæ¸ãã¾ãã
æ®éã®å¤ã®ã9
ãäºé²æ°ã§è¡¨ãã¦ã¿ã¾ãã
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00001001
ã¡ãã£ã¨é·ãã§ããã
æ±ãæ´æ°ã128bitãªã®ã§ããããªã«é·ããªãããã§ãã
æ¸ããããªãã®ã§æ¬¡ã®ããã«çç¥ãã¾ãã
00000000 ... 00001001
å·¦ãä¸ä½ãããã§ãå³ãä¸ä½ãããã§ãã
ããããã¬ãã¢ä½ã®æ¼ç®ãè¡ãéã«ã¯é転ããã®ã§ã
å·¦ãä¸ä½ãããã«ãªãã¾ãã
ãããè¦ãã¦ããã¦ãã ããã
âãã¬ãã¢ä½ã§ã®æä¸ä½ããã 00000000 ... 00001001 âãæ®éã®å¤ã®æä¸ä½ããã
å¤æ°ã«å¤ãæ´æ°ãå
¥ã£ã¦ããã¨ãã¦ã
ã¬ãã¢ä½ã§ã®çªç®ã®ããããã¨è¨è¼ãã¾ãã
ãã®ã¨ããã¯æ´æ°ã®æä¸ä½ãããã§ãããã¨ã«æ³¨æãã¦ãã ããã
ã¤ã¾ããå¤ã9ã®å ´åã¯ã§ãã
ãããæ°ãä»ãã¾ã§å
¨ç¶æå³ãåãããªãã£ãã
æ´æ°ã¯128bitãªã®ã§ããæä¸ä½ãããã«ãªãã¾ãã
ã¤ã¾ããå¤ã9ã®å ´åã¯ã§ãã
ã§ã¯ã¬ãã¢ä½ã«è©±ãé²ãã¾ãã
ã¬ãã¢ä½ã§ä½¿ãæ¼ç®ã¯äºç¨®é¡ã
å ç®ã¨ä¹ç®ã§ãã
å ç®ã¯åç´ãªXORã§ãã
æ®éã®å ç®ã¨éããç¹°ä¸ããèæ
®ããå¿
è¦ãããã¾ããã
ããã¯é常ã®å ç®ããã ãã¶æ¥½ã§ããã
XORã¯âã¨è¡¨ãã¾ãã
ã¤ã¾ããã¨ã®å ç®ã¯ãã§ãã
ä¹ç®ã¯ããªãé¢åã§ãã
ä¹ç®ã¯ã¨è¡¨ããã¨ã«ãã¾ãã
ä¹ç®ã®å®è£
ã¯ã2é²æ°ã®ã²ã£ç®ã§è¨ç®ããã¨æã£ã¦ãã ããã
ã²ã£ç®ã«å¿
è¦ãªå¦çã¯ãå ç®ã¨ã·ããã§ãã
ã·ããã¨ã¯ã²ã£ç®ãé²ãã¦ããããã§ã
足ãæ°ã®ä½ãä¸ãã¦ããå¦çã§ãã
ãã®ã·ãããè¡ã£ãéã«128bitãè¶
ããå ´åã¯ã
ã¬ãã¢ä½ç¹æã®æ¹æ³ã§å¤ãä¿®æ£ããå¿
è¦ãããã¾ãã
æ®éã«èããã°128bitã¨128bitã®ä¹ç®çµæã¯256bit以ä¸ã§ããã
ä»ã¯ã¬ãã¢ä½ã§ãã®ã§ä½ã¨ããã¦128bitã«ç´ããªããã°ãªããªãããã§ãã
ç´ æ°ã®ã¬ãã¢ä½ã ã£ããä¹ç®ãæ±ããå¾ã«ä½ããæ±ããã°ããã ãã§ããã
ä»åã¯ç´ æ°ã§ã¯ãªãã®ã§ç°¡åã«ã¯ããã¾ããã
åç´ã«128bitåãæ¨ã¦ã§ã¯ã ãã§ãã
ãããé¢åãã¨ãããé£ããããã§ãã
ããã¾ã§ãããã°ä¹ç®ã®ã³ã¼ããç解ã§ããã¨æãã¾ãã
3. ä¹ç®ã³ã¼ãã®èª¬æ
ä¹ç®ã®å®è£ ã示ãã¾ãã
Z â 0, V â X for i = 0 to 127 do if Yã®içªç®ã®ããã = 1 then Z â Zã¨Vã®å ç® end if V â Vã®2å end for return Z
ãã®ã³ã¼ãã¯ãä¹ç®ãè¡ãããã®æ±ç¨çãªã³ã¼ãã§ãã
ãã ã®æ´æ°ã ãããã¬ãã¢ä½ã ãããããããè¡ãã¨ä¹ç®ã§ãã¾ãã
ãã£ã¦ãããã¨ã¯ãã ã®2é²æ°ã®ã²ã£ç®ã§ãã
ã®ããããä¸ã¤ãã¤è¦ã¦ããã
ããããç«ã£ã¦ãããçµæã«ãã·ããããå¤ãå ç®ãã¾ãã
é çªã«è¦ã¦ããã¾ãã
for
ã®ä¸ã®å ç®ã®é¨åãåãä¸ãã¾ãã
if Yã®içªç®ã®ããã = 1 then Z â Zã¨Vã®å ç® end if
å
ã«ã®çªç®ã®ããããã¨è¡¨ãã¨æ¸ãã¾ããã
ã¨ãããã¨ã§ã次ã®ããã«æ¸ãç´ãã¾ãã
if Yi = 1 then Z â Zã¨Vã®å ç® end if
for
æã®æåã¯i=0
ã§ãããã¯ã«æ ¼ç´ããã¦ããæ´æ°ã®
æä¸ä½ãããã§ãããã¨ã«æ³¨æãã¦ãã ããã
ä½åº¦ãæ¸ãã¦ãã¾ãããã¬ãã¢ä½ã®æ¼ç®ã§ã¯ãããã®é çªãé転ãã¾ãã
Y0
ã¯ãå®éã®å¤Y
ã®æä¸ä½ãããã§ãã
Y127
ã¯ãå®éã®å¤Y
ã®æä¸ä½ãããã§ãã
ã³ã¼ãã®å
容ã¯ãããããç«ã£ã¦ããã
ã«ãå ç®ããã¨ããå
容ã§ãã
ã¯ãããã®ä½ç½®ã«å¿ãã¦ããã·ããããå
容ã§ãã
ã¨ã®å ç®ã¯ãåç´ãªXORã§ãã®ã§ã
ã¨ã¦ãç°¡åã«æ¸ãç´ããã¨ãã§ãã¾ãã
if Yi = 1 then Z â Z â V end if
ãã®å ç®(XOR)ã¯é常ã®å ç®ã¨ã¯ç°ãªãç¹°ãä¸ããããã¾ããã®ã§ã
128bitãè¶
ãããã¨ãããã¾ããã
ããã§å ç®ã®é¨åã¯å®äºã§ãã
åé¡ã¯æ¬¡ã®æã§ãã
V â Vã®2å
ããã¯ã¡ããã¨ç解ãã¦ãã ããã
PDFã«ã¯æ¬¡ã®ããã«è¨è¼ããã¦ãã¾ãã
if V127 = 0 then V â rightshift(V) else V â rightshift(V) â R end if
ã¾ãæåã®V127
ã¨ããã®ã¯ã
ã®ãã¨ã§ã
ã¬ãã¢ä½ã®æ¼ç®ã«ãããã®æä¸ä½ãããã表ãã¾ãã
ã¤ã¾ããæ ¼ç´ããã¦ããæ´æ°ã®æä¸ä½ãããã§ãã
æä¸ä½ã ã®æä¸ä½ã ã®ãç´ããããã§ãã
ããããä½ããããã¨ãã¦ããã®ãã説æãã¾ãã
ããã§ã¯ãã2åããããã«ã·ãããã¦ãã¾ãã
æ®éã®ããã°ã©ãã³ã°ã§ããã°å·¦ã«1ã¤ã·ãããããã¨ã§2åã«ãããã¨ãã§ãã¾ããã
ä»ã¯ã¬ãã¢ä½ã§ã®æ¼ç®ã®ããã«å³ã«ã·ãããã¦ãã¾ãã
èªåã¯ããã«æ°ãä»ãã®ã«æéãããã£ã¦ãã¾ãã¾ããã
ãªãã§å³ã«ã·ãããã¦ãã®ï¼ã£ã¦ãã£ã¨æã£ã¦ãã
ããã·ãããã¦128bitãè¶
ãããããªå ´åã«ã¯ã
ã¬ãã¢ä½ã§ã¯åç´ã«åãæ¨ã¦ããã¨ãã§ããªããã§ãã
ããã§ãã·ããã§åãæ¨ã¦ããããããããããããå¤å®ãã¦ããã¦ã
ããããã1ã§ãã£ãã¨ãã¯ã
ãããªãåºã¦ããã¨ããå®æ°ã®XORãåãã¾ãã
ãä½è
ãã¨ããã¨æ¬¡ã®éãã
0xE1
ã120bitåã ãå·¦ã«ã·ãããã¦ãã ããã
ããã¯ã¬ãã¢ä½ã§ã®è©±ã§ã¯ãªãã
0xE1
ã«å¯¾ãã¦æ®éã«ãä¹ç®ãã¦ãã ããã¨ããæå³ã§ãã
ããããå®æ°ã®XORãåããã¨ã§ã
ã¯ã¿åºããã®ããããä½ã¨ããã¦ããããã ããã§ãã
ã©ããã¦ãããªããã¯PDFã«èª¬æãããã¾ãã®ã§ã確èªãã ããã
ç§ã«ã¯ãããã¾ããã
ããããå
¨é¨ã¾ã¨ããã¨ã
PDFã«è¨è¼ããã¦ããã³ã¼ãã«ãªãã¾ãã
次ã®ã³ã¼ãã¯å¼ç¨ã§ãã
Z â 0, V â X for i = 0 to 127 do if Yi = 1 then Z â Z â V end if if V127 = 0 then V â rightshift(V) else V â rightshift(V) â R end if end for return Z
ãããå®æå½¢ã§ãã
ä¸è¨ã®ã³ã¼ãããã®ã¾ã¾å®è£
ããã°æ£ããåãã¾ãã
ã2åãããã¨ãã«é¢ãã¦ã¯ããå°ã説æãå¿
è¦ã§ãã
2åããã¨ããæ¼ç®ãã次ã®ããã«è¡¨ããã¨ãã§ããã¨ã®ãã¨ã
ã¨ã®ä¹ç®ã®å
容ã¯ãå
ã»ã©æ示ããã³ã¼ãã«ããéãã
å³ã«ã·ãããã¦ããµããå ´åã¯ã§XORãåãã¨ãããã®ã§ãã
ãã®ã¯ããã¨ã§ãã£ã±ã使ãã®ã§è¦ãã¦ããã¦ãã ããã
ã³ã¼ãã次ã®ããã«æ¸ãç´ãã¾ãã
Z â 0, V â X for i = 0 to 127 do if Yi = 1 then Z â Z â V end if V â V â P end for return Z
ä¹ç®ãè¨ç®ããã³ã¼ãã«ã®ä¹ç®ãç¾ãã¦æ··ä¹±ãã¾ããã å¥ã«åèµ·å¼ã³åºããããã®ã§ã¯ãªãã ãåç´ã«2åã表ãã¨èãã¦ããã¨æãã¾ãã
ã¯è¤æ°åä¹ç®ã§ãããã¨ãè¦ãã¦ããã¾ãããã
ä¾ãã°åããã¨ãã¯æ¬¡ã®ããã«ãªãã¾ãã
4. é åãç¨ããå®è£
ä¹ç®ã¯ã工夫ããããã¨ã§æ©ãå®è¡ã§ãã¾ãã
ã¾ãã¯ä¹ç®ãã©ãã§ä½¿ããããè¦ã¦ããã¾ãããã
AES-GCMã§ã¯ãGHASHãç®åºããã¨ãã«ä½¿ç¨ããã¾ãã
PDFã®GHASHã®ç®åºå¼ãè¦ã¦ãããã°ãããã®ã§ããã
ä¹ç®ã¯ãã¹ã¦æ¬¡ã®ãããªå½¢ã«ãªã£ã¦ãã¾ãã
ã«ãã³ã®ä¸ã¯ããããã ã£ãã®ã§çç¥ãã¾ããã
ããã§ã®ãã¤ã³ãã¯ãå¿
ãå³ãããã«ãªã£ã¦ãããã¨ã§ãã
ä¹ç®ã®çæ¹ã¯ã§åºå®ãªã®ã§ãã
ã¯AESã®éµãè¨å®ããããã°æ±ãããã¨ãã§ããã®ã§ã
encrypt/decryptãè¡ãåã«
ãç¨ãã¦æé©åã®æºåããããã¨ãã§ãã¾ãã
å
¥åãã¨ããåºå®å¤ã¨ã®ä¹ç®ãæ±ãããã¨ãèãã¾ãã
ãã®æã«ç¨ããæé©åç¨ã®é
åãã¨ãã¾ãã
é
åã«ã¢ã¯ã»ã¹ããããã®å¼æ°ããæ·»åãã¨å¼ã³ã¾ãã
ä¾ãã°ãã¨è¡¨ããã¨ãããæ·»åã§ãã
4.1. æ·»å8bitã®é å
æ·»å8bitã®é
åã¨ã¯ãã¤ã¾ãè¦ç´ æ°ã256åã®é
åã®ãã¨ã§ãã
ãã¡ããç¨ãã¦æé©åãããã¨ãèãã¦ããã¾ãããã
ã¾ãã¯ã¨ä¹ç®ãããå
¥åã1byteãã¨ã«åå²ãã¾ãã
128bit=16byteãªã®ã§ã16åã«åå²ãããã¨ãã§ãã¾ãã
ä¾ãã°æ¬¡ã®ãããªå¤ã§ãã£ãã¨ãã¾ãã
X = 0xFFEEDDCCBBAA99887766554433221100
ããã16åã«åãã¾ãã
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
ãããã¯ããããããç¨æãã¦ããã16åã®
é
åã®è¦ç´ ãåç
§ããã®ã«ä½¿ãã¾ãã
ã¨ã®ä¹ç®ã®çµæãã次ã®ããã«è¡¨ãã¾ãã
ãã¾ãã®åè¦ç´ ãã ã®ã ã®å®æ°ãç´æã¡ãã¦ãã¾ããã
ããã§ã¯ãªãå¤æ°ã1byteãã¤åå²ããå
容ã
ã¨ãã¾ãã
ãã®ã¨ãä¹ç®ã¯æ¬¡ã®ããã«è¡¨ãã¾ãã
é
åããããã°ã
ä¹ç®ãæ±ã¾ãã¨ãããã¨ã§ãã
ãããªãã¨ããã¦ä½ãããããã®ãã¨ããã¨ã
ä¹ç®ãããã16åã®XORã§æ±ãããã¨ãã§ããã®ã§ä½ã³ã¹ããªã®ã§ãã
代åãããã¾ãã
ãã¼ã¿ã®å®¹éãããã¦åæå¤ã®è¨ç®ã§ãã
ãã¾ã¯ä¸ã¤ã®é
åã®è¦ç´ æ°ã¯256åã§ããã
ãã®ä¸ã«128bitã®ãã¼ã¿ãæ ¼ç´ããã
ãã®é
åã16åããã¨ããç¶æ³ã§ãã
äºåã«256 [å] à 16 [byte] à 16 [å] = 65536 [byte]ã®å®¹éãå¿
è¦ã«ãªãã¾ãã
ããã«ã決ã¾ã£ãå¾ã§ã
16åã®é
åãæåã«è¨ç®ããªããã°ãªãã¾ããã
ãã®ã³ã¹ãã¯ã256 [å] à 16 [å] = 4096 [å]åã§ãã
4.2. æ·»å4bitã®é å
4bitã®é
åãä½ããã¨ãèãã¾ãã
8bitã«æ¯ã¹ãã¨æ¬¡ã®ç¹å¾´ãããã¾ãã
- é åã®ãµã¤ãºãå°ããï¼ã²ã¨ã¤ããã256åã§ã¯ãªã16åï¼
- åæå¤ã®è¨å®ãæ©ã
- ä¹ç®ã®ç®åºé度ãé ã
ããã¨ãããæªãã¨ããããã¾ãã
ã¾ãã¯ã4bitãã¨ã«åå²ãã¾ãã
128bitãªã®ã§ã32åã«åå²ãããã¨ãã§ãã¾ãã
ä¾ãã°æ¬¡ã®ããã«åå²ããã¨ãã¾ãã
F F E E ... 1 1 0 0
32åã¯å¤ãã®ã§çç¥ãã¾ããã
ã4bitãã¤åå²ããå
容ã
ã¨ãã¾ãã
ã¾ããé
åãã¨ãã¾ãã
ãã®ã¨ãä¹ç®ã¯æ¬¡ã®ããã«è¡¨ãã¾ãã
8bitã®ã¨ãã¯15åã®XORã§è¨ç®ã§ãã¾ãããã
4bitã®ã¨ãã¯31åã«ãªã£ã¦ããã
ããã2åã®ã³ã¹ãã¨ãªã£ã¦ãã¾ãã
ããããã®åã¹ãã¼ã¹ã¯å°ãããªãã¾ãã
é
åã®è¦ç´ æ°ã¯16åã§ããã
ãã®ä¸ã«128bitã®ãã¼ã¿ãæ ¼ç´ããã
ãã®é
åã32åããã¾ãã
32 [å] à 16 [byte] à 16 [å] = 8192 [byte]ã®å®¹éãå¿
è¦ã«ãªãã¾ãã
ããã«ã決ã¾ã£ãå¾ã§ã
16åã®é
åãäºåã«è¨ç®ããªããã°ãªãã¾ããã
ãã®ã³ã¹ãã¯ã32 [å] à 16 [å] = 512 [å]åã§ãã
4.3. æ·»å1bitã®é å
ãã¡ãã¯PDFã«ã¯ãªããã®ã§ããã
ã¨ã¦ãç°¡åã«å®è£
ã§ãã¾ããã
é
åãç¨ããªãå ´åãããå¹çãè¯ããªãã¾ãã
ã¹ãã¼ã¹ã¯2048 [byte]å¿
è¦ã«ãªãã¾ãã
åæå¤ã®è¨ç®ã¯128ååã
ä¹ç®ã®æ¼ç®ã¯ããã128åã§ãã
ä¹ç®ãæ±ããã³ã¼ãã¯é
åãç¨ããªãã¨ãã¨ä¼¼ã¦ãã¾ããã
ããã«åç´ã«ãªã£ã¦ãã¾ãã
Z â 0 for i = 0 to 127 do if Xi = 1 then Z â Z â M[i] end if end for return Z
å人çã«ã¯æ軽ã§ããã®ã§ã¯ãªããã¨æãã¾ãã
åããã¦è¦ã¦ããã¾ãã
5. é åã®ä½ãæ¹
é
åã¨ããã«ç¶ããªã®ã§ããã
ä½ææ¹æ³ãããã¾ã詳ããä¹ã£ã¦ãªãã®ã§è¦å´ãã¾ããã
ä»åã®æ稿ã®ä¸»ãªç®çã¯ããã®é
åãä½æããæ¹æ³ã示ããã¨ã§ãã
5.1. æ·»å8bité åã®ä½ãæ¹
æ·»å8bitã®é
åãä½æãã¾ãããã
å¤ããã¨ã«ããä½æãã¾ãã
åé
åã®è¦ç´ æ°ã¯ãæ·»å8bitã¨ãããã¨ãªã®ã§256åã§ãã
ã¾ãã¯æ·»åã®bitããã£ã1ã¤ã®å ´åã®å¤ãç®åºãã¾ãã
å¤ã¯128bitãªã®ã§128éãããã¾ããã
æåã®é
ååã®8åãè¦ã¦ããã¾ãããã
å
·ä½çã«æ¸ãã¨æ¬¡ã®8éãã
10000000 æ·»å128ã®ã¨ã 01000000 æ·»å64ã®ã¨ã 00100000 æ·»å32ã®ã¨ã 00010000 æ·»å16ã®ã¨ã 00001000 æ·»å8ã®ã¨ã 00000100 æ·»å4ã®ã¨ã 00000010 æ·»å2ã®ã¨ã 00000001 æ·»å1ã®ã¨ã
ãããã®é åã®å¤ã¯ãã®å¤ãã·ãããã¦ããã ãã§ãã
- æ·»å
10000000
ã¯ã - æ·»å
01000000
ã¯ã - æ·»å
00100000
ã¯ã - æ·»å
00010000
ã¯ã ...
- æ·»å
00000001
ã¯ã
ãªã®ã§ã ç´åã«ç®åºããå¤ã使ããã¨ãã§ãã¾ãã
ã§ã¯ã次ã«bitãããããããå ´åãèãã¾ãã
ä¾ã¨ãã¦24
ãèãã¾ãããã
24
ã®2é²æ°è¡¨è¨ã¯æ¬¡ã®ããã«ãªãã¾ãã
00011000 24
ã¤ã¾ãã¯ã8
ã¨16
ã®å ç®ã§ãã
00010000 16ã®ã¨ã 00001000 8ã®ã¨ã ---------------- 00011000 24
å¤ã®æ±ãæ¹ã¯ç°¡åã§ããã8ã¨16ã®ããããã®é
åã®å¤ã足ãã ãã§ãã
ã¤ã¾ãã
å³è¾ºã®2ã¤ã®å¤ã¯ãã§ã«ç®åºãã¦ãã¾ãã®ã§ã
é
åã«æ ¼ç´ããã¦ãã¾ãã
ãã§ã«ç®åºããå¤ãç¨ãã¦ã
å¿
è¦ãªå¤ãå
¨é¨æ±ãããã¨ãã§ãã¾ãã
ã§ã¯ãä»ã®é
åãåããã¦è¦ã¦ããã¾ãã
128bitã8bitã«åå²ããã®ã§ãé
åã¯å
¨é¨ã§16åã
é
åãã¨ãã¾ãã
ã¾ãã¯ããã¹ã¦ã®é
åã«ããã¦ãbitã1ã¤ã®å ´åãå
è¡ãã¦æ±ãã¦ãã¾ãã¾ãã
ã®å 容ã¯ä¸è¨ã®éãã
-
10000000
ã¯ã01000000
ã¯ã00100000
ã¯ã00010000
ã¯ã...
00000001
ã¯ã
ç¶ãã¦ã®é
åãä½æãã¾ãã
å
容ã¯ã®ç¶ãã§ãã
-
10000000
ã¯ã01000000
ã¯ã00100000
ã¯ã00010000
ã¯ã...
00000001
ã¯ã
æåã«ã®å¤ãåç
§ãã¦ããç¹ã«æ³¨æãã¦ãã ããã
å®è£
ã§ã¯ç´åã«ç®åºããå¤ãã§ã·ãããã¦æ±ãã¦ãã ããã
æå¾ã¯ãããªæãã
-
10000000
ã¯ã01000000
ã¯ã00100000
ã¯ã00010000
ã¯ã...
00000001
ã¯ã
ããã§ãbitã1ã¤ã ãç«ã£ã¦ãã128åã®è¦ç´ ã¯ç®åºã§ãã¾ããã
ãã¡ããç®åºããã³ã¼ãã§ããã
PDFã«Algorithm 3ã¨ãã¦è¨è¼ãããã¾ãã
ããããããããããããªãã£ãã
PDFãåèã«ä¼¼ããããªã³ã¼ããä½ã£ã¦ã¿ã¾ãã
M0[128] = H for i = 1 to 128 - 1 do j â trunc(i / 8) k â i % 8 H â H â P Mj[1 << (8 - k - 1)] â H end for
ããã¤ãã®å½ä»¤ãåæã«èªä½ãã¦ãã¾ãã¾ããã
trunc
ã¯ãå°æ°ç¹ãåãæ¨ã¦ãå½ä»¤ã§ãã
i % 8
ã¯ãi
ã8
ã§å²ã£ããã¾ãã§ãã
1 << n
ã¨ããé¨åã¯1
ãn
bitåå·¦ã«ã·ããããã¨ããæå³ã§ãã
ä»ã®è¦ç´ ã¯bitã®çµã¿åããã§å
¨é¨åºãã¾ãã
ã¾ãã¯æåã®ãå®æããã¦ã¿ã¾ãããã
i â 2 while i < 256 do for j = 1 to i â 1 do M0[i + j] = M0[i] â M0[j] end for i â 2 * i end while
åãããã«ãã»ãã®é
åãä½æã§ãã¾ãã
ãããä¸è¨ã®ã³ã¼ããé
åãã¨ã«åããå¿
è¦ã¯ãªãã®ã§ã
ã¾ã¨ãã¦ãã£ã¦ãã¾ãã¾ãããã
次ã®ããã«æ¸ãæãã¾ãã
i â 2 while i < 256 do for j = 1 to i â 1 do for k = 0 to 16 - 1 do Mk[i + j] â Mk[i] â Mk[j] end for end for i â 2 * i end while
以ä¸ã«ãããæ·»å0以å¤ã¯ãã¹ã¦å®äºã§ãã
æå¾ã®å¤ã¯ä¸è¨ã®éãã
00000000
ã¯ã
ã³ã¼ãã¯æ¬¡ã®éãã
for k = 0 to 16 - 1 do Mk[0] â 0 end for
å®æã§ãï¼
5.2. æ·»å4bité åã®ä½ãæ¹
æ·»å4bitã®é
åãä½æãã¾ãããã
128bitã4bitã«åå²ããã®ã§ãé
åã¯å
¨é¨ã§32åã
é
åãã¨ãã¾ãã
ã¾ãã¯ããã¹ã¦ã®é
åã«ããã¦ãbitã1ã¤ã®å ´åãå
è¡ãã¦æ±ãã¦ãã¾ãã¾ãã
-
1000
ã¯ã0100
ã¯ã0010
ã¯ã0001
ã¯ã
-
1000
ã¯ã0100
ã¯ã0010
ã¯ã0001
ã¯ã
以éçç¥
æ±ãæ¹ã¯8bitã®æã¨ã»ã¼åãã§ãã
å®è£
ã示ãã¾ãã
M0[8] = H for i = 1 to 128 - 1 do j â trunc(i / 4) k â i % 4 H â H â P Mj[1 << (4 - k - 1)] â H end for
ä»ã®é¨åãæ±ãã¾ãã
i â 2 while i < 16 do for j = 1 to i â 1 do for k = 0 to 32 - 1 do Mk[i + j] â Mk[i] â Mk[j] end for end for i â 2 * i end while
æå¾ã¯0ã§ãã
for k = 0 to 32 - 1 do Mk[0] â 0 end for
5.3. æ·»å1bité åã®ä½ãæ¹
ãã¡ãã¯8bit, 4bitã¨åãå
容ã§ã¯ãããã®ã®ã
ç´ ç´ã«1bitã®é
åãä½ãããã§ã¯ããã¾ããã
ãããªãã¨ãã¦ããã©ãèãã¦ãæ±ãã¥ããã§ãããã
代ããã«128bitåã®ã128åã®é
åãä½ãã¾ãã
åè¦ç´ ã«ã¯ããã·ããããå¤ãå
¥ãã¾ãã
ã¤ã¾ãã¯æ¬¡ã®ããã«ãªãã¾ãã
-
0
ã¯ã1
ã¯ã2
ã¯ã3
ã¯ã...
127
ã¯ã
ããã¤ã注æç¹ãããã¾ãã
ã¾ããä»ã¨ã¯éããé
åã¯ã²ã¨ã¤ã ãã§ãã
æ·»å0ã®å¤ã¯ã0ã§ã¯ãªãã§ãã
ã³ã¼ãã¯ç°¡åã§ãã
for i = 0 to 128 - 1 do M[i] â H H â H â P end for
6. ä¹ç®ã®ç®åº
ã§ã¯å®éã«ç®åºãã¦ããã¾ãããã
6.1. æ·»å8bité åã®ä¹ç®ã®ç®åº
ç®åºãã¦ããããããã§ããã
ã¾ãå
¥åã8bitã«åå²ããå¿
è¦ãããã¾ãã
ããã¯ããã°ã©ãã³ã°è¨èªã«ãã£ã¦
å®è£
æ¹æ³ãç°ãªãã®ã§ã¯ãªãã§ããããã
çä¼¼ã³ã¼ãã§ç¡çããä½ã£ã¦ã¿ã¾ãã
for i = 0 to 16 - 1 do r[i] â (X >> (i * 8)) % 0x0100 end for
X >> n
ã¨ããé¨åã¯X
ãn
bitåå³ã«ã·ããããã¨ããæå³ã§ãã
å
¥åã®ä»£ããã«ãæ±ããé
år
ã使ããä¹ç®ãè¨ç®ãã¾ãã
Z â 0 for i = 0 to 16 - 1 do k â r[16 - i - 1] Z â Z â Mi[k] end for return Z
以ä¸ã§ããæ±ã¾ãã¾ããã
6.2. æ·»å4bité åã®ä¹ç®ã®ç®åº
ãã£ããã³ã¼ãã示ãã¾ãã
for i = 0 to 32 - 1 do r[i] â (X >> (i * 4)) % 0x10 end for Z â 0 for i = 0 to 32 - 1 do k â r[32 - i - 1] Z â Z â Mi[k] end for return Z
6.3. æ·»å1bité åã®ä¹ç®ã®ç®åº
ãã¡ãã¯ãã§ã«ä½ææ¸ã¿ã§ãã
åããã®ãä¸è¨ã«ç¤ºãã¾ãã
Z â 0 for i = 0 to 127 do if Xi = 1 then Z â Z â M[i] end if end for return Z
7. ä¹ç®ã®é度
次ã®è¡¨ã¯ãAES-GCMã®encrypt/decryptã®å®è¡çµæã§ãã
Common Lispã§ãã¹ããè¡ãã¾ããã
GHASHã ãã®è¨æ¸¬ã§ã¯ãªãã®ã§æ³¨æãã¦ãã ããã
Type | Size [byte] | Time [sec] | process [cycle] | cons [byte] |
---|---|---|---|---|
table0 | 0 | 33.7 | 111 G | 14.7 M |
table1 | 2048 | 27.2 | 89.7 G | 6.68 M |
table4 | 8192 | 25.0 | 82.6 G | 5.38 M |
table8 | 65536 | 24.3 | 80.5 G | 4.67 M |
Typeã«å¯¾ãã¦ãä¸è¨ã®ã³ã¹ããåºãã¾ããã
- Sizeã¯é åã®ãµã¤ãºãbyteã§è¡¨ãããã®ã§ãã
- Timeã¯ããããã¹ãããã°ã©ã ã®å®è¡æéï¼ç§ï¼ã§ãã
- processã¯CPUã®å½ä»¤åæ°ã§ãã
- consã¯Lispç¹æã®å¤ã§ãããå®è¡æã«çæãããconsã®ç·æ°ã§ãã
ä¸è¨ã®è¡¨ã§ããããã¨ã¯ãé
åã使ã£ãã³ã¼ãã¯
ã¡ããã¨é度ã«ããã¦æ¹åããã¦ããã¨ãããã¨ã§ãã
ããã¯AES-GCMã®encrypt/decryptãã¹ã¦ã®å®è¡çµæã§ããã
GHASHã ãã®ãã®ãgcm-spec.pdfã«ããã¾ãã®ã§è¦ã¦ã¿ã¾ãããã
- The Galois/Counter Mode of Operation (GCM)
https://csrc.nist.rip/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
Table 1
Method | Storage requirement | Throughput (cycles per byte) |
---|---|---|
No tables | 16 bytes/key | 119 |
Simple, 4-bit tables | 8,192 bytes/key | 17.3 |
Simple, 8-bit tables | 65,535 bytes/key | 13.1 |
ãã®è¡¨ãè¦ã¾ãã¨ãããããtable0ã¨table4ã®ã³ã¹ãã119ã¨17.3ã«ãªã£ã¦ããã
å·®ãã¨ã¦ã大ãããã¨ããããã¾ãã
é
åã使ã£ãæ¹ã大å¹
ã«æ©ãã¨ãããã¨ã«ãªãã¾ãã
ãã®æã®æé©åã¯ã¡ã¢ãªã¨é度ã®ãã¬ã¼ããªãã«ãªãã¨æãã¾ããã
ããã«ãã¦ãä½ããã®è¡¨ã使ã£ãæ¹ãããã¿ããã§ãã
ãã¹ãã«ä½¿ã£ãã³ã¼ãã¯ä¸è¨ã®éãã
(defpackage work (:use common-lisp aes)) (in-package work) (defconstant +size+ (* 10 1024 1024)) (defun make-vector8 (size) (make-array size :element-type '(unsigned-byte 8))) (defun integer-big-vector (v size) (let ((a (make-array size :element-type '(unsigned-byte 8)))) (dotimes (i size) (let ((k (- size i 1))) (setf (aref a i) (ldb (byte 8 (* k 8)) v)))) a)) (defun main () (let* ((x (make-vector8 +size+)) (g (make-aes-gcm-128)) (key (aes-gcm-key g)) (nonce (aes-gcm-nonce g)) a b) (dotimes (i 32) (setf (aref key i) (random #x0100))) (dotimes (i 12) (setf (aref nonce i) (random #x0100))) (dotimes (i +size+) (setf (aref x i) (random #x0100))) (multiple-value-bind (y a) (aes-gcm-encrypt g x) (multiple-value-bind (z b) (aes-gcm-decrypt g y) (unless (equalp a b) (format t "tab error, ~S /= ~S.~%" a b)) (unless (equalp x z) (format t "decode error.~%")))))) (let ((aes::*aes-gcm-mode* 'aes::table8)) (time (main)))
Cè¨èªã§æ¥åæ²ç·DSAãå®è£ ãã
ã¿ã¤ãã«ã®éãCè¨èªã§æ¥åæ²ç·DSAãä½ãã¾ããã
https://github.com/nptcl/fixed
ä½æããæ²ç·ã¯æ¬¡ã®éãã
- secp256k1
- secp256r1
- ed25519
- ed448
Cã®ãµã³ãã«ã³ã¼ãã¿ãããªæ±ãã ã¨æã£ã¦ãã ããã
以åãCommon Lispã§ä½æãããããã®ã¾ã¾Cè¨èªã«ãã¦ãã¾ãã
ã·ãªã¼ãºãã®ã§ãã®ã§ãããããã£ããã©ããã
Common Lispで楕円曲線DSAを実装する1 - nptclのブログ
Common Lispで楕円曲線DSAを実装する2(加算) - nptclのブログ
Common Lispで楕円曲線DSAを実装する3(乗算など) - nptclのブログ
Common Lispで楕円曲線DSAを実装する4(確認) - nptclのブログ
Common Lispで楕円曲線DSAを実装する5(鍵生成) - nptclのブログ
Common Lispで楕円曲線DSAを実装する6(署名) - nptclのブログ
ã³ã³ãã¤ã«ã¯ãã¶ãç°¡åã«ã§ãã¾ãã
$ make
ãããã¯ã
$ cc *.c
ã§è¡ããã¨æãã¾ãã
ä¹±æ°ã®åæåã«/dev/random
ãç¨ããã®ã§ã
å®å
¨ã«å®è¡ããããªãFreeBSDãLinuxä¸ã§å®è¡ãã¦ãã ããã
ä¸å¿ã³ã³ãã¤ã«ãªãã·ã§ã³ãããã¾ãã
$ cc *.c -DFIXED_FREEBSD $ cc *.c -DFIXED_LINUX
å®è¡ããã¨æ¬¡ã®ãããªåºåãåºã¾ãã
$ ./a.out *** RSA .... make_prime: 3 .. make_prime: 1 e: 10001 d: 9FFD3DDCA87DB013AD4163DBD671EF0FC12C46DD31D498315352511390CF8141 n: EEA9EA825D6BDE300EC2A44B8E2C4863AFF28B4ED97BEB34CBAEA1282F56ACE5 p: FA6A10ED0E527D2D5FEA69D823E22055 q: F3FCC03A690A91D03ADCB175C80AAA51 x1: 10 x2: 20 x3: 30 x4: 40 x5: 50 x1.encode: 7C09DE52BE6CDB831CE4718F524E46D338BDBD43D975794AA45860A8D0033436 x2.encode: 2A70611168D18974D12BDB03170BFFD0C9BB42D4016691C4EBAC8CCC122B2ECE x3.encode: B76B913220960CD7F46940EFB5EFC354B43931CD102C8E11AEDB6E520D4A4275 x4.encode: C0BD68E1C79F60451B5B7A40245B8E793861B682372B365AA776AD6A12E835F x5.encode: 5F78B535002AE5741BC650F52E22753B8A804F0DE1C590B1A4DECDCDF0A0FC18 x1.decode: 10 x2.decode: 20 x3.decode: 30 x4.decode: 40 x5.decode: 50 *** ECDSA, EdDSA [private] secp256k1: "AE988C663941C1BF51CF6FB9389B4ED787D65F33C5B2113F9B841C7F476FE501" secp256r1: "C7A31EFEC292540504DD7BB2EC17AD5EA00D922AC628B9FD49F5C14C705F84B5" ed25519 : "8ED9CDAB174D36EF6ABCC187F3EA4A92F390FB3543B180F2880E2659027BE2FF" ed448 : "0C69F9CFFD0DED14D73A27AE7B31EC440F0E19FE2AD2D2E1B1D959493BB92ED79C08514EEA26752E182012538C21FDA59C45F10A093F222055" [public] secp256k1: "0395BD4A04A9FF7AE239FA42B7BA66B2E38E169DCA699AA1E25F6BAD2E5CDBBD33" secp256r1: "0380FF1091B9436ED1D170B6E0B5F55C52A0525C61C2A487237FEB4AA7149C92A8" ed25519 : "709F64FD1AC8DD64E5ADDEED6B6172B31D8BF0B589F303ED831FDF853D2C49CB" ed448 : "27A40DA2BCAE9DCB16151162A8A652137C5C60B56A7BDFD63568AA68831335509D11EAAA131A9C324E95868F870CA0FCE685896BC49E97F000" [sign] "Hello" secp256k1.r: "57861B1A29BEB58F52596ABB825B8D3A8BAA638D70A8A5FAC0AA7ABEC16E443E" secp256k1.s: "61711E5C9354B0113CB1290FB70D24112133038C2BDCE3DC911BAC03C1FB7186" secp256r1.r: "D647CC95D5658F2AF8408CCE2A1869B4F7F0BD37FE7018DE67C8A9D6BCB68059" secp256r1.s: "896B2F320EEB0ED2A0E079C215929E5B4E22D07FBD769338046F48C7CCFB50A2" ed25519.r : "77AEAFEB4D363B39D71B19A1C9585E78B6DCE1AC73F2EF01BA77E8AF67878B67" ed25519.s : "8DF76ADE07066909794E4F603CF8B3A1A12FC9437C868BD970713C7EC2E1B20D" ed448.r : "B694F15A8E7F5BFFE134876BEB8E4DE9F47CEB1F299174C1912FE940C6AF8A3908401AB65248E89F0A4C722468C4E508B6141E83C3DF5E5E80" ed448.s : "ED6956E23BCB50F5A611343F548EF5065758EF4B1E3F6AF42E6C15130ABF4B783F6459CFCA69004AFFFDAC8348B4D50338BFB0624E6C101100" [verify] secp256k1: T secp256r1: T ed25519 : T ed448 : T
ååã®*** RSA
ã¨ãå¾åã®*** ECDSA, EdDSA
ã§å¥ç©ã§ãã
ååã®RSAã¯ä»¥åè¨äºã§æ¸ãã¾ãããã
巨大ãªç´ æ°ãä½ã£ã¦æå·ã»å¾©å·ãã¦ãã¾ãã
å¾åã®ECDSA, EdDSAã¯æ¥åæ²ç·ã®
ç§å¯éµãå
¬ééµããã¨"Hello"
ã®ç½²åã¨æ¤è¨¼ãè¡ã£ã¦ãã¾ãã
ããéµãçæããããªãé©å½ã«ã©ããã
ä½ã£ãå
容ã¯Common Lispçã¨å
¨ãåãã§ãã
Cè¨èªçã§ã¯ãéµçæããç½²åãæ¤è¨¼ã¾ã§ã
å
¨é¨æååã ãã§ãªãã¨ãªããã®ãé¢ç½ãã£ãã§ãã
ä¾ãã°ç§å¯éµã¨å
¬ééµã®çæã¯æ¬¡ã®ããã«ãªãã¾ãã
#include "elliptic.h" #include "random.h" #include "signature.h" #include <stdio.h> int main(void) { char private_key[200]; char public_key[200]; init_fixrandom(); init_elliptic(); private_string_secp256k1(private_key); public_string_secp256k1(private_key, public_key); printf("%s\n", private_key); printf("%s\n", public_key); return 0; }
å®è¡çµæã¯ä¸è¨ã®éãï¼ä¸ä¾ï¼
E6BD585DCFB42001FD3DDBB0D53D7E1797210DF692567388A0D86D85571E7491 0322703DCC45AD4FBF96D04F662B82B1810B0A1C3E7D233BF570F3CFEC61B840B0
ä»åã¯é
å¸ç©ã¯ãªãã®å¤é¨ã¢ã¸ã¥ã¼ã«ãå¿
è¦ã¨ããã
ããåä½ã ãã§åä½ããããã«ãªã£ã¦ãã¾ãã
è¨ãæãããªãã°å¿
è¦ãªãã®ã¯å
¨é¨èªåã§ä½ã£ã¦ãã¾ãã
ãªã®ã§ãå¯ç£ç©ã¨ãã¦ããããã§ããã®ã§ç´¹ä»ãã¦ããã¾ãã
fixed.c
,fixed.h
- bignumã®åºå®é·çãä¾ãã°1024bitåºå®ã®æ´æ°æ¼ç®ãã§ãã
- ã¹ã¿ã³ãã¢ãã¼ã³ã§åä½å¯è½
- æ¬å½ã¯ããã主役
random.c
,random.h
- xorshiftã®ä¹±æ°çæå¨
- ã¹ã¿ã³ãã¢ãã¼ã³ã§åä½å¯è½
sha.c
,sha.h
- SHA-256, SHA-512, SHAKE-256ã®ããã«ä½æ
- ã¹ã¿ã³ãã¢ãã¼ã³ã§åä½å¯è½
- ä½æ ãMD5, SHA-1, SHA-2(ä¸é¨), SHA-3ã使ç¨å¯è½
sha.lisp
- SHA-256, SHA-512, SHAKE-256ã®ããã«ä½æ
- ã¹ã¿ã³ãã¢ãã¼ã³ã§åä½å¯è½
elliptic.lisp
ã§ä½¿ã
elliptic.lisp
- ECDSA, EdDSAã®Common Lispç
sha.lisp
ã ãã«ä¾åãã¦ãã
ãã¨ãä»åãã£ããè¦å´ãã¦ä½ã£ãã®ã§ã
æ¥åæ²ç·DSAã®ãã¤å
¬ééµãå
¬éãã¦ããã¾ãã
ããã«ä½ã®æå³ãããã®ãã¯ãããã¾ãããã
- secp256k1
03FEEF09658067CFBE3BE8685DDCE8E9C03B4A397ADC4A0255CE0B29FC63BCDC9C
- secp256r1
03CD92CF7B1C9CE9858383806B8540D72FB022BE577E21DE02B8EAA27371DB7AF2
- ed25519
75AB16F53A060E7AF9A4B8ECEA3D4DEF058AED2C626FEC96D5505C4A7D922960
- ed448
99AFC3768EE41B96F208EBAF8627908690DC6A5AC64659F93D0A46C2092B61E84AD14DD03F7B3F146799C29F65682126D517B7E1EA57716E00
ããã§ãã£ã¦ãã®ããªã
å
¨ç¶éã£ã¦ããæ²ããã
Common Lispã§æ¥åæ²ç·DSAãå®è£ ãã6ï¼ç½²åï¼
ååã®ç¶ãããã§ãã
Common Lispで楕円曲線DSAを実装する5(鍵生成) - nptclのブログ
9. ç½²åã¨æ¤è¨¼
DSAã®æ©è½ã§ãããç½²åã¨æ¤è¨¼ãè¡ãã¾ãã
9.1. ç½²å
ç½²åã®æ¹æ³ã¯ãECDSAã¨EdDSAã§éãã¾ãã
ECDSAã¯ãç½²åã®è¨ç®ã§ã¡ãã»ã¼ã¸ãä¸åº¦ãã使ããªãã®ã«å¯¾ãã¦ã
EdDSAã¯äºåº¦ä½¿ãã¾ãã
ã¤ã¾ãECDSAã¯ã¡ãã»ã¼ã¸ãã¹ããªã¼ãã³ã°ã§ããã®ã«ãEdDSAã¯ãããã§ãã¾ããã
EdDSAã¯ã¡ãã»ã¼ã¸ããããã¡ãªã³ã°ããå¿
è¦ãããã¨æãã¾ãã
ã¡ããã¨çç±ããã£ã¦ãECDSAã§ã¯ç½²åã«éã«ä¹±æ°ã使ã£ã¦ããããã§ãã
ã¨ããããç½²åã®ãã³ã«ä¹±æ°ã使ãã®ã¯å±éºãªã®ã§ããã¾ãããï¼ãã¨ããæµãããã£ããããã§ãã
ãã¾ãã®å®è£
ãããæ°ã¯ããã¾ããããRFC6979ã«æ¸ãã¦ãã¾ãã
Deterministic Usage of the Digital Signature Algorithm (DSA) and
Elliptic Curve Digital Signature Algorithm (ECDSA)
https://datatracker.ietf.org/doc/html/rfc6979
EdDSAã ã¨æ¹è¯ããã¦ãã¾ãã
ã¡ãã»ã¼ã¸ã®ããã·ã¥ãä½è¨ã«ã¨ããã¨ã§ãä¹±æ°ã®ä»£ç¨ãããããã§ãã
ã¨ãããã¨ã§ãEdDSAã¯ã¡ãã»ã¼ã¸ãäºåº¦ä½¿ãã¾ãã
ãã¶ããããã¡ãªã³ã°ãå¿
è¦ã«ãªãã¾ããã
å®å
¨ã®ããã®ç ç²ã§ãã
ã§ãã¾ãæ¤è¨¼ãªãã¨ããããç½²åã«ã¹ããªã¼ãã³ã°ã¯å¿
è¦ãªãããªã
ãã¨å®æ°ã«ã¤ãã¦æ³¨æãã¦ã»ãããã¨ãããã¾ãã
ç½²åã¨æ¤è¨¼ã§ã¯ãä½æ°ã®ãããããåºã¦ãã¾ãã
ä»ã¾ã§ã¯æéä½ã®è¨ç®ã¨ãããã¨ã§ã
ç´ æ°ã®ã°ããã ã£ãã®ã§ããã
ä»åº¦ã¯ãé »ç¹ã«åºã¦ããã®ã§æ³¨æãã¦ãã ããã
9.1.1. secp256k1, secp256r1
ç½²åã«å¿ è¦ãªã®ã¯ä¸è¨ã®æ å ±ã§ãã
- ç§å¯éµ
- ã¡ãã»ã¼ã¸
ã¾ãã¯èªåã®ç§å¯éµã¨ã¯å¥ã«ãç§å¯éµã¨å
¬ééµã®ãã¢ãçæãã¾ãã
çæããæ¹æ³ã¯èª¬ææ¸ã¿ã§ããã
ã¯æ°ããçæããä¹±æ°ã§ãããã§ãã
ã®ãã¨ããã³ã¹ï¼nonceï¼ã¨ãããããã§ãã
ãããªãããã
ã®X座æ¨ãã¨ããã¨ãã次ã®å¤ãæ±ãã¾ãã
ããã§ã¯åº§æ¨ã«å¯¾ããä½æ°ã§ãããæ²ç·ã®ãã©ã¡ã¼ã¿ã¼ã«è¼ã£ã¦ãã¾ãã
ããã®ã¨ãã¯ã¨ã©ã¼ãªã®ã§ãã®ä¹±æ°çæããããç´ãã¦ãã ããã
ã¡ãã»ã¼ã¸ã®ããã·ã¥å¤ãåå¾ãã¾ãã
ããã§ä½¿ç¨ããé¢æ°ã¯SHA-256ã§ãã
å¾ãããããã·ã¥å¤ãBig Endianã®æ´æ°ã¨ãã¾ãã
ä¸è¨ã®å¼ããå¤ãç®åºãã¾ãã
ãã®å¼ã¯ãç´ æ°ã§ã¯ãªãä½æ°ã®ãã¾ããªã®ã§æ°ãä»ãã¦ä¸ããã
ã¨ãã«ã®æ±ãæ¹ã¯ã
ããããä½æ°èªä½ãç´ æ°ãªã®ã§ã
å¼èªä½ã¯ãã¤ãã®ãã¤ã§åé¡ããã¾ããã
ãç´ æ°ã ã£ããã§ããã
ç¥ããªãã£ãã
åå¤æ°ã®å 容ãã¾ã¨ãã¾ãã
- ã¯ããã¨ãã¨ã®ç§å¯éµ
- ã¯ãä»åä½æããç§å¯éµ
- ã¯ãä»åä½æããå ¬ééµã®X座æ¨ï¼ãã ãï¼
- ã¯ãã¡ãã»ã¼ã¸ã®SHA-256
- ã¯ãã«å¯¾ããä½æ°ã§ãããæ²ç·ãã©ã¡ã¼ã¿ã«ä¹ã£ã¦ãã¾ã
ããè¨ç®çµæãã®ã¨ãã¯è¯ããªãã®ã§ã
ç§å¯éµã®ä½æããããç´ãã¦ãã ããã
以ä¸ã«ãããç½²åãæ±ãããã¨ãã§ãã¾ããã
話é¡ã ãåºãã¦ããã¾ããã
secgã®PDFã«ã¯ã
ã¯ãã¤ãã¹ã®å¤ã§ãæ¤è¨¼ã«åæ ¼ãã¦ãã¾ã
ã¿ãããªè©±é¡ãããã¾ããã
ãã¤ãã¹ã¨ã¯ã®ã§ãã
ãã®ã«ãã£ã¦ã¯ããã©ã¹ãã¤ãã¹ã®ä¸¡æ¹ãç®åºãã¦ã
å°ããã»ããã«ãããã¨ãããããã§ãã
ä»ã¯ä½ããã¾ããã
å®è£
ã«ã¤ãã¦èª¬æãã¾ãã
ç½²åã¨æ¤è¨¼ã§ä½¿ãããã·ã¥é¢æ°ã¯åããã®ã使ããã¨ãã§ãã¾ãã
ãã¼ã¿ã«å¯¾ãã¦Big Endianã®æ´æ°ãåºåãããã®ãå®ç¾©ãã¾ãã
(defun sign-sha-secp256k1 (message) (let ((sha (make-sha256encode))) (read-sha256encode sha message) (vector-big-integer (calc-sha256encode sha))))
ç½²åã®å®è£ ã¯ãé¸ã³ã«å¤±æããã å度å®è¡ããã¨ããå¦çãå¿ è¦ã«ãªãã¾ãã
(defun sign-loop-secp256k1 (private message) (let* ((k (make-private)) (b (affine (make-public k))) (r (modn (point2-x b)))) (unless (zerop r) (let* ((e (sign-sha-secp256k1 message)) (s (modn (* (inverse-n k) (+ e (* r private)))))) (unless (zerop s) (values r s)))))) (defun sign-secp256k1 (private message) (multiple-value-bind (r s) (sign-loop-secp256k1 private message) (if r (values r s) (sign-secp256k1 private message))))
9.1.2. ed25519
ç½²åã«å¿ è¦ãªã®ã¯ä¸è¨ã®æ å ±ã§ãã
- ç§å¯éµ
- ã¡ãã»ã¼ã¸
ã¾ãã¯ç§å¯éµãã次ã®å¤ãçæãã¾ãã
- ããã·ã¥å¤ã®ä¸ä½32byteã®æ´æ°ãã¨ãããï¼å ¬ééµçæã«ä½¿ç¨ï¼
- ããã·ã¥å¤ã®ä¸ä½32byteã®æ´æ°ãã¨ãããï¼ç½²åã«ä½¿ç¨ï¼
å
¬ééµã§èª¬æããéããmake-public-sign-ed25519
é¢æ°ã§çæã§ãã¾ãã
åå¾ãããç¨ãã¦ãå
¬ééµãè¨ç®ãã¾ãã
次ã®æé ã§æ´æ°ãä½æãã¾ãã
- SHA-512()ãè¨ç®ããã
- hashå¤64byteãLittle Endianã®æ´æ°ã¨ããã
- ã¨ãªããè¨ç®ããã
è¨å·ã¯é£çµã表ãã¾ãã
ã¯ä½æ°ã§ãããæ²ç·ã®ãã©ã¡ã¼ã¿ã¼ã«è¼ã£ã¦ãã¾ãã
ãã¤ãã¯ç´ æ°ã®å°ä½ãè¨ç®ãã¾ããã
ä»ã¯ã§ã¯ãªããªã®ã§æ³¨æãã¦ãã ããã
ç½²åãã¼ã¿ã§ãããæ±ãã¾ãã
ã¯èµ·ç¹ã§ãããæ²ç·ãã©ã¡ã¼ã¿ã¼ã«è¼ã£ã¦ãã¾ãã
次ã®æé ã§æ´æ°ãä½æãã¾ãã
- SHA-512()ãè¨ç®ããã
- hashå¤64byteãLittle Endianã®æ´æ°ã¨ããã
è¨å·ã¯é£çµã表ãã¾ãã
ããã§åã³ãåºã¦ãã¾ãã
æåã®ã¨ä»åã®ãããããç¬ç«ãã¦ããã°ããã®ã§ããã
ããããããã§ã¯ãªãã®ã§ã®ãããã¡ãªã³ã°ãªã©ãå¿
è¦ã«ãªãã¾ãã
æå¾ã«ãæ±ãã¾ãã
ã¨ã®é£çµãç½²åã®ãã¼ã¿ã§ãã
å®è£
ã«ã¤ãã¦èª¬æãã¾ãã
ã¾ãã¯ããã·ã¥é¢æ°ã§ããã次ã®2éãã®ãã¿ã¼ã³ãããã¾ãã
- SHA-512()
- SHA-512()
ããã§ã3ã¤ã®å¼æ°ãåãããã·ã¥é¢æ°ãä½æãã¾ããã
(defun sign-sha-ed25519 (x y message) (let ((sha (make-sha512encode))) (when x (little-endian-sha512encode sha x 32)) (when y (little-endian-sha512encode sha y 32)) (read-sha512encode sha message) (modn (vector-little-integer (calc-sha512encode sha)))))
ãã¡ãã¯æ¤è¨¼æã«ã使ãã¾ãã
ç½²åã®å®è£
ã¯ä¸è¨ã®éãã
(defun sign-ed25519 (private message) (multiple-value-bind (u v) (make-public-sign-ed25519 private) (let* ((a (encode (multiple u *elliptic-g*))) (w (sign-sha-ed25519 v nil message)) (r (encode (multiple w *elliptic-g*))) (k (sign-sha-ed25519 r a message)) (s (modn (+ w (* k u))))) (values r s))))
9.1.3. ed448
ed25519ã¨ã»ã¼åãã§ãããããã·ã¥é¢æ°ã ãç¬ç¹ã§ãã
ç½²åã¨æ¤è¨¼ã§ä½¿ãããã·ã¥é¢æ°SHAKE-256-ed448
ã次ã®ããã«å®ç¾©ãã¾ãã
SHAKE-256-ed448(x)
- sha3ã®shake-256ã§æ¬¡ã®ãã¼ã¿ãèªã
"SigEd448"
ãASCIIæåãåè¨8byte0x00
, åè¨1byte [phflag]0x00
, åè¨1byte [OLEN(context), context]- x, å¼æ°ã®ãã¼ã¿
- shake-256ã®ããã·ã¥å¤ã114byteè¿å´
- sha3ã®shake-256ã§æ¬¡ã®ãã¼ã¿ãèªã
ããã·ã¥é¢æ°ã®å®è£ ãå è¡ãã¦èª¬æãã¾ãã
(defun sign-sha-ed448 (x y message &optional (sha1 0) (sha2 #())) (let ((sha (make-shake-256-encode))) (read-sha3encode sha (map 'vector #'char-code "SigEd448")) (byte-sha3encode sha sha1) (byte-sha3encode sha (length sha2)) (read-sha3encode sha sha2) (when x (little-endian-sha3encode sha x 57)) (when y (little-endian-sha3encode sha y 57)) (read-sha3encode sha message) (modn (vector-little-integer (result-sha3encode sha 114)))))
ããã§ã¯ç½²åã®èª¬æãè¡ãã¾ãã
ç½²åã«å¿
è¦ãªã®ã¯ä¸è¨ã®æ
å ±ã§ãã
- ç§å¯éµ
- ã¡ãã»ã¼ã¸
ç§å¯éµãã次ã®å¤ãçæãã¾ãã
- ããã·ã¥å¤ã®ä¸ä½57byteã®æ´æ°ãã¨ãããï¼å ¬ééµçæã«ä½¿ç¨ï¼
- ããã·ã¥å¤ã®ä¸ä½57byteã®æ´æ°ãã¨ãããï¼ç½²åã«ä½¿ç¨ï¼
make-public-sign-ed448
é¢æ°ã§çæã§ãã¾ãã
åå¾ãããç¨ãã¦ãå
¬ééµãè¨ç®ãã¾ãã
次ã®æé ã§æ´æ°ãä½æãã¾ãã
- SHAKE-256-ed448()ãè¨ç®ããã
- hashå¤114byteãLittle Endianã®æ´æ°ã¨ããã
- ã¨ãªããè¨ç®ããã
ç½²åãã¼ã¿ã§ãããæ±ãã¾ãã
次ã®æé ã§æ´æ°ãä½æãã¾ãã
- SHAKE-256-ed448()ãè¨ç®ããã
- hashå¤114byteãLittle Endianã®æ´æ°ã¨ããã
æå¾ã«ãæ±ãã¾ãã
ã¨ã®é£çµãç½²åã®ãã¼ã¿ã§ãã
ç½²åã®å®è£ ã¯ä¸è¨ã®éãã
(defun sign-ed448 (private message) (multiple-value-bind (u v) (make-public-sign-ed448 private) (let* ((a (encode (multiple u *elliptic-g*))) (w (sign-sha-ed448 v nil message)) (r (encode (multiple w *elliptic-g*))) (k (sign-sha-ed448 r a message)) (s (modn (+ w (* k u))))) (values r s))))
9.2. æ¤è¨¼
æ¤è¨¼ãè¡ãã¾ãã
9.2.1. secp256k1, secp256r1
æ¤è¨¼ã«å¿ è¦ãªã®ã¯ä¸è¨ã®æ å ±ã§ãã
- å ¬ééµ
- ã¡ãã»ã¼ã¸
- ç½²å
å
¬ééµã¯åº§æ¨ã§ãããã¨ã¨ãã¾ãã
ããencodeãããå¤ã®ã¨ãã¯decodeãã¦ãã ããã
ã¾ãã¯ç½²åããã©ã¡ãã1以ä¸ä½æ°æªæºã§ãããã¨ã確èªãã¾ãã
ã¼ãã以ä¸ã®å ´åã¯ä½ããããããã®ã§ã失æãè¿å´ãã¦ä¸æãã¾ãã
ã¡ãã»ã¼ã¸ã®ããã·ã¥å¤ãåå¾ãã¾ãã
ããã§ä½¿ç¨ããé¢æ°ã¯SHA-256ã§ãã
å¾ãããããã·ã¥å¤ãBig Endianã®æ´æ°ã¨ãã¾ãã
ãããè¨ç®ããã¾ã§ã®æ¹æ³ã¯ç½²åã®æã¨åãã§ãã
次ã®ä¸è¨ã®å¼ãæ±ãã¾ãã
ããã§æ³¨æãã¦ã»ããã®ããã®éæ°ã§ãã
ç½²åã®ã¨ãã«ã話é¡ã«ãã¾ãããã
ä»ã¯ç´ æ°ã®ãã¾ãã§ã¯ãªããä½æ°ã®ãã¾ãã§ãã
ä½æ°èªä½ãç´ æ°ãªã®ã§ãã¯æ¬¡ã®å¼ã§æ±ãããã¨ãã§ãã¾ãã
座æ¨ãæ±ãã¾ãã
ãã®ã¨ãããªã失æãè¿å´ãã¦ä¸æãã¾ãã
ã®X座æ¨ãã¨ããã¨ãã
ãæ±ãã¾ãã
æ¤è¨¼ã®çµæã¯æ¬¡ã®éãã§ãã
- ãªãæ¤è¨¼æå
- ãªãæ¤è¨¼å¤±æ
å®è£ ã¯ä¸è¨ã®éãã
(defun verify-secp256k1 (public message r s) (and (<= 1 r (1- *elliptic-n*)) (<= 1 s (1- *elliptic-n*)) (let* ((e (sign-sha-secp256k1 message)) (s1 (inverse-n s)) (u1 (modn (* e s1))) (u2 (modn (* r s1))) (p (addition (multiple u1 *elliptic-g*) (multiple u2 public)))) (unless (zerop (point3-z p)) (let* ((a (affine p)) (v (modn (point2-x a)))) (= v r))))))
9.2.2. ed25519
æ¤è¨¼ã«å¿ è¦ãªãã¼ã¿ã¯ä¸è¨ã®éãã
- å ¬ééµ
- ã¡ãã»ã¼ã¸
- ç½²å
ã¾ãã¯ç½²åãã¼ã¿ã®ç¯å²ã確èªãã¾ã
- ãªãä¸æ£ãªå¤ãªã®ã§æ¤è¨¼å¤±æãä¸æ
å
¬ééµã¯åº§æ¨ã§ãããã¨ã¨ãã¾ãã
å
¬ééµã®encodeãã¨ãã¾ãã
次ã®æé ã§æ´æ°ãè¨ç®ãã¾ãã
- SHA-512()ãè¨ç®ããã
- hashå¤64byteãLittle Endianã®æ´æ°ã¨ããã
次ã«å¤ãdecodeãã¦ã座æ¨ãç®åºãã¾ãã
å¾ãããå¤ã¨ãã«ããã
次ã®å¼ããã£ã¦ãããã©ããã確ããã¾ãã
ã¯èµ·ç¹ã§ãããæ²ç·ã®ãã©ã¡ã¼ã¿ã¼ã«è¼ã£ã¦ãã¾ãã
å¼ããã£ã¦ãããæ¤è¨¼ã¯æåã§ãã
ãã£ã¦ããªãã£ããæ¤è¨¼ã¯å¤±æã§ãã
å®è£ ã¯ä¸è¨ã®éãã
(defun verify-ed25519 (public message r s) (when (< s *elliptic-n*) (let ((a (encode public)) (p (decode r))) (when p (let* ((k (sign-sha-ed25519 r a message)) (x (multiple s *elliptic-g*)) (y (addition p (multiple k public)))) (equal-point x y))))))
9.2.3. ed448
ed25519ã¨ã»ã¼åãã§ãã
æ¤è¨¼ã«å¿
è¦ãªãã¼ã¿ã¯ä¸è¨ã®éãã
- å ¬ééµ
- ã¡ãã»ã¼ã¸
- ç½²å
ç½²åãã¼ã¿ã®ç¯å²ã確èªãã¾ã
- ãªãä¸æ£ãªå¤ãªã®ã§æ¤è¨¼å¤±æãä¸æ
å
¬ééµã¯åº§æ¨ã§ãããã¨ã¨ãã¾ãã
å
¬ééµã®encodeãã¨ãã¾ãã
次ã®æé ã§æ´æ°ãè¨ç®ãã¾ãã
- SHAKE-256-ed448()ãè¨ç®ããã
- hashå¤114byteãLittle Endianã®æ´æ°ã¨ããã
次ã«å¤ãdecodeãã¦ã座æ¨ãç®åºãã¾ãã
å¾ãããå¤ã¨ãã«ããã
次ã®å¼ããã£ã¦ãããã©ããã確ããã¾ãã
ã¯èµ·ç¹ã§ãããæ²ç·ã®ãã©ã¡ã¼ã¿ã¼ã«è¼ã£ã¦ãã¾ãã
å¼ããã£ã¦ãããæ¤è¨¼ã¯æåã§ãã
ãã£ã¦ããªãã£ããæ¤è¨¼ã¯å¤±æã§ãã
å®è£ ã¯ä¸è¨ã®éãã
(defun verify-ed448 (public message r s) (when (< s *elliptic-n*) (let ((a (encode public)) (p (decode r))) (when p (let* ((k (sign-sha-ed448 r a message)) (x (multiple s *elliptic-g*)) (y (addition p (multiple k public)))) (equal-point x y))))))
9.3. ç½²åã¨æ¤è¨¼ã®ç¢ºèª
ããã§ã¯ãDSAã®èª¬æã«ãã£ãç¶æ³ããã£ã¦ã¿ã¾ãããã
次ã®å ´åãæ³å®ãã¾ãã
Hello
ãæ¤è¨¼ã§ããã¨ãHello
ããªããã¾ãã§éä¿¡ããã¨ãHello
ãæ¹ãããããã¨ã
Hello
ãæ¤è¨¼ã§ããã¨ããå®è¡ãã¾ãã
次ã®ãããªã³ã¼ããå®è¡ãã¾ãã
(defun verify-test () (let* ((private (make-private)) (public (make-public private)) (msg1 (map 'vector #'char-code "Hello")) (msg2 (map 'vector #'char-code "Hello"))) (multiple-value-bind (r s) (sign private msg1) (let ((v (verify public msg2 r s))) (format t "veriry: ~X~%" v))))) (let ((*random-state* (make-random-state t))) (with-elliptic-secp256k1 (verify-test)) (with-elliptic-secp256r1 (verify-test)) (with-elliptic-ed25519 (verify-test)) (with-elliptic-ed448 (verify-test)))
å®è¡çµæã¯ä¸è¨ã®éãã
veriry: T veriry: T veriry: T veriry: T
Hello
ããªããã¾ãã§éä¿¡ããã¨ãã¨ã¯ãéãå
¬ééµã§æ¤è¨¼ããå ´åã§ãã
public
ãä½æããã¨ãã«ãprivate
ã«1ã足ãã¦ã¿ã¾ãã
(defun verify-test () (let* ((private (make-private)) (public (make-public (mod (1+ private) *elliptic-p*))) (msg1 (map 'vector #'char-code "Hello")) (msg2 (map 'vector #'char-code "Hello"))) (multiple-value-bind (r s) (sign private msg1) (let ((v (verify public msg2 r s))) (format t "veriry: ~X~%" v)))))
å®è¡çµæã¯å¤±æã§ãã
Hello
ãæ¹ãããããã¨ãã¨ã¯ãã¡ãã»ã¼ã¸ãéã£ã¦ããã¨ãã§ãã
msg2
ã®å
容ããHallo
ã«ãã¦ã¿ã¾ãããã
(defun verify-test () (let* ((private (make-private)) (public (make-public private)) (msg1 (map 'vector #'char-code "Hello")) (msg2 (map 'vector #'char-code "Hallo"))) (multiple-value-bind (r s) (sign private msg1) (let ((v (verify public msg2 r s))) (format t "veriry: ~X~%" v)))))
å®è¡çµæã¯å¤±æã§ãã
ãã®ããã«ãã¦ãç½²åã¨æ¤è¨¼ãè¡ããã¨ãã§ãã¾ããã
çµããã§ã
é·ãã£ãã
ä»åº¦ã¯ãããCè¨èªã§ä½ããã¨æãã¾ããããã£ããçµããã
Common Lispã§æ¥åæ²ç·DSAãå®è£ ãã5ï¼éµçæï¼
ååã®ç¶ãããã§ãã
Common Lispで楕円曲線DSAを実装する4(確認) - nptclのブログ
7. DSAã¨ã¯ä½ã
ãã¾ãã«ãä»ããéãã¾ãããDSAã®èª¬æããã¾ãã
DSAã¨ã¯ããã£ã¸ã¿ã«ç½²åã¢ã«ã´ãªãºã (Digital Signature Algorithm)ã ããã§ãã
ãã£ã¸ã¿ã«ç½²åã¨ã¯ãããã®ã¡ãã»ã¼ã¸ã¯ã確ãã«ãã®äººãæ¸ãã¾ããããã¨ããã®ã確èªããããã®ãã®ã§ãã
ä¾ã¨ãã¦ãç§ãããã¿ããªã«åãã¦Hello
ã¨ããã¡ãã»ã¼ã¸ã
éä¿¡ãããã¨ãèãã¾ãã
Hello
ã¯å¤§å¤éè¦ãªã¡ãã»ã¼ã¸ãªã®ã§ã
æªæããã人ãã¡ã«ãã£ã¦ãç§ãã®ãªããã¾ãããã£ããã
ãããã¯Hello
ãæ¹ããããã¦Hallo
ã«ãªã£ãããããã¨ãæ¸å¿µããã¾ãã
äºåã«ç§ã¯ãç§å¯éµã¨å
¬ééµãä½æãã¾ãã
ç§å¯éµã¯ãåãªãä¹±æ°ã§ãã
ç§å¯ãªã®ã§é ãã¦ããã¾ãããã
å
¬ééµã¯ãèµ·ç¹ãç§å¯éµããhashãããç¨ãã¦ã¹ã«ã©ã¼åãããã®ã§ãã
å
¬éãªã®ã§èªåã®ãã¼ã¸ãã¡ã¼ã«ã§ã
ã¿ããªããããæã«å
¬éãã¦ããã¾ãããã
ããã§ã¯ç½²åãè¡ãã¾ãã
ç½²åã«ã¯ä¸è¨ã®ãã®ãå¿
è¦ã§ãã
- ç§å¯éµ
- ã¡ãã»ã¼ã¸
Hello
ç½²åã«ãã£ã¦æ¬¡ã®ãã®ãå¾ããã¾ãã
- ç½²å
ã¨ã¯ããããã32byteãããã®ãã¼ã¿ã§ãã
ã¨ãé£çµãããã®ãã¨è¡¨ç¾ãããããç½²åãã¼ã¿ã¨ãã¾ãã
ã§ã¯ã¿ããªã«ã¡ãã»ã¼ã¸ãéãã¾ãããã
ãã§ã«ã¿ããªã«å¨ç¥ããã¦ãããã®ã¯ä¸è¨ã«ãªãã¾ãã
- å ¬ééµ
ä»åæ°ãã«ã¿ããªã«éä¿¡ãããã®ã¯ä¸è¨ã«ãªãã¾ãã
- ã¡ãã»ã¼ã¸
Hello
- ç½²å
ããã§ç§ã®ä½æ¥ã¯çµããã§ãã
å ´é¢ãå¤ãããã¡ãã»ã¼ã¸Hello
ãåãåã£ãã¿ãªããã®ç«å ´ã«ãªãã¾ãã
ã¿ãªããã¯ãæ¬å½ã«ç§ãHello
ã¨ããã¡ãã»ã¼ã¸ãéä¿¡ããã®ãçã£ã¦ããããã§ãã
ããã§æ¬¡ã®ãã¼ã¿ããããã¾ãã
- å ¬ééµ
- ã¡ãã»ã¼ã¸
Hello
- ç½²å
ãããã®ãã¼ã¿ã使ãã
ç½²åãæ£ãããã©ããããã¯ããããããããã§çããã¨ããã®ãæ¤è¨¼ã§ãã
ãã¯ãããªãHello
ã¯ãç§ããæ¸ãããã®ã§ãã
ããããããªãHello
ã¯ãç§ããæ¸ãããã®ã§ã¯ããã¾ããã
ãªããã¾ããæ¹ãããè¡ãããã¨ãããã¨ã§ãã
7.1 ã¢ãªã¹ã¨ããã«ã¤ãã¦
ä½ãªã®ããã¤ã
7.2 æ²ç·ã¨ã¨ã³ãã£ã¢ã³
ããããã¯DSAãã®ãã®ãä½æãã¦ãããã¨ã«ãªãã¾ãã
èªåãæã£ããã¨ãªã®ã§ããã
æ²ç·ãã¨ã«ä½¿ãã¨ã³ãã£ã¢ã³ãéãããã§ãã
ä½ã£ã人ãéããããªã®ã§ãããã
次ã®ãããªæããåãã¾ããã
- secp256k1, secp256r1
- Big Endianã使ç¨ï¼ãã ãæªç¢ºèªï¼
- ed25519, ed448
- Little Endianã使ç¨ï¼ãã¡ãã¯ç¢ºå®ï¼
æªç¢ºèªã ã®ç¢ºå®ã ã®ããã¾ãã§ããã
secp256k1, secp256r1ã¯èªåãè¦ã¦ããæã£ãææ³ã¬ãã«ã§ãã
ãã ed25519ã¨ed448ã¯RFCã«åºåä¾ãè¼ã£ã¦ããã
Little Endianã¨ãã¦æ±ããªãã¨ãã®å¤ã«ãªãã¾ããã§ããã
ä½ã«ãããEndianãæ±ãããã®é¢æ°ãç¨æããå¿
è¦ãããã¾ãã
ã¾ãã¯Little Endianããã
(defun integer-little-vector (v size) (let ((a (make-array size :element-type '(unsigned-byte 8)))) (dotimes (i size) (setf (aref a i) (ldb (byte 8 (* i 8)) v))) a)) (defun vector-little-integer (v &key (start 0) end) (unless end (setq end (length v))) (let ((r 0) (k 0)) (loop for i from start below end do (setq r (logior r (ash (aref v i) (* k 8)))) (incf k 1)) r))
ä¸è¨ã®2ã¤ã®é¢æ°ã¯ã次ã®ãããªæ©è½ãæã¡ã¾ãã
integer-little-vector
- æ´æ°ãLittle Endianã¨è¦ãªãã¦ã8byteã®é åãè¿å´
vector-little-integer
- 8byteã®é åãLittle Endianã®åã¨ã¿ãªãã¦ãæ´æ°ãè¿å´ã
é£ããã¯ãªãã¨æãã¾ãã
* (integer-little-vector #x12345678 4) -> #(#x78 #x56 #x34 #x12) * (vector-little-integer #(#x78 #x56 #x34 #x12)) -> #x12345678
Big Endianã®æ¹ã¯æ¬¡ã®éãã
(defun integer-big-vector (v size) (let ((a (make-array size :element-type '(unsigned-byte 8)))) (dotimes (i size) (let ((k (- size i 1))) (setf (aref a i) (ldb (byte 8 (* k 8)) v)))) a)) (defun vector-big-integer (v &key (start 0) end) (unless end (setq end (length v))) (let ((r 0) (k (- end start 1))) (loop for i from start below end do (setq r (logior r (ash (aref v i) (* k 8)))) (decf k 1)) r))
8. éµãçæ
ç§å¯éµã¨å
¬ééµã®çæã説æãã¾ãã
ããããã®å½¢å¼ãè¦ãã¦ããã¦ãã ããã
- ç§å¯éµã¯ãæ´æ°ã§ãã
- å ¬ééµã¯ã座æ¨ã§ãã
ãã¶ãä¸è¬çã«éµã¨ããã®ã¯ã
16é²æ°ã§è¨è¼ãããããã¹ããªããããªãããªã¨æãã¾ãã
ã§ãããã§ã¯æ´æ°ã座æ¨ã¨ãã¦æ±ãã¾ãã
8.1. ç§å¯éµ
æåã«ç§å¯éµãä½ãã¾ãã
ç§å¯éµã¯ãã ã®ä¹±æ°ã§ãã
ãããç´°ããã¨ãããã¡ããã¨è¦ã¦ããã¾ãããã
8.1.1. ä¹±æ°ã使ãã¨ãã®æ³¨æ
æå·ã®ããã®ä¹±æ°ã¯ãé常ã«æ°ã使ã£ãæ¹ãããããã§ãã
æ®å¿µãªããä»ã®ç§ã«ã¯/dev/urandom
ã§ã¯ãªã
/dev/random
ã使ã£ã¦ä¸ãããããããç¥èãããã¾ããã
ãããããã£ã¦ç¢ºãä¹±æ°ãå¾ãããã®ãã®ã§ã¯ãªãã
ä¹±æ°ã®åæå¤ãè¨å®ããããã®ãã®ã ã£ãã¨æãã
ããæå·ã«ä½¿ãä¹±æ°ãçæããå ´åã ç似乱æ°çæã§æè¯ã¨èããããMersenne Twisterã§ãã äºæ¸¬å¯è½ãªä¹±æ°åãªã®ã§ãã®ã¾ã¾ä½¿ãã¹ãã§ã¯ãªãã¨è¨ã£ã¦ãã¾ãã
Mersenne Twister Home Page
ããèããã質å
http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/faq.html
SHAã¨çµã¿åããã¦ä¸ããã ããã§ãã
ã¾ããã«ãããªãããèãã¾ãããã
èªåãã¨ããããSHA-256ãããã絡ãã¦ã¿ããã¨æãã¾ãã
æè¿SHAãä½ã£ãã®ã§ã使ã£ã¦ã¿ãããªãã¾ããã
ã¨ãããã¨ã§ãSHAã使ã£ãä¾ã示ãã¾ãã
(defun make-private-256bit (&optional (n 4)) (let ((hash (make-sha256encode))) (dotimes (i n) (little-endian-sha256encode hash (random (ash 1 256)) 32)) (vector-little-integer (calc-sha256encode hash))))
256bitã®ä¹±æ°ãã4åã ãSHA-256ã«çªã£è¾¼ãã§ãã¾ãã
ããã©ããªã次ã®å½ä»¤ã§ä»£ç¨ã§ãã¾ãã
(random (ash 1 256))
å°ãã ãã§ããããªã£ã¦ããã®ãæå¾
ãã¾ãã
ed448ã®å ´åã¯SHA-512ã§ã使ã£ã¦ãã ããã
è¨ãå¿ãã¦ãã¾ãããããã®ããã«SHAãèªä½ãã¾ããã
好ãã«ä½¿ã£ã¦ä¸ããã
https://github.com/nptcl/fixed/blob/main/sha.lisp
8.1.2. secp256k1, secp256r1
å ¬å¼ãµã¤ãã®PDFãåèã«ãã¾ãã
3.2.1 Elliptic Curve Key Pair Generation Primitive
https://www.secg.org/sec1-v2.pdf
Randomly or pseudorandomly select an integer d in the interval [1,n â 1].
ã¤ã¾ãã1ï½n-1ã®ä¹±æ°ã§ãã
256bitã®ä¹±æ°ãåºããããnã§å²ã£ã¦ãã¾ããåãã¾ãããã
0ã®å ´åã¯ããç´ãã¾ãããã
ãã¨ãã¨ã§ã¨ã©ã¼ã«ãªãã£ã½ãã
â
注æ
ä¹±æ°ã®ç¯å²ã¯ãæ²ç·ãã©ã¡ã¼ã¿ã¼ã®æªæºã§ãã
ç´ æ°æªæºã§ã¯ãªãããã§ãã
ç§ã¯ééãã¾ããï¼
å®è£ ä¾ã示ãã¾ãã
(defun modn (x) (mod x *elliptic-n*)) (defun make-private-secp256k1 () (let ((x (modn (make-private-256bit)))) (if (zerop x) (make-private-secp256k1) x))) (defun make-private-secp256r1 () (make-private-secp256k1))
(modn x)
ã¯ããè¨ç®ããã ãã®é¢æ°ã§ãã
ç½²å以éã§ãããã使ãã¾ãã®ã§ãä»ã®ãã¡ã«ç¨æãã¦ããã¾ãããã
8.1.3. ed25519
RFCãåèã«ãã¾ãã
5.1.5. Key Generation
https://datatracker.ietf.org/doc/html/rfc8032
The private key is 32 octets (256 bits, corresponding to b) of
cryptographically secure random data.
ã¤ã¾ãã¯256bitã®ä¹±æ°ãã®ãã®ã§ãã
nã§å²ãå¿
è¦ã¯ããã¾ããã
ããã«è¨ãã¨ãã©ããç§å¯éµã®ããã·ã¥ã使ãããã®ã§ã0ã§ãåé¡ããã¾ããã
(defun make-private-ed25519 () (make-private-256bit))
8.1.4. ed448
RFCãåèã«ãã¾ãã
5.2.5. Key Generation
https://datatracker.ietf.org/doc/html/rfc8032
The private key is 57 octets (456 bits, corresponding to b) of
cryptographically secure random data.
SHA-512ã§ä¹±æ°ãä½ã£ã¦ã¿ã¾ãã
(defun make-private-ed448 (&optional (n 4)) (let ((hash (make-sha512encode))) (dotimes (i n) (little-endian-sha512encode hash (random (ash 1 512)) 64)) (vector-little-integer (calc-sha512encode hash) :end 57)))
é¢åãªã次ã®å½ä»¤ã§ä»£ç¨ã§ãã¾ãã
(random (ash 1 456))
ãã¨Little Endiané¢æ°ã使ã£ã¦ãã¾ããã ä¹±æ°ãä¹±æ°ã«ããã ããªã®ã§ã©ã£ã¡ã§ãããã§ãã
8.2. å ¬ééµ
ãã¡ãã¯ç¨®é¡ã«ãã£ã¦åºãæ¹ã大ããå¤ããã¾ãã
ãã ã©ãããããåºæ¬çã«ã¯ãèµ·ç¹ã®ã¹ã«ã©ã¼åãå
¬ééµã§ãã
8.2.1. secp256k1, secp256r1
èµ·ç¹ãç§å¯éµã§ã¹ã«ã©ã¼åãããã®ãå
¬ééµã§ãã
ç°¡åã§ããã
(defun make-public-secp256k1 (private) (multiple private *elliptic-g*)) (defun make-public-secp256r1 (private) (multiple private *elliptic-g*))
8.2.2. ed25519
EdDSAã®å ´åã¯ããããã¨ä½æ¥ãããã¾ãã
5.1.5. Key Generation
https://datatracker.ietf.org/doc/html/rfc8032
- ç§å¯éµãSHA-512ã«ãã£ã¦ã64byteã®ããã·ã¥å¤ãåºåããã
- ããã·ã¥å¤ã®ä¸ä½32byteãæ´æ°ã«å¤æ
- æ´æ°ã«å¯¾ãã¦æ¬¡ã®bitå¤ãã»ãã
- 0 bitã0
- 1 bitã0
- 2 bitã0
- 254 bitã1
- 255 bitã0
- èµ·ç¹ã®ã¹ã«ã©ã¼åãå ¬ééµã¨ããã
å
¨é¨Little Endianã¨ãã¦æ±ã£ã¦ä¸ããã
SHA-512ã®ä¸ä½32byteã¨ã¯0ï½31byteã®äºã§ãã
ä¸ä½32byteã®32ï½63byteã¯ç½²åã®æã«ä½¿ãã¾ãã
å®è£
ã®è©±ããã¾ãã
ãããªãå
¬ééµã¾ã§ä½æãã¦ãã¾ãã®ã§ã¯ãªãã
次ã®2ã¤ã®å¤ãè¿å´ããé¢æ°ãä½æãã¾ãã
- æ´æ°
- ããã·ã¥å¤ã®ä¸ä½32byteã®æ´æ°ï¼ç½²åæã«ä½¿ç¨ï¼
å®è£ ã¯æ¬¡ã®ããã«ãªãã¾ãã
(defun make-public-sign-ed25519 (private) (let ((sha (make-sha512encode))) (little-endian-sha512encode sha private 32) (let* ((v (calc-sha512encode sha)) (a (vector-little-integer v :start 0 :end 32)) (b (vector-little-integer v :start 32 :end 64))) (let ((v (ash 1 254))) (setq a (logand a (- v 8))) ;; cofactor (setq a (logior a v))) (values a b))))
å ¬ééµã ããçæãããã¨ãã¯ã æåã®è¿å´å¤ã®ã¿ã使ãã¾ãã
(defun make-public-ed25519 (private) (multiple (make-public-sign-ed25519 private) *elliptic-g*))
åèæ
å ±ã§ããã
1ï½3bitã0ã«ã¯ãªã¢ããã®ã¯cofactorã8ã ããã ããã§ãã
ed448ã¯cofactorã4ãªã®ã§ã2ãããããã¯ãªã¢ãã¾ããã
ãªãã§ãããããã¨ããã®ãã¯å
¨ç¶ç¥ãã¾ãããã
8.2.3. ed448
ed25519ã¨ã¯å¾®å¦ã«ç°ãªã£ã¦ãã¾ãããã ãããåãã§ãã
5.2.5. Key Generation
https://datatracker.ietf.org/doc/html/rfc8032
- ç§å¯éµãSHAKE-256ã«ãã£ã¦ã114byteã®ããã·ã¥å¤ãåºåããã
- ããã·ã¥å¤ã®ä¸ä½57byteãæ´æ°ã«å¤æ
- æ´æ°ã«å¯¾ãã¦æ¬¡ã®bitå¤ãã»ãã
- 0 bitã0
- 1 bitã0
- 447 bitã1
- 448ï½455 bitã0 ï¼æå¾ã®1byte=8bitããå ¨é¨0ã«ããï¼
- èµ·ç¹ã®ã¹ã«ã©ã¼åãå ¬ééµã¨ããã
(defun make-public-sign-ed448 (private) (let ((sha (make-shake-256-encode))) (little-endian-sha3encode sha private 57) (let* ((v (result-sha3encode sha 114)) (a (vector-little-integer v :start 0 :end 57)) (b (vector-little-integer v :start 57 :end 114))) (let ((v (ash 1 447))) (setq a (logand a (- v 4))) ;; cofactor (setq a (logior a v))) (values a b)))) (defun make-public-ed448 (private) (multiple (make-public-sign-ed448 private) *elliptic-g*))
8.3. 座æ¨ã®encode
å
¬ééµãã§ããã®ã§ãäºåã«ã¿ããªã«å¨ç¥ããããªãã¾ãã
ãããå
¬ééµã¯åº§æ¨ã§ãã
座æ¨ããã®ã¾ã¾èª°ãã«éã£ãããããããããªã®ããããããã¨ãªãã¨æãã¾ãã
ããã§ããããããå½¢å¼ã«å¤æãããencodeãè¡ãã¾ãããã
secp256k1ã¨secp256r1ã¯ãã¾ãã«ãã®ãããªç®çã§äººã®ããã«encodeãã¾ãã
ed25519ã¨ed448ã¯ãããã«å ãã¦ç½²åã®å¦çã§encodeã使ãã¾ãã
æ²ç·ã«ãã£ã¦ç¨éãå¾®å¦ã«éãã¾ãã
人ã«è¦ã¦æ¬²ããã ãã®å ´åãencodeã¯é
åãè¿å´ãããã¨ã«ãã¾ããã
ä¸æ¹ããã®å¾ã®ç½²åã®å¦çã§ã使ãå ´åãencodeã¯æ´æ°ãè¿å´ãã¾ãã
- encodeã®è¿å´å¤ã¯ã
- secp256k1ã¨secp256r1ã¯ãbyteã®é å
- ed25519ã¨ed449ã¯ãæ´æ°
encodeã®å
容ãè¦ã¦ããã¾ãããã
座æ¨ããåçã®æå³ããã¤å¤ãï¼é
åãæ´æ°ï¼ã¨ããã¨ãã
- 座æ¨ãããã¸ã®å¤æããencode
- ããã座æ¨ã¸ã®å¤æããdecode
ã¨ãªãã¾ãã
座æ¨ã¯ã¢ãã£ã³åº§æ¨ã«ãããã¨ã§ãããã32byteãããã®ãã¼ã¿ã«ãªãã¾ãã
ãã ããã®åº§æ¨ã¯ãã¶ãæ²ç·ä¸ã«ä½ç½®ãã¦ããã®ã§ã
çæ¹ããã°ç®åºãããã¨ãã§ãã¾ãã
ã©ã¡ããã®å¤ã ããä¿åãããã¨ã§çããããã¨ãã§ãããã¨ãè¦ãã¦ããã¾ãããã
8.3.1. secp256k1, secp256r1
ECDSAã®encodeã¯ã次ã®å ´æã§å®ç¾©ããã¦ãã¾ãã
2.3.3 Elliptic-Curve-Point-to-Octet-String Conversion
https://www.secg.org/sec1-v2.pdf
ãã®PDFã«ã¯encode/decodeã¨ããèªã¯åºã¦ãã¾ããã
ã¾ããECDSAã ãã®è©±ã«ãªãã¾ããã
compression, uncompressionã¨ãã
2ã¤ã®è¡¨ç¾æ¹æ³ãããã¾ãã
compressionã¯ãX座æ¨ã ãã表ãã¾ãï¼å
¨33byteé·ï¼ã
uncompressionã¯ãX座æ¨ã®æ¬¡ã«Y座æ¨ã表ãã¾ãï¼å
¨65byteé·ï¼ã
compressionãuncompressionã¯ãã ã®é¸æè¢ã§ããã
ã©ã£ã¡ã§ãããã®ã§å¥½ããªæ¹ãé¸ãã§ãã ããã
ããã§ã¯é ã«ã¿ã¦ããã¾ãããã
座æ¨ãencodeãããã¨ãèãã¾ãã
ãããªãã次ã®1byteã§çµããã§ãã
#(0x00)
å¤å®æ¹æ³ã¯ãã®ã¨ãã§åé¡ããã¾ããã
çæã³ã¼ãã¯æ¬¡ã®éãã
(integer-big-vector #x00 1)
`ã§ããã¨ããèãã¾ãã
座æ¨ãã¢ãã£ã³åº§æ¨ã«å¤æãã¦è©±ãé²ãã¾ãã
æåã«uncompressã®å ´åãè¦ã¦ããã¾ãã
次ã®é çªã§ãã¼ã¿ãé£çµãã¾ãã
ä¾ãã°x=0x0100
, y=0x0200
ã§ããã
ä¾æã®ããã«32bité·ã¨ããã¨ãã¯æ¬¡ã®ããã«ãªãã¾ãã
#(04 00 00 01 00 00 00 02 00)
è¿å´å¤ã¯é åã§ãããè¦ç´ 0çªç®ãå·¦(04ã®å¤)ã«ãªãã¾ãã
çæã³ã¼ãã¯æ¬¡ã®éãã
(defun encode-uncompress-secp256k1 (x y) (integer-big-vector (logior (ash #x04 (* 256 2)) (ash x 256) y) (1+ 64)))
ç¶ãã¦compressã示ãã¾ãã
æåã®1byteã¯ãã®æä¸ä½ãããã«ãã£ã¦å¤ããã¾ãã
次ã®é çªã§ãã¼ã¿ãé£çµãã¾ãã
- ã®æä¸ä½ããããã
0
ã®ã¨ãã¯0x02
1
ã®ã¨ãã¯0x03
- Big Endianã®å¤
ä¾ãã°x=0x00000100
, y=0x00000200
ã§ããã
32bité·ã®ã¨ãã¯æ¬¡ã®ããã«ãªãã¾ãã
#(02 00 00 01 00)
ä»ã®ä¾ã¨ãã¦ãx=0x00000100
, y=0x00000201
ã§ããã
32bité·ã®ã¨ãã¯æ¬¡ã®ããã«ãªãã¾ãã
#(03 00 00 01 00)
çæã³ã¼ãã¯æ¬¡ã®éãã
(defun encode-compress-secp256k1 (x y) (integer-big-vector (logior (ash (if (logbitp 0 y) #x03 #x02) 256) x) (1+ 32)))
8.3.2. ed25519
座æ¨ãã¢ãã£ã³åº§æ¨ã«ãã¾ãã
ã¨ã³ã³ã¼ããããæ´æ°ã¯ä¸è¨ã®ããã«ä½æããã¾ãã
- ã®æä¸ä½ããããã®æä¸ä½ããã(255bit)ã«ã³ãã¼
æ´æ°ã¯256bité·(32byte)ã®æ´æ°ã«ãªãã¾ãã
åºæ¬ã¯ã®å¤ã ããä¿åããã®ã§ããã
ä»ã«ã®ç¬¦å·æ
å ±ãå¿
è¦ã ã¨ã®ãã¨ã
ã®ç¬¦å·ã¯æä¸ä½ãããã§ãï¼æä¸ä½ã§ã¯ãªãï¼ã
çæã³ã¼ãã¯æ¬¡ã®éãã
(defun encode-ed25519 (v) (let* ((a (affine v)) (x (point2-x a)) (y (point2-y a))) (when (logbitp 0 x) (setq y (logior y (ash 1 255)))) y))
è¿å´å¤ã¯ãç½²åã§ã使ãã®ã§æ´æ°ã§ãã
ã¨ãããå
¬ééµã¨ãã¦ã¿ããªã«å¨ç¥ããããã¨ãããã¨æãã¾ãã
ãããªã¨ãã¯ãlittle endianã§32byteã®é
åã«ç´ãã¦ãã ããã
ã¤ã¾ããä¾ã¨ãã¦256bitã¯é·ãããã®ã§32bitã§ç¤ºãã¾ããã
#x12345678 -> $(#x78 #x56 #x34 #x12)
ã®ããã«ãªãã¾ãã
8.3.3. ed448
ed25519ã¨ã»ã¼åãã§ãããç¹å¾´ãããã¾ãã
ã¾ãããæ¹ããã
- ã®æä¸ä½ããããã®455bitã«ã³ãã¼
çæã³ã¼ãã¯æ¬¡ã®éãã
(defun encode-ed448 (v) (let* ((a (affine v)) (x (point2-x a)) (y (point2-y a))) (when (logbitp 0 x) (setq y (logior y (ash 1 455)))) y))
8.4. 座æ¨ã®decode
encodeããããã¼ã¿ãdecodeãã¦åº§æ¨ã«æ»ãã¾ãããã
ãã§ã«è¦ã¦ããéããæ²ç·ã«ãã£ã¦encodeã®è¿å´å¤ã®å½¢å¼ãéãã®ã§ã
decodeã®å¼æ°ã®å½¢å¼ããããã«åãããå¿
è¦ãããã¾ãã
次ã®ããã«ãªãã¾ãã
- decodeã®å¼æ°ã¯ã
- secp256k1ã¨secp256r1ã¯ãbyteã®é å
- ed25519ã¨ed449ã¯ãæ´æ°
è¿å´å¤ã¯ã©ã¡ãã座æ¨ã§ãã
8.4.1. secp256k1, secp256r1
å¼æ°ã®byteã®é åããã座æ¨ãæ±ãã¾ãã
次ã®å ´æã§å®ç¾©ããã¦ãã¾ãã
2.3.4 Octet-String-to-Elliptic-Curve-Point Conversion
https://www.secg.org/sec1-v2.pdf
ã®æåã®1 byteãè¦ã¦å½¢å¼ãå¤å®ãã¾ãã
ãããªæãã§å®è£
ãã¾ããã
(defun decode-secp256k1 (k) (case (aref k 0) (#x00 (make-point3 0 0 0)) (#x02 (decode-compress-secp256k1 k 0)) (#x03 (decode-compress-secp256k1 k 1)) (#x04 (decode-uncompress-secp256k1 k))))
ããæåã®byteã0x00
ãªãã座æ¨ã¯ç¡éé ç¹ã®ã§ãã
ããæåã®byteã0x04
ãªããcompressionã§ã¯ãªã座æ¨ãã¼ã¿ã§ãã
ç¶ã32byteããBig Endianã®æ´æ°ã¨ãã¦ã®å¤ã«ãã¾ãã
ç¶ã32byteããBig Endianã®æ´æ°ã¨ãã¦ã®å¤ã«ãã¾ãã
å¾ãããå¤ãããå°å½±åº§æ¨ã§è¿å´ãã¾ãã
é
åãããã®ã¾ã¾å¤ãåãåºãã¾ãããã
å¤ãvalid
ãã©ããã¯å¤å®ããã»ããããã¨æãã¾ãã
(defun decode-uncompress-secp256k1 (k) (let ((p (make-point3 (vector-big-integer k :start 1 :end 33) (vector-big-integer k :start 33 :end 65)))) (when (valid p) p)))
ããæåã®byteã0x02
ã0x03
ãªããcompressionã§ãã
ãã®å¤ã¯ã®ç¬¦å·ã表ãã¦ãã¾ãã
次ã®ããã«ç¬¦å·ã決ãã¦ãã ããã
- æåã®1byteã
0x02
ãªãã - æåã®1byteã
0x03
ãªãã
ç¶ã32byteããBig Endianã®æ´æ°ã¨ãã¦ã®å¤ã«ãã¾ãã
ã®å¤ãã®ãã®ã¯æ ¼ç´ããã¦ãã¾ããã®ã§ã次ã®æé ã§ç®åºããå¿
è¦ãããã¾ãã
次ã®å¼ãããæ±ãã¾ãã
ããã§ãã¨ã¯æ²ç·ã®ãã©ã¡ã¼ã¿ã¼ã§ãã
次ã«ã®å¹³æ¹æ ¹ãæ±ãã¾ãã
å¹³æ¹æ ¹ã®æ±ãæ¹ã¯ãã§ã«èª¬ææ¸ã¿ã§ãã
æå¾ã«ç¬¦å·ãåããã¾ãã
ç®åºããå¹³æ¹æ ¹ã®æä¸ä½ããããã¨ããã¨ãã
- ãªãã
- ãªãã
以ä¸ã§ãã¨ãæ±ã¾ãã¾ããã
å°å½±åº§æ¨ãDecodeã®è¿å´ã§ãã
å®è£ ã¯æ¬¡ã®éãã§ãã
(defun decode-compress-secp256k1 (k y0) (let* ((x (vector-big-integer k :start 1 :end 33)) (a (modp (+ (* x x x) (* *elliptic-a* x) *elliptic-b*))) (y (square-root-mod-4 a))) (when y (unless (= (logand y #x01) y0) (setq y (- *elliptic-p* y))) (make-point3 x y))))
8.4.2. ed25519
å¼æ°ã®æ´æ°ããã座æ¨ãæ±ãã¾ãã
ã¾ãã¯ãã次ã®å¤ãç®åºãã¾ãã
- ã®0ï½254bitãã¨ããã
- ã®255bitãã®ç¬¦å·ã¨ããã
encodeã®éã®å¦çããã¦ããã ããªã®ã§ãããã¨æãã¾ãã
æ±ããã®ãã§ãã¯ãè¡ãã¾ãã
- ã以ä¸ã®å ´åã¯decode失æ
次ã«ã¨ãç¨ãã¦ããæ±ãã¾ãããã
ã®ç®åºã¯ãæ²ç·ã®å¼ãç¨ãã¾ãã
次ã®, ãæ±ãã¾ãã
ãã®å¤ãç¨ãã¦ä»®æ±ºãã®ãæ±ãã¾ãã
確èªã®ããã®å¤ãè¨ç®ãã¾ãã
- ãªãããå¹³æ¹æ ¹
- ãªããå¹³æ¹æ ¹ã¯ã
- ãã以å¤ãªãå¹³æ¹æ ¹ã¯ãªãï¼decode失æï¼
符å·ã§å ´ååãããã¾ãã
- ããã§ãããã¤ã®ã¨ãã¯ãdecodeã¯å¤±æ
- ããã®æä¸ä½ãããã¨ãçãããªããªãããä»£å ¥
çµæã®ãdecodeã®è¿å´å¤ã§ãã
å®è£ ã¯ãå¼ã®è¨ç®ãããå°ã è¤éã§ãã
(defun decode-ed25519-x (y) (when (< y *elliptic-p*) (let* ((yy (* y y)) (u1 (modp (1- yy))) (v1 (1+ (* *elliptic-d* yy))) (v2 (* v1 v1)) (v3 (* v1 v2)) (v4 (* v2 v2)) (uv3 (* u1 v3)) (p (/ (- *elliptic-p* 5) 8)) (x (mulp uv3 (power-mod (* uv3 v4) p *elliptic-p*))) (v1x2 (mulp v1 x x))) (cond ((= v1x2 u1) x) ((= v1x2 (- *elliptic-p* u1)) (mulp x (power-mod 2 (/ (1- *elliptic-p*) 4) *elliptic-p*))))))) (defun decode-ed25519 (k) (let* ((y (ldb (byte 255 0) k)) (x0 (ldb (byte 1 255) k)) (x (decode-ed25519-x y))) (cond ((null x) nil) ((and (= x 0) (= x0 1)) nil) ((/= (logand x #x01) x0) (make-point4 (- *elliptic-p* x) y)) (t (make-point4 x y)))))
7.3.4. ed448
æµãã¯ed25519ã¨åãã§ãã
ãã次ã®å¤ãç®åºãã¾ãã
- ã®0ï½454bitãã¨ããã
- ã®455bitãã®ç¬¦å·ã¨ããã
æ±ããã®ãã§ãã¯ãè¡ãã¾ãã
- ã以ä¸ã®å ´åã¯decode失æ
, ãæ±ãã¾ãã
ed25519ã¨ã¯ç¬¦å·ãéãã®ã§æ³¨æã
仮決ãã®ãæ±ãã¾ãã
確èªã®ããã®å¤ãè¨ç®ãã¾ãã
符å·ã§å ´ååãããã¾ãã
- ããã§ãããã¤ã®ã¨ãã¯ãdecodeã¯å¤±æ
- ããã®æä¸ä½ãããã¨ãçãããªããªãããä»£å ¥
çµæã®ãdecodeã®è¿å´å¤ã§ãã
(defun decode-ed448-x (y) (when (< y *elliptic-p*) (let* ((yy (* y y)) (u1 (modp (1- yy))) (u2 (* u1 u1)) (u3 (* u1 u2)) (u5 (* u2 u3)) (v1 (1- (* *elliptic-d* yy))) (v3 (* v1 v1 v1)) (u3v1 (* u3 v1)) (u5v3 (* u5 v3)) (p (/ (- *elliptic-p* 3) 4)) (x (mulp u3v1 (power-mod u5v3 p *elliptic-p*))) (v1x2 (mulp v1 x x))) (when (= v1x2 u1) x)))) (defun decode-ed448 (k) (let* ((y (ldb (byte 455 0) k)) (x0 (ldb (byte 1 455) k)) (x (decode-ed448-x y))) (cond ((null x) nil) ((and (= x 0) (= x0 1)) nil) ((/= (logand x #x01) x0) (make-point3 (- *elliptic-p* x) y)) (t (make-point3 x y)))))
ç¶ãã¾ã
次ã¯ç½²åã¨æ¤è¨¼ãããã¾ãã
Common Lispで楕円曲線DSAを実装する6(署名) - nptclのブログ
Common Lispã§æ¥åæ²ç·DSAãå®è£ ãã4ï¼ç¢ºèªï¼
ååã®ç¶ãããã§ãã
Common Lispで楕円曲線DSAを実装する3(乗算など) - nptclのブログ
6. æ¼ç®ã®ãã¹ã
ããã¾ã§æ¥ããã¡ããã¨ãã¹ããã¾ãã
次ã®æ©è½ãæ£å¸¸ã«æ©è½ãããã¨ã確èªãã¾ãããã
- å ç® addition
- 2å doubling
- ã¹ã«ã©ã¼å multiple
æ¼ç®ã®å
容ã¯ã使ç¨ããæ²ç·ã«ãã£ã¦å
¨é¨ç°ãªãã¾ãã®ã§ã
å°éã«å
¨éãããå¿
è¦ãããã¾ãã
ä¸è¨ã®4種é¡ãå
¨é¨ããã¾ãã
- secp256k1
- secp256r1
- ed25519
- ed448
6.1. ä½æ°ã«ã¤ãã¦
ãã®åã«ããã¾ãã¡ç解ã§ãã¦ãªããä½æ°ãã®è©±é¡ããã¾ãã
ä¹ç®ã®ãã¹ãã®æã«ä½¿ãã¾ãã
ä½æ°ã¨ã¯ãã©ãã2ã¤æå³ãããããã§ãã
使ç¨ããæ²ç·ã®ãã¨ãã¨ãè¨ã£ãããã¾ãã
ãã®ã¨ãã¨ã¯ãã®ä½æ°ã¨ããã
æ²ç·ä¸ã«åå¨ã§ããç¹ã®æ°ã表ãã¾ãã
æ²ç·ã®ä½æ°ã¯ãå°ãªãã¨ãå®è£
ããã¨ãã«ã¯å
¨ã使ãã¾ããã
ä¸æ¹ãæ²ç·ãã©ã¡ã¼ã¿ã¼ã«ããã¯ãèµ·ç¹ã«å±ããä½æ°ã ããã§ãã
次ã®ããã«ãªãã¾ãã
ã¤ã¾ãã¯å ç®ãåãããç¹°ãè¿ãã¨
å
ã«æ»ã£ã¦ãã¦ããããåãã¨ãããã¨ãªã®ã§ãããã
å ´åã«ãã£ã¦ã¯ãã¨ãã¨ãè¨ããããããããã§ãã
ã¾ãã次ã®é¢ä¿ãæãç«ã¤ããã§ãã
ããã§ã¯æ²ç·ãã©ã¡ã¼ã¿ã¼ã«è¼ã£ã¦ããã
cofactorã¨ãè¨ããã¦ã¾ãã
ä½è
ãã¯ç¥ãã¾ããã
åèã¾ã§ã«ãsecp256k1ã®ä½æ°ã¯æ¬¡ã®ããã«ãªãã¾ãã
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
ããä¸ä½ããã¤ãªãã ã
ã§ããªãã¨ãªããããã¾ããã
ãã¹ãã§ä½¿ãã®ã¯æ¬¡ã®å¼ã§ãã
ããã§addition, doubling, multipleã æ£ããå®è£ ããã¦ãããã©ããã確èªãã¾ãã
6.2. æ²ç·ä¸ã®åº§æ¨ãã©ããã確èª
ãã®ç¢ºèªã¯ãã¡ã便å©ã§ãã
ãã座æ¨ããã¡ããã¨æ²ç·ä¸ã«ããã®ãã©ããã確èªãã¾ãããã
確èªã®æ¹æ³ã¯ç°¡åã§ãæ²ç·ã®å¼ã«ãã®ã¾ã¾ä»£å
¥ããã ãã§ãã
ãããã¢ãã£ã³åº§æ¨ã«å¤æããå¿
è¦ããããã¨ã ãã¯æ³¨æãã¦ãã ããã
ä¾ã¨ãã¦secp256k1ã®ç¢ºèªããã¦ã¿ã¾ãããã
確èªãã座æ¨ã次ã®ããã«è¡¨ãã¾ãã
ã¾ãã¯ã¢ãã£ã³åº§æ¨ã«å¤æãã¾ãã
ã¢ãã£ã³åº§æ¨ãç¨ãã¦ã次ã®å¼ãæãç«ã£ã¦ãããã確èªãã¾ãã
ããã§ãã¨ã¯ãæ²ç·ã®ãã©ã¡ã¼ã¿ã¼ã§ãã
確èªã¯ç°¡åã§ãããããã4ã¤ã®æ²ç·ã§ä½ããªããã°ãããªãã®ãåä»ã§ãã
æ²ç·ã®å¼ã¯2種é¡ããããã¾ããã®ã§ã
ã¨ã£ã¨ã¨ãã£ã¦ãã¾ãã¾ãããã
ed25519ã®ã¿ã®è©±ã§ãããæ¡å¼µãããå°å½±åº§æ¨ã®å ´åã¯ã
ã¤ãã§ã«ã確èªãã¾ãããã
ã©ããããã¨ãã¨ããã¨ã
ã®ã¨ããæ²ç·ä¸ã«ãããã©ããã確èªããå¾ã
ãä½µãã¦ç¢ºèªãã¾ãã
å ç®ã®éã«ã¯ç¬ç«ãã¦è¨ç®ãããã®ã§ã
確èªããæå³ã¯ããã¾ãã
å®æãããèµ·ç¹ã§ãã£ã¦ã¿ãã¨ããã¨æãã¾ãã
絶対ã«ãã£ã¦ããã¯ãã§ãã
6.2.1. 座æ¨ã®ãã§ãã¯
確èªããå 容ã¯å¤§ãã2ã¤ã
- æ²ç·ä¸ã«åå¨ãããã¨
- å¤ãæ£ãããã¨
ã¾ãã¯æ²ç·ä¸ã§ãããã¨ç¢ºèªãã¾ãã
æ²ç·ã¯2ã¤ä½¿ã£ã¦ãã¾ãã
- Weierstrassæ²ç·
- secp256k1
- secp256r1
- Edwardsæ²ç·
- ed25519
- ed448
ãã®2種é¡ã®æ²ç·ã®ç¢ºèªãè¡ãã¾ãã
æ¹æ³ã¯ç°¡åã§ããã å¼ã«ä»£å
¥ããã ãã§ãã
å®è£
ãã¾ãã
Weierstrassæ²ç·ã®ç¢ºèªã³ã¼ã
(defun valid-weierstrass (v) (let* ((a (affine v)) (x (point2-x a)) (y (point2-y a))) (zerop (modp (- (* y y) (+ (* x x x) (* *elliptic-a* x) *elliptic-b*))))))
Edwardsæ²ç·ã®ç¢ºèªã³ã¼ã
(defun valid-edwards (v) (let* ((a (affine v)) (x (point2-x a)) (y (point2-y a)) (xx (* x x)) (yy (* y y))) (zerop (modp (- (+ (* *elliptic-a* xx) yy) (+ 1 (* *elliptic-d* xx yy)))))))
6.2.2. å¤ã®ãã§ãã¯
ããã¯ãæã¤ãed25519ã§ã®ã¿è¡ããã確èªã§ãã
ã«ã¯æ¬¡ã®ãããªé¢ä¿ãããã¾ãã
åç´ã«ç¢ºèªãã¾ãã
(defun valid-point4 (v) (let* ((z (inverse (point4-z v))) (x (mulp (point4-x v) z)) (y (mulp (point4-y v) z)) (xy (mulp (point4-xy v) z))) (zerop (modp (- (* x y) xy))))) (defun valid-ed25519 (v) (and (valid-edwards v) (valid-point4 v)))
6.2.3. Common Lispã§ã®å®è£
確èªã®å
容ã¯æ²ç·ã«ãã£ã¦ç°ãªãã¾ãã
次ã®ãã®ã使ç¨ãã¾ãã
- secp256k1, secp256r1
valid-weierstrass
é¢æ°ã使ç¨
- ed25519
valid-ed25519
é¢æ°ã使ç¨
- ed448
valid-edwards
é¢æ°ã使ç¨
æ²ç·ãã¨ã«å ´ååããããå ´åã
addition
ãªã©ã¨åãããã«specialå¤æ°ã使ãã¾ãã
(defvar *elliptic-valid*) (defun valid (v) (funcall *elliptic-valid* v))
æç¸ããå 容ã¯æ¬¡ã®ããã«ãªãã¾ãã
;; secp256k1, secp256r1 (let ((*elliptic-valid* #'valid-weierstrass)) ...) ;; ed25519 (let ((*elliptic-valid* #'valid-ed25519)) ...) ;; ed448 (let ((*elliptic-valid* #'valid-edwards)) ...)
6.3. å°å½±åº§æ¨ã§åãå ´æãã©ãã確èª
2ã¤ã®å°å½±åº§æ¨, ãåãå ´æãã©ããã確èªããã«ã¯ã©ããããããã§ããããã
ãããã®ã§ãã¨ã ããæ¯è¼ãã¦ã
çãããã©ãããããããªãã®ã§ãã
ãããããã¢ãã£ã³åº§æ¨ã«å¤æãã¦æ¯è¼ããå¿
è¦ãããã¾ãã
ãã ãRFCã®Pythonã®ã³ã¼ãã§ã¯æ¬¡ã®ãããªãã®ãããã¾ããã
def point_equal(P, Q): # x1 / z1 == x2 / z2 <==> x1 * z2 == x2 * z1 if (P[0] * Q[2] - Q[0] * P[2]) % p != 0: return False if (P[1] * Q[2] - Q[1] * P[2]) % p != 0: return False return True
ã¤ã¾ãã¯éæ°ãæ±ããã¨ããä¹ç®ã ãã§èª¿ã¹ãããããã§ãã
é度ãå¿
è¦ãªå ´åã¯ä¾¿å©ãªã®ã§å®è£
ãã¾ãããã
ä¸å¿æ¥æ¬èªã§æé ã示ãã¾ãã 座æ¨ã¨ã次ã®ããã«ãã¾ãã
ãã®ã¨ãã次ã®å¤å®ãé çªã«è¡ã£ã¦ãã ããã
- ã®ã¨ããçãããªã
- ã®ã¨ããçãããªã
- ä¸è¨ãå½ã¦ã¯ã¾ããªãå ´åã¯ãçãã
å®è£ ä¾ã示ãã¾ãã
(defun equal-point (p q) (let ((pz (point3-z p)) (qz (point3-z q))) (and (zerop (modp (- (* (point3-x p) qz) (* (point3-x q) pz)))) (zerop (modp (- (* (point3-y p) qz) (* (point3-y q) pz)))))))
ã¢ãã£ã³åº§æ¨ã«ç´ãã¦æ¯è¼ããã®ã§ãããã¨æãã¾ãã
ããããããã§ããããã¡ããã¡ãéå¹çã§ãã
(defun equal-point-affine (x y) (let ((a (affine x)) (b (affine y))) (and (eql (point2-x a) (point2-x b)) (eql (point2-y a) (point2-y b)))))
6.4. æ¼ç®ã®ç¢ºèª
addition, doubling, multipleã®ç¢ºèªãè¡ãã¾ãããã
確èªããå
容ã¯æ¬¡ã®ã¨ããã
- addtion ãã§ãããã¨ããã§ãã¯
- addtion ãã§ãããã¨ããã§ãã¯
- doubling ãã§ãããã¨ããã§ãã¯
- doubling ã«additionãå®è¡ãã¦ãã§ãã¯
- addtion ãæ²ç·ä¸ã«ããããã§ãã¯
- doubling ãæ²ç·ä¸ã«ããããã§ãã¯
- multiple ãæ²ç·ä¸ã«ããããã§ãã¯
- ä¸è¨3ã¤ã®åº§æ¨ãçãããã確èª
ã¹ã«ã©ã¼åãã§ããã¨ããã¾ã§ç¢ºèªãã¾ãããã
- multiple ã¨ãã®ç¢ºèª
- multiple ã¨ãã®ç¢ºèª
- ã®ç¢ºèª
- ã®ç¢ºèª
- ã®ç¢ºèª
ããã¾ã§åºæ¥ãããã²ã¨ã¾ãã¯åé¡ãªãã§ãããã
ããåé¡ããã£ãå ´åã¯ãç½²åã®ãã¨ã®æ¤è¨¼ã®æã«å¼ã£ãããã¨æãã¾ãã
ãã¨ã座æ¨ã®ç¢ºèªã«æ¬¡ã®ã³ã¼ãã使ãã¾ãã
(defun print-point (v &optional s) (let ((s (or s t))) (format s "X: ~X~%" (point3-x v)) (format s "Y: ~X~%" (point3-y v)) (format s "Z: ~X~%" (point3-z v)) (format s " -> Valid: ~X~%" (valid v))))
ãã 座æ¨ãæ¨æºåºåã«åºãã ãã§ãã
6.4.1. ã®ç¢ºèª
ä¸è¨2ã¤ã¯ãä½äºããªãOKã§ããï¼çµæã¯çç¥ï¼ã
- addtion ãã§ãããã¨ããã§ãã¯
- addtion ãã§ãããã¨ããã§ãã¯
ãã ãè¦ãã¦ãããªããã°ãããªããã¨ãããã
ã«å¯¾ãã¦valid
ã®è¿å´å¤ãç°ãªã£ã¦ãã¾ãã
- secp256k1ã§ã¯ã
(valid O)
ã¯nil
- secp256r1ã§ã¯ã
(valid O)
ã¯nil
- ed25519ã§ã¯ã
(valid O)
ã¯t
- ed448ã§ã¯ã
(valid O)
ã¯t
ã¡ããã¨çç±ããã£ã¦ãã¢ãã£ã³åº§æ¨ã«å¤æããã¨
- secp256k1ã§ã¯ã
- secp256r1ã§ã¯ã
- ed25519ã§ã¯ã
- ed448ã§ã¯ã
ã«ãªãã®ã§ããããæ²ç·ä¸ã«ãããã©ãããå¤å®ãã¦ããã ãã§ãã
ãã®çµæã¯è¦ãã¦ããã¦ãã ããã
ä¸è¨ã«ã¤ãã¦
- doubling ãã§ãããã¨ããã§ãã¯
- doubling ã«additionãå®è¡ãã¦ãã§ãã¯
secp256k1, secp256r1ã¯ã
ã®ããã«Xã«ä½ããå
¥ã£ã¦ããã¨ãã
doubling
ãåãã¨Yã«ä½ããã®å¤ãå
¥ãã¾ãã
å 容ãåºåãã¦ã¿ã¾ãã
X: 0 Y: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFF56F Z: 0 -> Valid: NIL X: 0 Y: FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFF93F Z: 0 -> Valid: NIL
ããããªã®ã§ãããã¯åé¡ãªãã
ed25519, ed448ã¯ãããããå¤ãå¤åãã¾ãã
X: 0 Y: 7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC Z: 7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC -> Valid: T X: 0 Y: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE Z: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE -> Valid: T
ããã¯æ£ããã®ã§ããããã
ã¢ãã£ã³åº§æ¨ã¯æ£ãããã§ãããå ç®ããã°ã©ããªãããã£ã¦ã¿ã¾ãã
確èªããã³ã¼ãã¯æ¬¡ã®éãã
(defun check-equal2 () (let* ((g *elliptic-g*) (o (doubling (make-point4 0 1 1 0))) (r (addition g o))) (print-point r) (format t "equal: ~A~%" (equal-point g r))))
å®è¡çµæã¯ä¸è¨ã®éãã
X: 5A4DB4F35B94FFB029388C7F75B7171A4B31D8254969ECB2558B5823C97547B Y: 1999999999999999999999999999999999999999999999999999999999999999 Z: 4 -> Valid: T equal: T X: 4F1970C66BED0DED221D15A622BF36DA9E146570470F1767EA6DE324A3D3A46412AE1AF72AB66511433B80E18B00938E2626A82BC70CC05E Y: 693F46716EB6BC248876203756C9C7624BEA73736CA3984087789C1E05A0C2D73AD3FF1CE67C39C4FDBD132C4ED7C8AD9808795BF230FA14 Z: 1 -> Valid: T equal: T
åé¡ãªãããã§ããã
6.4.1. ã®ç¢ºèª
次ã®ãããªç¢ºèªã®ã³ã¼ããä½ãã¾ããã
(defun check-2g () (let* ((g *elliptic-g*) (x (addition g g)) (y (doubling g)) (z (multiple 2 g))) (format t "Check: ~A, ~A, ~A, ~A, ~A, ~A~%" (valid g) (valid x) (valid y) (valid z) (equal-point2 x y) (equal-point2 x z)))) (format t "~&secp256k1: ") (with-elliptic-secp256k1 (check-2g)) (format t "~&secp256r1: ") (with-elliptic-secp256r1 (check-2g)) (format t "~&ed25519 : ") (with-elliptic-ed25519 (check-2g)) (format t "~&ed448 : ") (with-elliptic-ed448 (check-2g))
å®è¡çµæã¯æ¬¡ã®ã¨ããã
secp256k1: Check: T, T, T, T, T, T secp256r1: Check: T, T, T, T, T, T ed25519 : Check: T, T, T, T, T, T ed448 : Check: T, T, T, T, T, T
6.4.2. ã®ç¢ºèª
次ã®ãããªç¢ºèªã®ã³ã¼ããä½ãã¾ããã
(defun check-5g () (let* ((g *elliptic-g*) (x (addition (addition (addition (addition g g) g) g) g)) (y (addition (addition (doubling g) (doubling g)) g)) (z (multiple 5 g))) (format t "Check: ~A, ~A, ~A, ~A, ~A, ~A~%" (valid g) (valid x) (valid y) (valid z) (equal-point2 x y) (equal-point2 x z)))) (format t "~&secp256k1: ") (with-elliptic-secp256k1 (check-5g)) (format t "~&secp256r1: ") (with-elliptic-secp256r1 (check-5g)) (format t "~&ed25519 : ") (with-elliptic-ed25519 (check-5g)) (format t "~&ed448 : ") (with-elliptic-ed448 (check-5g))
å®è¡çµæã¯æ¬¡ã®ã¨ããã
secp256k1: Check: T, T, T, T, T, T secp256r1: Check: T, T, T, T, T, T ed25519 : Check: T, T, T, T, T, T ed448 : Check: T, T, T, T, T, T
6.4.1. ã®ç¢ºèª
ããã¯å°ã詳ããããã¾ãã
ã¾ãã¯åç´ã«ãæ±ãã¦ã¿ã¾ãã
(defun check-equal () (let* ((g *elliptic-g*) (n *elliptic-n*) (v (multiple n g))) (print-point v))) (with-elliptic-secp256k1 (check-equal)) (with-elliptic-secp256r1 (check-equal)) (with-elliptic-ed25519 (check-equal)) (with-elliptic-ed448 (check-equal))
å®è¡çµæã¯ä¸è¨ã®éãã
X: 0 Y: C0763AD39DC594D1509227D46650EEC24211044CC52DB049C500EF738489D9C6 Z: 0 -> Valid: NIL X: 0 Y: 5E2FFD6BB8888CC31BE98BAB3E36FBB12FCE891128CBFA8E9ADF5EBFF836DE1D Z: 0 -> Valid: NIL X: 0 Y: 7B28334BD670BFDB3E6C2C75B4D19ADA29D561FEB787C0ECA004B27857423FE5 Z: 7B28334BD670BFDB3E6C2C75B4D19ADA29D561FEB787C0ECA004B27857423FE5 -> Valid: T X: 0 Y: F75B3B69D1456D113231CD7C343FB1B95DD34B30558BDF5426A2BA922049929C833BEE409B5EC2163906BC7A835A51212C3CBB1DDD8FBF12 Z: F75B3B69D1456D113231CD7C343FB1B95DD34B30558BDF5426A2BA922049929C833BEE409B5EC2163906BC7A835A51212C3CBB1DDD8FBF12 -> Valid: T
ã¡ããã¨ããã¹ã¦ã®ãã¿ã¼ã³ã«ããã¦ãè¿å´ããã¦ãã¾ãã
ã§ã¯ããã®ã«ã足ãã¦ã¿ã¾ãã
(defun check-equal () (let* ((g *elliptic-g*) (n *elliptic-n*) (v (multiple n g)) (r (addition v g))) (format t "equal: ~A~%" (equal-point g r)))) (with-elliptic-secp256k1 (check-equal)) (with-elliptic-secp256r1 (check-equal)) (with-elliptic-ed25519 (check-equal)) (with-elliptic-ed448 (check-equal))
å®è¡çµæã¯ä¸è¨ã®éãã
equal: T equal: T equal: T equal: T
åé¡ãªãã§ãã
ã§ã¯æå¾ã«1000ã§åãããã®ãå®è¡ãã¾ãã
(defun check-equal () (let* ((g *elliptic-g*) (n *elliptic-n*) (v1 (multiple 1000 g)) (v2 (multiple (- n 1000) g)) (r (addition v1 v2))) (print-point r))) (with-elliptic-secp256k1 (check-equal)) (with-elliptic-secp256r1 (check-equal)) (with-elliptic-ed25519 (check-equal)) (with-elliptic-ed448 (check-equal))
çµæã¯åºãã¾ããããã¡ããã¨ã§ããã
ããã§ãaddition, doubling, multipleã¯åé¡ãªãããã§ãã
ç¶ãã¾ã
次ã¯ç§å¯éµã¨å
¬ééµã§ãã
Common Lispで楕円曲線DSAを実装する5(鍵生成) - nptclのブログ
Common Lispã§æ¥åæ²ç·DSAãå®è£ ãã3ï¼ä¹ç®ãªã©ï¼
ååã®ç¶ãããã§ãã
Common Lispで楕円曲線DSAを実装する2(加算) - nptclのブログ
5. ãã®ä»ã®æ¼ç®
次ã®æ¼ç®ãããã¾ãã
- ä¹ç®
- éæ°
- ã¢ãã£ã³åº§æ¨ã¸ã®å¤æ
- å¹³æ¹æ ¹
ãã¨ã§ãã£ã±ã使ãã¾ãã
5.1. ä¹ç®
ä¹ç®ãè¡ãã¾ãã
ä¹ç®ã¯æ¬å½ã«ããããªã¨ããã§ä½¿ãã¾ãã
ä¹ç®ã¨ã¯ã座æ¨ã®ã¹ã«ã©ã¼åã®äºã§ãã
ã¹ã«ã©ã¼ã¨ã¯åãªãæ´æ°ã§ãããä¹ç®ã®å¯¾è±¡ã¨ãªãã®ã¯åº§æ¨ã§ãã
ãã
ã¿ããã«ããããããã¾ãã
座æ¨ã¯ã座æ¨ãã¹ã«ã©ã¼å¤ã§ããåããå ´æã§ãã
ä¾ãã°ã®å ´åã¯ã
ã¨ãªãããã§ãã
ãã®æ¼ç®ã®é£ããç¹ã¨ãã¦ã¯ãã¹ã«ã©ã¼å¤ããã®ããã大ãããã¨ã«ããã¾ãã
RSAããã£ããã¨ããã人ãªãã
ã¿ãããªæ¼ç®ã«é ãæ©ã¾ãããã¨ãããããããã¾ããã
äºæ
ã¯å
¨ãåãã§ããã巨大ãªã¹ã«ã©ã¼å¤ãããããã¨ã«å¤å®ãã¦
doublingã¨addtionãç¹°ãè¿ãã¾ãã
5.1.1. ä¹ç®ã®å®è£ æ¹æ³
RFCã®ãµã³ãã«ã³ã¼ããè¦ã¾ãããã
ããã¯ed25519ã®ãã®ã§ãã
def point_mul(s, P): Q = (0, 1, 1, 0) # Neutral element while s > 0: if s & 1: Q = point_add(Q, P) P = point_add(P, P) s >>= 1 return Q
ãã®ã³ã¼ãã§æ³¨æããªããã°ãããªãã®ã¯æ¬¡ã®2ç¹ã§ã
point_add(P, P)
ã¯ãæ£ããè¨ç®ã§ããªãå ´åãããQ
ã®åæå¤ã¯ãæ²ç·ã®ç¨®é¡ã«åã£ãå¤ã«ãã
ã¾ãpoint_add(P, P)
ã®è¨ç®ãæ£ããè¡ãããªãã¨ãã¯ã
doublingãå¥éç¨æãã¦ãã ããã
ãã§ã«èª¬ææ¸ã¿ãªã®ã§åé¡ãªãã§ãããã
Q
ã®åæå¤ã¯ãå¿
ãåä½å
ã®ã«ããå¿
è¦ãããã¾ãã
ã¤ã¾ãã¯æ¬¡ã®ããã«ãªãã¾ãã
- secp256k1, secp256r1ã®ã¨ãã
- ed25519ã®ã¨ãã
- ed448ã®ã¨ãã
ãããå®ç¾ããããã«ãspecialå¤æ°ãæ°ãã«ç¨æãã¾ãã
(defvar *elliptic-o*)
æ²ç·ã«ãã£ã¦é©åãªå¤ãå²ãå½ã¦ã¦ãã ããã
let
ã«ããä¾ã示ãã¾ãã
;; secp256k1, secp256r1 (let ((*elliptic-o* (make-point3 0 0 0))) ...) ;; ed25519 (let ((*elliptic-o* (make-point4 0 1 1 0))) ...) ;; ed448 (let ((*elliptic-o* (make-point4 0 1 1))) ...)
æ²ç·ã«ãã£ã¦åæå¤ãå¤ããã®ãé¢åãªå ´åã¯ã«ãã ãããã
å
¨ãå¥ã®å¤ã«ããæ¹æ³ãããã¨ã¯æãã¾ãã
ä¾ãã°ã«nil
ãçªã£è¾¼ãã§ããã¦ã
point_add(Q, P)
ã次ã®ããã«ãã¾ãã
point_add(Q, P)
- ã
nil
ã®å ´åã¯ãè¿å´ - ããã§ãªããã°ãè¿å´
- ã
ãããããã®ããã«å®è£
ããå ´åã¯ã
å¼æ°ã®ã¹ã«ã©ã¼ãã®ã¨ããã¡ããã¨æ³å®ãã¦ãã ããã
ã®ã¨ããè¿å´ã¯ã§ãã
ã©ã®ããã«å®è£
ããã¨ããã§ãå¿
è¦ã«ãªãã¾ãã
5.1.2. ä¹ç®ã®å®è£
Common Lispã®å®è£ ã示ãã¾ãã
(defun multiple (s p &optional (q *elliptic-o*)) (if (< 0 s) (multiple (ash s -1) (doubling p) (if (logbitp 0 s) (addition q p) q)) q))
åèµ·å¼ã³åºãã®ä½ãã«ãªã£ã¦ãã¾ãã
æåã®å¼ã³åºãã§ã¯ãq
ã*elliptic-o*
ã§ãã®ã§ã
ãããã«ãªãã¾ãã
ä¹ç®ã®ãã¹ãã¯å¥ã®ç« ã§ããã¾ãã
5.2. éæ°
éæ°ã®æ±ãæ¹ã説æãã¾ãã
éæ°ã¯å°å½±åº§æ¨ããã¢ãã£ã³åº§æ¨ã«å¤æããã¨ãã«ç¨ãã¾ãã
ä¾ãã°æ¬¡ã®å°å½±åº§æ¨ããã£ãã¨ãã¾ãã
ãããã¢ãã£ã³åº§æ¨ã«å¤æããã¨ãã¯æ¬¡ã®è¨ç®ãè¡ãã¾ãã
ã¤ã¾ãã¯ã®éæ°ãæ±ããã®ã¡ã
ã¨ããä¹ç®ãè¨ç®ããã°ããããã§ãã
ããã§ã¯ã®éæ°ãæ±ãã¦ã¿ã¾ãããã
éæ°ã®è¨ç®ã¯ããã§ã«ãã¼ã®å°å®çãã次ã®å¼ãå°ãããã¨ã®ãã¨ã
ããã§ã¯ç´ æ°ã§ããå¿
è¦ãããã¾ãã
ããã¦ãã®å ´åãã¯æ²ç·ãã©ã¡ã¼ã¿ã¼ã®ç´ æ°ã«ãªãã¾ãã
éæ°ã¨ãã£ã¦ããæéä½ã§ã®åºæ¥äºãªã®ã§çµæã¯æ´æ°ã§ãã
å°æ°ã«ãªãããã§ã¯ããã¾ããã
ã¾ã¨ããã¨ãã¨ã¦ãç°¡åãªçµè«ã«ãªãã¾ãã
ã®éæ°ãæ±ããããªããã®ä¹ãè¨ç®ããã
5.2.2. éæ°ã®å®è£
ã¾ãã¯ãã¹ãä¹ã®è¨ç®ãå¿
è¦ã§ãã
RSAãªããã§ã¯ãã£ã¤ãè¨ç®ãã¦ãããã¤ã§ãã
次ã®å¼ã
ãæ±ããé¢æ°ãpower-mod
ã¨ããååã«ãã¾ãã
(defun power-mod (x y n &optional (r 1)) (if (< 0 y) (power-mod (mod (* x x) n) (ash y -1) n (if (logbitp 0 y) (mod (* r x) n) r)) r))
åèµ·å¼ã³åºãã«ããå®è£
ãã¦ãã¾ãã
ã¢ã«ã´ãªãºã ã¨ãã¦ã¯ä¹ç®ã¨åããªã®ã§ã
å½¢ãã¨ã¦ãããä¼¼ã¦ãã¾ãã
éæ°ãæ±ããã³ã¼ãã¯é常ã«ç°¡åã§ãã
ãã®é¢æ°ãinverse
ã¨ãã¾ãã
(defun inverse (x) (power-mod x (- *elliptic-p* 2) *elliptic-p*))
5.2.3. éæ°ã®ãã¹ã
ããã¤ãä¾ãåºãã¦ã¿ã¾ãã
(let ((*random-state* (make-random-state t))) (with-elliptic-secp256k1 (dotimes (i 4) (let* ((x (random *elliptic-p*)) (y (inverse x)) (z (inverse y))) (format t " x: ~X~%" x) (format t "inv1 x: ~X~%" y) (format t "inv2 x: ~X~%" z) (format t " equal: ~A~%" (= x z))))))
å®è¡ä¾ã¯ä¸è¨ã®éã
x: AF9064D272615B0B8F8D0207B623F34418F6B95F1929CE565625C45C311C4B10 inv1 x: 1CFE207D9EFBE7CF0B4D8826C51EB95627B1413E109B4599495CCDA19BE93D6 inv2 x: AF9064D272615B0B8F8D0207B623F34418F6B95F1929CE565625C45C311C4B10 equal: T x: D559DE25AEC052C9E0A53B78317D4C966C8790408B4607E44E3F97BDB733CC87 inv1 x: 89D7E9896239C480706CD1304E2E324631808624C673AC6E21F9BB77CEDB6B61 inv2 x: D559DE25AEC052C9E0A53B78317D4C966C8790408B4607E44E3F97BDB733CC87 equal: T x: B002513CEF087FC21A9F8C1CDECAEE691702A438293421AFF426D7744CD47566 inv1 x: FB9656792D4EF972D46144D1AB2489EC77D0C5CB9E7E6E3A7722401AD87A273F inv2 x: B002513CEF087FC21A9F8C1CDECAEE691702A438293421AFF426D7744CD47566 equal: T x: 735E87838D7F97172DE9DF17B81553C94BFA85235ED74D854F49D1899E32E775 inv1 x: 7DD810A7D8893A094B94E466D1417A60F2D1C8EB32A7BEF80DDE0FF0AEB00C8E inv2 x: 735E87838D7F97172DE9DF17B81553C94BFA85235ED74D854F49D1899E32E775 equal: T
5.2.4. ã¢ãã£ã³åº§æ¨ã¸ã®å¤æ
éæ°ã使ã£ã¦ã座æ¨å¤æããã¾ãããã
ããã§ã¯å°å½±åº§æ¨ããã¢ãã£ã³åº§æ¨ã¸ã®å¤æããã¾ãã
座æ¨ã次ã®ããã«ããããããã¨ãã¾ãã
ãããã¯æ¬¡ã®ããã§ãåé¡ããã¾ããã
å¤æãããã¨ã®ã¢ãã£ã³åº§æ¨ãã¨ããå ´åã次ã®ãããªé¢ä¿ãããã¾ãã
ã¤ã¾ãã¯ãã®éæ°ãè¨ç®ããã®ã¡ãä¹ç®ããã°ããã®ã§ãã
è¨ç®ã¯æ¬¡ã®ããã«ãªãã¾ãã
å®è£ ã¯æ¬¡ã®ããã«ãªãã¾ãã
(defun affine (v) (let ((z (inverse (point3-z v)))) (make-point2 (mulp (point3-x v) z) (mulp (point3-y v) z))))
5.3. å¹³æ¹æ ¹
å¹³æ¹æ ¹ã¯ãä¾ãã°åº§æ¨ã®ã ããããã£ã¦ããã¨ãã
ãæ±ããã¨ãã«ä½¿ãã¾ãã
å
·ä½çã«ã¯åº§æ¨ã®Decodeã®ã¨ãã«ä½¿ãã¾ãã
ã§ã¯ã®å¹³æ¹æ ¹ãæ±ãã¾ãããã
æ¹æ³ã¯å
¨é¨EdDSAã®RFCã«è¼ã£ã¦ãã¾ãã
ãã°ãããã
RFCã®æ å ±ã§ã¯ã次ã®2éãã®ç¶æ³ã§å¹³æ¹æ ¹ãæ±ãããã¨ãã§ãã¾ãã
ã§ã¯ãã£ããç´ æ°ãå²ã£ã¦ç¢ºèªãã¾ãã
ã¾ãã¯ã§å²ãã¾ãã
;; secp256k1 * (with-elliptic-secp256k1 (rem *elliptic-p* 4)) 3 ;; secp256r1 * (with-elliptic-secp256r1 (rem *elliptic-p* 4)) 3 ;; ed25519 * (with-elliptic-ed25519 (rem *elliptic-p* 4)) 1 ;; ed448 * (with-elliptic-ed448 (rem *elliptic-p* 4)) 3
次ã«ã§å²ãã¾ãã
;; secp256k1 * (with-elliptic-secp256k1 (rem *elliptic-p* 8)) 7 ;; secp256r1 * (with-elliptic-secp256r1 (rem *elliptic-p* 8)) 7 ;; ed25519 * (with-elliptic-ed25519 (rem *elliptic-p* 8)) 5 ;; ed448 * (with-elliptic-ed448 (rem *elliptic-p* 8)) 7
以ä¸ã«ããã次ã®ãããªçµæãå¾ããã¾ããã
-
- secp256k1
- secp256r1
- ed448
-
- ed25519
ããã§ã¯é çªã«ãã£ã¦ãããã¨æãã¾ãã
次ã®è¨ç®ã説æãã¾ãã
5.3.1. ã®ã¨ãã®å¹³æ¹æ ¹
è¨ç®æ¹æ³ã¯ããã«è¨è¼ããã¦ãã¾ãã
- 5.2. Ed448ph and Ed448
5.2.1. Modular Arithmetic
https://datatracker.ietf.org/doc/html/rfc8032
ãã¡ãã¯secp256k1, secp256r1ã§ã使ãã¾ãã
RFCã§è¦ãã¨ããã¯ãed25519ã§ã¯ãªãed448ã®æ¹ã§ãã®ã§æ³¨æã
ãã¨æ³¨æãã¦ã»ããã®ã§ããã
ããã«æ¸ããã¦ããæ¹æ³ã¯ed448ã§ã¯ä½¿ãã¾ããã
RFCã§ã¯ãçµå±å¥ã®æ¹æ³ã§å®è£
ãã¾ãã
ãã¶ãåèã¨ãã¦è¼ãã¦ããã¦ããã ã¨æãã¾ãã
ããã§ã¯æ°ãåãç´ãã¦ç¶ããè¡ãã¾ãã
ã¾ãã¯å¹³æ¹æ ¹ã®åè£ã¨ãã¦ã次ã®å¼ã§æ±ãã¾ãã
ç®åºããããè¨ç®ãã¦ãã ããã
次ã®å ´åã«ã¤ãã¦ã
å®è£ ããã¨ãããªæãã§ãã
(defun square-root-mod-4 (a) (let* ((x (power-mod a (/ (+ *elliptic-p* 1) 4) *elliptic-p*)) (x2 (mulp x x))) (if (= x2 a) x)))
å¹³æ¹æ ¹ããªãå ´åã¯ãnil
ãè¿å´ãã¾ãã
5.3.2. ã®ã¨ãã®å¹³æ¹æ ¹
æåã«è¨ã£ã¦ããã¨ãããã¯ä½¿ãã¾ããã
ed25519ã§ã¯ãå¥ã®æ¹æ³ã§ç®åºããããã«èª¬æãã¦ãã¾ãã
èå³ããªããªããã£é£ã°ãã¦ãã ããã
è¨ç®æ¹æ³ã¯ããã«è¨è¼ããã¦ãã¾ãã
- 5.1. Ed25519ph, Ed25519ctx, and Ed25519
5.1.1. Modular Arithmetic
https://datatracker.ietf.org/doc/html/rfc8032
ã¾ãã¯å¹³æ¹æ ¹ã®åè£ã¨ãã¦ã次ã®å¼ã§æ±ãã¾ãã
ç®åºããããè¨ç®ãã¦ãã ããã
ãã®å¤ã«ãã£ã¦ã次ã®3ã¤ã®å ´åãããã¾ãã
ãªãã¯ãã¨åãã§ãã
å®è£ ããã¨ãããªæãã§ãã
(defun square-root-mod-8 (a) (let* ((x (power-mod a (/ (+ *elliptic-p* 3) 8) *elliptic-p*)) (x2 (mulp x x))) (cond ((= x2 a) x) ((= x2 (- *elliptic-p* a)) (mulp x (power-mod 2 (/ (- *elliptic-p* 1) 4) *elliptic-p*))))))
å¹³æ¹æ ¹ããªãå ´åã¯ãnil
ãè¿å´ãã¾ãã
5.3.3. å®è£ ã®ãã¹ã
ä¹±æ°ã§é©å½ãªå¤ãæ±ãããæ±ãã¦ãã å®è£ ããã³ã¼ãã§å ã®ãæ±ãã¦ã¿ã¾ãã
ã¾ãã¯square-root-mod-4
ããã
(let ((*random-state* (make-random-state t))) (with-elliptic-secp256k1 (let* ((x (random *elliptic-p*)) (y (modp (- *elliptic-p* x))) (a (mulp x x)) (z (square-root-mod-4 a))) (format t "~X~%" x) (format t "~X~%" y) (format t "~X~%" z) (format t "equal: ~A~%" (or (= x z) (= y z))))))
ä¹±æ°ã®å¤ããã¤ãã¹ã§ãããã¨ãèæ
®ãã¦ãã ããã
ãã¤ãã¹ã®æã¯ãäºä¹ããçµæãã©ã¹ã«ãªãã®ã§ã
ãã®å¹³æ¹æ ¹ã§ã¯æåã®å¤ã¨ä¸è´ãã¾ããã
ä¸è¨ã®å¤å®ã§ã¯ãæ£ã¨è² ã®ä¸¡æ¹ã®å ´åãèæ
®ãã¦ãã¾ãã
æ£ã®å ´åã®å®è¡ä¾ã示ãã¾ãã
3475A6298CF6C10E3586EE8A12BCD0F584F1579AFE4D53351DE19DE9D5300572 CB8A59D673093EF1CA791175ED432F0A7B0EA86501B2ACCAE21E62152ACFF6BD 3475A6298CF6C10E3586EE8A12BCD0F584F1579AFE4D53351DE19DE9D5300572 equal: T
è² ã®å ´åã®å®è¡ä¾ã示ãã¾ãã
617559E463B7B0B19743D53F53546966723BFBBD96BB07544C21102307F3B116 9E8AA61B9C484F4E68BC2AC0ACAB96998DC404426944F8ABB3DEEFDBF80C4B19 9E8AA61B9C484F4E68BC2AC0ACAB96998DC404426944F8ABB3DEEFDBF80C4B19 equal: T
square-root-mod-8
ãåãã§ãã
(let ((*random-state* (make-random-state t))) (with-elliptic-ed25519 (let* ((x (random *elliptic-p*)) (y (modp (- *elliptic-p* x))) (a (mulp x x)) (z (square-root-mod-8 a))) (format t "~X~%" x) (format t "~X~%" y) (format t "~X~%" z) (format t "equal: ~A~%" (or (= x z) (= y z))))))
æ£ã®å ´åã®å®è¡ä¾ã示ãã¾ãã
73EC17B0C2C37B013F0793E1C0BDC116118231F773262F18FC045981F57BFF2A C13E84F3D3C84FEC0F86C1E3F423EE9EE7DCE088CD9D0E703FBA67E0A8400C3 73EC17B0C2C37B013F0793E1C0BDC116118231F773262F18FC045981F57BFF2A equal: T
è² ã®å ´åã®å®è¡ä¾ã示ãã¾ãã
518EB5A3821819110B19C0925EC335DFEDE8F70F8040A8715B83AFA42D8EC2B3 2E714A5C7DE7E6EEF4E63F6DA13CCA20121708F07FBF578EA47C505BD2713D3A 2E714A5C7DE7E6EEF4E63F6DA13CCA20121708F07FBF578EA47C505BD2713D3A equal: T
5.3.4. 符å·ã«ã¤ãã¦
ä»å座æ¨ãæ±ããããã«å¹³æ¹æ ¹ãç®åºããã®ã§ããã
符å·ã ãã¯æ£ããç®åºã§ãã¾ããã
ããã§ç¬¦å·ã®æ
å ±ã ãå¥ã§ä¿åãã¦ããããã¨ã§å¾©å¸°ããã¨ããæ¹æ³ãè¡ããã¾ãã
ä¾ãã°ã¢ãã£ã³åº§æ¨ã®ã ããä¿åããã¨ãã¾ãã
ãã®å ´åã§ããã®ç¬¦å·ã ãã¯å¥éä¿åãã¦ãã ããã
ãã®å ´åã符å·æ
å ±ã¯ã®æä¸ä½ãããã§ãï¼æä¸ä½ã§ã¯ãªãï¼ã
ãããããä¿åãã¦ããã符å·æ
å ±ãã¨ãã¾ãã
ã«ã¯ããå
¥ã£ã¦ãã¾ãã
ãã¾ãä½ããã®å¤ã®å¹³æ¹æ ¹ãç¨ãã¦ã仮決ãã®ãå¾ãã¨ãã¾ãã
(setq y (square-root-mod-4 a))
ãã®ç¶æ
ã§ã¯ãã®ç¬¦å·ããã£ã¦ããªãå¯è½æ§ãããã¾ãã
ããã§ã仮決ãã®ã®æä¸ä½ãããã¨ãçããã確èªãã¾ãã
çãããªãå ´åã¯ç¬¦å·ãå転ããã¾ãã
(when (/= (logand y #x01) y0) (setq y (- *elliptic-p* y)))
ããã§ã符å·ãå«ãã¦ãæ£ãããæ±ãããã¨ãã§ãã¾ããã
5.3.5. å¹³æ¹æ ¹ã®æ±ãæ¹ã«ã¤ãã¦
ãå¹³æ¹å°ä½ãã¨ãããããã§ãã
ããã¾ããã«ãããããªãå¹³æ¹å°ä½ã§èª¿ã¹ã¦ãã ããã
æ±ãæ¹ã¯æ¬¡ã®ãµã¤ããåèã«ãªãã¾ãã
bigintegerï¼ä»»æ精度æ´æ°ï¼ã§elliptic curve/secp256k1ï¼æ¥åæ²ç·ï¼
å¹³æ¹æ ¹ï¼square rootï¼
https://qiita.com/tnakagawa/items/c0396c294eaf67f55fdcå¹³æ¹å°ä½
ç´ æ°ã¾ãã¯ç´ æ°ã¹ãä¹ã®æ³
https://ja.wikipedia.org/wiki/%E5%B9%B3%E6%96%B9%E5%89%B0%E4%BD%99Quadratic residue
Prime or prime power modulus
https://en.wikipedia.org/wiki/Quadratic_residue
å¹³æ¹æ ¹ã®åºãæ¹ã¯secg.orgã®PDFã«ã¯è¼ã£ã¦ã¾ããã§ããã
æ¢ãæ¹ãæªãã£ãã ãã§ããã®ããããã¾ããã
å¶ç¶EdDSAã®RFCã«è¼ã£ã¦ããã¤ã§ä»£ç¨ã§ããã®ã§ããã£ãã§ãã
ç¶ãã¾ã
次ã¯ããã¾ã§ã®ãã¹ãã§ãã
Common Lispで楕円曲線DSAを実装する4(確認) - nptclのブログ
Common Lispã§æ¥åæ²ç·DSAãå®è£ ãã2ï¼å ç®ï¼
ååã®ç¶ãããã§ãã
Common Lispで楕円曲線DSAを実装する1 - nptclのブログ
4. å ç®
æ¥åæ²ç·æå·ã®å ç®ãè¡ãã¾ãã
ãªãã§ãããªãå ç®ãªãã ã¨ä¸æè°ã«æãæ¹ã«ã¯ç³ã訳ããã¾ããã
èªåã§ã¯èª¬æã§ãã¾ããã
ããããªãã¦ãããã£ã¦ããã°å¾ã§ä½¿ãã¾ãã
ç¹ã¨ç¹ã®å ç®ã次ã®ããã«è¡¨ãã¾ãã
ã¤ã¾ããããè¨ç®ãã¾ãã
åãªããã¯ãã«ã®æ¼ç®ã§ã¯ãªãã®ã§æ³¨æãã¦ãã ããã
å ç®ã®æ¼ç®å
容ã¯ä½¿ãæ²ç·ã«ãã£ã¦å¤ããã¾ãã
ä¸è¨4ã¤ã¯ããããéã£ãå½¢ã®æ²ç·ã§ãã®ã§ã²ã¨ã¤ãã¤è¦ã¦ããã¾ãããã
ä¸å¿è¨ã£ã¦ããã¾ãããå
¨é¨æ´æ°ã§è¨ç®ãã¾ãã
æµ®åå°æ°ã¯åºã¦ãã¾ããã
4.1. å ç®ã®è¨ç®æ¹æ³
便å©ãªä¸ã®ä¸ã«ãªã£ããã®ã§ã
ed25519ã¨ed448ã«ã¤ãã¦ã¯ã
RFCã«è©³ããè¨è¼ããã¦ãã¾ããã
ããä½ãèããã«ä¸¸ãã¨è¦ã¦ãã¾ãã¾ãããã
RFC8032
Edwards-Curve Digital Signature Algorithm (EdDSA)
https://datatracker.ietf.org/doc/html/rfc8032
ãã®ã5.1.4. Point Additionãã«
å ç®ã®ãã¹ã¦ãæ¸ããã¦ãã¾ãã
å¼ã使ããã¨ããªãã¨ãã£ãã®ã¯ã
ãã§ã«RFCã«æé ãè¼ã£ã¦ããããã§ãã
ãã è¦ãã¦ãããªããã°ãããªããã¨ãããã¾ãã
5.1.4.ãè¦ãã¨ãããªã座æ¨å¤æã®è©±ããå§ã¾ã£ã¦ãã¾ãã
ãextended homogeneous coordinatesãã ããã§ãã
æ¤ç´¢ããã¨ãæ¡å¼µãããå次座æ¨ç³»ãã¨åºã¦ãã¾ããã
ä»ã®äººãã¡ã®ãµã¤ããè¦ãéãã ã¨ãå次座æ¨ç³»ãã§ã¯ãªã
ãå°å½±åº§æ¨ (projective coordinates)ãã¨å¼ãã§ãã¾ããã
ããããã¯ãæ¡å¼µãããå°å½±åº§æ¨ãã§ããã®ãã¨æãã¾ãã
ããã¯ä½è
ãã¨ããã¨ãä»ã¾ã§åº§æ¨ã«ã¨ãç¨ãã¦ãã¾ãããã
ãããã®4ã¤ã«ãã¾ãããã¨ãã話ã§ãã
åãªãå°å½±åº§æ¨ã ã¨ã®3ã¤ã ã£ãã¨æãã®ã§ã
æ¡å¼µããã¦4ã¤ã«ãªã£ããã§ããããã
ä»ã¾ã§ã®ã¯ã¢ãã£ã³åº§æ¨ã¨ãããããã§ãã
対å¿é¢ä¿ã¯ä¸è¨ã®éãã
ã®ã¨ãã¢ãã£ã³åº§æ¨ãã®ãã®ã§ãããã®ã¨ãã¯ç¡éé ç¹ã«ãªããã ããã§ãã
ã¢ãã£ã³åº§æ¨ããå°å½±åº§æ¨ã¸ã¯
ã«ãªãã¾ãã
éã«ãã¢ãã£ã³åº§æ¨ã«ããã¨ãã¯ã
ãã§ã¯ãªãã¨ãã§ãã
ä½ã®ããã«ãããªãã¨ããã®ãã¨ããã¨ãæ¼ç®ã®æé©åã®ããã§ãã
ãã¡ãæéããããä¹ç®ãé¤ç®ã®å¦çãæ¸ããã¦ã
å¯è½ãªéãå ç®ã«åããã¨ãã工夫ãããã§ãã
ã§ãä»ã¯ãããªãã¨ãããRFCã§å
¬éããã¦ããæé ã«å¾ã£ã¦
楽ã«å ç®ãå®ç¾ããããã¨ããæãããããã¾ããã
å ç®ã¯æ¬¡ã®ããã«ãªãã¨æ¸ãã¦ãã¾ãã
A = (Y1-X1)*(Y2-X2) B = (Y1+X1)*(Y2+X2) C = T1*2*d*T2 D = Z1*2*Z2 E = B-A F = D-C G = D+C H = B+A X3 = E*F Y3 = G*H T3 = E*H Z3 = F*G
ãã¡ãã¯ed25519ã®å ç®ã§ãã
ãããªã®ãããããè¨ç®ããã ãã§ã
ãå¾ãããã¨ãããã¨ã«ãªãã¾ãã
é¢ç½ãã§ããã
ä»èª¬æããã®ããaddingãã¾ãã¯ãadditionãã¨å¼ã°ãããã®ã§ãã
ãã®ã»ãã«ãç¹ã2åãããdoublingããç´¹ä»ããã¦ãã¾ãã
doublingã¯ã¹ã«ã©ã¼ä¹ç®ã§ä½¿ãã¾ãã
5.2.4.ã§ã¯ed448ã®å ç®ãç´¹ä»ããã¦ãã¾ãã
ed448ããsecp256k1, secp256r1ã¯ã
æ®éã®å°å½±åº§æ¨ã§ãããã¨ã«æ³¨æãã¦ãã ããã
ããã«6. Ed25519 Python Illustrationã§ã¯ã
ed25519ã®å®è£
ä¾ãè¦ããã¨ãã§ãã¾ãã
ãã¯ã欲ãããã®ã¯å
¨é¨æã«å
¥ãã¾ããã
å ç®ä»¥å¤ã«ãèããªããã°ãããªããã¨ã¯ããã®ã§ããã
ãã®RFCã«å
¨é¨è¼ã£ã¦ãã¾ãã
æ¬å½ã«ç´ æ´ãããã
ãã ããEdDSAã ãã§ãã
èªåã調ã¹ãéãã ã¨ãECDSAã«ã¤ãã¦ã¯ããã¾ã§è©³ããæ¸ããã
RFCã¯ããã¾ããã§ããã
æ¬å½ã«ãªãã®ããªã
ECDSAãã¼ã¸ã§ã³ã¯èªåã§ä½ããããªãããã§ãã
ããããããã¨ã«æé ãå
¬éãã¦ãããµã¤ããããã¾ãã
Explicit-Formulas Database
https://hyperelliptic.org/EFD
ãã®ãã¼ã¸ã®ãTwisted Edwards curves: a*x2+y2=1+d*x2*y2
ããã
ãã¾è¦ã¦ããæ
å ±ã§ãã
ä¸è¨ã®ãã¼ã¸ã«å
¨ãåãè¨ç®ãè¼ã£ã¦ãã¾ãã
https://hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
ECDSAã®å ´åã¯ãShort Weierstrass curves: y2=x3+a*x+b
ããé¸ãã§ãã ããã
ãã®ä¸ã«å ´ååãããããProjective coordinatesãã
ããã¤ãç´¹ä»ããã¦ããã®ã§ã
ããã«å¾ã£ã¦ä½æã§ããã¯ãã§ãã
ãã¨ã§èª¬æãã¾ãã
ã¾ãã¯æ
å ±ããããã®ããä½ãã¾ãããã
ed25519ããè¡ãã¾ãã
4.2. å ç®ã®å®è£
ã¾ãã¯ãµã³ãã«ãè¦ã¾ãããã
Pythonã«ããå®è£
ã次ã®ããã«ãªãã¾ãã
def point_add(P, Q): A, B = (P[1]-P[0]) * (Q[1]-Q[0]) % p, (P[1]+P[0]) * (Q[1]+Q[0]) % p; C, D = 2 * P[3] * Q[3] * d % p, 2 * P[2] * Q[2] % p; E, F, G, H = B-A, D-C, D+C, B+A; return (E*F, G*H, F*G, E*H);
ãããä¸åã«ä¸¦ã³å¤ãã¾ãã
def point_add(P, Q): A = (P[1]-P[0]) * (Q[1]-Q[0]) % p; B = (P[1]+P[0]) * (Q[1]+Q[0]) % p; C = 2 * P[3] * Q[3] * d % p; D = 2 * P[2] * Q[2] % p; E = B-A; F = D-C; G = D+C; H = B+A; return (E*F, G*H, F*G, E*H);
ãã¨ã¯ãããCè¨èªããã«å°éã«å¤æãã¦ããã°ããããã§ãã
ãããã®æ¼ç®ã¯ãæéä½ã¨å¼ã°ããç¶æ³ä¸ã§è¡ããã¾ãã
Pythonã¯è¶
é«ç´ãªè¨èªãªã®ã§ã
æãç®ã¨ããé£ç¶ã§è¡ã£ã¦ãé©åã«ã¡ã¢ãªç¢ºä¿ãã¦ããã¾ããã
ã¡ããã©ããã¿ã¤ãã³ã°ã«ãªã£ãã
ç´ æ°ã§å²ãç®ãã¦ãã¾ãã«ãã¦ããã¾ãããã
Cè¨èªã ã¨ãã©ã®ã¿ã¤ãã³ã°ã§ãã¾ããæ±ããã°ããã®ãã
çµæ§çå£ã«èãããã¨ã«ãªãã¾ãã
以éã§ã¯ã使ç¨ããæ¼ç®ãåæãã¦ãããã¨æãã¾ãã
çµå±ã¯å
¨é¨ä¸è¨ã®ãã¼ã¸ã®ãã®ã§ãã
Explicit-Formulas Database
https://www.hyperelliptic.org/EFD/index.html
åæããå 容ã¯æ¬¡ã®éãã
- secp256k1
- secp256r1
- ed25519
- ed448
ããããã«å¯¾ãã¦ã®æ¬¡ã®æ¼ç®æ¹æ³ã§ãã
- addition
- doubling
dounlingã¯ãadditionã§ä»£ç¨ã§ããå¯è½æ§ãããã¾ãã
æå¾ã«å®è£
ã®æ³¨æç¹ãã¾ã¨ãã¾ãã
ãã¨ã座æ¨ç³»ã«æ³¨æãã¦ãã ããã
å°å½±åº§æ¨ã¯æ¬¡ã®ãã®ã該å½ãã¾ãã
- secp256k1
- secp256r1
- ed448
æ¡å¼µãããå°å½±åº§æ¨ã¯æ¬¡ã®ãã®ã該å½ãã¾ãã
- ed25519
4.3. addition
èªåã使ç¨ããå®è£
ãåæãã¾ãã
æå¾ã«æ³¨æç¹ãã¾ã¨ãã¾ãã®ã§ã確èªãã ããã
4.3.1. secp256k1, secp256r1
使ç¨ãã座æ¨ã¯ãå°å½±åº§æ¨ã§ãã
Projective coordinates for short Weierstrass curves
https://hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#addition-add-2007-bl
U1 = X1*Z2 U2 = X2*Z1 S1 = Y1*Z2 S2 = Y2*Z1 ZZ = Z1*Z2 T = U1+U2 TT = T^2 M = S1+S2 R = TT-U1*U2+a*ZZ^2 F = ZZ*M L = M*F LL = L^2 G = (T+L)^2-TT-LL W = 2*R^2-G X3 = 2*F*W Y3 = R*(G-2*W)-2*LL Z3 = 4*F*F^2
4.3.2. ed25519
使ç¨ãã座æ¨ã¯ãæ¡å¼µãããå°å½±åº§æ¨ã§ãã
Extended coordinates with a=-1 for twisted Edwards curves
https://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#addition-add-2008-hwcd-3
RFC8032
https://datatracker.ietf.org/doc/html/rfc8032
A = (Y1-X1)*(Y2-X2) B = (Y1+X1)*(Y2+X2) C = T1*2*d*T2 D = Z1*2*Z2 E = B-A F = D-C G = D+C H = B+A X3 = E*F Y3 = G*H T3 = E*H Z3 = F*G
T3
ã¨Z3
ã®é çªã«æ³¨æã
4.3.3. ed448
使ç¨ãã座æ¨ã¯ãå°å½±åº§æ¨ã§ãã
Projective coordinates for Edwards curves
https://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#addition-add-2007-bl
RFC8032
https://datatracker.ietf.org/doc/html/rfc8032
A = Z1*Z2 B = A^2 C = X1*X2 D = Y1*Y2 E = d*C*D F = B-E G = B+E H = (X1+Y1)*(X2+Y2) X3 = A*F*(H-C-D) Y3 = A*G*(D-C) Z3 = F*G
4.4. doubling
èªåã使ç¨ããå®è£
ãåæãã¾ãã
æå¾ã«æ³¨æç¹ãã¾ã¨ãã¾ãã®ã§ã確èªãã ããã
4.4.1. secp256k1, secp256r1
Projective coordinates for short Weierstrass curves
https://hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#doubling-dbl-2007-bl
XX = X1^2 ZZ = Z1^2 w = a*ZZ+3*XX s = 2*Y1*Z1 ss = s^2 sss = s*ss R = Y1*s RR = R^2 B = (X1+R)^2-XX-RR h = w^2-2*B X3 = h*s Y3 = w*(B-h)-2*RR Z3 = sss
4.4.2. ed25519
Extended coordinates with a=-1 for twisted Edwards curves
https://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#doubling-dbl-2008-hwcd
RFC8032
https://datatracker.ietf.org/doc/html/rfc8032
A = X1^2 B = Y1^2 C = 2*Z1^2 H = A+B E = H-(X1+Y1)^2 G = A-B F = C+G X3 = E*F Y3 = G*H T3 = E*H Z3 = F*G
T3
ã¨Z3
ã®é çªã«æ³¨æã
4.4.3. ed448
Projective coordinates for Edwards curves
https://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#doubling-dbl-2007-bl
RFC8032
https://datatracker.ietf.org/doc/html/rfc8032
B = (X1+Y1)^2 C = X1^2 D = Y1^2 E = C+D H = Z1^2 J = E-2*H X3 = (B-E)*J Y3 = E*(C-D) Z3 = E*J
4.4. å®è£ ã®æ³¨æç¹
ããããã¨æ³¨æããªããã°ãããªããã¨ãããã¾ãã
ããèªåã§å®è£
ãããªãããã®ç« ã®å
容ãè¦ãã¦ãããæ¹ãããã¨æãã¾ãã
話é¡ã¯æ¬¡ã®éãã
- doublingãadditionã§ä»£ç¨ããã¨ãã®æ³¨æ
- ç¡éé ç¹ã®å ç®
- å ç®ã®å®è£ ä¾
4.4.1. doublingãadditionã§ä»£ç¨ããã¨ãã®æ³¨æ
doublingã¯addtionã§ä»£ç¨ã§ããããããã¾ããã
doublingã¨ã¯ã¤ã¾ãã
ã®äºãªã®ã§ã
P = doubling(Q) = addtion(Q, Q)
ã§è¨ç®ã§ããå ´åãããã¾ãã
ãªãã§æè¨ããªããã¨ããã¨ãã§ããªãå ´åãããããã§ãã
RFCã«è¼ã£ã¦ãããed25519ã¨ed448ã¯ä»£ç¨ã§ãã¾ãã
ããããdoublingãè¼ãã¦ããã¦ãã¾ããã
é¢åãªãadditionã ãã§å¤§ä¸å¤«ã§ãã
åé¡ã¯secp256k1ã¨secp256r1ãªã®ã§ãã
èªåã¯ãä¸è¨ã®ãµã¤ããã好ããªã®ãé¸ãã§å®è£
ãã¦ãã¾ããã
Projective coordinates for short Weierstrass curves
https://hyperelliptic.org/EFD/g1p/auto-shortw-projective.html
ã¨ããããä¸è¨ã®ãã®
https://hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#addition-add-1998-cmo-2
ã¯ãdoublingã®ä»£ç¨ãã§ãã¾ããã§ããã
ã¤ã¾ã次ã®è¨ç®ã¯ã§ãã¾ããã
addition(P, P)
çµæã¯ãå ¨é¨ã«ãªãã¾ãã
å
ã®è«æãè¦ãããã§ã¯ãªãã®ã§ããããè¯ãã®ãæªãã®ãå
¨ç¶ãããã¾ããã
ãã®ãããªå ´åã¯ãå ç®ãããã¾ãã«äºã¤ã®åº§æ¨ã確èªããå¿
è¦ãããããã§ãã
ããåé¡ãããããªå ´åãdoublingã«åãæ¿ãããããªå®è£
ã«ãªãã¨æãã¾ãã
èªåã«ã¯é£ããããã®ã§ãéãå¼ã§ç®åºãããã¨ã«ãã¾ããã
additionã®ã³ã¼ãã¯ããããããã¾ããã
å®éã«ä½¿ã£ã¦ã¿ãªãéãããããªããã¨ãããã¨æãã¾ãã
èªåã§å®è£
ããå ´åã¯ãããããä½ã£ã¦è©¦ãã¦ã¿ã¦ãã ããã
4.4.2. ç¡éé ç¹ã®å ç®
ç¡éé ç¹ã®å ç®ã¯ãã¡ããã¨èããªããã°ããã¾ããã
ããã¦ãã§ããããã«ããªããã°ãªãã¾ããã
ã¾ãã¯ããã£ããä½ãç¡éé ç¹ãªã®ããã¾ã¨ãã¾ãã
ããããã¯æ¬¡ã®ãããªå¤å®ã«ãªãã¨æãã¾ãã
- secp256k1, secp256r1
- ãªã
- ed25519
- ã¢ãã£ã³åº§æ¨ãªããä¾ãã°å°å½±åº§æ¨
- ed448
- ã¢ãã£ã³åº§æ¨ãªããä¾ãã°å°å½±åº§æ¨
ed25519ã¨ed448ã¯å
·ä½çãªåº§æ¨ãããã
ãããä¸ç«å
(neutral point)ã¨ãããããã§ãã
ããããããä¸ç«å
ã¨ã¯åä¸ã§ã¯ãªãã®ããããã¾ãããã
å®å
¨ã«ã¨ãã¦æ©è½ãã¦ãã¾ãã
ãã®è¾ºãåå¼·ä¸è¶³ã§ãããããã¾ããã
次ã®2ã¤ã®æ§è³ªãæã£ã¦ãã¾ãã
ã¨ãããã¨ãªã®ã§ãed25519ã¨ed448ãªãç¡éé ç¹ã®å ç®ã¯ã
ä¸ç«å
ããã¾ããã¨ãã£ã¦ãããã®ã§åé¡ããã¾ããã
ãããsecp256k1ã¨secp256r1ã¯ããã§ã¯ããã¾ããã
ã¡ããã¨èããªãã¨ã§ãã¾ããã
æ®éã«ãã®ã¾ã¾å ç®ããã¨ãå¤ãªå¤ã«ãªãã¾ãã
secp256k1ã¨secp256r1ã®å ´åã¯æ¬¡ã®ããã«å ´ååãããã¾ãã
- 次ã®æé ã§ãå®è¡ãã
- ã®ããªãããè¿å´
- ã®ããªãããè¿å´
- ãã以å¤ãªããadditionã®ã³ã¼ãã§è¨ç®ãã
ä¸è¨ã®ããã«ã«ããå ´ååãã®å®è£
ã¯ã
ã©ããå¿
ããããªããã°ãããªãããã§ãã
次ã«å®è£
ã®ä¾ã示ãã¾ãã
4.4.3. å ç®ã®å®è£ ä¾
å ´ååããå¿ è¦ãªãsecp256k1ã®additionã«ã¤ãã¦ç¤ºãã¾ãã
(defun addition-weierstrass (p1 p2) (let* ((x1 (point3-x p1)) (y1 (point3-y p1)) (z1 (point3-z p1)) (x2 (point3-x p2)) (y2 (point3-y p2)) (z2 (point3-z p2)) (u1 (* x1 z2)) (u2 (* x2 z1)) (s1 (* y1 z2)) (s2 (* y2 z1)) (zz (* z1 z2)) (t1 (+ u1 u2)) (t2 (* t1 t1)) (m (+ s1 s2)) (r (+ (- t2 (* u1 u2)) (* *elliptic-a* zz zz))) (f (* zz m)) (k1 (* m f)) (k2 (* k1 k1)) (g1 (+ t1 k1)) (g2 (- (* g1 g1) t2 k2)) (w (- (* 2 r r) g2))) (make-point3 (modp (* 2 f w)) (modp (- (* r (- g2 (* 2 w))) (* 2 k2))) (modp (* 4 f f f))))) (defun addition-secp256k1 (p1 p2) (cond ((zerop (point3-z p1)) p2) ((zerop (point3-z p2)) p1) (t (addition-weierstrass p1 p2))))
addition-weierstrass
ã ãã ã¨ã
ç¡éé ç¹ã®å ç®ã®æã«å¤±æãã¾ãã
ããã§ã®å¤ãè¦ã¦å ´ååãããããã¨ã§å¯¾å¦ãã¾ãã
ãããããªãã¨ãå ç®ãä¹ç®ã®æä¸ã«å¶ç¶ã«ãã«ããã£ã¦ãã¾ã£ãå ´åã
æ£ããç®åºã§ãã¾ããã
å
·ä½çãªçç¶ã¨ãã¦ã¯ãå¾ã
ã®è©±ã«ãªãã¾ãããå
¬ééµãçæããã¨ãã«å¤±æãã¾ãã
ã²ã¨ã¤ã ã¨å¯ããã®ã§ãed25519ã®additionã示ãã¾ãã
(defun addition-ed25519 (p1 p2) (let* ((x1 (point4-x p1)) (y1 (point4-y p1)) (z1 (point4-z p1)) (t1 (point4-xy p1)) (x2 (point4-x p2)) (y2 (point4-y p2)) (z2 (point4-z p2)) (t2 (point4-xy p2)) (a (* (- y1 x1) (- y2 x2))) (b (* (+ y1 x1) (+ y2 x2))) (c (* t1 2 *elliptic-d* t2)) (d (* z1 2 z2)) (e (- b a)) (f (- d c)) (g (+ d c)) (h (+ b a))) (make-point4 (modp (* e f)) (modp (* g h)) (modp (* f g)) (modp (* e h)))))
ç¶ãã¾ã
次ã¯ä¹ç®ããããããªæ¼ç®ããã¾ãã
Common Lispで楕円曲線DSAを実装する3(乗算など) - nptclのブログ