[stupid-crypto] push by ben@links.org - Make things work enough so sha256 can be tested. on 2010-03-15 01:30 GMT

1 view
Skip to first unread message

stupid...@googlecode.com

unread,
Mar 14, 2010, 9:31:25 PM3/14/10
to stupi...@googlegroups.com
Revision: e46d65b4c6
Author: Ben Laurie <be...@google.com>
Date: Sun Mar 14 18:28:32 2010
Log: Make things work enough so sha256 can be tested.
http://code.google.com/p/stupid-crypto/source/detail?r=e46d65b4c6

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

=======================================
--- /dev/null
+++ /test/struct.stupid Sun Mar 14 18:28:32 2010
@@ -0,0 +1,19 @@
+"EXPECT:";
+
+struct test (
+ uint32 a,
+ uint32 b
+);
+
+struct test2 (
+ array(uint32, 2) a
+);
+
+function() test() {
+ struct test c = (0, 0);
+ struct test2 e = ((0,0));
+
+ c.a = 1;
+ c.b = 23;
+ e.a[0] = 456;
+}
=======================================
--- /src/Stupid/C.pm Sat Mar 13 16:10:17 2010
+++ /src/Stupid/C.pm Sun Mar 14 18:28:32 2010
@@ -43,6 +43,15 @@
$self->{body}->emitCode();
print "}\n";
}
+
+sub Stupid::Function::maybeAddSelf {
+}
+
+sub Stupid::Function::emitCall {
+ my $self = shift;
+
+ print $self->{name};
+}

sub Stupid::ArgList::emitReturnDecls {
my $self = shift;
@@ -104,12 +113,24 @@
sub Stupid::FunctionCall::emitCode {
my $self = shift;

- $self->{function}->emitCode();
+ $self->{function}->emitCall();
print '(';
$self->{function}->maybeAddSelf();
$self->{args}->emitCode();
print ");\n";
}
+
+sub Stupid::FunctionCall::emitCallWithLValue {
+ my $self = shift;
+ my $lvalue = shift;
+
+ $self->{function}->emitCall();
+ print '(';
+ $lvalue->emitPointer();
+ print ', ';
+ $self->{args}->emitCode();
+ print ");\n";
+}

sub Stupid::MemberRef::emitCode {
my $self = shift;
@@ -117,6 +138,12 @@
$self->{owner}->emitCode();
$self->{owner}->emitMemberRef($self->{member});
}
+
+sub Stupid::MemberRef::emitCall {
+ my $self = shift;
+
+ $self->emitCode();
+}

sub Stupid::MemberRef::maybeAddSelf {
my $self = shift;
@@ -178,6 +205,14 @@

sub Stupid::Set::emitCode {
my $self = shift;
+
+ # special case ... clearly we could do this in full generality,
+ # e.g f()[8] or f().foo or (a, b) = (c, d) or (a, b) = (b, a)
+ # [hmmm]
+ if(ref($self->{right}) eq 'Stupid::FunctionCall') {
+ $self->{right}->emitCallWithLValue($self->{left});
+ return;
+ }

$self->{left}->emitLValue();
print ' = ';
@@ -225,6 +260,13 @@
$self->{type}->dereference() if $self->{isReturn};
print $self->{name};
}
+
+sub Stupid::Variable::emitPointer {
+ my $self = shift;
+
+ $self->{type}->emitPointer() if !$self->{isReturn};
+ print $self->{name};
+}

sub Stupid::Variable::maybeAddSelf {
my $self = shift;
@@ -366,6 +408,10 @@
print $self->{type}->typeName(), ' ', $name,
'[', $self->{size}->value(), ']';
}
+
+sub Stupid::Type::Array::emitPointer {
+ # because of C bizarroness this is not printing '&'
+}

sub Stupid::ArrayRef::emitLValue {
my $self = shift;
@@ -560,6 +606,16 @@
$self->{right}->emitCode();
print ')';
}
+
+sub Stupid::Ne8::emitCode {
+ my $self = shift;
+
+ print '(';
+ $self->{left}->emitCode();
+ print ' != ';
+ $self->{right}->emitCode();
+ print ')';
+}

sub Stupid::Not32::emitCode {
my $self = shift;
=======================================
--- /src/grammar.y Sat Mar 13 18:58:34 2010
+++ /src/grammar.y Sun Mar 14 18:28:32 2010
@@ -41,7 +41,7 @@
function : 'function' '(' arglist ')' WORD '(' arglist ')'
'{' statements '}'
{ $_[3]->markAsReturn();
- new Stupid::Function($_[5], $_[3], $_[7], $_[10]); }
+ new Stupid::Function($::Context, $_[5], $_[3], $_[7], $_[10]); }
;

arglist : arglist ',' arg
@@ -113,6 +113,8 @@
{ new Stupid::Mod32($_[1], $_[3]); }
| expr 'ne32' expr
{ new Stupid::Ne32($_[1], $_[3]); }
+ | expr 'ne8' expr
+ { new Stupid::Ne8($_[1], $_[3]); }
| expr 'or8' expr
{ new Stupid::Or8($_[1], $_[3]); }
| expr 'plus8' expr
@@ -138,10 +140,11 @@
| var
{ $_[1]; }
| VALUE
+ | CHAR
;

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

call: | expr '(' exprlist ')'
- { new Stupid::FunctionCall($_[1], $_[3], $_[5]); }
+ { new Stupid::FunctionCall($_[1], $_[3]); }
;

decl : type vardecl '=' VALUE
@@ -192,6 +195,7 @@

value : arrayval
| VALUE
+ | CHAR
;

vardecl : WORD
=======================================
--- /src/stupid.pl Sat Mar 13 16:10:17 2010
+++ /src/stupid.pl Sun Mar 14 18:28:32 2010
@@ -49,7 +49,7 @@
@keywords = qw(array uint8 uint32 ostream
function struct if else while
and8 and32 band bor eq32 ge8 le8 lshift32 lshift8 mask32to8 minus8 minus32
- mod8 mod32 ne32 not32 not8 or8 plus8 plus32 rrotate32 rshift32 widen8to32
+ mod8 mod32 ne32 ne8 not32 not8 or8 plus8 plus32 rrotate32 rshift32
widen8to32
xor32);
}

@@ -91,6 +91,10 @@
$type = 'VALUE';
$value = new Stupid::DecimalValue($1);
$code = $2;
+ } elsif($code =~ /^'(.)'(.*)$/s) {
+ $type = 'CHAR';
+ $value = new Stupid::DecimalValue(ord($1));
+ $code = $2;
# WORD
} elsif($code =~ /^([A-Za-z][A-Za-z0-9_]*)(.*)$/s) {
$value = $1;
@@ -227,6 +231,7 @@

sub new {
my $class = shift;
+ my $context = shift;
my $name = shift;
my $returns = shift;
my $args = shift;
@@ -240,8 +245,16 @@
$self->{args} = $args;
$self->{body} = $body;

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

package Stupid::TopLevelList;

@@ -903,7 +916,23 @@

use strict;

-# Unsigned decimal value, any length
+sub new {
+ my $class = shift;
+ my $l = shift;
+ my $r = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{left} = $l;
+ $self->{right} = $r;
+
+ return $self;
+}
+
+package Stupid::Ne8;
+
+use strict;

sub new {
my $class = shift;
=======================================
--- /test/sha256-struct.stupid Sat Mar 13 18:58:34 2010
+++ /test/sha256-struct.stupid Sun Mar 14 18:28:32 2010
@@ -1,4 +1,4 @@
-"EXPECT:";
+"EXPECT:********************************";

struct sha256 (
array(uint32, 8) h
@@ -195,3 +195,23 @@
}

}
+
+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 );
+ array(uint8, 32) r = ( 0 );
+ r = sha256(m1, 24);
+ uint32 i = 0;
+ while(i ne32 32) {
+ if(h1[i] ne8 r[i]) {
+ out.put('!');
+ } else {
+ out.put('*');
+ }
+ i = i plus32 1;
+ }
+}

Reply all
Reply to author
Forward
0 new messages