[stupid-crypto] 11 new revisions pushed by ben@links.org on 2010-03-21 17:29 GMT

3 views
Skip to first unread message

stupid...@googlecode.com

unread,
Mar 21, 2010, 1:30:10 PM3/21/10
to stupi...@googlegroups.com
11 new revisions:

Revision: 6ab2dd6922
Author: Ben Laurie <b...@links.org>
Date: Sat Mar 20 10:58:37 2010
Log: Nng. FreeBSD env doesn't work the same as others. I hope -S doesn't...
http://code.google.com/p/stupid-crypto/source/detail?r=6ab2dd6922

Revision: e94db1b59d
Author: Ben Laurie <b...@links.org>
Date: Sat Mar 20 11:02:13 2010
Log: Still breaking for the wrong reason.
http://code.google.com/p/stupid-crypto/source/detail?r=e94db1b59d

Revision: 5f96e628de
Author: Ben Laurie <b...@links.org>
Date: Sat Mar 20 11:33:49 2010
Log: Make types more general.
http://code.google.com/p/stupid-crypto/source/detail?r=5f96e628de

Revision: 0f8828293a
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 05:09:59 2010
Log: More generality in the parser. We'll have to counterbalance with
some...
http://code.google.com/p/stupid-crypto/source/detail?r=0f8828293a

Revision: 91ac95f525
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 05:57:24 2010
Log: Constify. sha256-struct now fails.
http://code.google.com/p/stupid-crypto/source/detail?r=91ac95f525

Revision: 4e79ab2c52
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:08:41 2010
Log: Check that another bug is detected.
http://code.google.com/p/stupid-crypto/source/detail?r=4e79ab2c52

Revision: 1f56f3ec7c
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:16:18 2010
Log: One step closer to complete SHA-256 - split out the inner block
comput...
http://code.google.com/p/stupid-crypto/source/detail?r=1f56f3ec7c

Revision: f875da1aaa
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:26:13 2010
Log: Deal with the fact that the final block might actually be two blocks.
http://code.google.com/p/stupid-crypto/source/detail?r=f875da1aaa

Revision: dc36ed7cdc
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:35:49 2010
Log: Test new final block logic.
http://code.google.com/p/stupid-crypto/source/detail?r=dc36ed7cdc

Revision: 263664445f
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:37:42 2010
Log: Remove incorrect comment.
http://code.google.com/p/stupid-crypto/source/detail?r=263664445f

Revision: 2e344ebce3
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 10:29:19 2010
Log: A lot of fiddling around resulting in ......
http://code.google.com/p/stupid-crypto/source/detail?r=2e344ebce3

==============================================================================
Revision: 6ab2dd6922
Author: Ben Laurie <b...@links.org>
Date: Sat Mar 20 10:58:37 2010
Log: Nng. FreeBSD env doesn't work the same as others. I hope -S doesn't
break other platforms.
http://code.google.com/p/stupid-crypto/source/detail?r=6ab2dd6922

Modified:
/src/stupid.pl
/test/test.pl

=======================================
--- /src/stupid.pl Sun Mar 14 18:28:32 2010
+++ /src/stupid.pl Sat Mar 20 10:58:37 2010
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl -w
+#!/usr/bin/env -S perl -w

use strict;

=======================================
--- /test/test.pl Sun Mar 7 15:55:58 2010
+++ /test/test.pl Sat Mar 20 10:58:37 2010
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl -w
+#!/usr/bin/env -S perl -w

use strict;


==============================================================================
Revision: e94db1b59d
Author: Ben Laurie <b...@links.org>
Date: Sat Mar 20 11:02:13 2010
Log: Still breaking for the wrong reason.
http://code.google.com/p/stupid-crypto/source/detail?r=e94db1b59d

Modified:
/test/bug-double-declaration.stupid

=======================================
--- /test/bug-double-declaration.stupid Wed Mar 3 04:51:49 2010
+++ /test/bug-double-declaration.stupid Sat Mar 20 11:02:13 2010
@@ -1,6 +1,6 @@
"EXPECT-BUILD-FAIL:";

-function() test(ostream out) {
+function(ostream out) test() {
out.put(65);
}


==============================================================================
Revision: 5f96e628de
Author: Ben Laurie <b...@links.org>
Date: Sat Mar 20 11:33:49 2010
Log: Make types more general.
http://code.google.com/p/stupid-crypto/source/detail?r=5f96e628de

Modified:
/src/Stupid/C.pm
/src/grammar.y
/test/sha256-struct.stupid

=======================================
--- /src/Stupid/C.pm Sun Mar 14 18:28:32 2010
+++ /src/Stupid/C.pm Sat Mar 20 11:33:49 2010
@@ -288,6 +288,19 @@

print "struct $self->{name} $name";
}
+
+sub Stupid::Type::StructInstance::emitReturnDecl {
+ my $self = shift;
+ my $name = shift;
+
+ print "struct $self->{name} *$name";
+}
+
+sub Stupid::Type::StructInstance::dereference {
+ my $self = shift;
+
+ print '*';
+}

sub Stupid::AbstractDeclList::emitCode {
my $self = shift;
=======================================
--- /src/grammar.y Sun Mar 14 18:28:32 2010
+++ /src/grammar.y Sat Mar 20 11:33:49 2010
@@ -162,15 +162,9 @@
{ new Stupid::FunctionCall($_[1], $_[3]); }
;

-decl : type vardecl '=' VALUE
+decl : type vardecl '=' value
{ new Stupid::Declare($::Context,
new Stupid::Variable($_[1], $_[2]), $_[4]); }
- | 'array' '(' type ',' VALUE ')' vardecl '=' arrayval
- { new Stupid::Declare($::Context,
- new Stupid::Variable(new Stupid::Type::Array($_[3], $_[5]),
- $_[7]), $_[9]); }
- | 'struct' WORD vardecl '=' arrayval
- { new Stupid::Declare($::Context, new Stupid::Variable(new
Stupid::Type::StructInstance($_[2]), $_[3]), $_[5]); }
;

type : 'uint32'
@@ -179,6 +173,10 @@
{ new Stupid::Type::UInt8(); }
| 'ostream'
{ new Stupid::Type::OStream(); }
+ | 'array' '(' type ',' VALUE ')'
+ { new Stupid::Type::Array($_[3], $_[5]); }
+ | 'struct' WORD
+ { new Stupid::Type::StructInstance($_[2]); }
;

arrayval : '(' val_list ')'
=======================================
--- /test/sha256-struct.stupid Sun Mar 14 18:28:32 2010
+++ /test/sha256-struct.stupid Sat Mar 20 11:33:49 2010
@@ -1,10 +1,19 @@
"EXPECT:********************************";

struct sha256 (
- array(uint32, 8) h
+ array(uint32, 8) h,
+ array(uint8, 64) buffer,
+ uint8 used
);

-function (array(uint8, 32) output) sha256(array(uint8, 64) message, uint32
message_bits ) {
+function(struct sha256 state) sha256_init() {
+ struct sha256 tmp = ((0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19));
+ state = tmp;
+}
+
+function (array(uint8, 32) output) sha256(array(uint8, 64) message,
+ uint32 message_bits ) {
"This code adapted from Wikipedia pseudocode";

"message must include enough space for padding to happen";

==============================================================================
Revision: 0f8828293a
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 05:09:59 2010
Log: More generality in the parser. We'll have to counterbalance with some
checking of the parse tree (but we needed that anyway).

Change array delimiters to [], otherwise we can't parse anymore.

Now we can initialise the sha256 state with a function.
http://code.google.com/p/stupid-crypto/source/detail?r=0f8828293a

Modified:
/src/Stupid/C.pm
/src/grammar.y
/src/stupid.pl
/test/bug-ostream-input.stupid
/test/compilet1.stupid
/test/ostream.stupid
/test/sha256-struct.stupid
/test/struct.stupid

=======================================
--- /src/Stupid/C.pm Sat Mar 20 11:33:49 2010
+++ /src/Stupid/C.pm Sun Mar 21 05:09:59 2010
@@ -127,7 +127,7 @@
$self->{function}->emitCall();
print '(';
$lvalue->emitPointer();
- print ', ';
+ print ', ' if !$self->{args}->isEmpty();
$self->{args}->emitCode();
print ");\n";
}
@@ -301,6 +301,12 @@

print '*';
}
+
+sub Stupid::Type::StructInstance::emitPointer {
+ my $self = shift;
+
+ print '&';
+}

sub Stupid::AbstractDeclList::emitCode {
my $self = shift;
=======================================
--- /src/grammar.y Sat Mar 20 11:33:49 2010
+++ /src/grammar.y Sun Mar 21 05:09:59 2010
@@ -33,9 +33,6 @@

abstract_decl : type vardecl
{ new Stupid::AbstractDeclare($_[1], $_[2]); }
- | 'array' '(' type ',' VALUE ')' vardecl
- { new Stupid::AbstractDeclare(
- new Stupid::Type::Array($_[3], $_[5]), $_[7]); }
;

function : 'function' '(' arglist ')' WORD '(' arglist ')'
@@ -57,10 +54,6 @@
arg : type vardecl
{ new Stupid::Declare($::Context, new Stupid::Variable($_[1],
$_[2])); }
- | 'array' '(' type ',' VALUE ')' vardecl
- { new Stupid::Declare($::Context,
- new Stupid::Variable(new Stupid::Type::Array($_[3], $_[5]),
- $_[7])); }
;

statements : statements statement
@@ -139,14 +132,15 @@
{ $_[2]; }
| var
{ $_[1]; }
- | VALUE
- | CHAR
+ | value
;

exprlist: exprlist ',' expr
{ $_[1]->appendExpr($_[3]); $_[1]; }
| expr
{ my $t = new Stupid::ExprList(); $t->appendExpr($_[1]); $t; }
+ |
+ { new Stupid::ExprList(); }
;

var : WORD
@@ -162,7 +156,7 @@
{ new Stupid::FunctionCall($_[1], $_[3]); }
;

-decl : type vardecl '=' value
+decl : type vardecl '=' expr
{ new Stupid::Declare($::Context,
new Stupid::Variable($_[1], $_[2]), $_[4]); }
;
@@ -179,19 +173,19 @@
{ new Stupid::Type::StructInstance($_[2]); }
;

-arrayval : '(' val_list ')'
+arrayval : '[' val_list ']'
{ $_[2]; }
;

-val_list : val_list ',' value
+val_list : val_list ',' expr
{ $_[1]->append($_[3]); $_[1]; }
- | value
+ | expr
{ my $t = new Stupid::ArrayValue(); $t->append($_[1]); $t; }
| STRING
{ Stupid::ArrayFromString($_[1]); }
;

-value : arrayval
+value : arrayval
| VALUE
| CHAR
;
=======================================
--- /src/stupid.pl Sat Mar 20 10:58:37 2010
+++ /src/stupid.pl Sun Mar 21 05:09:59 2010
@@ -375,6 +375,12 @@

push @{$self->{expressions}}, $expr;
}
+
+sub isEmpty {
+ my $self = shift;
+
+ return $#{$self->{expressions}} == -1;
+}

package Stupid::FunctionCall;

=======================================
--- /test/bug-ostream-input.stupid Sun Mar 7 15:55:58 2010
+++ /test/bug-ostream-input.stupid Sun Mar 21 05:09:59 2010
@@ -1,7 +1,7 @@
"EXPECT-BUILD-FAIL:";

function() test(ostream out) {
- array(uint8, 13) hello = ( "Hello, world!" );
+ array(uint8, 13) hello = [ "Hello, world!" ];
uint32 n = 0;

while(n ne32 13) {
=======================================
--- /test/compilet1.stupid Mon Mar 8 03:45:58 2010
+++ /test/compilet1.stupid Sun Mar 21 05:09:59 2010
@@ -19,7 +19,7 @@
"Initialize table of round constants";
"(first 32 bits of the fractional parts of the cube roots of the first 64
primes 2..311):";
array(uint32, 64) k =
- (0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+ [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
@@ -34,13 +34,13 @@
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];

"For now, dummy in the message instead of declaring a function wrapper";
"Also, for now, allow enough room in the input for padding, etc, to
simplify the loop";
uint32 message_bits = 123;
array(uint8, 64) message =
- (0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
+ [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
@@ -48,16 +48,16 @@
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
- );
+ ];
uint32 pad_byte = 0;
uint32 pad_bit = 0;
uint32 tmp = 0;
uint32 tmp2 = 0;
array(uint32, 64) w =
- (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
uint32 i = 0;
uint32 s0 = 0;
uint32 s1 = 0;
=======================================
--- /test/ostream.stupid Sun Mar 7 15:55:58 2010
+++ /test/ostream.stupid Sun Mar 21 05:09:59 2010
@@ -1,7 +1,7 @@
"EXPECT:Hello, world!";

function(ostream out) test() {
- array(uint8, 13) hello = ( "Hello, world!" );
+ array(uint8, 13) hello = [ "Hello, world!" ];
uint32 n = 0;

while(n ne32 13) {
=======================================
--- /test/sha256-struct.stupid Sat Mar 20 11:33:49 2010
+++ /test/sha256-struct.stupid Sun Mar 21 05:09:59 2010
@@ -7,8 +7,8 @@
);

function(struct sha256 state) sha256_init() {
- struct sha256 tmp = ((0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19));
+ struct sha256 tmp = [[0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]];
state = tmp;
}

@@ -22,14 +22,13 @@

"Initialize variables";
"(first 32 bits of the fractional parts of the square roots of the first 8
primes 2..19):";
- struct sha256 state = ((0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19));
+ struct sha256 state = [[ 0 ]];


"Initialize table of round constants";
"(first 32 bits of the fractional parts of the cube roots of the first 64
primes 2..311):";
array(uint32, 64) k =
- (0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+ [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
@@ -44,17 +43,17 @@
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];

uint32 pad_byte = 0;
uint32 pad_bit = 0;
uint32 tmp = 0;
uint32 tmp2 = 0;
array(uint32, 64) w =
- (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
uint32 i = 0;
uint32 s0 = 0;
uint32 s1 = 0;
@@ -71,6 +70,8 @@
uint32 t2 = 0;
uint32 ch = 0;

+state = sha256_init();
+
"Pre-processing:";
"append the bit '1' to the message";

@@ -206,13 +207,13 @@
}

function(ostream out) test() {
- array(uint8, 64) m1 = ( 0x61, 0x62, 0x63 );
- array(uint8, 32) h1 = (
+ array(uint8, 64) m1 = [ 0x61, 0x62, 0x63 ];
+ array(uint8, 32) h1 = [
0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,
0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
- 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad );
- array(uint8, 32) r = ( 0 );
+ 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad ];
+ array(uint8, 32) r = [ 0 ];
r = sha256(m1, 24);
uint32 i = 0;
while(i ne32 32) {
=======================================
--- /test/struct.stupid Sun Mar 14 18:28:32 2010
+++ /test/struct.stupid Sun Mar 21 05:09:59 2010
@@ -10,8 +10,8 @@
);

function() test() {
- struct test c = (0, 0);
- struct test2 e = ((0,0));
+ struct test c = [0, 0];
+ struct test2 e = [[0, 0]];

c.a = 1;
c.b = 23;

==============================================================================
Revision: 91ac95f525
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 05:57:24 2010
Log: Constify. sha256-struct now fails.
http://code.google.com/p/stupid-crypto/source/detail?r=91ac95f525

Modified:
/src/Stupid/C.pm

=======================================
--- /src/Stupid/C.pm Sun Mar 21 05:09:59 2010
+++ /src/Stupid/C.pm Sun Mar 21 05:57:24 2010
@@ -341,6 +341,7 @@
my $self = shift;
my $name = shift;

+ print 'const ';
$self->emitDeclaration($name);
}

@@ -367,6 +368,7 @@
my $self = shift;
my $name = shift;

+ print 'const ';
$self->emitDeclaration($name);
}

@@ -417,6 +419,7 @@
my $self = shift;
my $name = shift;

+ print 'const ';
$self->emitDeclaration($name);
}


==============================================================================
Revision: 4e79ab2c52
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:08:41 2010
Log: Check that another bug is detected.
http://code.google.com/p/stupid-crypto/source/detail?r=4e79ab2c52

Added:
/test/bug-context.stupid

=======================================
--- /dev/null
+++ /test/bug-context.stupid Sun Mar 21 06:08:41 2010
@@ -0,0 +1,12 @@
+"EXPECT-BUILD-FAIL:";
+
+function() f1() {
+ uint32 x = 0;
+
+ x = 1;
+}
+
+function() f2() {
+ "error: not declared in this context";
+ x = 2;
+}

==============================================================================
Revision: 1f56f3ec7c
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:16:18 2010
Log: One step closer to complete SHA-256 - split out the inner block
computation.
http://code.google.com/p/stupid-crypto/source/detail?r=1f56f3ec7c

Modified:
/src/Stupid/C.pm
/test/sha256-struct.stupid

=======================================
--- /src/Stupid/C.pm Sun Mar 21 05:57:24 2010
+++ /src/Stupid/C.pm Sun Mar 21 06:16:18 2010
@@ -288,6 +288,14 @@

print "struct $self->{name} $name";
}
+
+sub Stupid::Type::StructInstance::emitArg {
+ my $self = shift;
+ my $name = shift;
+
+ print 'const ';
+ $self->emitDeclaration($name);
+}

sub Stupid::Type::StructInstance::emitReturnDecl {
my $self = shift;
=======================================
--- /test/sha256-struct.stupid Sun Mar 21 05:09:59 2010
+++ /test/sha256-struct.stupid Sun Mar 21 06:16:18 2010
@@ -12,19 +12,8 @@
state = tmp;
}

-function (array(uint8, 32) output) sha256(array(uint8, 64) message,
- uint32 message_bits ) {
-"This code adapted from Wikipedia pseudocode";
-
-"message must include enough space for padding to happen";
-
-"Note 2: All constants in this pseudo code are in big endian";
-
-"Initialize variables";
-"(first 32 bits of the fractional parts of the square roots of the first 8
primes 2..19):";
- struct sha256 state = [[ 0 ]];
-
-
+function(struct sha256 out) sha256_block(struct sha256 in,
+ array(uint8, 64) block) {
"Initialize table of round constants";
"(first 32 bits of the fractional parts of the cube roots of the first 64
primes 2..311):";
array(uint32, 64) k =
@@ -45,8 +34,6 @@
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];

-uint32 pad_byte = 0;
-uint32 pad_bit = 0;
uint32 tmp = 0;
uint32 tmp2 = 0;
array(uint32, 64) w =
@@ -69,6 +56,95 @@
uint32 t1 = 0;
uint32 t2 = 0;
uint32 ch = 0;
+
+" break chunk into sixteen 32-bit big-endian words w[0..15]";
+tmp = 0;
+while(tmp ne32 16) {
+ tmp2 = tmp lshift32 2;
+ w[tmp] = ((widen8to32 block[tmp2]) lshift32 24)
+ plus32 ((widen8to32 block[tmp2 plus32 1]) lshift32 16)
+ plus32 ((widen8to32 block[tmp2 plus32 2]) lshift32 8)
+ plus32 (widen8to32 block[tmp2 plus32 3]);
+ tmp = tmp plus32 1;
+}
+
+" Extend the sixteen 32-bit words into sixty-four 32-bit words";
+i = 16;
+while(i ne32 64) {
+ s0 = (w[i minus32 15] rrotate32 7) xor32 (w[i minus32 15] rrotate32
18) xor32 (w[i minus32 15] rshift32 3);
+ s1 = (w[i minus32 2] rrotate32 17) xor32 (w[i minus32 2] rrotate32 19)
xor32 (w[i minus32 2] rshift32 10);
+ w[i] = w[i minus32 16] plus32 s0 plus32 w[i minus32 7] plus32 s1;
+ i = i plus32 1;
+}
+
+" Initialize hash value for this chunk:";
+
+a = in.h[0];
+b = in.h[1];
+c = in.h[2];
+d = in.h[3];
+e = in.h[4];
+f = in.h[5];
+g = in.h[6];
+h = in.h[7];
+
+" Main loop:";
+
+i = 0;
+while(i ne32 64) {
+ s0 = (a rrotate32 2) xor32 (a rrotate32 13) xor32 (a rrotate32 22);
+ maj = (a and32 b) xor32 (a and32 c) xor32 (b and32 c);
+ t2 = s0 plus32 maj;
+ s1 = (e rrotate32 6) xor32 (e rrotate32 11) xor32 (e rrotate32 25);
+ ch = (e and32 f) xor32 ((not32 e) and32 g);
+ t1 = h plus32 s1 plus32 ch plus32 k[i] plus32 w[i];
+ h = g;
+ g = f;
+ f = e;
+ e = d plus32 t1;
+ d = c;
+ c = b;
+ b = a;
+ a = t1 plus32 t2;
+ i = i plus32 1;
+}
+
+" Add this chunk's hash to result so far:";
+
+out.h[0] = in.h[0] plus32 a;
+out.h[1] = in.h[1] plus32 b;
+out.h[2] = in.h[2] plus32 c;
+out.h[3] = in.h[3] plus32 d;
+out.h[4] = in.h[4] plus32 e;
+out.h[5] = in.h[5] plus32 f;
+out.h[6] = in.h[6] plus32 g;
+out.h[7] = in.h[7] plus32 h;
+}
+
+function (array(uint8, 32) output) sha256(array(uint8, 64) message_in,
+ uint32 message_bits ) {
+"This code adapted from Wikipedia pseudocode";
+
+"message must include enough space for padding to happen";
+
+"Note 2: All constants in this pseudo code are in big endian";
+
+"Initialize variables";
+"(first 32 bits of the fractional parts of the square roots of the first 8
primes 2..19):";
+ struct sha256 state = [[ 0 ]];
+
+uint32 pad_byte = 0;
+uint32 pad_bit = 0;
+uint32 tmp = 0;
+uint32 i = 0;
+array(uint8, 64) message = [ 0 ];
+
+"copy the input message to a mutable array";
+i = 0;
+while(i ne32 ((message_bits plus32 7) rshift32 3)) {
+ message[i] = message_in[i];
+ i = i plus32 1;
+}

state = sha256_init();

@@ -127,68 +203,7 @@

"for each chunk (we only have one, so don't bother with the loop for now)";

-" break chunk into sixteen 32-bit big-endian words w[0..15]";
-tmp = 0;
-while(tmp ne32 16) {
- tmp2 = tmp lshift32 2;
- w[tmp] = ((widen8to32 message[tmp2]) lshift32 24)
- plus32 ((widen8to32 message[tmp2 plus32 1]) lshift32 16)
- plus32 ((widen8to32 message[tmp2 plus32 2]) lshift32 8)
- plus32 (widen8to32 message[tmp2 plus32 3]);
- tmp = tmp plus32 1;
-}
-
-" Extend the sixteen 32-bit words into sixty-four 32-bit words";
-i = 16;
-while(i ne32 64) {
- s0 = (w[i minus32 15] rrotate32 7) xor32 (w[i minus32 15] rrotate32
18) xor32 (w[i minus32 15] rshift32 3);
- s1 = (w[i minus32 2] rrotate32 17) xor32 (w[i minus32 2] rrotate32 19)
xor32 (w[i minus32 2] rshift32 10);
- w[i] = w[i minus32 16] plus32 s0 plus32 w[i minus32 7] plus32 s1;
- i = i plus32 1;
-}
-
-" Initialize hash value for this chunk:";
-
-a = state.h[0];
-b = state.h[1];
-c = state.h[2];
-d = state.h[3];
-e = state.h[4];
-f = state.h[5];
-g = state.h[6];
-h = state.h[7];
-
-" Main loop:";
-
-i = 0;
-while(i ne32 64) {
- s0 = (a rrotate32 2) xor32 (a rrotate32 13) xor32 (a rrotate32 22);
- maj = (a and32 b) xor32 (a and32 c) xor32 (b and32 c);
- t2 = s0 plus32 maj;
- s1 = (e rrotate32 6) xor32 (e rrotate32 11) xor32 (e rrotate32 25);
- ch = (e and32 f) xor32 ((not32 e) and32 g);
- t1 = h plus32 s1 plus32 ch plus32 k[i] plus32 w[i];
- h = g;
- g = f;
- f = e;
- e = d plus32 t1;
- d = c;
- c = b;
- b = a;
- a = t1 plus32 t2;
- i = i plus32 1;
-}
-
-" Add this chunk's hash to result so far:";
-
-state.h[0] = state.h[0] plus32 a;
-state.h[1] = state.h[1] plus32 b;
-state.h[2] = state.h[2] plus32 c;
-state.h[3] = state.h[3] plus32 d;
-state.h[4] = state.h[4] plus32 e;
-state.h[5] = state.h[5] plus32 f;
-state.h[6] = state.h[6] plus32 g;
-state.h[7] = state.h[7] plus32 h;
+state = sha256_block(state, message);

"end of outer loop (when we do it)";


==============================================================================
Revision: f875da1aaa
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:26:13 2010
Log: Deal with the fact that the final block might actually be two blocks.
http://code.google.com/p/stupid-crypto/source/detail?r=f875da1aaa

Modified:
/src/grammar.y
/test/sha256-struct.stupid

=======================================
--- /src/grammar.y Sun Mar 21 05:09:59 2010
+++ /src/grammar.y Sun Mar 21 06:26:13 2010
@@ -62,6 +62,8 @@
{ my $t1 = new Stupid::StatementList();
$t1->appendStatement($_[1]);
$t1; }
+ |
+ { new Stupid::StatementList(); }
;

statement : decl ';'
=======================================
--- /test/sha256-struct.stupid Sun Mar 21 06:16:18 2010
+++ /test/sha256-struct.stupid Sun Mar 21 06:26:13 2010
@@ -13,7 +13,7 @@
}

function(struct sha256 out) sha256_block(struct sha256 in,
- array(uint8, 64) block) {
+ array(uint8, 128) block, uint32 base) {
"Initialize table of round constants";
"(first 32 bits of the fractional parts of the cube roots of the first 64
primes 2..311):";
array(uint32, 64) k =
@@ -61,10 +61,10 @@
tmp = 0;
while(tmp ne32 16) {
tmp2 = tmp lshift32 2;
- w[tmp] = ((widen8to32 block[tmp2]) lshift32 24)
- plus32 ((widen8to32 block[tmp2 plus32 1]) lshift32 16)
- plus32 ((widen8to32 block[tmp2 plus32 2]) lshift32 8)
- plus32 (widen8to32 block[tmp2 plus32 3]);
+ w[tmp] = ((widen8to32 block[base plus32 tmp2]) lshift32 24)
+ plus32 ((widen8to32 block[base plus32 tmp2 plus32 1]) lshift32 16)
+ plus32 ((widen8to32 block[base plus32 tmp2 plus32 2]) lshift32 8)
+ plus32 (widen8to32 block[base plus32 tmp2 plus32 3]);
tmp = tmp plus32 1;
}

@@ -121,8 +121,8 @@
out.h[7] = in.h[7] plus32 h;
}

-function (array(uint8, 32) output) sha256(array(uint8, 64) message_in,
- uint32 message_bits ) {
+function (array(uint8, 32) output) sha256_final(array(uint8, 64)
message_in,
+ uint32 message_bits ) {
"This code adapted from Wikipedia pseudocode";

"message must include enough space for padding to happen";
@@ -137,7 +137,7 @@
uint32 pad_bit = 0;
uint32 tmp = 0;
uint32 i = 0;
-array(uint8, 64) message = [ 0 ];
+array(uint8, 128) message = [ 0 ];

"copy the input message to a mutable array";
i = 0;
@@ -201,11 +201,13 @@
tmp = tmp rshift32 8;
message[pad_byte plus32 4] = mask32to8 tmp;

-"for each chunk (we only have one, so don't bother with the loop for now)";
-
-state = sha256_block(state, message);
-
-"end of outer loop (when we do it)";
+state = sha256_block(state, message, 0);
+"if the message went over two blocks";
+if(pad_byte ne32 56) {
+ state = sha256_block(state, message, 64);
+} else {
+}
+

"Produce the final hash value (big-endian):
digest = hash = h0 append h1 append h2 append h3 append h4 append h5
append h6 append h7";
@@ -229,7 +231,7 @@
0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad ];
array(uint8, 32) r = [ 0 ];
- r = sha256(m1, 24);
+ r = sha256_final(m1, 24);
uint32 i = 0;
while(i ne32 32) {
if(h1[i] ne8 r[i]) {

==============================================================================
Revision: dc36ed7cdc
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:35:49 2010
Log: Test new final block logic.
http://code.google.com/p/stupid-crypto/source/detail?r=dc36ed7cdc

Modified:
/test/sha256-struct.stupid

=======================================
--- /test/sha256-struct.stupid Sun Mar 21 06:26:13 2010
+++ /test/sha256-struct.stupid Sun Mar 21 06:35:49 2010
@@ -1,4 +1,4 @@
-"EXPECT:********************************";
+"EXPECT:****************************************************************";

struct sha256 (
array(uint32, 8) h,
@@ -223,18 +223,14 @@

}

-function(ostream out) test() {
- array(uint8, 64) m1 = [ 0x61, 0x62, 0x63 ];
- array(uint8, 32) h1 = [
- 0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,
- 0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
- 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
- 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad ];
+function(ostream out) test_one(array(uint8, 64) message, uint32 bits,
+ array(uint8, 32) hash) {
array(uint8, 32) r = [ 0 ];
- r = sha256_final(m1, 24);
uint32 i = 0;
+
+ r = sha256_final(message, bits);
while(i ne32 32) {
- if(h1[i] ne8 r[i]) {
+ if(hash[i] ne8 r[i]) {
out.put('!');
} else {
out.put('*');
@@ -242,3 +238,24 @@
i = i plus32 1;
}
}
+
+function(ostream out) test() {
+ "test vectors taken from OpenSSL tests/sha256t.c";
+ array(uint8, 64) m1 = [ 0x61, 0x62, 0x63 ];
+ array(uint8, 32) h1 = [
+ 0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,
+ 0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
+ 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
+ 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad ];
+ array(uint8, 64) m2
+ = [ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" ];
+ array(uint8, 32) h2 = [
+ 0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,
+ 0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
+ 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,
+ 0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1 ];
+
+ out = test_one(m1, 24, h1);
+ "just big enough to force it into the second block";
+ out = test_one(m2, 448, h2);
+}

==============================================================================
Revision: 263664445f
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 06:37:42 2010
Log: Remove incorrect comment.
http://code.google.com/p/stupid-crypto/source/detail?r=263664445f

Modified:
/test/sha256-struct.stupid

=======================================
--- /test/sha256-struct.stupid Sun Mar 21 06:35:49 2010
+++ /test/sha256-struct.stupid Sun Mar 21 06:37:42 2010
@@ -125,8 +125,6 @@
uint32 message_bits ) {
"This code adapted from Wikipedia pseudocode";

-"message must include enough space for padding to happen";
-
"Note 2: All constants in this pseudo code are in big endian";

"Initialize variables";

==============================================================================
Revision: 2e344ebce3
Author: Ben Laurie <b...@links.org>
Date: Sun Mar 21 10:29:19 2010
Log: A lot of fiddling around resulting in ...

Tada! Full SHA-256!
http://code.google.com/p/stupid-crypto/source/detail?r=2e344ebce3

Modified:
/src/Stupid/C.pm
/src/grammar.y
/src/stupid.pl
/test/sha256-struct.stupid
/test/struct.stupid

=======================================
--- /src/Stupid/C.pm Sun Mar 21 06:16:18 2010
+++ /src/Stupid/C.pm Sun Mar 21 10:29:19 2010
@@ -99,6 +99,7 @@
my $self = shift;

for my $s (@{$self->{statements}}) {
+# use Data::Dumper; $Data::Dumper::Indent=1; print "/*\n",
Data::Dumper->Dump([$s]), " */\n";
$s->emitCode();
}
}
@@ -116,7 +117,7 @@
$self->{function}->emitCall();
print '(';
$self->{function}->maybeAddSelf();
- $self->{args}->emitCode();
+ $self->{args}->emitParameters();
print ");\n";
}

@@ -128,7 +129,7 @@
print '(';
$lvalue->emitPointer();
print ', ' if !$self->{args}->isEmpty();
- $self->{args}->emitCode();
+ $self->{args}->emitParameters();
print ");\n";
}

@@ -155,7 +156,16 @@
my $self = shift;

$self->{owner}->emitCode();
- print ".$self->{member}";
+ $self->{owner}->emitMemberRef($self->{member});
+}
+
+sub Stupid::MemberRef::emitParameter {
+ my $self = shift;
+
+ $self->{member}->{type}->emitParameter();
+ print '(';
+ $self->emitLValue();
+ print ')';
}

sub Stupid::ExprList::emitCode {
@@ -168,6 +178,17 @@
$expr->emitCode();
}
}
+
+sub Stupid::ExprList::emitParameters {
+ my $self = shift;
+
+ my $first = 1;
+ for my $expr (@{$self->{expressions}}) {
+ print ', ' if !$first;
+ $first = 0;
+ $expr->emitParameter();
+ }
+}

sub Stupid::If::emitCode {
my $self = shift;
@@ -243,15 +264,27 @@
sub Stupid::Variable::emitCode {
my $self = shift;

+ print '(';
+ $self->{type}->dereference() if $self->{isReturn};
+ $self->{type}->derefArgument() if $self->{isArgument};
print $self->{name};
+ print ')';
+}
+
+sub Stupid::Variable::emitParameter {
+ my $self = shift;
+
+ $self->{type}->emitParameter()
+ if !$self->{isReturn} && !$self->{isArgument};;
+ print $self->{name};
}

sub Stupid::Variable::emitMemberRef {
my $self = shift;
- my $name = shift;
-
- print $self->{isReturn} ? '->' : '.';
- print $name;
+ my $member = shift;
+
+ print '.';
+ print $member->{name};
}

sub Stupid::Variable::emitLValue {
@@ -286,22 +319,21 @@
my $self = shift;
my $name = shift;

- print "struct $self->{name} $name";
+ print "struct $self->{struct}->{name} $name";
}

sub Stupid::Type::StructInstance::emitArg {
my $self = shift;
my $name = shift;

- print 'const ';
- $self->emitDeclaration($name);
+ print "const struct $self->{struct}->{name} * const $name";
}

sub Stupid::Type::StructInstance::emitReturnDecl {
my $self = shift;
my $name = shift;

- print "struct $self->{name} *$name";
+ print "struct $self->{struct}->{name} *$name";
}

sub Stupid::Type::StructInstance::dereference {
@@ -309,12 +341,24 @@

print '*';
}
+
+sub Stupid::Type::StructInstance::derefArgument {
+ my $self = shift;
+
+ print '*';
+}

sub Stupid::Type::StructInstance::emitPointer {
my $self = shift;

print '&';
}
+
+sub Stupid::Type::StructInstance::emitParameter {
+ my $self = shift;
+
+ print '&';
+}

sub Stupid::AbstractDeclList::emitCode {
my $self = shift;
@@ -337,6 +381,10 @@

print '*';
}
+
+sub Stupid::Type::UInt32::derefArgument {
+# ints are passed by value
+}

sub Stupid::Type::UInt32::emitReturnDecl {
my $self = shift;
@@ -406,7 +454,7 @@
my $name = shift;

print "stupid_ostream *$name";
-}
+}

sub Stupid::Type::OStream::emitArg {
croak "ostreams must be outputs";
@@ -415,6 +463,10 @@
sub Stupid::Type::OStream::needsSelf {
return 1;
}
+
+sub Stupid::Type::OStream::dereference {
+ print '*';
+}

sub Stupid::Type::Array::emitReturnDecl {
my $self = shift;
@@ -442,6 +494,18 @@
sub Stupid::Type::Array::emitPointer {
# because of C bizarroness this is not printing '&'
}
+
+sub Stupid::Type::Array::emitParameter {
+ # because of C bizarroness this is not printing '&'
+}
+
+sub Stupid::Type::Array::dereference {
+ # once more, no need for a *
+}
+
+sub Stupid::Type::Array::derefArgument {
+ # once more, no need for a *
+}

sub Stupid::ArrayRef::emitLValue {
my $self = shift;
@@ -451,6 +515,12 @@
$self->{offset}->emitCode();
print ']';
}
+
+sub Stupid::ArrayRef::emitParameter {
+ my $self = shift;
+
+ $self->emitLValue();
+}

sub Stupid::ArrayRef::emitCode {
my $self = shift;
@@ -469,6 +539,12 @@

print $self->{value}, 'U';
}
+
+sub Stupid::DecimalValue::emitParameter {
+ my $self = shift;
+
+ $self->emitCode();
+}

sub Stupid::ArrayValue::emitCode {
my $self = shift;
@@ -532,7 +608,6 @@
$self->{right}->emitCode();
print ')';
}
-

sub Stupid::Ge8::emitCode {
my $self = shift;
@@ -543,7 +618,6 @@
$self->{right}->emitCode();
print ')';
}
-

sub Stupid::Le8::emitCode {
my $self = shift;
@@ -554,8 +628,6 @@
$self->{right}->emitCode();
print ')';
}
-
-

sub Stupid::LShift32::emitCode {
my $self = shift;
@@ -566,6 +638,13 @@
$self->{right}->emitCode();
print ')';
}
+
+# FIXME: we really want this in some common ancestor
+sub Stupid::LShift32::emitParameter {
+ my $self = shift;
+
+ $self->emitCode();
+}

sub Stupid::LShift8::emitCode {
my $self = shift;
=======================================
--- /src/grammar.y Sun Mar 21 06:26:13 2010
+++ /src/grammar.y Sun Mar 21 10:29:19 2010
@@ -20,7 +20,7 @@
;

struct_decl : 'struct' WORD '(' abstract_decl_list ')' ';'
- { new Stupid::Type::Struct($_[2], $_[4]); }
+ { new Stupid::Type::Struct($::Context, $_[2], $_[4]); }
;

abstract_decl_list : abstract_decl_list ',' abstract_decl
@@ -38,6 +38,7 @@
function : 'function' '(' arglist ')' WORD '(' arglist ')'
'{' statements '}'
{ $_[3]->markAsReturn();
+ $_[7]->markAsArgument();
new Stupid::Function($::Context, $_[5], $_[3], $_[7], $_[10]); }
;

@@ -172,7 +173,7 @@
| 'array' '(' type ',' VALUE ')'
{ new Stupid::Type::Array($_[3], $_[5]); }
| 'struct' WORD
- { new Stupid::Type::StructInstance($_[2]); }
+ { new Stupid::Type::StructInstance($::Context, $_[2]); }
;

arrayval : '[' val_list ']'
=======================================
--- /src/stupid.pl Sun Mar 21 05:09:59 2010
+++ /src/stupid.pl Sun Mar 21 10:29:19 2010
@@ -30,7 +30,7 @@
yyerror => \&yyerror,
yydebug => $debug ? 6 : 0);

-use Data::Dumper; Data::Dumper->Indent(1); print STDERR
Data::Dumper->Dump([\$ptree]) if $debug;
+use Data::Dumper; $Data::Dumper::Indent=1; print STDERR
Data::Dumper->Dump([\$ptree]) if $debug;

#$ptree->value();
#$::Context->dumpSymbols();
@@ -197,6 +197,22 @@
croak "Can't find symbol $name" if !$symbol;
return $symbol
}
+
+sub addStruct {
+ my $self = shift;
+ my $struct = shift;
+
+ $self->{structs}->{$struct->{name}} = $struct;
+}
+
+sub findStruct {
+ my $self = shift;
+ my $name = shift;
+
+ my $struct = $self->{structs}->{$name};
+ croak "Can't find struct $name" if !$struct;
+ return $struct;
+}

sub dumpSymbols {
my $self = shift;
@@ -286,6 +302,7 @@

sub new {
my $class = shift;
+ my $context = shift;
my $name = shift;
my $decls = shift;

@@ -295,8 +312,17 @@
$self->{name} = $name;
$self->{decls} = $decls;

+ $context->addStruct($self);
+
return $self;
}
+
+sub findMember {
+ my $self = shift;
+ my $name = shift;
+
+ return $self->{decls}->findDeclaration($name);
+}

package Stupid::Type::StructInstance;

@@ -304,15 +330,23 @@

sub new {
my $class = shift;
+ my $context = shift;
my $name = shift;

my $self = {};
bless $self, $class;

- $self->{name} = $name;
+ $self->{struct} = $context->findStruct($name);

return $self;
}
+
+sub findMember {
+ my $self = shift;
+ my $name = shift;
+
+ return $self->{struct}->findMember($name);
+}

package Stupid::AbstractDeclare;

@@ -336,6 +370,8 @@

use strict;

+use Carp;
+
sub new {
my $class = shift;

@@ -353,6 +389,16 @@

push @{$self->{decls}}, $decl;
}
+
+sub findDeclaration {
+ my $self = shift;
+ my $name = shift;
+
+ foreach my $decl (@{$self->{decls}}) {
+ return $decl if $decl->{name} eq $name;
+ }
+ croak "Can't find declaration of $name";
+}

package Stupid::ExprList;

@@ -413,7 +459,7 @@
bless $self, $class;

$self->{owner} = $owner;
- $self->{member} = $member;
+ $self->{member} = $self->{owner}->findMember($member);

return $self;
}
@@ -470,6 +516,14 @@
$a->markAsReturn();
}
}
+
+sub markAsArgument {
+ my $self = shift;
+
+ for my $a (@{$self->{args}}) {
+ $a->markAsArgument();
+ }
+}

package Stupid::ArrayRef;

@@ -1202,25 +1256,65 @@

$self->{var}->markAsReturn();
}
+
+sub markAsArgument {
+ my $self = shift;
+
+ $self->{var}->markAsArgument();
+}

sub value {
my $self = shift;

$self->{var}->setValue($self->{init}->value());
}
+
+sub findMember {
+ my $self = shift;
+ my $member = shift;
+
+ return $self->{var}->findMember($member);
+}
+
+package Stupid::Type::OStream::Put;
+
+use strict;
+
+sub new {
+ my $class = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{name} = 'put';
+
+ return $self;
+}

package Stupid::Type::OStream;

use strict;

+use Carp;
+
sub new {
my $class = shift;

my $self = {};
bless $self, $class;

+ $self->{put} = new Stupid::Type::OStream::Put();
+
return $self;
}
+
+sub findMember {
+ my $self = shift;
+ my $name = shift;
+
+ croak "ostreams do not have the member $name" if $name ne 'put';
+ return $self->{put};
+}

package Stupid::Type::UInt32;

@@ -1357,6 +1451,19 @@

$self->{isReturn} = 1;
}
+
+sub markAsArgument {
+ my $self = shift;
+
+ $self->{isArgument} = 1;
+}
+
+sub findMember {
+ my $self = shift;
+ my $member = shift;
+
+ return $self->{type}->findMember($member);
+}

package Stupid::HexValue;

=======================================
--- /test/sha256-struct.stupid Sun Mar 21 06:37:42 2010
+++ /test/sha256-struct.stupid Sun Mar 21 10:29:19 2010
@@ -121,7 +121,8 @@
out.h[7] = in.h[7] plus32 h;
}

-function (array(uint8, 32) output) sha256_final(array(uint8, 64)
message_in,
+function (array(uint8, 32) output) sha256_final(struct sha256 state_in,
+ array(uint8, 64) message_in,
uint32 message_bits ) {
"This code adapted from Wikipedia pseudocode";

@@ -129,13 +130,13 @@

"Initialize variables";
"(first 32 bits of the fractional parts of the square roots of the first 8
primes 2..19):";
- struct sha256 state = [[ 0 ]];

uint32 pad_byte = 0;
uint32 pad_bit = 0;
uint32 tmp = 0;
uint32 i = 0;
array(uint8, 128) message = [ 0 ];
+struct sha256 state = state_in;

"copy the input message to a mutable array";
i = 0;
@@ -143,8 +144,6 @@
message[i] = message_in[i];
i = i plus32 1;
}
-
-state = sha256_init();

"Pre-processing:";
"append the bit '1' to the message";
@@ -221,12 +220,43 @@

}

-function(ostream out) test_one(array(uint8, 64) message, uint32 bits,
+"Note that to avoid complication and inefficiency, we assume a whole
number of
+bytes, even though the implementation could actually deal with partial
bytes";
+function(struct sha256 out) sha256_update(struct sha256 in,
+ array(uint8, 64) message,
+ uint32 bytes) {
+ uint32 pos = 0;
+
+ out = in;
+ while(pos ne32 bytes) {
+ while((out.used ne32 64) band (pos ne32 bytes)) {
+ out.buffer[out.used] = message[pos];
+ out.used = out.used plus32 1;
+ pos = pos plus32 1;
+ }
+ if(out.used eq32 64) {
+ out = sha256_block(out, out.buffer, 0);
+ out.used = 0;
+ } else {
+ }
+ }
+}
+
+function(array(uint8, 32) digest) sha256_finish(struct sha256 state) {
+ digest = sha256_final(state, state.buffer, state.used lshift32 3);
+}
+
+function(ostream out) test_one(array(uint8, 64) message, uint32 bytes,
array(uint8, 32) hash) {
array(uint8, 32) r = [ 0 ];
uint32 i = 0;
-
- r = sha256_final(message, bits);
+ struct sha256 state = [[ 0 ]];
+
+ state = sha256_init();
+ "FIXME: inputs should not be allowed to be outputs";
+ state = sha256_update(state, message, bytes);
+ r = sha256_finish(state);
+
while(i ne32 32) {
if(hash[i] ne8 r[i]) {
out.put('!');
@@ -253,7 +283,7 @@
0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,
0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1 ];

- out = test_one(m1, 24, h1);
+ out = test_one(m1, 3, h1);
"just big enough to force it into the second block";
- out = test_one(m2, 448, h2);
-}
+ out = test_one(m2, 56, h2);
+}
=======================================
--- /test/struct.stupid Sun Mar 21 05:09:59 2010
+++ /test/struct.stupid Sun Mar 21 10:29:19 2010
@@ -9,11 +9,12 @@
array(uint32, 2) a
);

-function() test() {
+function(struct test t) test() {
struct test c = [0, 0];
struct test2 e = [[0, 0]];

c.a = 1;
c.b = 23;
e.a[0] = 456;
-}
+ t.a = t.a plus32 1;
+}

Ben Clifford

unread,
Mar 21, 2010, 1:58:10 PM3/21/10
to stupi...@googlegroups.com

> I hope -S doesn't... [break other platforms.]

it does. at least my main linux box and my os x 10.5 box.

$ /usr/bin/env -S uptime
/usr/bin/env: illegal option -- S
usage: env [-i] [name=value ...] [utility [argument ...]]

this is nuts.

--

Ben Laurie

unread,
Mar 21, 2010, 2:04:03 PM3/21/10
to stupi...@googlegroups.com

Rarg! Excuse me while I go and kill someone at FreeBSD.

--
http://www.apache-ssl.org/ben.html http://www.links.org/

"There is no limit to what a man can do or how far he can go if he
doesn't mind who gets the credit." - Robert Woodruff

Ben Clifford

unread,
Mar 28, 2010, 8:57:47 AM3/28/10
to stupi...@googlegroups.com

> >> I hope -S doesn't... [break other platforms.]
> >
> > it does. at least my main linux box and my os x 10.5 box.
> >
> > $ /usr/bin/env -S uptime
> > /usr/bin/env: illegal option -- S
> > usage: env [-i] [name=value ...] [utility [argument ...]]
> >
> > this is nuts.
>
> Rarg! Excuse me while I go and kill someone at FreeBSD.

At the moment, I think this is only a problem for stupid's use of perl.
Could put 'use warnings;' at the top of each perl source file, which looks
like its roughly the same (though I haven't tried it).

--

Reply all
Reply to author
Forward
0 new messages