11import { test , expect } from '@jest/globals'
22import { DiffieHellman , MasseyOmura } from './protocols'
3+ import { Koblitz } from './algorithms'
4+ import { Point } from './core'
35
46test ( 'diffie-hellman: compute shared secret' , ( ) => {
57 const private_key_alice = 12345n
@@ -18,31 +20,66 @@ test('diffie-hellman: compute shared secret', () => {
1820} )
1921
2022test ( 'massey-omura: encryption decryption' , ( ) => {
21- const private_key_sender = 123456n
22- const mo_sender = new MasseyOmura ( private_key_sender )
23+ // Initialize the Koblitz instance for the elliptic curve 'secp192k1'
24+ const koblitz = new Koblitz ( 'secp192k1' )
2325
24- const private_key_receiver = 654321n
25- const mo_receiver = new MasseyOmura ( private_key_receiver )
26+ // Sender's side
27+ // -------------
28+ const privateKeySender = BigInt ( '70604135' )
29+ // Initialize Massey-Omura protocol with the sender's private key
30+ const moSender = new MasseyOmura ( privateKeySender )
2631
27- const message = mo_sender . curve . G // Using the curve's generator point for simplicity
32+ // Encode the message using the Koblitz method
33+ // `j` is used to handle the ambiguity in the decoding process
34+ const [ message , j ] = koblitz . encode ( 'Hello, world!' )
2835
29- // Sender encrypts the message
30- const encrypted_by_sender = mo_sender . first_encryption_step ( message )
36+ // Perform the first encryption step with Massey-Omura protocol
37+ const encryptedMsgSender = moSender . first_encryption_step ( message )
3138
32- // Receiver encrypts the already encrypted message
33- const encrypted_by_receiver =
34- mo_receiver . second_encryption_step ( encrypted_by_sender )
39+ // The encoded message is now sent to the receiver...
40+ // (transmission of encryptedMsgSender)
3541
36- // Sender decrypts the message partly
37- const partially_decrypted_by_sender = mo_sender . partial_decryption_step (
38- encrypted_by_receiver ,
39- )
42+ // Receiver's side
43+ // ---------------
44+ const privateKeyReceiver = BigInt ( '48239108668' )
45+ // Initialize Massey-Omura protocol with the receiver's private key
46+ const moReceiver = new MasseyOmura ( privateKeyReceiver )
4047
41- // Receiver completes decryption
42- const fully_decrypted_message = mo_receiver . partial_decryption_step (
43- partially_decrypted_by_sender ,
44- )
48+ // Perform the second encryption step with Massey-Omura protocol
49+ const encryptedMsgReceiver =
50+ moReceiver . second_encryption_step ( encryptedMsgSender )
51+
52+ // The double-encrypted message is sent back to the sender...
53+ // (transmission of encryptedMsgReceiver)
54+
55+ // Sender's side again
56+ // -------------------
57+ const partialDecryptedMsg =
58+ moSender . partial_decryption_step ( encryptedMsgReceiver )
59+
60+ // The partially decrypted message is sent back to the receiver...
61+ // (transmission of partialDecryptedMsg)
62+
63+ // Receiver's final decryption
64+ // ---------------------------
65+ const originalMessage =
66+ moReceiver . partial_decryption_step ( partialDecryptedMsg )
67+
68+ // Decode the message using the Koblitz method
69+ // `j` is used to resolve the mapping from the elliptic curve point back to the message
70+ const decodedMessage = koblitz . decode ( originalMessage , j )
4571
4672 // The fully decrypted message should match the original message
47- expect ( fully_decrypted_message ) . toStrictEqual ( message )
73+ expect ( decodedMessage ) . toStrictEqual ( 'Hello, world!' )
74+ } )
75+
76+ test ( 'massey-omura: validate the public key value' , ( ) => {
77+ const moSender = new MasseyOmura ( 654564n )
78+
79+ const P1 = new Point (
80+ 2561645154652864168258282342383115659685709191094067233983n ,
81+ 89709827696035605786509950363037856155865475850237621433n ,
82+ )
83+
84+ expect ( moSender . public_key . toString ( ) ) . toStrictEqual ( P1 . toString ( ) )
4885} )
0 commit comments