From 13b910f01a9869acc68af1e34e57177b0be8f777 Mon Sep 17 00:00:00 2001 From: Markku Rossi Date: Sat, 3 Aug 2024 11:31:20 +0200 Subject: [PATCH] Pointer fixes. --- compiler/ast/ssagen.go | 6 ++-- compiler/ssa/value.go | 29 +++++++++++++++++++ .../internal/edwards25519/ed25519.mpcl | 10 ++----- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/compiler/ast/ssagen.go b/compiler/ast/ssagen.go index cbc6d352..435cc8cc 100644 --- a/compiler/ast/ssagen.go +++ b/compiler/ast/ssagen.go @@ -2261,11 +2261,11 @@ func (ast *Copy) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) ( return nil, nil, ast.errf(ctx, ast.Src, "got multivalue %T", ast.Src) } src := v[0] - srcType := src.IndirectType() - if !srcType.Type.Array() { + src = src.Indirect(block, gen) + if !src.Type.Type.Array() { return nil, nil, ast.errf(ctx, ast.Src, "got %v", src.Type) } - if !dst.Type.ElementType.Equal(*srcType.ElementType) { + if !dst.Type.ElementType.Equal(*src.Type.ElementType) { return nil, nil, ctx.Errorf(ast, "arguments to copy have different element types: %s and %s", dst.Type.ElementType, src.Type.ElementType) diff --git a/compiler/ssa/value.go b/compiler/ssa/value.go index f402974f..530508c5 100644 --- a/compiler/ssa/value.go +++ b/compiler/ssa/value.go @@ -84,6 +84,35 @@ func (v Value) IndirectType() types.Info { return v.Type } +// Indirect returns Value that v points to. If v is not a pointer, +// Indirect returns v. +func (v Value) Indirect(block *Block, gen *Generator) Value { + if v.Type.Type != types.TPtr { + return v + } + + // Get container value. + b, ok := v.PtrInfo.Bindings.Get(v.PtrInfo.Name) + if !ok { + panic("Value.Indirect: could not find pointer target") + } + cv := b.Value(block, gen) + + if v.PtrInfo.ContainerType.Type != types.TStruct { + return cv + } + + // Get struct field value. + elType := *v.Type.ElementType + ev := gen.AnonVal(elType) + fromConst := gen.Constant(int64(v.PtrInfo.Offset), types.Undefined) + toConst := gen.Constant(int64(v.PtrInfo.Offset+elType.Bits), + types.Undefined) + block.AddInstr(NewSliceInstr(cv, fromConst, toConst, ev)) + + return ev +} + // ContainerType returs the pointer container type of the value. For // non-pointer values, this returns the value type itself. func (v Value) ContainerType() types.Info { diff --git a/pkg/crypto/ed25519/internal/edwards25519/ed25519.mpcl b/pkg/crypto/ed25519/internal/edwards25519/ed25519.mpcl index 29b5814f..695d0e2b 100644 --- a/pkg/crypto/ed25519/internal/edwards25519/ed25519.mpcl +++ b/pkg/crypto/ed25519/internal/edwards25519/ed25519.mpcl @@ -1,6 +1,6 @@ // -*- go -*- // -// Copyright (c) 2020-2023 Markku Rossi +// Copyright (c) 2020-2024 Markku Rossi // // Ed25519 signature algorithm in MPCL. This file is derived from the // crypto/ed25519/internal/edwards25519 package of Go 1.16.9. The @@ -24,9 +24,7 @@ type FieldElement [10]int32 var zero FieldElement func FeZero(fe *FieldElement) { - for i := 0; i < len(fe); i++ { - fe[i] = zero[i] - } + copy(fe[:], zero[:]) } func FeOne(fe *FieldElement) { @@ -61,9 +59,7 @@ func FeSub(dst, a, b *FieldElement) { } func FeCopy(dst, src *FieldElement) { - for i := 0; i < len(dst); i++ { - dst[i] = src[i] - } + copy(dst[:], src[:]) } // Replace (f,g) with (g,g) if b == 1;