Skip to content

Commit

Permalink
Add Chacha::rand* to AST, tyc, and codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Jun 28, 2023
1 parent 3e7140e commit b14dbf0
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
48 changes: 48 additions & 0 deletions compiler/ast/src/functions/core_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ pub enum CoreFunction {
BHP1024HashToU128,
BHP1024HashToScalar,

ChaChaRandAddress,
ChaChaRandBool,
ChaChaRandField,
ChaChaRandGroup,
ChaChaRandI8,
ChaChaRandI16,
ChaChaRandI32,
ChaChaRandI64,
ChaChaRandI128,
ChaChaRandU8,
ChaChaRandU16,
ChaChaRandU32,
ChaChaRandU64,
ChaChaRandU128,
ChaChaRandScalar,

Pedersen64CommitToAddress,
Pedersen64CommitToField,
Pedersen64CommitToGroup,
Expand Down Expand Up @@ -257,6 +273,22 @@ impl CoreFunction {
(sym::BHP1024, sym::hash_to_u128) => Self::BHP1024HashToU128,
(sym::BHP1024, sym::hash_to_scalar) => Self::BHP1024HashToScalar,

(sym::ChaCha, sym::rand_address) => Self::ChaChaRandAddress,
(sym::ChaCha, sym::rand_bool) => Self::ChaChaRandBool,
(sym::ChaCha, sym::rand_field) => Self::ChaChaRandField,
(sym::ChaCha, sym::rand_group) => Self::ChaChaRandGroup,
(sym::ChaCha, sym::rand_i8) => Self::ChaChaRandI8,
(sym::ChaCha, sym::rand_i16) => Self::ChaChaRandI16,
(sym::ChaCha, sym::rand_i32) => Self::ChaChaRandI32,
(sym::ChaCha, sym::rand_i64) => Self::ChaChaRandI64,
(sym::ChaCha, sym::rand_i128) => Self::ChaChaRandI128,
(sym::ChaCha, sym::rand_scalar) => Self::ChaChaRandScalar,
(sym::ChaCha, sym::rand_u8) => Self::ChaChaRandU8,
(sym::ChaCha, sym::rand_u16) => Self::ChaChaRandU16,
(sym::ChaCha, sym::rand_u32) => Self::ChaChaRandU32,
(sym::ChaCha, sym::rand_u64) => Self::ChaChaRandU64,
(sym::ChaCha, sym::rand_u128) => Self::ChaChaRandU128,

(sym::Pedersen64, sym::commit_to_address) => Self::Pedersen64CommitToAddress,
(sym::Pedersen64, sym::commit_to_field) => Self::Pedersen64CommitToField,
(sym::Pedersen64, sym::commit_to_group) => Self::Pedersen64CommitToGroup,
Expand Down Expand Up @@ -424,6 +456,22 @@ impl CoreFunction {
Self::BHP1024HashToU128 => 1,
Self::BHP1024HashToScalar => 1,

Self::ChaChaRandAddress => 0,
Self::ChaChaRandBool => 0,
Self::ChaChaRandField => 0,
Self::ChaChaRandGroup => 0,
Self::ChaChaRandI8 => 0,
Self::ChaChaRandI16 => 0,
Self::ChaChaRandI32 => 0,
Self::ChaChaRandI64 => 0,
Self::ChaChaRandI128 => 0,
Self::ChaChaRandU8 => 0,
Self::ChaChaRandU16 => 0,
Self::ChaChaRandU32 => 0,
Self::ChaChaRandU64 => 0,
Self::ChaChaRandU128 => 0,
Self::ChaChaRandScalar => 0,

Self::Pedersen64CommitToAddress => 2,
Self::Pedersen64CommitToField => 2,
Self::Pedersen64CommitToGroup => 2,
Expand Down
27 changes: 27 additions & 0 deletions compiler/passes/src/code_generation/visit_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,33 @@ impl<'a> CodeGenerator<'a> {
_ => unreachable!("The only associated methods of group are to_x_coordinate and to_y_coordinate"),
}
}
Type::Identifier(Identifier { name: sym::ChaCha, .. }) => {
// Get the destination register.
let destination_register = get_destination_register();
// Construct the instruction template.
let mut instruction = format!(" rand.chacha into {destination_register} as ");
// Write the return type.
match input.name {
Identifier { name: sym::rand_address, .. } => writeln!(instruction, "address;"),
Identifier { name: sym::rand_bool, .. } => writeln!(instruction, "boolean;"),
Identifier { name: sym::rand_field, .. } => writeln!(instruction, "field;"),
Identifier { name: sym::rand_group, .. } => writeln!(instruction, "group;"),
Identifier { name: sym::rand_i8, .. } => writeln!(instruction, "i8;"),
Identifier { name: sym::rand_i16, .. } => writeln!(instruction, "i16;"),
Identifier { name: sym::rand_i32, .. } => writeln!(instruction, "i32;"),
Identifier { name: sym::rand_i64, .. } => writeln!(instruction, "i64;"),
Identifier { name: sym::rand_i128, .. } => writeln!(instruction, "i128;"),
Identifier { name: sym::rand_scalar, .. } => writeln!(instruction, "scalar;"),
Identifier { name: sym::rand_u8, .. } => writeln!(instruction, "u8;"),
Identifier { name: sym::rand_u16, .. } => writeln!(instruction, "u16;"),
Identifier { name: sym::rand_u32, .. } => writeln!(instruction, "u32;"),
Identifier { name: sym::rand_u64, .. } => writeln!(instruction, "u64;"),
Identifier { name: sym::rand_u128, .. } => writeln!(instruction, "u128;"),
_ => unreachable!("The only associated methods of ChaCha are `rand_*`"),
}
.expect("failed to write to string");
(destination_register, instruction)
}
_ => unreachable!("All core functions should be known at this phase of compilation"),
};
// Add the instruction to the list of instructions.
Expand Down
15 changes: 15 additions & 0 deletions compiler/passes/src/type_checking/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,21 @@ impl<'a> TypeChecker<'a> {
self.assert_group_type(&arguments[0].0, arguments[0].1);
Some(Type::Field)
}
CoreFunction::ChaChaRandAddress => Some(Type::Address),
CoreFunction::ChaChaRandBool => Some(Type::Boolean),
CoreFunction::ChaChaRandField => Some(Type::Field),
CoreFunction::ChaChaRandGroup => Some(Type::Group),
CoreFunction::ChaChaRandI8 => Some(Type::Integer(IntegerType::I8)),
CoreFunction::ChaChaRandI16 => Some(Type::Integer(IntegerType::I16)),
CoreFunction::ChaChaRandI32 => Some(Type::Integer(IntegerType::I32)),
CoreFunction::ChaChaRandI64 => Some(Type::Integer(IntegerType::I64)),
CoreFunction::ChaChaRandI128 => Some(Type::Integer(IntegerType::I128)),
CoreFunction::ChaChaRandScalar => Some(Type::Scalar),
CoreFunction::ChaChaRandU8 => Some(Type::Integer(IntegerType::U8)),
CoreFunction::ChaChaRandU16 => Some(Type::Integer(IntegerType::U16)),
CoreFunction::ChaChaRandU32 => Some(Type::Integer(IntegerType::U32)),
CoreFunction::ChaChaRandU64 => Some(Type::Integer(IntegerType::U64)),
CoreFunction::ChaChaRandU128 => Some(Type::Integer(IntegerType::U128)),
}
}

Expand Down
16 changes: 16 additions & 0 deletions compiler/span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ symbols! {
BHP512,
BHP768,
BHP1024,
ChaCha,
commit_to_address,
commit_to_field,
commit_to_group,
Expand All @@ -174,6 +175,21 @@ symbols! {
Poseidon2,
Poseidon4,
Poseidon8,
rand_address,
rand_bool,
rand_field,
rand_group,
rand_i8,
rand_i16,
rand_i32,
rand_i64,
rand_i128,
rand_scalar,
rand_u8,
rand_u16,
rand_u32,
rand_u64,
rand_u128,
set,
to_x_coordinate,
to_y_coordinate,
Expand Down

0 comments on commit b14dbf0

Please sign in to comment.