mit-ocw

Source code for MIT-OCW coursework
git clone git://git.laack.co/mit-ocw.git
Log | Files | Refs

commit a38ef9edcc4163a7abb023f9a476320b5b33fec5
parent 818ec852d7d1f2c4294208dc1be75337b98e699c
Author: Andrew Laack <andrew@laack.co>
Date:   Sat, 16 May 2026 21:11:13 -0500

Formatting

Diffstat:
Mmas.s62-cryptocurrency-engineering-and-design/lamport-signature/main.go | 39++++++++++-----------------------------
Mmas.s62-cryptocurrency-engineering-and-design/lamport-signature/main_test.go | 5-----
2 files changed, 10 insertions(+), 34 deletions(-)

diff --git a/mas.s62-cryptocurrency-engineering-and-design/lamport-signature/main.go b/mas.s62-cryptocurrency-engineering-and-design/lamport-signature/main.go @@ -6,21 +6,12 @@ import ( "fmt" ) -// 1. publicKey, privateKey := GenerateKeys(randomSeed) -// 2. signature := Sign(privateKey, message) -// 3. valid := Verify(publicKey, message, signature) - - -// 256 blocks of 256 bits (32 bytes) -// spread across two rows - type Key struct { - firstRow [256][32] byte - secondRow[256][32] byte + firstRow [256][32]byte + secondRow [256][32]byte } - -func GenerateKeys () (Key, Key){ +func GenerateKeys() (Key, Key) { privateKey := Key{} @@ -38,7 +29,6 @@ func GenerateKeys () (Key, Key){ } } - publicKey := Key{} for i := range 256 { @@ -64,9 +54,9 @@ func Sign(privateKey Key, message string) [256][32]byte { for idx := range 8 { currentBit := currentByteString[idx] if currentBit == '1' { - signature[(index * 8) + idx] = privateKey.firstRow[(index * 8) + idx] + signature[(index*8)+idx] = privateKey.firstRow[(index*8)+idx] } else { - signature[(index * 8) + idx] = privateKey.secondRow[(index * 8) + idx] + signature[(index*8)+idx] = privateKey.secondRow[(index*8)+idx] } } } @@ -74,9 +64,8 @@ func Sign(privateKey Key, message string) [256][32]byte { return signature } - func Verify(publicKey Key, message string, signature [256][32]byte) bool { - + messageHash := sha256.Sum256([]byte(message)) for index := range 32 { @@ -85,12 +74,12 @@ func Verify(publicKey Key, message string, signature [256][32]byte) bool { currentBit := currentByteString[idx] if currentBit == '1' { // signature[(index * 8) + idx] = privateKey.firstRow[(index * 8) + idx] - if sha256.Sum256(signature[(index * 8) + idx][:]) != publicKey.firstRow[(index * 8) + idx] { - return false + if sha256.Sum256(signature[(index*8)+idx][:]) != publicKey.firstRow[(index*8)+idx] { + return false } } else { - if sha256.Sum256(signature[(index * 8) + idx][:]) != publicKey.secondRow[(index * 8) + idx] { - return false + if sha256.Sum256(signature[(index*8)+idx][:]) != publicKey.secondRow[(index*8)+idx] { + return false } } } @@ -102,18 +91,10 @@ func Verify(publicKey Key, message string, signature [256][32]byte) bool { func main() { - // Generally, you'd pass a random seed to generatekeys, but crypto/rand.Read - // doesn't accept a seed. - publicKey, privateKey := GenerateKeys() - message := rand.Text() - signature := Sign(privateKey, message) - - valid := Verify(publicKey, message, signature) - fmt.Printf("Verified message matches signature: %v\n", valid) } diff --git a/mas.s62-cryptocurrency-engineering-and-design/lamport-signature/main_test.go b/mas.s62-cryptocurrency-engineering-and-design/lamport-signature/main_test.go @@ -22,7 +22,6 @@ func signAndValidate(t *testing.T, wg *sync.WaitGroup) { signature := Sign(privateKey, message) - valid := Verify(publicKey, message, signature) if valid != true { @@ -30,7 +29,6 @@ func signAndValidate(t *testing.T, wg *sync.WaitGroup) { } } - func signChangeAndValidate(t *testing.T, wg *sync.WaitGroup) { defer wg.Done() @@ -85,7 +83,6 @@ func TestSignatureIsValid(t *testing.T) { wg.Wait() - } func TestSignatureChangeInvalidation(t *testing.T) { @@ -99,7 +96,6 @@ func TestSignatureChangeInvalidation(t *testing.T) { wg.Wait() - } func TestMessageChangeInvalidation(t *testing.T) { @@ -113,5 +109,4 @@ func TestMessageChangeInvalidation(t *testing.T) { wg.Wait() - }