-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1729 from o1-labs/v2-encryption
Encryption v2
- Loading branch information
Showing
4 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import assert from 'assert'; | ||
import { | ||
Bytes, | ||
PrivateKey, | ||
initializeBindings, | ||
Encryption, | ||
Encoding, | ||
} from 'o1js'; | ||
|
||
await initializeBindings(); | ||
|
||
class Bytes256 extends Bytes(256) {} | ||
const priv = PrivateKey.random(); | ||
const pub = priv.toPublicKey(); | ||
|
||
const plainMsg = 'The quick brown fox jumped over the angry dog.'; | ||
|
||
console.log('en/decryption of field elements'); | ||
const cipher2 = Encryption.encryptV2(Encoding.stringToFields(plainMsg), pub); | ||
const plainText2 = Encryption.decryptV2(cipher2, priv); | ||
|
||
assert( | ||
Encoding.stringFromFields(plainText2) === plainMsg, | ||
'Plain message and decrypted message are the same' | ||
); | ||
|
||
console.log('en/decryption of bytes'); | ||
const message = Bytes256.fromString(plainMsg); | ||
console.log('plain message', plainMsg); | ||
const cipher = Encryption.encryptBytes(message, pub); | ||
const plainText = Encryption.decryptBytes(cipher, priv); | ||
console.log('decrypted message', Buffer.from(plainText.toBytes()).toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters