GoofyCoin의 파이썬 구현

간단하고 쓸모없는 암호 화폐



Liam ReadUnsplash님의 사진

1. 소개



GoofyCoin은 우리가 상상할 수 있는 가장 단순한(따라서 쓸모 없는) 암호화폐입니다. Arvind Narayanan, Joseph Bonneau, Edward Felten, Andrew Miller, Steven Goldfeder가 저술하고 Princeton University Press에서 2016년에 출판한 책 "Bitcoin and Cryptocurrency Technologies"의 1장에 설명되어 있습니다. 학습 목적으로 파이썬으로 구현하십시오. 내 코드에는 실수가 포함되어 있으며 책에 설명된 GoofyCoin 규칙에 대한 이해가 100% 정확하지 않을 수 있습니다. 누구든지 이러한 문제를 발견하면 여기에 의견을 말하십시오.

이 소개, GoofyCoin에 대한 설명(책에 설명된 대로) 및 내 코드가 포함된 jupyter 노트북은 다음 GitHub에서 사용할 수 있습니다. https://github.com/thiagoguimaraesdf/goofyCoinPython

2 — GoofyCoin 규칙 및 프로토콜



두 가지 주요 GoofyCoin 규칙은 다음과 같습니다.

(i) Goofy, which is a unique entity, can create new coins whenever he wants and these newly created coins belong to him.

(ii) Hoever owns a GoofyCoin can transfer it on to someone else. Transferring a coin is not simply a matter of sending the coin data structure to the recipient — it’s done using cryptographic operations.

새로운 GoofyCoin을 생성하기 위한 프로토콜은 아래에 설명되어 있습니다.

"To create a coin, Goofy generates a unique coin ID uniqueCoinID that he’s never generated before
and constructs the string “CreateCoin [uniqueCoinID]”. He then computes the digital signature of
this string with his secret signing key. The string, together with Goofy’s signature, is a coin. Anyone
can verify that the coin contains Goofy’s valid signature of a CreateCoin statement, and is therefore a
valid coin."

기존 코인을 전송하는 프로토콜은 다음과 같습니다.

"Let’s say Goofy wants to transfer a coin that he created to Alice. To do this he creates a new
statement that says “Pay this to Alice” where “this” is a hash pointer that references the coin in
question. And as we saw earlier, identities are really just public keys, so “Alice” refers to Alice’s public
key. Finally, Goofy signs the string representing the statement. Since Goofy is the one who originally
owned that coin, he has to sign any transaction that spends the coin. Once this data structure
representing Goofy’s transaction signed by him exists, Alice owns the coin. She can prove to anyone
that she owns the coin, because she can present the data structure with Goofy’s valid signature.
Furthermore, it points to a valid coin that was owned by Goofy. So the validity and ownership of coins
are self‐evident in the system.

Once Alice owns the coin, she can spend it in turn. To do this she creates a statement that says, “Pay
this coin to Bob’s public key” where “this” is a hash pointer to the coin that was owned by her. And of
course, Alice signs this statement. Anyone, when presented with this coin, can verify that Bob is the
owner. They would follow the chain of hash pointers back to the coin’s creation and verify that at
each step, the rightful owner signed a statement that says “pay this coin to [new owner]”."

GoofyCoin 규칙에 대한 보다 공식적인 요약은 다음 책에 설명되어 있습니다.

● Goofy can create new coins by simply signing a statement that he’s making a new coin with a
unique coin ID.

● Whoever owns a coin can pass it on to someone else by signing a statement that saying, “Pass
on this coin to X” (where X is specified as a public key)

● Anyone can verify the validity of a coin by following the chain of hash pointers back to its
creation by Goofy, verifying all of the signatures along the way.

3 — 구현 코드


3.1 — 라이브러리 가져오기



여기서 사용되는 주요 라이브러리는 디지털 서명을 생성하기 위한 ecdsa와 해시 함수(SHA-3)를 사용하기 위한 Haslib입니다.

ecdsa에 대한 추가 정보: https://pypi.org/project/ecdsa/

hashlib에 대한 추가 정보: https://docs.python.org/3/library/hashlib.html




3.2 — 당사 임의 문자열 생성기



길이가 n인 임의의 문자열을 생성하는 함수를 정의합니다.


참고: 임의의 n 길이 str을 생성하기 위한 더 많은 무작위 알고리즘이 있을 수 있습니다. 저는 여기서 아주 간단한 것을 사용하고 있습니다.




<script id="gist-ltag"src="https://gist.github.com/thiagoguimaraesdf/580fdf7ff96a735cdc675fd25a752bd3.js"/>


3.3 — 구피 키 만들기



우리는 GoofyCoin 클래스 외부에서 실제 Goofy 서명 및 공개 키를 만들 것입니다. 여기에서 ECDSA 라이브러리(및 알고)를 사용하여 Goofy 개인 서명을 생성하고 추가로 확인합니다.


ECDSA 알고리즘에 대한 자세한 내용: https://www.cs.miami.edu/home/burt/learning/Csc609.142/ecdsa-cert.pdf




<script id="gist-ltag"src="https://gist.github.com/thiagoguimaraesdf/435005d9dae2e81b24544da588d70178.js"/>


3.4 — 구피코인 클래스



GoofyCoin 클래스 생성을 위한 코드 — 이를 통해 (i) 코인 생성 — Goofy 비밀 서명으로만 가능, (ii) 코인 전송; 및 (iii) 동전이 유효한지 확인하십시오. — 아래에 설명되어 있습니다.




<script id="gist-ltag"src="https://gist.github.com/thiagoguimaraesdf/479711d276c249a8a59cf12d75f94317.js"/>


4 — 코인 테스트



여기에서 다음과 같은 경우를 테스트합니다.



테스트 1: 새로운 코인 확인

테스트 2: 존재하지 않는 코인 전송 시도

테스트 3: Goofy Secret Key 없이 새 코인 생성 시도

테스트 4: 이미 한 번 전송된 코인 확인

테스트 5: 이미 두 번 전송된 코인 확인

테스트 6: Max가 전송한 가짜 코인 확인




<script id="gist-ltag"src="https://gist.github.com/thiagoguimaraesdf/27ae391c7dceac7fe832df3ba1d0170c.js"/>


5 — 최종 설명



엉뚱한 코인임에도 불구하고 GoofyCoin은 중앙 기관(이 경우 불행히도 Goofy)이 코인을 생성하고 유효한 검증 방법으로 전송할 수 있는 권한을 보장합니다. 코인을 받는 사람은 누구나 "경로"를 따라 올바른 사용자가 전송했는지 확인하여 해당 코인이 유효한지 확인할 수 있습니다.



구피 코인의 주요 문제는 누구나 자신의 코인을 이중으로 사용할 수 있다는 것입니다. 나쁜 사용자가 자신의 코인을 둘 이상의 다른 사람에게 양도하지 않도록 보장할 유효한 방법이 없습니다. 그래서 Goofy Coin이라고 부릅니다.



<시간/>

좋은 웹페이지 즐겨찾기