import%20marimo%0A%0A__generated_with%20%3D%20%220.15.3%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20hashlib%0A%0A%20%20%20%20from%20fastecdsa%20import%20keys%0A%20%20%20%20from%20fastecdsa.curve%20import%20secp256k1%20as%20curve%0A%20%20%20%20from%20mod%20import%20Mod%0A%0A%0A%40app.function%0A%23%20every%20message%20needs%20to%20be%20hashed%20into%20a%20number%0Adef%20encryptstring(hashstr)%3A%0A%20%20%20%20%22%22%22Convert%20a%20string%20message%20into%20a%20numeric%20hash%20value.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20hashstr%3A%20The%20string%20to%20be%20hashed%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20int%3A%20The%20numeric%20representation%20of%20the%20SHA-256%20hash%20of%20the%20input%20string%0A%20%20%20%20%22%22%22%0A%20%20%20%20return%20int(hashlib.sha256(hashstr.encode()).hexdigest()%2C%2016)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20private%20key%20is%20a%20random%20number%20from%20%5B%201%20%2C%20p%20%E2%88%92%201%20%5D%20where%0A%20%20%20%20%23%20p%20is%20the%20order%20of%20the%20underlying%20Galoisfield%0A%20%20%20%20privatekey%20%3D%20keys.gen_private_key(curve)%0A%0A%20%20%20%20%23%20The%20publickey%20is%0A%20%20%20%20publickey%20%3D%20curve.G%20*%20privatekey%0A%20%20%20%20return%20privatekey%2C%20publickey%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20There%20are%20n%20solutions%20on%20the%20elliptic%20curve%0A%20%20%20%20%23%20including%20the%20point%20at%20%E2%80%9Dinfinity%E2%80%9D%0A%20%20%20%20n%20%3D%20curve.q%0A%0A%20%20%20%20%23%20We%20send%20the%20clear%20message%20.%20.%20.%0A%20%20%20%20message%20%3D%20%22I%20love%20this%20lecture%22%0A%20%20%20%20return%20message%2C%20n%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20nonce%0A%20%20%20%20i%20%3D%20keys.gen_private_key(curve)%0A%20%20%20%20print(type(i))%0A%20%20%20%20%23%20nonce%20on%20elliptic%20curve%0A%20%20%20%20p_point%20%3D%20curve.G%20*%20i%0A%20%20%20%20return%20i%2C%20p_point%0A%0A%0A%40app.cell%0Adef%20_(i%2C%20message%2C%20n%2C%20p_point%2C%20privatekey)%3A%0A%20%20%20%20%23%20compute%20the%20signature%0A%20%20%20%20r%20%3D%20Mod(p_point.x%2C%20n)%0A%20%20%20%20inv_i%20%3D%20Mod(i%2C%20n).inverse%0A%0A%20%20%20%20s%20%3D%20(inv_i%20*%20(encryptstring(message)%20%2B%20r%20*%20privatekey))._value%0A%20%20%20%20return%20r%2C%20s%0A%0A%0A%40app.cell%0Adef%20_(message%2C%20n%2C%20r%2C%20s)%3A%0A%20%20%20%20%23%20The%20sender%20transmits%20(r%2Cs)%20%2C%20the%20clear%20message%20and%20his%20public%20key%0A%20%20%20%20%23%20s%20depends%20on%20the%20private%20key%20and%20the%20hashcode%20of%20the%20message.%0A%20%20%20%20%23%20However%20it%20is%20not%20possible%20to%20extract%20the%20private%20key%20from%20s%0A%0A%20%20%20%20%23%20check%20the%20signature%0A%20%20%20%20w%20%3D%20Mod(s%2C%20n).inverse%0A%20%20%20%20u1%20%3D%20encryptstring(message)%20*%20w%0A%20%20%20%20u2%20%3D%20r%20*%20w%0A%20%20%20%20return%20u1%2C%20u2%0A%0A%0A%40app.cell%0Adef%20_(publickey%2C%20r%2C%20u1%2C%20u2)%3A%0A%20%20%20%20%23%20addition%20of%20two%20residue%20classes%20and%20multiplication%20with%20Point%0A%20%20%20%20(curve.G%20*%20u1._value%20%2B%20publickey%20*%20u2._value).x%20%3D%3D%20r%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b671dfe39db18b3e1f3e6283a0122b3be04415b6ac9a2928ffb678dfd409127a