Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add u16/32/64 #206

Merged
merged 6 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,11 @@ impl Expr {
// sanity check
if !matches!(
self.kind,
ExprKind::Variable { .. } | ExprKind::FieldAccess { .. }
ExprKind::Variable { .. }
| ExprKind::FieldAccess { .. }
| ExprKind::ArrayAccess { .. }
) {
panic!("an array access can only follow a variable");
panic!("an array access can only follow a variable or another array access");
}

// array[idx]
Expand Down
108 changes: 108 additions & 0 deletions src/stdlib/native/int.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::bits;
use std::comparator;

// u8
struct Uint8 {
inner: Field,
bit_len: Field,
}

fn Uint8.new(val: Field) -> Uint8 {
let bit_len = 8;

// range check
let ignore_ = bits::to_bits(bit_len, val);

return Uint8 {
inner: val,
bit_len: bit_len
};
}

// u16
struct Uint16 {
inner: Field,
bit_len: Field,
katat marked this conversation as resolved.
Show resolved Hide resolved
}

fn Uint16.new(val: Field) -> Uint16 {
let bit_len = 16;

// range check
let ignore_ = bits::to_bits(bit_len, val);

return Uint16 {
inner: val,
bit_len: bit_len
};
}

// u32
struct Uint32 {
inner: Field,
bit_len: Field,
}

fn Uint32.new(val: Field) -> Uint32 {
let bit_len = 32;

// range check
let ignore_ = bits::to_bits(bit_len, val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually the syntax is let _ = or let _bits =

Copy link
Collaborator Author

@katat katat Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current rule doesn't allow starting with _, maybe it is time to change this rule?


return Uint32 {
inner: val,
bit_len: bit_len
};
}

// u64
struct Uint64 {
inner: Field,
bit_len: Field,
}

fn Uint64.new(val: Field) -> Uint64 {
let bit_len = 64;

// range check
let ignore_ = bits::to_bits(bit_len, val);

return Uint64 {
inner: val,
bit_len: bit_len
};
}

// implement comparator

fn Uint8.less_than(self, rhs: Uint8) -> Bool {
return comparator::less_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint8.less_eq_than(self, rhs: Uint8) -> Bool {
return comparator::less_eq_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint16.less_than(self, rhs: Uint16) -> Bool {
return comparator::less_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint16.less_eq_than(self, rhs: Uint16) -> Bool {
return comparator::less_eq_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint32.less_than(self, rhs: Uint32) -> Bool {
return comparator::less_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint32.less_eq_than(self, rhs: Uint32) -> Bool {
return comparator::less_eq_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint64.less_than(self, rhs: Uint64) -> Bool {
return comparator::less_than(self.bit_len, self.inner, rhs.inner);
}

fn Uint64.less_eq_than(self, rhs: Uint64) -> Bool {
return comparator::less_eq_than(self.bit_len, self.inner, rhs.inner);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also implement add, sub, etc.
this is when having a bit_len field would be interesting... because theoretically we could track how many bits max the result of an operation is, and only trigger a range check when we might be above 8 bits for u8. But in the absence of that we can simply perform a range check after every operation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To take it further, maybe we can make it support builtin operations, such as + -, by mapping these operations to these functions. #214

}
Loading